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
This commit is contained in:
Austin Abell 2019-10-31 11:09:40 -04:00 committed by GitHub
parent c130105b86
commit 9294fa8423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 8 deletions

View File

@ -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) {

View File

@ -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))

View File

@ -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

View File

@ -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"`