Skip to content

Commit

Permalink
Merge PR :fix function error in keywords of block.number/hash/timesta…
Browse files Browse the repository at this point in the history
…mp (#879)

* fix error in keywords of block.number/hash/timestamp

* optimize code
  • Loading branch information
KamiD authored May 25, 2021
1 parent df1f078 commit 77305ed
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
41 changes: 37 additions & 4 deletions app/rpc/namespaces/eth/simulation/evm.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package simulation

import (
"time"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/ethereum/go-ethereum/common"
"github.com/okex/exchain/x/evm"
evmtypes "github.com/okex/exchain/x/evm/types"
"github.com/okex/exchain/x/evm/watcher"
Expand All @@ -25,12 +28,42 @@ func NewEvmFactory(chainId string, q *watcher.Querier) EvmFactory {

func (ef EvmFactory) BuildSimulator(qoc QueryOnChainProxy) *EvmSimulator {
keeper := ef.makeEvmKeeper(qoc)

if !watcher.IsWatcherEnabled() {
return nil
}
timestamp := time.Now()

latest, _ := ef.WrappedQuerier.GetLatestBlockNumber()
hash, e := ef.WrappedQuerier.GetBlockHashByNumber(latest)
if e != nil {
hash = common.HexToHash("0x000000000000000000000000000000")
}

block, e := ef.WrappedQuerier.GetBlockByHash(hash, false)

if e == nil {
timestamp = time.Unix(int64(block.Timestamp), 0)
}
req := abci.RequestBeginBlock{
Header: abci.Header{
ChainID: ef.ChainId,
LastBlockId: abci.BlockID{
Hash: hash.Bytes(),
},
Height: int64(latest),
Time: timestamp,
},
Hash: hash.Bytes(),
}

ctx := ef.makeContext(keeper, req.Header)

keeper.BeginBlock(ctx, req)

return &EvmSimulator{
handler: evm.NewHandler(keeper),
ctx: ef.makeContext(keeper),
ctx: ctx,
}
}

Expand Down Expand Up @@ -60,7 +93,7 @@ func (ef EvmFactory) makeEvmKeeper(qoc QueryOnChainProxy) *evm.Keeper {
return evm.NewSimulateKeeper(cdc, sdk.NewKVStoreKey(evm.StoreKey), NewSubspaceProxy(), NewAccountKeeperProxy(qoc), SupplyKeeperProxy{}, NewBankKeeperProxy(), NewInternalDba(qoc))
}

func (ef EvmFactory) makeContext(k *evm.Keeper) sdk.Context {
func (ef EvmFactory) makeContext(k *evm.Keeper, header abci.Header) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
authKey := sdk.NewKVStoreKey(auth.StoreKey)
Expand All @@ -72,7 +105,7 @@ func (ef EvmFactory) makeContext(k *evm.Keeper) sdk.Context {
cms.MountStoreWithDB(paramsTKey, sdk.StoreTypeTransient, db)

cms.LoadLatestVersion()
latest, _ := ef.WrappedQuerier.GetLatestBlockNumber()
ctx := sdk.NewContext(cms, abci.Header{ChainID: ef.ChainId, Height: int64(latest)}, true, tmlog.NewNopLogger()).WithGasMeter(sdk.NewGasMeter(evmtypes.DefaultMaxGasLimitPerTx))

ctx := sdk.NewContext(cms, header, true, tmlog.NewNopLogger()).WithGasMeter(sdk.NewGasMeter(evmtypes.DefaultMaxGasLimitPerTx))
return ctx
}
52 changes: 47 additions & 5 deletions app/rpc/namespaces/eth/simulation/impl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package simulation

import (
"encoding/binary"

"github.com/cosmos/cosmos-sdk/codec"
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -201,18 +203,62 @@ func (i InternalDba) NewStore(parent store.KVStore, Prefix []byte) evmtypes.Stor
return ContractDeploymentWhitelist{watcher.NewQuerier()}
case evmtypes.KeyPrefixCode[0]:
return CodeStore{q: watcher.NewQuerier(), ocProxy: i.ocProxy}
case evmtypes.KeyPrefixHeightHash[0]:
return HeightHashStore{watcher.NewQuerier()}
case evmtypes.KeyPrefixBlockHash[0]:
return BlockHashStore{}
}
return nil
}

type HeightHashStore struct {
q *watcher.Querier
}

func (s HeightHashStore) Set(key, value []byte) {
//just ignore all set opt
}

func (s HeightHashStore) Get(key []byte) []byte {
h, _ := s.q.GetBlockHashByNumber(binary.BigEndian.Uint64(key))
return h.Bytes()
}

func (s HeightHashStore) Has(key []byte) bool {
return false
}

func (s HeightHashStore) Delete(key []byte) {
return
}

type BlockHashStore struct {
}

func (s BlockHashStore) Set(key, value []byte) {
//just ignore all set opt
}

func (s BlockHashStore) Get(key []byte) []byte {

return nil
}

func (s BlockHashStore) Has(key []byte) bool {
return false
}

func (s BlockHashStore) Delete(key []byte) {
return
}

type StateStore struct {
addr common.Address
ocProxy QueryOnChainProxy
}

func (s StateStore) Set(key, value []byte) {
//just ignore all set opt
return
}

func (s StateStore) Get(key []byte) []byte {
Expand Down Expand Up @@ -258,7 +304,6 @@ type BloomStore struct {

func (s BloomStore) Set(key, value []byte) {
//just ignore all set opt
return
}

func (s BloomStore) Get(key []byte) []byte {
Expand All @@ -280,7 +325,6 @@ type CodeStore struct {

func (s CodeStore) Set(key, value []byte) {
//just ignore all set opt
return
}

func (s CodeStore) Get(key []byte) []byte {
Expand All @@ -306,7 +350,6 @@ type ContractBlockedListStore struct {

func (s ContractBlockedListStore) Set(key, value []byte) {
//just ignore all set opt
return
}

func (s ContractBlockedListStore) Get(key []byte) []byte {
Expand All @@ -328,7 +371,6 @@ type ContractDeploymentWhitelist struct {

func (s ContractDeploymentWhitelist) Set(key, value []byte) {
//just ignore all set opt
return
}

func (s ContractDeploymentWhitelist) Get(key []byte) []byte {
Expand Down
19 changes: 19 additions & 0 deletions x/evm/watcher/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ func (q Querier) GetBlockByHash(hash common.Hash, fullTx bool) (*EthBlock, error
return &block, nil
}

func (q Querier) GetBlockHashByNumber(number uint64) (common.Hash, error) {
if !q.enabled() {
return common.Hash{}, errors.New(MsgFunctionDisable)
}
var height = number
var err error
if height == 0 {
height, err = q.GetLatestBlockNumber()
if err != nil {
return common.Hash{}, err
}
}
hash, e := q.store.Get(append(prefixBlockInfo, []byte(strconv.Itoa(int(height)))...))
if e != nil {
return common.Hash{}, e
}
return common.HexToHash(string(hash)), e
}

func (q Querier) GetBlockByNumber(number uint64, fullTx bool) (*EthBlock, error) {
if !q.enabled() {
return nil, errors.New(MsgFunctionDisable)
Expand Down

0 comments on commit 77305ed

Please sign in to comment.