Skip to content

Commit 76f3735

Browse files
committed
all: rework statedb utils
1 parent 07328dc commit 76f3735

File tree

7 files changed

+55
-57
lines changed

7 files changed

+55
-57
lines changed

contracts/utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func BuildTxOpeningRandomize(nonce uint64, randomizeAddr common.Address, randomi
210210

211211
// Get signers signed for blockNumber from blockSigner contract.
212212
func GetSignersFromContract(statedb *state.StateDB, block *types.Block) ([]common.Address, error) {
213-
return state.GetSigners(statedb, block), nil
213+
return statedb.GetSigners(block), nil
214214
}
215215

216216
// Get signers signed for blockNumber from blockSigner contract.
@@ -412,8 +412,7 @@ func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*
412412

413413
// Get candidate owner by address.
414414
func GetCandidatesOwnerBySigner(statedb *state.StateDB, signerAddr common.Address) common.Address {
415-
owner := state.GetCandidateOwner(statedb, signerAddr)
416-
return owner
415+
return statedb.GetCandidateOwner(signerAddr)
417416
}
418417

419418
func CalculateRewardForHolders(foundationWalletAddr common.Address, state *state.StateDB, signer common.Address, calcReward *big.Int, blockNumber uint64) (map[common.Address]*big.Int, error) {
@@ -431,7 +430,7 @@ func GetRewardBalancesRate(foundationWalletAddr common.Address, statedb *state.S
431430
rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100))
432431
balances[owner] = rewardMaster
433432
// Get voters for masternode.
434-
voters := state.GetVoters(statedb, masterAddr)
433+
voters := statedb.GetVoters(masterAddr)
435434

436435
if len(voters) > 0 {
437436
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent))
@@ -443,7 +442,7 @@ func GetRewardBalancesRate(foundationWalletAddr common.Address, statedb *state.S
443442
if _, ok := voterCaps[voteAddr]; ok && common.TIP2019Block.Uint64() <= blockNumber {
444443
continue
445444
}
446-
voterCap := state.GetVoterCap(statedb, masterAddr, voteAddr)
445+
voterCap := statedb.GetVoterCap(masterAddr, voteAddr)
447446
totalCap.Add(totalCap, voterCap)
448447
voterCaps[voteAddr] = voterCap
449448
}

core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,7 @@ func (bc *BlockChain) UpdateM1() error {
27882788
} else if stateDB == nil {
27892789
return errors.New("nil stateDB in UpdateM1")
27902790
} else {
2791-
candidates = state.GetCandidates(stateDB)
2791+
candidates = stateDB.GetCandidates()
27922792
}
27932793

27942794
var ms []utils.Masternode

core/state/statedb_utils.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/XinFinOrg/XDPoSChain/common"
77
"github.com/XinFinOrg/XDPoSChain/core/types"
8-
98
"github.com/XinFinOrg/XDPoSChain/crypto"
109
)
1110

@@ -16,19 +15,19 @@ var (
1615
}
1716
)
1817

