Skip to content

Commit

Permalink
Change validatorCache to a struct
Browse files Browse the repository at this point in the history
As proposed by Jacky, change `db.stateValidators` into a structure
containing an LRU cache of 1000 items, and a dictionary of dirtied
validators with addresses as keys and wrappers as values. Change the
signature of `db.ValidatorWrapper` to allow callers to inform the
function that they do not intend to dirty the wrapper.
  • Loading branch information
MaxMustermann2 committed Jan 4, 2022
1 parent 0a32a00 commit 944b001
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 84 deletions.
12 changes: 6 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,7 @@ func (bc *BlockChain) ReadValidatorInformationAtState(
if state == nil {
return nil, errors.New("empty state")
}
wrapper, err := state.ValidatorWrapper(addr, true, false)
wrapper, err := state.ValidatorWrapper(addr, true, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2408,7 +2408,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
}
// This means it's already in staking epoch
if currentEpochSuperCommittee.Epoch != nil {
wrapper, err := state.ValidatorWrapper(currentValidator, true, false)
wrapper, err := state.ValidatorWrapper(currentValidator, true, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2489,7 +2489,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
// compute APR for validators in current committee only
if currentEpochSuperCommittee.Epoch != nil {
if _, ok := existing.LookupSet[key]; ok {
wrapper, err := state.ValidatorWrapper(key, true, false)
wrapper, err := state.ValidatorWrapper(key, true, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2555,7 +2555,7 @@ func (bc *BlockChain) UpdateValidatorSnapshots(
// Read all validator's current data and snapshot them
for i := range allValidators {
// The snapshot will be captured in the state after the last epoch block is finalized
validator, err := state.ValidatorWrapper(allValidators[i], true, false)
validator, err := state.ValidatorWrapper(allValidators[i], true, false, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -2701,7 +2701,7 @@ func (bc *BlockChain) UpdateStakingMetaData(
}

// Update validator snapshot for the new validator
validator, err := state.ValidatorWrapper(addr, true, false)
validator, err := state.ValidatorWrapper(addr, true, false, false)
if err != nil {
return newValidators, err
}
Expand Down Expand Up @@ -2868,7 +2868,7 @@ func (bc *BlockChain) addDelegationIndex(

// Found the delegation from state and add the delegation index
// Note this should read from the state of current block in concern
wrapper, err := state.ValidatorWrapper(validatorAddress, true, false)
wrapper, err := state.ValidatorWrapper(validatorAddress, true, false, false)
if err != nil {
return delegations, err
}
Expand Down
12 changes: 6 additions & 6 deletions core/staking_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func checkDuplicateFields(

for _, addr := range addrs {
if !bytes.Equal(validator.Bytes(), addr.Bytes()) {
wrapper, err := state.ValidatorWrapper(addr, true, false)
wrapper, err := state.ValidatorWrapper(addr, true, false, false)

if err != nil {
return err
Expand Down Expand Up @@ -157,7 +157,7 @@ func VerifyAndEditValidatorFromMsg(
return nil, err
}
// request a copy, but delegations are not being changed so do not deep copy them
wrapper, err := stateDB.ValidatorWrapper(msg.ValidatorAddress, false, false)
wrapper, err := stateDB.ValidatorWrapper(msg.ValidatorAddress, false, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func VerifyAndDelegateFromMsg(
for i := range delegations {
delegationIndex := &delegations[i]
// request a copy, and since delegations will be changed, copy them too
wrapper, err := stateDB.ValidatorWrapper(delegationIndex.ValidatorAddress, false, true)
wrapper, err := stateDB.ValidatorWrapper(delegationIndex.ValidatorAddress, false, false, true)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func VerifyAndDelegateFromMsg(
if delegateeWrapper == nil {
var err error
// request a copy, and since delegations will be changed, copy them too
delegateeWrapper, err = stateDB.ValidatorWrapper(msg.ValidatorAddress, false, true)
delegateeWrapper, err = stateDB.ValidatorWrapper(msg.ValidatorAddress, false, false, true)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func VerifyAndUndelegateFromMsg(
return nil, errValidatorNotExist
}

wrapper, err := stateDB.ValidatorWrapper(msg.ValidatorAddress, false, true)
wrapper, err := stateDB.ValidatorWrapper(msg.ValidatorAddress, false, false, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func VerifyAndCollectRewardsFromDelegation(
for i := range delegations {
delegation := &delegations[i]
// request a copy, and since delegations will be changed (.Reward.Set), copy them too
wrapper, err := stateDB.ValidatorWrapper(delegation.ValidatorAddress, false, true)
wrapper, err := stateDB.ValidatorWrapper(delegation.ValidatorAddress, false, false, true)
if err != nil {
return nil, nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions core/staking_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestCheckDuplicateFields(t *testing.T) {
bc: makeFakeChainContextForStake(),
sdb: func(t *testing.T) *state.DB {
sdb := makeStateDBForStake(t)
vw, err := sdb.ValidatorWrapper(makeTestAddr(0), false, true)
vw, err := sdb.ValidatorWrapper(makeTestAddr(0), false, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestVerifyAndEditValidatorFromMsg(t *testing.T) {
// 13: cannot update a banned validator
sdb: func(t *testing.T) *state.DB {
sdb := makeStateDBForStake(t)
vw, err := sdb.ValidatorWrapper(validatorAddr, false, true)
vw, err := sdb.ValidatorWrapper(validatorAddr, false, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func makeStateForRedelegate(t *testing.T) *state.DB {
}

func addStateUndelegationForAddr(sdb *state.DB, addr common.Address, epoch *big.Int) error {
w, err := sdb.ValidatorWrapper(addr, false, true)
w, err := sdb.ValidatorWrapper(addr, false, false, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -1253,7 +1253,7 @@ func TestVerifyAndUndelegateFromMsg(t *testing.T) {
// 4: Extract tokens from banned validator
sdb: func(t *testing.T) *state.DB {
sdb := makeDefaultStateForUndelegate(t)
w, err := sdb.ValidatorWrapper(validatorAddr, false, true)
w, err := sdb.ValidatorWrapper(validatorAddr, false, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1574,7 +1574,7 @@ func makeStateForReward(t *testing.T) *state.DB {
}

func addStateRewardForAddr(sdb *state.DB, addr common.Address, rewards []*big.Int) error {
w, err := sdb.ValidatorWrapper(addr, false, true)
w, err := sdb.ValidatorWrapper(addr, false, false, true)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type (
validatorWrapperChange struct {
address *common.Address
prev *stk.ValidatorWrapper
dirty bool
}

// Changes to other state values.
Expand Down Expand Up @@ -199,7 +200,12 @@ func (ch codeChange) dirtied() *common.Address {
}

func (ch validatorWrapperChange) revert(s *DB) {
s.stateValidators.Add(ch.address.Hex(), ch.prev)
s.stateValidators.cache.Add(ch.address.Hex(), ch.prev)
if ch.dirty {
s.stateValidators.dirty[*ch.address] = ch.prev
} else {
delete(s.stateValidators.dirty, *ch.address)
}
}

func (ch validatorWrapperChange) dirtied() *common.Address {
Expand Down
Loading

0 comments on commit 944b001

Please sign in to comment.