Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

saving resivision id to store #680

Closed
wants to merge 11 commits into from
10 changes: 10 additions & 0 deletions x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
// reset counters that are used on CommitStateDB.Prepare
k.Bloom = big.NewInt(0)
k.TxCount = 0

//loading revision id from store in every block, make sure the `csdb.nextRevisionId` consistent in every node
id, err := k.GetRevisionID(ctx)
if err != nil {
panic(err)
KamiD marked this conversation as resolved.
Show resolved Hide resolved
}
k.CommitStateDB.SetRevisionID(id)
KamiD marked this conversation as resolved.
Show resolved Hide resolved
KamiD marked this conversation as resolved.
Show resolved Hide resolved
}

// EndBlock updates the accounts and commits state objects to the KV Store, while
Expand All @@ -57,5 +64,8 @@ func (k Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.Valid
bloom := ethtypes.BytesToBloom(k.Bloom.Bytes())
k.SetBlockBloom(ctx, req.Height, bloom)

//set the `next revision id` to store
k.SetRevisionID(ctx, k.CommitStateDB.GetRevisionID())
KamiD marked this conversation as resolved.
Show resolved Hide resolved

return []abci.ValidatorUpdate{}
}
1 change: 1 addition & 0 deletions x/evm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (suite *KeeperTestSuite) TestBeginBlock() {
suite.app.EvmKeeper.Bloom.SetInt64(10)
suite.app.EvmKeeper.TxCount = 10

suite.app.EvmKeeper.EndBlock(suite.ctx, abci.RequestEndBlock{})
suite.app.EvmKeeper.BeginBlock(suite.ctx, abci.RequestBeginBlock{})
suite.Require().NotZero(suite.app.EvmKeeper.Bloom.Int64())
suite.Require().NotZero(suite.app.EvmKeeper.TxCount)
Expand Down
17 changes: 17 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/binary"
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -129,6 +130,22 @@ func (k Keeper) SetBlockBloom(ctx sdk.Context, height int64, bloom ethtypes.Bloo
store.Set(types.BloomKey(height), bloom.Bytes())
}

func (k Keeper) GetRevisionID(ctx sdk.Context) (int, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixRevisionID)
bz := store.Get(types.RevisionKey())
if len(bz) == 0 {
return 0, errors.New("failed to get revision id from store")
}

revision := binary.BigEndian.Uint64(bz)
return int(revision), nil
}

func (k Keeper) SetRevisionID(ctx sdk.Context, revisionID int) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixRevisionID)
store.Set(types.RevisionKey(), sdk.Uint64ToBigEndian(uint64(revisionID)))
}

// GetAllTxLogs return all the transaction logs from the store.
func (k Keeper) GetAllTxLogs(ctx sdk.Context) []types.TransactionLogs {
store := ctx.KVStore(k.storeKey)
Expand Down
7 changes: 7 additions & 0 deletions x/evm/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (

// RouterKey uses module name for routing
RouterKey = ModuleName

RevisionIDKey = ModuleName + "_RevisionID"
KamiD marked this conversation as resolved.
Show resolved Hide resolved
)

// KVStore key prefixes
Expand All @@ -28,6 +30,7 @@ var (
KeyPrefixStorage = []byte{0x05}
KeyPrefixChainConfig = []byte{0x06}
KeyPrefixHeightHash = []byte{0x07}
KeyPrefixRevisionID = []byte{0x07}
)

// HeightHashKey returns the key for the given chain epoch and height.
Expand All @@ -44,6 +47,10 @@ func BloomKey(height int64) []byte {
return sdk.Uint64ToBigEndian(uint64(height))
}

func RevisionKey() []byte {
KamiD marked this conversation as resolved.
Show resolved Hide resolved
return []byte(RevisionIDKey)
}

// AddressStoragePrefix returns a prefix to iterate over a given account storage.
func AddressStoragePrefix(address ethcmn.Address) []byte {
return append(KeyPrefixStorage, address.Bytes()...)
Expand Down
8 changes: 8 additions & 0 deletions x/evm/types/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,14 @@ func (csdb *CommitStateDB) Snapshot() int {
return id
}

func (csdb *CommitStateDB) GetRevisionID() int {
return csdb.nextRevisionID
KamiD marked this conversation as resolved.
Show resolved Hide resolved
}

func (csdb *CommitStateDB) SetRevisionID(id int) {
csdb.nextRevisionID = id
KamiD marked this conversation as resolved.
Show resolved Hide resolved
}

// RevertToSnapshot reverts all state changes made since the given revision.
func (csdb *CommitStateDB) RevertToSnapshot(revID int) {
// find the snapshot in the stack of valid snapshots
Expand Down