From cb9738691382b7fcfc0ee876380ef732cb2962dd Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 10 Mar 2022 16:45:54 +0800 Subject: [PATCH 1/3] fix bug --- core/store/ledgerstore/state_store.go | 3 +++ http/ethrpc/eth/api.go | 35 +++++++++++++++++++++------ smartcontract/storage/statedb.go | 4 +++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/core/store/ledgerstore/state_store.go b/core/store/ledgerstore/state_store.go index b67d513ec..fbeb5bfd6 100644 --- a/core/store/ledgerstore/state_store.go +++ b/core/store/ledgerstore/state_store.go @@ -328,6 +328,9 @@ func (self *StateStore) GetEthState(addr common2.Address, stateKey common2.Hash) key := genStateKey(addr, stateKey) value, err := self.store.Get(key) if err != nil { + if err == scom.ErrNotFound { + return nil, nil + } return nil, err } return value, nil diff --git a/http/ethrpc/eth/api.go b/http/ethrpc/eth/api.go index 2e0ff2bd7..3bcaa5139 100644 --- a/http/ethrpc/eth/api.go +++ b/http/ethrpc/eth/api.go @@ -36,6 +36,7 @@ import ( "github.com/ontio/ontology/common/log" "github.com/ontio/ontology/core/states" common2 "github.com/ontio/ontology/core/store/common" + sCom "github.com/ontio/ontology/core/store/common" otypes "github.com/ontio/ontology/core/types" ontErrors "github.com/ontio/ontology/errors" bactor "github.com/ontio/ontology/http/base/actor" @@ -202,7 +203,7 @@ func (api *EthereumAPI) GetCode(address common.Address, blockNumber types2.Block if err != nil { return nil, err } - if account.IsEmpty() { + if account.IsEmptyContract() { return nil, nil } code, err := bactor.GetEthCode(account.CodeHash) @@ -291,6 +292,9 @@ func (api *EthereumAPI) Call(args types2.CallArgs, blockNumber types2.BlockNumbe if err != nil { return nil, err } + if res.Failed() { + return nil, res.Err + } if len(res.Revert()) > 0 { return nil, newRevertError(res) } @@ -401,10 +405,13 @@ func (api *EthereumAPI) GetBlockByHash(hash common.Hash, fullTx bool) (interface log.Debugf("eth_getBlockByHash hash %v, fullTx %v", hash, fullTx) block, err := bactor.GetBlockFromStore(oComm.Uint256(hash)) if err != nil { + if err == sCom.ErrNotFound { + return nil, nil + } return nil, err } if block == nil { - return nil, fmt.Errorf("block: %v not found", hash.String()) + return nil, nil } return utils2.EthBlockFromOntology(block, fullTx), nil } @@ -417,10 +424,13 @@ func (api *EthereumAPI) GetBlockByNumber(blockNum types2.BlockNumber, fullTx boo } block, err := bactor.GetBlockByHeight(height) if err != nil { + if err == sCom.ErrNotFound { + return nil, nil + } return nil, err } if block == nil { - return nil, fmt.Errorf("block: %v not found", blockNum.Int64()) + return nil, nil } return utils2.EthBlockFromOntology(block, fullTx), nil } @@ -462,16 +472,19 @@ func (api *EthereumAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx log.Debugf("eth_getTransactionByBlockHashAndIndex hash %v, idx %v", hash.Hex(), idx.String()) block, err := bactor.GetBlockFromStore(oComm.Uint256(hash)) if err != nil { + if err == sCom.ErrNotFound { + return nil, nil + } return nil, err } if block == nil { - return nil, fmt.Errorf("block: %v not found", hash.Hex()) + return nil, nil } header := block.Header blockHash := header.Hash() txs := block.Transactions if len(txs) >= int(idx) { - return nil, fmt.Errorf("access block: %v overflow %v", hash.Hex(), idx) + return nil, nil } tx := txs[idx] return utils2.OntTxToEthTx(*tx, common.Hash(blockHash), uint64(header.Height), uint64(idx)) @@ -485,16 +498,19 @@ func (api *EthereumAPI) GetTransactionByBlockNumberAndIndex(blockNum types2.Bloc } block, err := bactor.GetBlockByHeight(height) if err != nil { + if err == sCom.ErrNotFound { + return nil, nil + } return nil, err } if block == nil { - return nil, fmt.Errorf("block: %v not found", height) + return nil, nil } header := block.Header blockHash := header.Hash() txs := block.Transactions if len(txs) >= int(idx) { - return nil, fmt.Errorf("access block: %v overflow %v", height, idx) + return nil, nil } tx := txs[idx] return utils2.OntTxToEthTx(*tx, common.Hash(blockHash), uint64(header.Height), uint64(idx)) @@ -504,6 +520,9 @@ func (api *EthereumAPI) GetTransactionReceipt(hash common.Hash) (interface{}, er log.Debugf("eth_getTransactionReceipt hash %d", hash.Hex()) notify, err := bactor.GetEventNotifyByTxHash(utils2.EthToOntHash(hash)) if err != nil { + if err == sCom.ErrNotFound { + return nil, nil + } return nil, nil } if notify == nil { @@ -578,7 +597,7 @@ func (api *EthereumAPI) PendingTransactionsByHash(target common.Hash) (*types2.T log.Debugf("eth_pendingTransactionsByHash target %v", target.Hex()) ethTx := api.txpool.PendingTransactionsByHash(target) if ethTx == nil { - return nil, fmt.Errorf("tx: %v not found", target.String()) + return nil, nil } return utils2.NewTransaction(ethTx, ethTx.Hash(), common.Hash{}, 0, 0) } diff --git a/smartcontract/storage/statedb.go b/smartcontract/storage/statedb.go index 5c9b5b907..bc8942c21 100644 --- a/smartcontract/storage/statedb.go +++ b/smartcontract/storage/statedb.go @@ -169,6 +169,10 @@ func (self *EthAccount) IsEmpty() bool { return self.Nonce == 0 && self.CodeHash == common.Hash{} } +func (self *EthAccount) IsEmptyContract() bool { + return self.CodeHash == common.Hash{} +} + func (self *EthAccount) Serialization(sink *comm.ZeroCopySink) { sink.WriteUint64(self.Nonce) sink.WriteHash(comm.Uint256(self.CodeHash)) From d5d76d29cf32c62141d5bb7317c7c57e980843af Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 10 Mar 2022 17:32:04 +0800 Subject: [PATCH 2/3] fix --- http/ethrpc/eth/api.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/http/ethrpc/eth/api.go b/http/ethrpc/eth/api.go index 3bcaa5139..190a77a97 100644 --- a/http/ethrpc/eth/api.go +++ b/http/ethrpc/eth/api.go @@ -292,12 +292,12 @@ func (api *EthereumAPI) Call(args types2.CallArgs, blockNumber types2.BlockNumbe if err != nil { return nil, err } - if res.Failed() { - return nil, res.Err - } if len(res.Revert()) > 0 { return nil, newRevertError(res) } + if res.Failed() { + return nil, res.Err + } return res.Return(), res.Err } From 435a6a483cd9d0eff86c8454462c8f6ed1f9bb16 Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 10 Mar 2022 17:44:24 +0800 Subject: [PATCH 3/3] fix --- http/ethrpc/eth/api.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/http/ethrpc/eth/api.go b/http/ethrpc/eth/api.go index 190a77a97..b9564b4f3 100644 --- a/http/ethrpc/eth/api.go +++ b/http/ethrpc/eth/api.go @@ -295,9 +295,6 @@ func (api *EthereumAPI) Call(args types2.CallArgs, blockNumber types2.BlockNumbe if len(res.Revert()) > 0 { return nil, newRevertError(res) } - if res.Failed() { - return nil, res.Err - } return res.Return(), res.Err }