From 9294fa8423c458d37c81894abefc323f7b9f7204 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Thu, 31 Oct 2019 11:09:40 -0400 Subject: [PATCH] Fix edge case for accessing accounts through rpc (#134) * Add mutex and close keybase database correctly after use * Add options http request to rpc API for remix --- rpc/config.go | 2 +- rpc/eth_api.go | 15 +++++++++++---- rpc/filter_api.go | 2 +- rpc/filters.go | 2 +- rpc/tester/tester_test.go | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/rpc/config.go b/rpc/config.go index 72afbae16..2360850f6 100644 --- a/rpc/config.go +++ b/rpc/config.go @@ -74,7 +74,7 @@ func registerRoutes(rs *lcd.RestServer) { } } - rs.Mux.HandleFunc("/rpc", s.ServeHTTP).Methods("POST") + rs.Mux.HandleFunc("/", s.ServeHTTP).Methods("POST", "OPTIONS") } func unlockKeyFromNameAndPassphrase(accountName, passphrase string) (emintKey emintcrypto.PrivKeySecp256k1, err error) { diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 7b2d6684b..203ae015c 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -6,6 +6,7 @@ import ( "log" "math/big" "strconv" + "sync" emintcrypto "github.com/cosmos/ethermint/crypto" emintkeys "github.com/cosmos/ethermint/keys" @@ -37,10 +38,11 @@ import ( // PublicEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec. type PublicEthAPI struct { - cliCtx context.CLIContext - key emintcrypto.PrivKeySecp256k1 - nonceLock *AddrLocker - gasLimit *int64 + cliCtx context.CLIContext + key emintcrypto.PrivKeySecp256k1 + nonceLock *AddrLocker + keybaseLock sync.Mutex + gasLimit *int64 } // NewPublicEthAPI creates an instance of the public ETH Web3 API. @@ -103,6 +105,8 @@ func (e *PublicEthAPI) GasPrice() *hexutil.Big { // Accounts returns the list of accounts available to this node. func (e *PublicEthAPI) Accounts() ([]common.Address, error) { + e.keybaseLock.Lock() + addresses := make([]common.Address, 0) // return [] instead of nil if empty keybase, err := emintkeys.NewKeyBaseFromHomeFlag() if err != nil { @@ -114,6 +118,9 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) { return addresses, err } + keybase.CloseDB() + e.keybaseLock.Unlock() + for _, info := range infos { addressBytes := info.GetPubKey().Address().Bytes() addresses = append(addresses, common.BytesToAddress(addressBytes)) diff --git a/rpc/filter_api.go b/rpc/filter_api.go index 0025a9624..983de4733 100644 --- a/rpc/filter_api.go +++ b/rpc/filter_api.go @@ -77,4 +77,4 @@ func returnLogs(logs []*ethtypes.Log) []*ethtypes.Log { return []*ethtypes.Log{} } return logs -} \ No newline at end of file +} diff --git a/rpc/filters.go b/rpc/filters.go index 01711f3e7..8631ff15a 100644 --- a/rpc/filters.go +++ b/rpc/filters.go @@ -17,7 +17,7 @@ type Filter struct { addresses []common.Address topics [][]common.Hash - block common.Hash // Block hash if filtering a single block + block common.Hash // Block hash if filtering a single block } // NewBlockFilter creates a new filter which directly inspects the contents of diff --git a/rpc/tester/tester_test.go b/rpc/tester/tester_test.go index d492d9857..0cdbf7dbd 100644 --- a/rpc/tester/tester_test.go +++ b/rpc/tester/tester_test.go @@ -27,7 +27,7 @@ const ( addrAStoreKey = 0 ) -var addr = fmt.Sprintf("http://%s:%d/rpc", host, port) +var addr = fmt.Sprintf("http://%s:%d", host, port) type Request struct { Version string `json:"jsonrpc"`