Skip to content

Commit

Permalink
Merge 5ea945a into bb168c5
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovers authored May 16, 2023
2 parents bb168c5 + 5ea945a commit 29274d7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
54 changes: 17 additions & 37 deletions app/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backend

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -147,31 +146,27 @@ func (b *EthermintBackend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullT
if block, err := b.backendCache.GetBlockByNumber(uint64(blockNum), fullTx); err == nil {
return block, nil
}
block, err := b.wrappedBackend.GetBlockByNumber(uint64(blockNum), fullTx)
if err == nil {
b.backendCache.AddOrUpdateBlock(block.Hash, block, fullTx)
return block, nil
}
height := blockNum.Int64()
if height <= 0 {
// get latest block height
height = b.BlockNumber()
}
res, _, err := b.clientCtx.Query(fmt.Sprintf("custom/%s/%s/%d", evmtypes.ModuleName, evmtypes.QueryEthBlockByHeight, height))
resBlock, err := b.Block(&height)
if err != nil {
return nil, fmt.Errorf("not found block by height(%d)", blockNum)
}
var ethBlock evmtypes.Block
if err := json.Unmarshal(res, &ethBlock); err != nil {

block, err = rpctypes.RpcBlockFromTendermint(b.clientCtx, resBlock.Block, fullTx)
if err != nil {
return nil, err
}
// return empty slice instead of null when there is no transactions in block
if ethBlock.TransactionsRoot == ethtypes.EmptyRootHash {
ethBlock.Transactions = []common.Hash{}
} else if fullTx {
ethTxs, err := b.getBlockFullTxs(height, ethBlock.Hash)
if err != nil {
return nil, err
}
ethBlock.Transactions = ethTxs
}
b.backendCache.AddOrUpdateBlock(ethBlock.Hash, &ethBlock, fullTx)
return &ethBlock, nil
b.backendCache.AddOrUpdateBlock(block.Hash, block, fullTx)
return block, nil
}

func (b *EthermintBackend) getBlockFullTxs(height int64, blockHash common.Hash) ([]*watcher.Transaction, error) {
Expand All @@ -195,32 +190,19 @@ func (b *EthermintBackend) GetBlockByHash(hash common.Hash, fullTx bool) (*evmty
if block, err := b.backendCache.GetBlockByHash(hash, fullTx); err == nil {
return block, err
}
// query block by eth block hash
res, _, err := b.clientCtx.Query(fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryEthBlockByHash, hash.Hex()))
//query block from watch db
block, err := b.wrappedBackend.GetBlockByHash(hash, fullTx)
if err == nil {
var ethBlock evmtypes.Block
if err := json.Unmarshal(res, &ethBlock); err != nil {
return nil, err
}
// return empty slice instead of null when there is no transactions in block
if ethBlock.TransactionsRoot == ethtypes.EmptyRootHash {
ethBlock.Transactions = []common.Hash{}
} else if fullTx {
ethTxs, err := b.getBlockFullTxs(int64(ethBlock.Number), ethBlock.Hash)
if err != nil {
return nil, err
}
ethBlock.Transactions = ethTxs
}
b.backendCache.AddOrUpdateBlock(hash, &ethBlock, fullTx)
return &ethBlock, nil
b.backendCache.AddOrUpdateBlock(hash, block, fullTx)
return block, nil
}

// query block by tendermint block hash
resBlock, err := b.clientCtx.Client.BlockByHash(hash.Bytes())
if err != nil {
return nil, err
}
block, err := rpctypes.RpcBlockFromTendermint(b.clientCtx, resBlock.Block, fullTx)
block, err = rpctypes.RpcBlockFromTendermint(b.clientCtx, resBlock.Block, fullTx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -477,7 +459,6 @@ func (b *EthermintBackend) GetLogs(height int64) ([][]*ethtypes.Log, error) {
default:
return nil, ErrInvalidBlock
}
ethBlockHash := block.EthHash()
// return empty directly when block was produced during stress testing.
var blockLogs = [][]*ethtypes.Log{}
if b.logsLimit > 0 && len(txs) > b.logsLimit {
Expand All @@ -502,7 +483,6 @@ func (b *EthermintBackend) GetLogs(height int64) ([][]*ethtypes.Log, error) {
var validLogs []*ethtypes.Log
for _, log := range execRes.Logs {
if log.BlockNumber == uint64(block.Number) {
log.BlockHash = ethBlockHash
validLogs = append(validLogs, log)
}
}
Expand Down
1 change: 0 additions & 1 deletion app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,6 @@ func (api *PublicEthereumAPI) GetTransactionReceipt(hash common.Hash) (*watcher.
if err != nil {
return nil, err
}

blockHash := common.BytesToHash(block.Block.Hash())

// Convert tx bytes to eth transaction
Expand Down
5 changes: 2 additions & 3 deletions x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ func (k *Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.Vali
params := k.GetParams(ctx)
k.Watcher.SaveParams(params)

k.Watcher.SaveBlock(block, ethBlockHash)

k.Watcher.SaveBlockStdTxHash(ethBlockHash)
k.Watcher.SaveBlock(block)
k.Watcher.SaveBlockStdTxHash()
}
}

Expand Down
12 changes: 7 additions & 5 deletions x/evm/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,32 @@ func (w *Watcher) ExecuteDelayEraseKey(delayEraseKey [][]byte) {
}
}

func (w *Watcher) SaveBlock(block evmtypes.Block, ethBlockHash common.Hash) {
func (w *Watcher) SaveBlock(block evmtypes.Block) {
if !w.Enabled() {
return
}
// update block hash
block.Hash = w.blockHash
if w.InfuraKeeper != nil {
w.InfuraKeeper.OnSaveBlock(block)
}
wMsg := NewMsgBlock(block, ethBlockHash)
wMsg := NewMsgBlock(block, w.blockHash)
if wMsg != nil {
w.batch = append(w.batch, wMsg)
}

wInfo := NewMsgBlockInfo(w.height, ethBlockHash)
wInfo := NewMsgBlockInfo(w.height, w.blockHash)
if wInfo != nil {
w.batch = append(w.batch, wInfo)
}
w.SaveLatestHeight(w.height)
}

func (w *Watcher) SaveBlockStdTxHash(blockHash common.Hash) {
func (w *Watcher) SaveBlockStdTxHash() {
if !w.Enabled() || (len(w.blockStdTxs) == 0) {
return
}
wMsg := NewMsgBlockStdTxHash(w.blockStdTxs, blockHash)
wMsg := NewMsgBlockStdTxHash(w.blockStdTxs, w.blockHash)
if wMsg != nil {
w.batch = append(w.batch, wMsg)
}
Expand Down

0 comments on commit 29274d7

Please sign in to comment.