diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-05-20 17:29:28 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-05-20 17:29:28 +0800 |
commit | 1580ec180414bce1e37acc614bc2445f778efb75 (patch) | |
tree | 7c8276f3f1558b5ce62edd0bff87745956084a4c /accounts/abi/bind/backends | |
parent | e798e4fd750745cec99c5a531e42998d9a7be85e (diff) | |
download | dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar.gz dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar.bz2 dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar.lz dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar.xz dexon-1580ec180414bce1e37acc614bc2445f778efb75.tar.zst dexon-1580ec180414bce1e37acc614bc2445f778efb75.zip |
accounts/abi/bind, eth: rely on getCode for sanity checks, not estimate and call
Diffstat (limited to 'accounts/abi/bind/backends')
-rw-r--r-- | accounts/abi/bind/backends/nil.go | 1 | ||||
-rw-r--r-- | accounts/abi/bind/backends/remote.go | 20 | ||||
-rw-r--r-- | accounts/abi/bind/backends/simulated.go | 10 |
3 files changed, 31 insertions, 0 deletions
diff --git a/accounts/abi/bind/backends/nil.go b/accounts/abi/bind/backends/nil.go index 3b1e6dce7..f10bb61ac 100644 --- a/accounts/abi/bind/backends/nil.go +++ b/accounts/abi/bind/backends/nil.go @@ -38,6 +38,7 @@ func (*nilBackend) ContractCall(common.Address, []byte, bool) ([]byte, error) { func (*nilBackend) EstimateGasLimit(common.Address, *common.Address, *big.Int, []byte) (*big.Int, error) { panic("not implemented") } +func (*nilBackend) HasCode(common.Address, bool) (bool, error) { panic("not implemented") } func (*nilBackend) SuggestGasPrice() (*big.Int, error) { panic("not implemented") } func (*nilBackend) PendingAccountNonce(common.Address) (uint64, error) { panic("not implemented") } func (*nilBackend) SendTransaction(*types.Transaction) error { panic("not implemented") } diff --git a/accounts/abi/bind/backends/remote.go b/accounts/abi/bind/backends/remote.go index 9b3647192..d903cbc8f 100644 --- a/accounts/abi/bind/backends/remote.go +++ b/accounts/abi/bind/backends/remote.go @@ -111,6 +111,26 @@ func (b *rpcBackend) request(method string, params []interface{}) (json.RawMessa return res.Result, nil } +// HasCode implements ContractVerifier.HasCode by retrieving any code associated +// with the contract from the remote node, and checking its size. +func (b *rpcBackend) HasCode(contract common.Address, pending bool) (bool, error) { + // Execute the RPC code retrieval + block := "latest" + if pending { + block = "pending" + } + res, err := b.request("eth_getCode", []interface{}{contract.Hex(), block}) + if err != nil { + return false, err + } + var hex string + if err := json.Unmarshal(res, &hex); err != nil { + return false, err + } + // Convert the response back to a Go byte slice and return + return len(common.FromHex(hex)) > 0, nil +} + // ContractCall implements ContractCaller.ContractCall, delegating the execution of // a contract call to the remote node, returning the reply to for local processing. func (b *rpcBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) { diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 4866c4f58..54b1ce603 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -78,6 +78,16 @@ func (b *SimulatedBackend) Rollback() { b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database) } +// HasCode implements ContractVerifier.HasCode, checking whether there is any +// code associated with a certain account in the blockchain. +func (b *SimulatedBackend) HasCode(contract common.Address, pending bool) (bool, error) { + if pending { + return len(b.pendingState.GetCode(contract)) > 0, nil + } + statedb, _ := b.blockchain.State() + return len(statedb.GetCode(contract)) > 0, nil +} + // ContractCall implements ContractCaller.ContractCall, executing the specified // contract with the given input data. func (b *SimulatedBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) { |