From 77305edd00c1920a1529c378b8f81f6b20096f05 Mon Sep 17 00:00:00 2001 From: KamiD <44460798+KamiD@users.noreply.github.com> Date: Tue, 25 May 2021 10:42:19 +0800 Subject: [PATCH] =?UTF-8?q?Merge=20PR=20=EF=BC=9Afix=20function=20error=20?= =?UTF-8?q?in=20keywords=20of=20block.number/hash/timestamp=20(#879)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix error in keywords of block.number/hash/timestamp * optimize code --- app/rpc/namespaces/eth/simulation/evm.go | 41 ++++++++++++++++-- app/rpc/namespaces/eth/simulation/impl.go | 52 ++++++++++++++++++++--- x/evm/watcher/querier.go | 19 +++++++++ 3 files changed, 103 insertions(+), 9 deletions(-) diff --git a/app/rpc/namespaces/eth/simulation/evm.go b/app/rpc/namespaces/eth/simulation/evm.go index 3a2e06d1ef..9b0a57f04c 100644 --- a/app/rpc/namespaces/eth/simulation/evm.go +++ b/app/rpc/namespaces/eth/simulation/evm.go @@ -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" @@ -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, } } @@ -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) @@ -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 } diff --git a/app/rpc/namespaces/eth/simulation/impl.go b/app/rpc/namespaces/eth/simulation/impl.go index 473c82d9ce..90e01dab5c 100644 --- a/app/rpc/namespaces/eth/simulation/impl.go +++ b/app/rpc/namespaces/eth/simulation/impl.go @@ -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" @@ -201,10 +203,55 @@ 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 @@ -212,7 +259,6 @@ type StateStore struct { func (s StateStore) Set(key, value []byte) { //just ignore all set opt - return } func (s StateStore) Get(key []byte) []byte { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/x/evm/watcher/querier.go b/x/evm/watcher/querier.go index ff3c299630..0039162487 100644 --- a/x/evm/watcher/querier.go +++ b/x/evm/watcher/querier.go @@ -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)