go-ethereum/core/state/statedb_utils.go
2025-11-18 11:26:28 +05:30

185 lines
6.1 KiB
Go

package state
import (
"math/big"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
)
var (
slotBlockSignerMapping = map[string]uint64{
"blockSigners": 0,
"blocks": 1,
}
)
func (s *StateDB) GetSigners(block *types.Block) []common.Address {
slot := slotBlockSignerMapping["blockSigners"]
keys := []common.Hash{}
keyArrSlot := GetLocMappingAtKey(block.Hash(), slot)
arrSlot := s.GetState(common.BlockSignersBinary, common.BigToHash(keyArrSlot))
arrLength := arrSlot.Big().Uint64()
for i := range arrLength {
key := GetLocDynamicArrAtElement(common.BigToHash(keyArrSlot), i, 1)
keys = append(keys, key)
}
rets := []common.Address{}
for _, key := range keys {
ret := s.GetState(common.BlockSignersBinary, key)
rets = append(rets, common.HexToAddress(ret.Hex()))
}
return rets
}
var (
slotRandomizeMapping = map[string]uint64{
"randomSecret": 0,
"randomOpening": 1,
}
)
func (s *StateDB) GetSecret(address common.Address) [][32]byte {
slot := slotRandomizeMapping["randomSecret"]
locSecret := GetLocMappingAtKey(address.Hash(), slot)
arrLength := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
keys := []common.Hash{}
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
key := GetLocDynamicArrAtElement(common.BigToHash(locSecret), i, 1)
keys = append(keys, key)
}
rets := [][32]byte{}
for _, key := range keys {
ret := s.GetState(common.RandomizeSMCBinary, key)
rets = append(rets, ret)
}
return rets
}
func (s *StateDB) GetOpening(address common.Address) [32]byte {
slot := slotRandomizeMapping["randomOpening"]
locOpening := GetLocMappingAtKey(address.Hash(), slot)
ret := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
return ret
}
// The smart contract and the compiled byte code (in corresponding *.go file) is at commit "KYC Layer added." 7f856ffe672162dfa9c4006c89afb45a24fb7f9f
// Notice that if smart contract and the compiled byte code (in corresponding *.go file) changes, below also changes
var (
slotValidatorMapping = map[string]uint64{
"withdrawsState": 0,
"validatorsState": 1,
"voters": 2,
"KYCString": 3,
"invalidKYCCount": 4,
"hasVotedInvalid": 5,
"ownerToCandidate": 6,
"owners": 7,
"candidates": 8,
"candidateCount": 9,
"ownerCount": 10,
"minCandidateCap": 11,
"minVoterCap": 12,
"maxValidatorNumber": 13,
"candidateWithdrawDelay": 14,
"voterWithdrawDelay": 15,
}
)
func (s *StateDB) GetCandidates() []common.Address {
slot := slotValidatorMapping["candidates"]
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
arrLength := s.GetState(common.MasternodeVotingSMCBinary, slotHash)
count := arrLength.Big().Uint64()
rets := make([]common.Address, 0, count)
for i := range count {
key := GetLocDynamicArrAtElement(slotHash, i, 1)
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
if !ret.IsZero() {
rets = append(rets, common.HexToAddress(ret.Hex()))
}
}
return rets
}
func (s *StateDB) GetCandidateOwner(candidate common.Address) common.Address {
slot := slotValidatorMapping["validatorsState"]
// validatorsState[_candidate].owner;
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
locCandidateOwner := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(0)))
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
return common.HexToAddress(ret.Hex())
}
func (s *StateDB) GetCandidateCap(candidate common.Address) *big.Int {
slot := slotValidatorMapping["validatorsState"]
// validatorsState[_candidate].cap;
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
locCandidateCap := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(1)))
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateCap))
return ret.Big()
}
func (s *StateDB) GetVoters(candidate common.Address) []common.Address {
//mapping(address => address[]) voters;
slot := slotValidatorMapping["voters"]
locVoters := GetLocMappingAtKey(candidate.Hash(), slot)
arrLength := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
keys := []common.Hash{}
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
key := GetLocDynamicArrAtElement(common.BigToHash(locVoters), i, 1)
keys = append(keys, key)
}
rets := []common.Address{}
for _, key := range keys {
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
rets = append(rets, common.HexToAddress(ret.Hex()))
}
return rets
}
func (s *StateDB) GetVoterCap(candidate, voter common.Address) *big.Int {
slot := slotValidatorMapping["validatorsState"]
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
locCandidateVoters := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(2)))
retByte := crypto.Keccak256(voter.Hash().Bytes(), common.BigToHash(locCandidateVoters).Bytes())
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
return ret.Big()
}
var (
slotMintedRecordTotalMinted uint64 = 0
slotMintedRecordLastEpochNum uint64 = 1
)
func (s *StateDB) GetTotalMinted() common.Hash {
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
return totalMinted
}
func (s *StateDB) PutTotalMinted(value common.Hash) {
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
s.SetState(common.MintedRecordAddressBinary, hash, value)
}
func (s *StateDB) GetLastEpochNum() common.Hash {
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
return totalMinted
}
func (s *StateDB) PutLastEpochNum(value common.Hash) {
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
s.SetState(common.MintedRecordAddressBinary, hash, value)
}
func (s *StateDB) IncrementMintedRecordNonce() {
nonce := s.GetNonce(common.MintedRecordAddressBinary)
s.SetNonce(common.MintedRecordAddressBinary, nonce+1)
}