diff --git a/api/api.go b/api/api.go index 7e5223b53..456bad223 100644 --- a/api/api.go +++ b/api/api.go @@ -3,6 +3,8 @@ package api import ( "github.com/MinterTeam/minter-go-node/config" "github.com/MinterTeam/minter-go-node/eventsdb" + "io" + "io/ioutil" "log" "net/http" @@ -37,25 +39,25 @@ func RunApi(b *minter.Blockchain, node *node.Node) { router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/api/bipVolume", GetBipVolume).Methods("GET") - router.HandleFunc("/api/candidates", GetCandidates).Methods("GET") - router.HandleFunc("/api/candidate/{pubkey}", GetCandidate).Methods("GET") - router.HandleFunc("/api/validators", GetValidators).Methods("GET") - router.HandleFunc("/api/balance/{address}", GetBalance).Methods("GET") - router.HandleFunc("/api/balanceWS", GetBalanceWatcher) - router.HandleFunc("/api/transactionCount/{address}", GetTransactionCount).Methods("GET") - router.HandleFunc("/api/sendTransaction", SendTransaction).Methods("POST") - router.HandleFunc("/api/sendTransactionSync", SendTransactionSync).Methods("POST") - router.HandleFunc("/api/sendTransactionAsync", SendTransactionAsync).Methods("POST") - router.HandleFunc("/api/transaction/{hash}", Transaction).Methods("GET") - router.HandleFunc("/api/block/{height}", Block).Methods("GET") - router.HandleFunc("/api/transactions", Transactions).Methods("GET") - router.HandleFunc("/api/status", Status).Methods("GET") - router.HandleFunc("/api/net_info", NetInfo).Methods("GET") - router.HandleFunc("/api/coinInfo/{symbol}", GetCoinInfo).Methods("GET") - router.HandleFunc("/api/estimateCoinSell", EstimateCoinSell).Methods("GET") - router.HandleFunc("/api/estimateCoinBuy", EstimateCoinBuy).Methods("GET") - router.HandleFunc("/api/estimateTxCommission", EstimateTxCommission).Methods("GET") + router.HandleFunc("/api/bipVolume", wrapper(GetBipVolume)).Methods("GET") + router.HandleFunc("/api/candidates", wrapper(GetCandidates)).Methods("GET") + router.HandleFunc("/api/candidate/{pubkey}", wrapper(GetCandidate)).Methods("GET") + router.HandleFunc("/api/validators", wrapper(GetValidators)).Methods("GET") + router.HandleFunc("/api/balance/{address}", wrapper(GetBalance)).Methods("GET") + router.HandleFunc("/api/balanceWS", wrapper(GetBalanceWatcher)) + router.HandleFunc("/api/transactionCount/{address}", wrapper(GetTransactionCount)).Methods("GET") + router.HandleFunc("/api/sendTransaction", wrapper(SendTransaction)).Methods("POST") + router.HandleFunc("/api/sendTransactionSync", wrapper(SendTransactionSync)).Methods("POST") + router.HandleFunc("/api/sendTransactionAsync", wrapper(SendTransactionAsync)).Methods("POST") + router.HandleFunc("/api/transaction/{hash}", wrapper(Transaction)).Methods("GET") + router.HandleFunc("/api/block/{height}", wrapper(Block)).Methods("GET") + router.HandleFunc("/api/transactions", wrapper(Transactions)).Methods("GET") + router.HandleFunc("/api/status", wrapper(Status)).Methods("GET") + router.HandleFunc("/api/net_info", wrapper(NetInfo)).Methods("GET") + router.HandleFunc("/api/coinInfo/{symbol}", wrapper(GetCoinInfo)).Methods("GET") + router.HandleFunc("/api/estimateCoinSell", wrapper(EstimateCoinSell)).Methods("GET") + router.HandleFunc("/api/estimateCoinBuy", wrapper(EstimateCoinBuy)).Methods("GET") + router.HandleFunc("/api/estimateTxCommission", wrapper(EstimateTxCommission)).Methods("GET") c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, @@ -71,6 +73,15 @@ func RunApi(b *minter.Blockchain, node *node.Node) { log.Fatal(http.ListenAndServe(config.GetConfig().APIListenAddress, handler)) } +func wrapper(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + defer io.Copy(ioutil.Discard, r.Body) + defer r.Body.Close() + + f(w, r) + } +} + func waitForTendermint() { for { _, err := client.Health() diff --git a/core/minter/minter.go b/core/minter/minter.go index cab692463..1a1c9763f 100644 --- a/core/minter/minter.go +++ b/core/minter/minter.go @@ -53,7 +53,7 @@ var ( func NewMinterBlockchain() *Blockchain { - db, err := mintdb.NewLDBDatabase(utils.GetMinterHome()+"/data", 1000, 1000) + db, err := mintdb.NewLDBDatabase(utils.GetMinterHome()+"/data", 1024, 512) if err != nil { panic(err) diff --git a/eventsdb/eventsdb.go b/eventsdb/eventsdb.go index d90584c7a..fc7995d10 100644 --- a/eventsdb/eventsdb.go +++ b/eventsdb/eventsdb.go @@ -20,9 +20,8 @@ func init() { } func GetCurrent() *EventsDB { - if edb == nil { - eventsDB, err := mintdb.NewLDBDatabase(utils.GetMinterHome()+"/events", 1000, 1000) + eventsDB, err := mintdb.NewLDBDatabase(utils.GetMinterHome()+"/events", 128, 128) if err != nil { panic(err) @@ -58,6 +57,11 @@ func (db *EventsDB) AddEvent(height int64, event Event) { } func (db *EventsDB) FlushEvents(height int64) error { + + if !eventsEnabled { + return nil + } + events := db.GetEvents(height) key := getKeyForHeight(height) diff --git a/mintdb/memory_database.go b/mintdb/memory_database.go deleted file mode 100644 index f2e8a49e2..000000000 --- a/mintdb/memory_database.go +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright 2014 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package mintdb - -import ( - "errors" - "sync" - - "github.com/MinterTeam/minter-go-node/core/types" -) - -/* - * This is a test memory database. Do not use for any production it does not get persisted - */ -type MemDatabase struct { - db map[string][]byte - lock sync.RWMutex -} - -func NewMemDatabase() (*MemDatabase, error) { - return &MemDatabase{ - db: make(map[string][]byte), - }, nil -} - -func NewMemDatabaseWithCap(size int) (*MemDatabase, error) { - return &MemDatabase{ - db: make(map[string][]byte, size), - }, nil -} - -func (db *MemDatabase) Put(key []byte, value []byte) error { - db.lock.Lock() - defer db.lock.Unlock() - - db.db[string(key)] = types.CopyBytes(value) - return nil -} - -func (db *MemDatabase) Has(key []byte) (bool, error) { - db.lock.RLock() - defer db.lock.RUnlock() - - _, ok := db.db[string(key)] - return ok, nil -} - -func (db *MemDatabase) Get(key []byte) ([]byte, error) { - db.lock.RLock() - defer db.lock.RUnlock() - - if entry, ok := db.db[string(key)]; ok { - return types.CopyBytes(entry), nil - } - return nil, errors.New("not found") -} - -func (db *MemDatabase) Keys() [][]byte { - db.lock.RLock() - defer db.lock.RUnlock() - - keys := [][]byte{} - for key := range db.db { - keys = append(keys, []byte(key)) - } - return keys -} - -func (db *MemDatabase) Delete(key []byte) error { - db.lock.Lock() - defer db.lock.Unlock() - - delete(db.db, string(key)) - return nil -} - -func (db *MemDatabase) Close() {} - -func (db *MemDatabase) NewBatch() Batch { - return &memBatch{db: db} -} - -func (db *MemDatabase) Len() int { return len(db.db) } - -type kv struct{ k, v []byte } - -type memBatch struct { - db *MemDatabase - writes []kv - size int -} - -func (b *memBatch) Put(key, value []byte) error { - b.writes = append(b.writes, kv{types.CopyBytes(key), types.CopyBytes(value)}) - b.size += len(value) - return nil -} - -func (b *memBatch) Write() error { - b.db.lock.Lock() - defer b.db.lock.Unlock() - - for _, kv := range b.writes { - b.db.db[string(kv.k)] = kv.v - } - return nil -} - -func (b *memBatch) ValueSize() int { - return b.size -} - -func (b *memBatch) Reset() { - b.writes = b.writes[:0] - b.size = 0 -}