Skip to content

Commit

Permalink
fix ethrpc bug (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
hero5512 authored Mar 10, 2022
1 parent 2bf19c1 commit accbf14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions core/store/ledgerstore/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions http/ethrpc/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -401,10 +402,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
}
Expand All @@ -417,10 +421,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
}
Expand Down Expand Up @@ -462,16 +469,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))
Expand All @@ -485,16 +495,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))
Expand All @@ -504,6 +517,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 {
Expand Down Expand Up @@ -578,7 +594,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)
}
Expand Down
4 changes: 4 additions & 0 deletions smartcontract/storage/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit accbf14

Please sign in to comment.