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
3 changes: 3 additions & 0 deletions x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,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.CommitStateDB.SetRevisionID()

return []abci.ValidatorUpdate{}
}
5 changes: 5 additions & 0 deletions x/evm/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
KeyPrefixStorage = []byte{0x05}
KeyPrefixChainConfig = []byte{0x06}
KeyPrefixHeightHash = []byte{0x07}
KeyPrefixRevisionID = []byte{0x08}
)

// HeightHashKey returns the key for the given chain epoch and height.
Expand All @@ -44,6 +45,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(ModuleName + "_RevisionID")
}

// AddressStoragePrefix returns a prefix to iterate over a given account storage.
func AddressStoragePrefix(address ethcmn.Address) []byte {
return append(KeyPrefixStorage, address.Bytes()...)
Expand Down
22 changes: 21 additions & 1 deletion x/evm/types/statedb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/binary"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -595,7 +596,7 @@ func (csdb *CommitStateDB) deleteStateObject(so *stateObject) {

// Snapshot returns an identifier for the current revision of the state.
func (csdb *CommitStateDB) Snapshot() int {
id := csdb.nextRevisionID
id := csdb.nextRevisionID + csdb.GetRevisionID()
csdb.nextRevisionID++

csdb.validRevisions = append(
Expand All @@ -609,6 +610,25 @@ func (csdb *CommitStateDB) Snapshot() int {
return id
}

//GetRevisionID return the revision id from store, return value will be zero if revision id not exists in store
func (csdb *CommitStateDB) GetRevisionID() int {
store := prefix.NewStore(csdb.ctx.KVStore(csdb.storeKey), KeyPrefixRevisionID)
bz := store.Get(RevisionKey()) //if not exist, that means RevisionId was never used, return 0
if len(bz) == 0 {
return 0
}

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

//set newest revision id to store
func (csdb *CommitStateDB) SetRevisionID() {
revisionID := csdb.GetRevisionID() + csdb.nextRevisionID
KamiD marked this conversation as resolved.
Show resolved Hide resolved
store := prefix.NewStore(csdb.ctx.KVStore(csdb.storeKey), KeyPrefixRevisionID)
store.Set(RevisionKey(), sdk.Uint64ToBigEndian(uint64(revisionID)))
}

// 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