19-
func GetSigners(statedb *StateDB, block *types.Block) []common.Address {
18+
func (s *StateDB) GetSigners(block *types.Block) []common.Address {
2019
slot := slotBlockSignerMapping["blockSigners"]
2120
keys := []common.Hash{}
2221
keyArrSlot := GetLocMappingAtKey(block.Hash(), slot)
23-
arrSlot := statedb.GetState(common.BlockSignersBinary, common.BigToHash(keyArrSlot))
22+
arrSlot := s.GetState(common.BlockSignersBinary, common.BigToHash(keyArrSlot))
2423
arrLength := arrSlot.Big().Uint64()
25-
for i := uint64(0); i < arrLength; i++ {
24+
for i := range arrLength {
2625
key := GetLocDynamicArrAtElement(common.BigToHash(keyArrSlot), i, 1)
2726
keys = append(keys, key)
2827
}
2928
rets := []common.Address{}
3029
for _, key := range keys {
31-
ret := statedb.GetState(common.BlockSignersBinary, key)
30+
ret := s.GetState(common.BlockSignersBinary, key)
3231
rets = append(rets, common.HexToAddress(ret.Hex()))
3332
}
3433

@@ -42,27 +41,27 @@ var (
4241
}
4342
)
4443

45-
func GetSecret(statedb *StateDB, address common.Address) [][32]byte {
44+
func (s *StateDB) GetSecret(address common.Address) [][32]byte {
4645
slot := slotRandomizeMapping["randomSecret"]
4746
locSecret := GetLocMappingAtKey(address.Hash(), slot)
48-
arrLength := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
47+
arrLength := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
4948
keys := []common.Hash{}
5049
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
5150
key := GetLocDynamicArrAtElement(common.BigToHash(locSecret), i, 1)
5251
keys = append(keys, key)
5352
}
5453
rets := [][32]byte{}
5554
for _, key := range keys {
56-
ret := statedb.GetState(common.RandomizeSMCBinary, key)
55+
ret := s.GetState(common.RandomizeSMCBinary, key)
5756
rets = append(rets, ret)
5857
}
5958
return rets
6059
}
6160

62-
func GetOpening(statedb *StateDB, address common.Address) [32]byte {
61+
func (s *StateDB) GetOpening(address common.Address) [32]byte {
6362
slot := slotRandomizeMapping["randomOpening"]
6463
locOpening := GetLocMappingAtKey(address.Hash(), slot)
65-
ret := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
64+
ret := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
6665
return ret
6766
}
6867

@@ -89,16 +88,16 @@ var (
8988
}
9089
)
9190

92-
func GetCandidates(statedb *StateDB) []common.Address {
91+
func (s *StateDB) GetCandidates() []common.Address {
9392
slot := slotValidatorMapping["candidates"]
9493
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
95-
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, slotHash)
94+
arrLength := s.GetState(common.MasternodeVotingSMCBinary, slotHash)
9695
count := arrLength.Big().Uint64()
9796
rets := make([]common.Address, 0, count)
9897

99-
for i := uint64(0); i < count; i++ {
98+
for i := range count {
10099
key := GetLocDynamicArrAtElement(slotHash, i, 1)
101-
ret := statedb.GetState(common.MasternodeVotingSMCBinary, key)
100+
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
102101
if !ret.IsZero() {
103102
rets = append(rets, common.HexToAddress(ret.Hex()))
104103
}
@@ -107,49 +106,49 @@ func GetCandidates(statedb *StateDB) []common.Address {
107106
return rets
108107
}
109108

110-
func GetCandidateOwner(statedb *StateDB, candidate common.Address) common.Address {
109+
func (s *StateDB) GetCandidateOwner(candidate common.Address) common.Address {
111110
slot := slotValidatorMapping["validatorsState"]
112111
// validatorsState[_candidate].owner;
113112
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
114113
locCandidateOwner := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(0)))
115-
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
114+
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
116115
return common.HexToAddress(ret.Hex())
117116
}
118117

119-
func GetCandidateCap(statedb *StateDB, candidate common.Address) *big.Int {
118+
func (s *StateDB) GetCandidateCap(candidate common.Address) *big.Int {
120119
slot := slotValidatorMapping["validatorsState"]
121120
// validatorsState[_candidate].cap;
122121
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
123122
locCandidateCap := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(1)))
124-
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateCap))
123+
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateCap))
125124
return ret.Big()
126125
}
127126

128-
func GetVoters(statedb *StateDB, candidate common.Address) []common.Address {
127+
func (s *StateDB) GetVoters(candidate common.Address) []common.Address {
129128
//mapping(address => address[]) voters;
130129
slot := slotValidatorMapping["voters"]
131130
locVoters := GetLocMappingAtKey(candidate.Hash(), slot)
132-
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
131+
arrLength := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
133132
keys := []common.Hash{}
134133
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
135134
key := GetLocDynamicArrAtElement(common.BigToHash(locVoters), i, 1)
136135
keys = append(keys, key)
137136
}
138137
rets := []common.Address{}
139138
for _, key := range keys {
140-
ret := statedb.GetState(common.MasternodeVotingSMCBinary, key)
139+
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
141140
rets = append(rets, common.HexToAddress(ret.Hex()))
142141
}
143142

144143
return rets
145144
}
146145

147-
func GetVoterCap(statedb *StateDB, candidate, voter common.Address) *big.Int {
146+
func (s *StateDB) GetVoterCap(candidate, voter common.Address) *big.Int {
148147
slot := slotValidatorMapping["validatorsState"]
149148
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
150149
locCandidateVoters := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(2)))
151150
retByte := crypto.Keccak256(voter.Hash().Bytes(), common.BigToHash(locCandidateVoters).Bytes())
152-
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
151+
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
153152
return ret.Big()
154153
}
155154

@@ -158,29 +157,29 @@ var (
158157
slotMintedRecordLastEpochNum uint64 = 1
159158
)
160159

161-
func GetTotalMinted(statedb *StateDB) common.Hash {
160+
func (s *StateDB) GetTotalMinted() common.Hash {
162161
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
163-
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
162+
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
164163
return totalMinted
165164
}
166165

167-
func PutTotalMinted(statedb *StateDB, value common.Hash) {
166+
func (s *StateDB) PutTotalMinted(value common.Hash) {
168167
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
169-
statedb.SetState(common.MintedRecordAddressBinary, hash, value)
168+
s.SetState(common.MintedRecordAddressBinary, hash, value)
170169
}
171170

172-
func GetLastEpochNum(statedb *StateDB) common.Hash {
171+
func (s *StateDB) GetLastEpochNum() common.Hash {
173172
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
174-
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
173+
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
175174
return totalMinted
176175
}
177176

178-
func PutLastEpochNum(statedb *StateDB, value common.Hash) {
177+
func (s *StateDB) PutLastEpochNum(value common.Hash) {
179178
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
180-
statedb.SetState(common.MintedRecordAddressBinary, hash, value)
179+
s.SetState(common.MintedRecordAddressBinary, hash, value)
181180
}
182181

183-
func IncrementMintedRecordNonce(statedb *StateDB) {
184-
nonce := statedb.GetNonce(common.MintedRecordAddressBinary)
185-
statedb.SetNonce(common.MintedRecordAddressBinary, nonce+1)
182+
func (s *StateDB) IncrementMintedRecordNonce() {
183+
nonce := s.GetNonce(common.MintedRecordAddressBinary)
184+
s.SetNonce(common.MintedRecordAddressBinary, nonce+1)
186185
}

eth/api_backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func (b *EthAPIBackend) GetVotersCap(checkpoint *big.Int, masterAddr common.Addr
557557

558558
voterCaps := make(map[common.Address]*big.Int)
559559
for _, voteAddr := range voters {
560-
voterCap := state.GetVoterCap(statedb, masterAddr, voteAddr)
560+
voterCap := statedb.GetVoterCap(masterAddr, voteAddr)
561561
voterCaps[voteAddr] = voterCap
562562
}
563563
return voterCaps
@@ -590,11 +590,11 @@ func (b *EthAPIBackend) GetMasternodesCap(checkpoint uint64) map[common.Address]
590590
return nil
591591
}
592592

593-
candicates := state.GetCandidates(statedb)
593+
candicates := statedb.GetCandidates()
594594

595595
masternodesCap := map[common.Address]*big.Int{}
596596
for _, candicate := range candicates {
597-
masternodesCap[candicate] = state.GetCandidateCap(statedb, candicate)
597+
masternodesCap[candicate] = statedb.GetCandidateCap(candicate)
598598
}
599599

600600
return masternodesCap

eth/hooks/engine_v1_hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func AttachConsensusV1Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
232232
return nil, errors.New("nil stateDB in HookGetSignersFromContract")
233233
}
234234

235-
candidateAddresses = state.GetCandidates(stateDB)
235+
candidateAddresses = stateDB.GetCandidates()
236236
for _, address := range candidateAddresses {
237237
v, err := validator.GetCandidateCap(opts, address)
238238
if err != nil {

eth/hooks/engine_v2_hooks.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
361361
rewardsMap[rwt.key] = rewardResults
362362
}
363363
// record the total reward into state db
364-
totalMinted := state.GetTotalMinted(stateBlock).Big()
365-
lastEpochNum := state.GetLastEpochNum(stateBlock)
364+
totalMinted := stateBlock.GetTotalMinted().Big()
365+
lastEpochNum := stateBlock.GetLastEpochNum()
366366
if lastEpochNum.IsZero() {
367367
// if `lastEpochNum` is zero, the total minted has not included tokens before TIPUpgradeReward
368368
// calculate the tokens before TIPUpgradeReward and set to totalMinted
@@ -377,10 +377,10 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
377377
log.Warn("[HookReward] total minted overflow max u256")
378378
}
379379
log.Debug("[HookReward] total minted in hook", "value", totalMinted)
380-
state.PutTotalMinted(stateBlock, common.BigToHash(totalMinted))
381-
state.PutLastEpochNum(stateBlock, common.Uint64ToHash(epochNum))
380+
stateBlock.PutTotalMinted(common.BigToHash(totalMinted))
381+
stateBlock.PutLastEpochNum(common.Uint64ToHash(epochNum))
382382
// Increment nonce so that statedb does not treat it as empty account
383-
state.IncrementMintedRecordNonce(stateBlock)
383+
stateBlock.IncrementMintedRecordNonce()
384384
}
385385
log.Debug("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
386386
return rewardsMap, nil
@@ -433,12 +433,12 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type
433433
nodesToKeep[MasterNodeBeneficiary] = c.GetMasternodesFromCheckpointHeader(h)
434434
// in reward upgrade, add protector and observer nodes
435435
if chain.Config().IsTIPUpgradeReward(header.Number) {
436-
candidates := state.GetCandidates(parentState)
436+
candidates := parentState.GetCandidates()
437437
var ms []utils.Masternode
438438
for _, candidate := range candidates {
439439
// ignore "0x0000000000000000000000000000000000000000"
440440
if !candidate.IsZero() {
441-
v := state.GetCandidateCap(parentState, candidate)
441+
v := parentState.GetCandidateCap(candidate)
442442
ms = append(ms, utils.Masternode{Address: candidate, Stake: v})
443443
}
444444
}

internal/ethapi/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,10 @@ func (api *BlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAddres
837837
result[fieldSuccess] = false
838838
return result, errors.New("nil statedb in GetCandidateStatus")
839839
}
840-
candidatesAddresses := state.GetCandidates(statedb)
840+
candidatesAddresses := statedb.GetCandidates()
841841
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
842842
for _, address := range candidatesAddresses {
843-
v := state.GetCandidateCap(statedb, address)
843+
v := statedb.GetCandidateCap(address)
844844
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
845845
}
846846
}
@@ -996,10 +996,10 @@ func (api *BlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.EpochNumb
996996
result[fieldSuccess] = false
997997
return result, errors.New("nil statedb in GetCandidates")
998998
}
999-
candidatesAddresses := state.GetCandidates(statedb)
999+
candidatesAddresses := statedb.GetCandidates()
10001000
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
10011001
for _, address := range candidatesAddresses {
1002-
v := state.GetCandidateCap(statedb, address)
1002+
v := statedb.GetCandidateCap(address)
10031003
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
10041004
}
10051005
}
@@ -2610,8 +2610,8 @@ func (api *BlockChainAPI) GetCurrentTotalMinted(ctx context.Context) (*currentTo
26102610
if err != nil {
26112611
return nil, err
26122612
}
2613-
totalMinted := state.GetTotalMinted(statedb).Big()
2614-
lastEpochNum := state.GetLastEpochNum(statedb).Big()
2613+
totalMinted := statedb.GetTotalMinted().Big()
2614+
lastEpochNum := statedb.GetLastEpochNum().Big()
26152615
result := &currentTotalMinted{
26162616
TotalMinted: (*hexutil.Big)(totalMinted),
26172617
LastEpochNum: (*hexutil.Big)(lastEpochNum),

0 commit comments

Comments
 (0)