mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-15 17:30:44 +00:00
all: rework statedb utils (#1785)
This commit is contained in:
parent
0050bef807
commit
6235de71ba
9 changed files with 71 additions and 75 deletions
|
|
@ -7,7 +7,6 @@ import (
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/common"
|
"github.com/XinFinOrg/XDPoSChain/common"
|
||||||
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
|
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/state"
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
"github.com/XinFinOrg/XDPoSChain/eth/hooks"
|
"github.com/XinFinOrg/XDPoSChain/eth/hooks"
|
||||||
"github.com/XinFinOrg/XDPoSChain/eth/util"
|
"github.com/XinFinOrg/XDPoSChain/eth/util"
|
||||||
|
|
@ -52,7 +51,7 @@ func TestHookRewardV2(t *testing.T) {
|
||||||
parentState = statedb.Copy()
|
parentState = statedb.Copy()
|
||||||
reward, err = adaptor.EngineV2.HookReward(blockchain, statedb, parentState, header2700)
|
reward, err = adaptor.EngineV2.HookReward(blockchain, statedb, parentState, header2700)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
owner := state.GetCandidateOwner(parentState, signer)
|
owner := parentState.GetCandidateOwner(signer)
|
||||||
result := reward["rewards"].(map[common.Address]interface{})
|
result := reward["rewards"].(map[common.Address]interface{})
|
||||||
assert.Equal(t, 1, len(result))
|
assert.Equal(t, 1, len(result))
|
||||||
for _, x := range result {
|
for _, x := range result {
|
||||||
|
|
@ -147,14 +146,14 @@ func TestHookRewardV2SplitReward(t *testing.T) {
|
||||||
for addr, x := range result {
|
for addr, x := range result {
|
||||||
if addr == acc1Addr {
|
if addr == acc1Addr {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("149999999999999999999", 10)
|
a, _ := big.NewInt(0).SetString("149999999999999999999", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]))
|
assert.Zero(t, a.Cmp(r[owner]))
|
||||||
b, _ := big.NewInt(0).SetString("16666666666666666666", 10)
|
b, _ := big.NewInt(0).SetString("16666666666666666666", 10)
|
||||||
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]))
|
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]))
|
||||||
} else if addr == signer {
|
} else if addr == signer {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("74999999999999999999", 10)
|
a, _ := big.NewInt(0).SetString("74999999999999999999", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]))
|
assert.Zero(t, a.Cmp(r[owner]))
|
||||||
b, _ := big.NewInt(0).SetString("8333333333333333333", 10)
|
b, _ := big.NewInt(0).SetString("8333333333333333333", 10)
|
||||||
|
|
@ -229,14 +228,14 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
|
||||||
for addr, x := range result {
|
for addr, x := range result {
|
||||||
if addr == acc1Addr {
|
if addr == acc1Addr {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
|
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
||||||
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
|
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
|
||||||
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
||||||
} else if addr == signer {
|
} else if addr == signer {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
|
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
||||||
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
|
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
|
||||||
|
|
@ -268,14 +267,14 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
|
||||||
for addr, x := range resultProtector {
|
for addr, x := range resultProtector {
|
||||||
if addr == protector1Addr {
|
if addr == protector1Addr {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
|
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
||||||
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
|
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
|
||||||
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
||||||
} else if addr == protector2Addr {
|
} else if addr == protector2Addr {
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
|
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
|
||||||
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
||||||
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
|
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
|
||||||
|
|
@ -291,16 +290,16 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
|
||||||
for addr, x := range resultObserver {
|
for addr, x := range resultObserver {
|
||||||
assert.Equal(t, addr, observer1Addr)
|
assert.Equal(t, addr, observer1Addr)
|
||||||
r := x.(map[common.Address]*big.Int)
|
r := x.(map[common.Address]*big.Int)
|
||||||
owner := state.GetCandidateOwner(parentState, addr)
|
owner := parentState.GetCandidateOwner(addr)
|
||||||
a, _ := big.NewInt(0).SetString("270112500000000000000", 10) // this value tests the float64 reward
|
a, _ := big.NewInt(0).SetString("270112500000000000000", 10) // this value tests the float64 reward
|
||||||
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
|
||||||
b, _ := big.NewInt(0).SetString("30012500000000000000", 10) // this value tests the float64 reward
|
b, _ := big.NewInt(0).SetString("30012500000000000000", 10) // this value tests the float64 reward
|
||||||
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
|
||||||
}
|
}
|
||||||
totalMinted := state.GetTotalMinted(statedb).Big()
|
totalMinted := statedb.GetTotalMinted().Big()
|
||||||
totalExpect, _ := big.NewInt(0).SetString("2100125000000000000000", 10)
|
totalExpect, _ := big.NewInt(0).SetString("2100125000000000000000", 10)
|
||||||
assert.Zero(t, totalMinted.Cmp(totalExpect), "statedb records wrong total minted")
|
assert.Zero(t, totalMinted.Cmp(totalExpect), "statedb records wrong total minted")
|
||||||
lastEpochNum := state.GetLastEpochNum(statedb).Big().Int64()
|
lastEpochNum := statedb.GetLastEpochNum().Big().Int64()
|
||||||
assert.Equal(t, 3, int(lastEpochNum))
|
assert.Equal(t, 3, int(lastEpochNum))
|
||||||
common.TIPUpgradeReward = backup
|
common.TIPUpgradeReward = backup
|
||||||
}
|
}
|
||||||
|
|
@ -366,7 +365,7 @@ func TestFinalizeAfterUpgrade(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// the recorded reward cannot be zero
|
// the recorded reward cannot be zero
|
||||||
minted := state.GetTotalMinted(statedbAfterFinalize)
|
minted := statedbAfterFinalize.GetTotalMinted()
|
||||||
assert.False(t, minted.IsZero())
|
assert.False(t, minted.IsZero())
|
||||||
t.Log("total minted", minted)
|
t.Log("total minted", minted)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,7 @@ func BuildTxOpeningRandomize(nonce uint64, randomizeAddr common.Address, randomi
|
||||||
|
|
||||||
// Get signers signed for blockNumber from blockSigner contract.
|
// Get signers signed for blockNumber from blockSigner contract.
|
||||||
func GetSignersFromContract(statedb *state.StateDB, block *types.Block) ([]common.Address, error) {
|
func GetSignersFromContract(statedb *state.StateDB, block *types.Block) ([]common.Address, error) {
|
||||||
return state.GetSigners(statedb, block), nil
|
return statedb.GetSigners(block), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get signers signed for blockNumber from blockSigner contract.
|
// Get signers signed for blockNumber from blockSigner contract.
|
||||||
|
|
@ -412,8 +412,7 @@ func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*
|
||||||
|
|
||||||
// Get candidate owner by address.
|
// Get candidate owner by address.
|
||||||
func GetCandidatesOwnerBySigner(statedb *state.StateDB, signerAddr common.Address) common.Address {
|
func GetCandidatesOwnerBySigner(statedb *state.StateDB, signerAddr common.Address) common.Address {
|
||||||
owner := state.GetCandidateOwner(statedb, signerAddr)
|
return statedb.GetCandidateOwner(signerAddr)
|
||||||
return owner
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateRewardForHolders(foundationWalletAddr common.Address, state *state.StateDB, signer common.Address, calcReward *big.Int, blockNumber uint64) (map[common.Address]*big.Int, error) {
|
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
|
||||||
rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100))
|
rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100))
|
||||||
balances[owner] = rewardMaster
|
balances[owner] = rewardMaster
|
||||||
// Get voters for masternode.
|
// Get voters for masternode.
|
||||||
voters := state.GetVoters(statedb, masterAddr)
|
voters := statedb.GetVoters(masterAddr)
|
||||||
|
|
||||||
if len(voters) > 0 {
|
if len(voters) > 0 {
|
||||||
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent))
|
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent))
|
||||||
|
|
@ -443,7 +442,7 @@ func GetRewardBalancesRate(foundationWalletAddr common.Address, statedb *state.S
|
||||||
if _, ok := voterCaps[voteAddr]; ok && common.TIP2019Block.Uint64() <= blockNumber {
|
if _, ok := voterCaps[voteAddr]; ok && common.TIP2019Block.Uint64() <= blockNumber {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
voterCap := state.GetVoterCap(statedb, masterAddr, voteAddr)
|
voterCap := statedb.GetVoterCap(masterAddr, voteAddr)
|
||||||
totalCap.Add(totalCap, voterCap)
|
totalCap.Add(totalCap, voterCap)
|
||||||
voterCaps[voteAddr] = voterCap
|
voterCaps[voteAddr] = voterCap
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ import (
|
||||||
"github.com/XinFinOrg/XDPoSChain/common"
|
"github.com/XinFinOrg/XDPoSChain/common"
|
||||||
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
|
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
|
||||||
contractValidator "github.com/XinFinOrg/XDPoSChain/contracts/validator/contract"
|
contractValidator "github.com/XinFinOrg/XDPoSChain/contracts/validator/contract"
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/state"
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
"github.com/XinFinOrg/XDPoSChain/crypto"
|
"github.com/XinFinOrg/XDPoSChain/crypto"
|
||||||
"github.com/XinFinOrg/XDPoSChain/log"
|
"github.com/XinFinOrg/XDPoSChain/log"
|
||||||
|
|
@ -318,7 +317,7 @@ func TestStatedbUtils(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get candidates: %v", err)
|
t.Fatalf("can't get candidates: %v", err)
|
||||||
}
|
}
|
||||||
candidates_statedb := state.GetCandidates(statedb)
|
candidates_statedb := statedb.GetCandidates()
|
||||||
if !reflect.DeepEqual(candidates, candidates_statedb) {
|
if !reflect.DeepEqual(candidates, candidates_statedb) {
|
||||||
t.Fatalf("candidates not equal, statedb utils is wrong,\nbind calling result\n%v\nstatedb result\n%v", candidates, candidates_statedb)
|
t.Fatalf("candidates not equal, statedb utils is wrong,\nbind calling result\n%v\nstatedb result\n%v", candidates, candidates_statedb)
|
||||||
}
|
}
|
||||||
|
|
@ -330,7 +329,7 @@ func TestStatedbUtils(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get candidate cap: %v", err)
|
t.Fatalf("can't get candidate cap: %v", err)
|
||||||
}
|
}
|
||||||
cap_statedb := state.GetCandidateCap(statedb, it)
|
cap_statedb := statedb.GetCandidateCap(it)
|
||||||
if cap.Cmp(cap_statedb) != 0 {
|
if cap.Cmp(cap_statedb) != 0 {
|
||||||
t.Fatalf("cap not equal, statedb utils is wrong")
|
t.Fatalf("cap not equal, statedb utils is wrong")
|
||||||
}
|
}
|
||||||
|
|
@ -341,7 +340,7 @@ func TestStatedbUtils(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get candidate owner: %v", err)
|
t.Fatalf("can't get candidate owner: %v", err)
|
||||||
}
|
}
|
||||||
owner_statedb := state.GetCandidateOwner(statedb, it)
|
owner_statedb := statedb.GetCandidateOwner(it)
|
||||||
if !reflect.DeepEqual(owner, owner_statedb) {
|
if !reflect.DeepEqual(owner, owner_statedb) {
|
||||||
t.Fatalf("owner not equal, statedb utils is wrong")
|
t.Fatalf("owner not equal, statedb utils is wrong")
|
||||||
}
|
}
|
||||||
|
|
@ -350,7 +349,7 @@ func TestStatedbUtils(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get voters: %v", err)
|
t.Fatalf("can't get voters: %v", err)
|
||||||
}
|
}
|
||||||
voters_statedb := state.GetVoters(statedb, acc3Addr)
|
voters_statedb := statedb.GetVoters(acc3Addr)
|
||||||
if !reflect.DeepEqual(voters, voters_statedb) {
|
if !reflect.DeepEqual(voters, voters_statedb) {
|
||||||
t.Fatalf("voters not equal, statedb utils is wrong,\nbind calling result\n%v\nstatedb result\n%v", voters, voters_statedb)
|
t.Fatalf("voters not equal, statedb utils is wrong,\nbind calling result\n%v\nstatedb result\n%v", voters, voters_statedb)
|
||||||
}
|
}
|
||||||
|
|
@ -362,7 +361,7 @@ func TestStatedbUtils(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't get voter cap: %v", err)
|
t.Fatalf("can't get voter cap: %v", err)
|
||||||
}
|
}
|
||||||
cap_statedb := state.GetVoterCap(statedb, acc3Addr, it)
|
cap_statedb := statedb.GetVoterCap(acc3Addr, it)
|
||||||
if cap.Cmp(cap_statedb) != 0 {
|
if cap.Cmp(cap_statedb) != 0 {
|
||||||
t.Fatalf("cap not equal, statedb utils is wrong")
|
t.Fatalf("cap not equal, statedb utils is wrong")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2788,7 +2788,7 @@ func (bc *BlockChain) UpdateM1() error {
|
||||||
} else if stateDB == nil {
|
} else if stateDB == nil {
|
||||||
return errors.New("nil stateDB in UpdateM1")
|
return errors.New("nil stateDB in UpdateM1")
|
||||||
} else {
|
} else {
|
||||||
candidates = state.GetCandidates(stateDB)
|
candidates = stateDB.GetCandidates()
|
||||||
}
|
}
|
||||||
|
|
||||||
var ms []utils.Masternode
|
var ms []utils.Masternode
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/common"
|
"github.com/XinFinOrg/XDPoSChain/common"
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/crypto"
|
"github.com/XinFinOrg/XDPoSChain/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -16,19 +15,19 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetSigners(statedb *StateDB, block *types.Block) []common.Address {
|
func (s *StateDB) GetSigners(block *types.Block) []common.Address {
|
||||||
slot := slotBlockSignerMapping["blockSigners"]
|
slot := slotBlockSignerMapping["blockSigners"]
|
||||||
keys := []common.Hash{}
|
keys := []common.Hash{}
|
||||||
keyArrSlot := GetLocMappingAtKey(block.Hash(), slot)
|
keyArrSlot := GetLocMappingAtKey(block.Hash(), slot)
|
||||||
arrSlot := statedb.GetState(common.BlockSignersBinary, common.BigToHash(keyArrSlot))
|
arrSlot := s.GetState(common.BlockSignersBinary, common.BigToHash(keyArrSlot))
|
||||||
arrLength := arrSlot.Big().Uint64()
|
arrLength := arrSlot.Big().Uint64()
|
||||||
for i := uint64(0); i < arrLength; i++ {
|
for i := range arrLength {
|
||||||
key := GetLocDynamicArrAtElement(common.BigToHash(keyArrSlot), i, 1)
|
key := GetLocDynamicArrAtElement(common.BigToHash(keyArrSlot), i, 1)
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
rets := []common.Address{}
|
rets := []common.Address{}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
ret := statedb.GetState(common.BlockSignersBinary, key)
|
ret := s.GetState(common.BlockSignersBinary, key)
|
||||||
rets = append(rets, common.HexToAddress(ret.Hex()))
|
rets = append(rets, common.HexToAddress(ret.Hex()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,10 +41,10 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetSecret(statedb *StateDB, address common.Address) [][32]byte {
|
func (s *StateDB) GetSecret(address common.Address) [][32]byte {
|
||||||
slot := slotRandomizeMapping["randomSecret"]
|
slot := slotRandomizeMapping["randomSecret"]
|
||||||
locSecret := GetLocMappingAtKey(address.Hash(), slot)
|
locSecret := GetLocMappingAtKey(address.Hash(), slot)
|
||||||
arrLength := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
|
arrLength := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
|
||||||
keys := []common.Hash{}
|
keys := []common.Hash{}
|
||||||
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
|
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
|
||||||
key := GetLocDynamicArrAtElement(common.BigToHash(locSecret), i, 1)
|
key := GetLocDynamicArrAtElement(common.BigToHash(locSecret), i, 1)
|
||||||
|
|
@ -53,16 +52,16 @@ func GetSecret(statedb *StateDB, address common.Address) [][32]byte {
|
||||||
}
|
}
|
||||||
rets := [][32]byte{}
|
rets := [][32]byte{}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
ret := statedb.GetState(common.RandomizeSMCBinary, key)
|
ret := s.GetState(common.RandomizeSMCBinary, key)
|
||||||
rets = append(rets, ret)
|
rets = append(rets, ret)
|
||||||
}
|
}
|
||||||
return rets
|
return rets
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOpening(statedb *StateDB, address common.Address) [32]byte {
|
func (s *StateDB) GetOpening(address common.Address) [32]byte {
|
||||||
slot := slotRandomizeMapping["randomOpening"]
|
slot := slotRandomizeMapping["randomOpening"]
|
||||||
locOpening := GetLocMappingAtKey(address.Hash(), slot)
|
locOpening := GetLocMappingAtKey(address.Hash(), slot)
|
||||||
ret := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
|
ret := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,16 +88,16 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetCandidates(statedb *StateDB) []common.Address {
|
func (s *StateDB) GetCandidates() []common.Address {
|
||||||
slot := slotValidatorMapping["candidates"]
|
slot := slotValidatorMapping["candidates"]
|
||||||
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
|
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
|
||||||
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, slotHash)
|
arrLength := s.GetState(common.MasternodeVotingSMCBinary, slotHash)
|
||||||
count := arrLength.Big().Uint64()
|
count := arrLength.Big().Uint64()
|
||||||
rets := make([]common.Address, 0, count)
|
rets := make([]common.Address, 0, count)
|
||||||
|
|
||||||
for i := uint64(0); i < count; i++ {
|
for i := range count {
|
||||||
key := GetLocDynamicArrAtElement(slotHash, i, 1)
|
key := GetLocDynamicArrAtElement(slotHash, i, 1)
|
||||||
ret := statedb.GetState(common.MasternodeVotingSMCBinary, key)
|
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
|
||||||
if !ret.IsZero() {
|
if !ret.IsZero() {
|
||||||
rets = append(rets, common.HexToAddress(ret.Hex()))
|
rets = append(rets, common.HexToAddress(ret.Hex()))
|
||||||
}
|
}
|
||||||
|
|
@ -107,29 +106,29 @@ func GetCandidates(statedb *StateDB) []common.Address {
|
||||||
return rets
|
return rets
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCandidateOwner(statedb *StateDB, candidate common.Address) common.Address {
|
func (s *StateDB) GetCandidateOwner(candidate common.Address) common.Address {
|
||||||
slot := slotValidatorMapping["validatorsState"]
|
slot := slotValidatorMapping["validatorsState"]
|
||||||
// validatorsState[_candidate].owner;
|
// validatorsState[_candidate].owner;
|
||||||
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
||||||
locCandidateOwner := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(0)))
|
locCandidateOwner := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(0)))
|
||||||
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
|
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
|
||||||
return common.HexToAddress(ret.Hex())
|
return common.HexToAddress(ret.Hex())
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCandidateCap(statedb *StateDB, candidate common.Address) *big.Int {
|
func (s *StateDB) GetCandidateCap(candidate common.Address) *big.Int {
|
||||||
slot := slotValidatorMapping["validatorsState"]
|
slot := slotValidatorMapping["validatorsState"]
|
||||||
// validatorsState[_candidate].cap;
|
// validatorsState[_candidate].cap;
|
||||||
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
||||||
locCandidateCap := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(1)))
|
locCandidateCap := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(1)))
|
||||||
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateCap))
|
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateCap))
|
||||||
return ret.Big()
|
return ret.Big()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVoters(statedb *StateDB, candidate common.Address) []common.Address {
|
func (s *StateDB) GetVoters(candidate common.Address) []common.Address {
|
||||||
//mapping(address => address[]) voters;
|
//mapping(address => address[]) voters;
|
||||||
slot := slotValidatorMapping["voters"]
|
slot := slotValidatorMapping["voters"]
|
||||||
locVoters := GetLocMappingAtKey(candidate.Hash(), slot)
|
locVoters := GetLocMappingAtKey(candidate.Hash(), slot)
|
||||||
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
|
arrLength := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
|
||||||
keys := []common.Hash{}
|
keys := []common.Hash{}
|
||||||
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
|
for i := uint64(0); i < arrLength.Big().Uint64(); i++ {
|
||||||
key := GetLocDynamicArrAtElement(common.BigToHash(locVoters), i, 1)
|
key := GetLocDynamicArrAtElement(common.BigToHash(locVoters), i, 1)
|
||||||
|
|
@ -137,19 +136,19 @@ func GetVoters(statedb *StateDB, candidate common.Address) []common.Address {
|
||||||
}
|
}
|
||||||
rets := []common.Address{}
|
rets := []common.Address{}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
ret := statedb.GetState(common.MasternodeVotingSMCBinary, key)
|
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
|
||||||
rets = append(rets, common.HexToAddress(ret.Hex()))
|
rets = append(rets, common.HexToAddress(ret.Hex()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return rets
|
return rets
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVoterCap(statedb *StateDB, candidate, voter common.Address) *big.Int {
|
func (s *StateDB) GetVoterCap(candidate, voter common.Address) *big.Int {
|
||||||
slot := slotValidatorMapping["validatorsState"]
|
slot := slotValidatorMapping["validatorsState"]
|
||||||
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
|
||||||
locCandidateVoters := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(2)))
|
locCandidateVoters := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(2)))
|
||||||
retByte := crypto.Keccak256(voter.Hash().Bytes(), common.BigToHash(locCandidateVoters).Bytes())
|
retByte := crypto.Keccak256(voter.Hash().Bytes(), common.BigToHash(locCandidateVoters).Bytes())
|
||||||
ret := statedb.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
|
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
|
||||||
return ret.Big()
|
return ret.Big()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,29 +157,29 @@ var (
|
||||||
slotMintedRecordLastEpochNum uint64 = 1
|
slotMintedRecordLastEpochNum uint64 = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTotalMinted(statedb *StateDB) common.Hash {
|
func (s *StateDB) GetTotalMinted() common.Hash {
|
||||||
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
|
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
|
||||||
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
|
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
|
||||||
return totalMinted
|
return totalMinted
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutTotalMinted(statedb *StateDB, value common.Hash) {
|
func (s *StateDB) PutTotalMinted(value common.Hash) {
|
||||||
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
|
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
|
||||||
statedb.SetState(common.MintedRecordAddressBinary, hash, value)
|
s.SetState(common.MintedRecordAddressBinary, hash, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLastEpochNum(statedb *StateDB) common.Hash {
|
func (s *StateDB) GetLastEpochNum() common.Hash {
|
||||||
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
|
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
|
||||||
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
|
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
|
||||||
return totalMinted
|
return totalMinted
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutLastEpochNum(statedb *StateDB, value common.Hash) {
|
func (s *StateDB) PutLastEpochNum(value common.Hash) {
|
||||||
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
|
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
|
||||||
statedb.SetState(common.MintedRecordAddressBinary, hash, value)
|
s.SetState(common.MintedRecordAddressBinary, hash, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IncrementMintedRecordNonce(statedb *StateDB) {
|
func (s *StateDB) IncrementMintedRecordNonce() {
|
||||||
nonce := statedb.GetNonce(common.MintedRecordAddressBinary)
|
nonce := s.GetNonce(common.MintedRecordAddressBinary)
|
||||||
statedb.SetNonce(common.MintedRecordAddressBinary, nonce+1)
|
s.SetNonce(common.MintedRecordAddressBinary, nonce+1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -557,7 +557,7 @@ func (b *EthAPIBackend) GetVotersCap(checkpoint *big.Int, masterAddr common.Addr
|
||||||
|
|
||||||
voterCaps := make(map[common.Address]*big.Int)
|
voterCaps := make(map[common.Address]*big.Int)
|
||||||
for _, voteAddr := range voters {
|
for _, voteAddr := range voters {
|
||||||
voterCap := state.GetVoterCap(statedb, masterAddr, voteAddr)
|
voterCap := statedb.GetVoterCap(masterAddr, voteAddr)
|
||||||
voterCaps[voteAddr] = voterCap
|
voterCaps[voteAddr] = voterCap
|
||||||
}
|
}
|
||||||
return voterCaps
|
return voterCaps
|
||||||
|
|
@ -590,11 +590,11 @@ func (b *EthAPIBackend) GetMasternodesCap(checkpoint uint64) map[common.Address]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
candicates := state.GetCandidates(statedb)
|
candicates := statedb.GetCandidates()
|
||||||
|
|
||||||
masternodesCap := map[common.Address]*big.Int{}
|
masternodesCap := map[common.Address]*big.Int{}
|
||||||
for _, candicate := range candicates {
|
for _, candicate := range candicates {
|
||||||
masternodesCap[candicate] = state.GetCandidateCap(statedb, candicate)
|
masternodesCap[candicate] = statedb.GetCandidateCap(candicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
return masternodesCap
|
return masternodesCap
|
||||||
|
|
|
||||||
|
|
@ -232,7 +232,7 @@ func AttachConsensusV1Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
|
||||||
return nil, errors.New("nil stateDB in HookGetSignersFromContract")
|
return nil, errors.New("nil stateDB in HookGetSignersFromContract")
|
||||||
}
|
}
|
||||||
|
|
||||||
candidateAddresses = state.GetCandidates(stateDB)
|
candidateAddresses = stateDB.GetCandidates()
|
||||||
for _, address := range candidateAddresses {
|
for _, address := range candidateAddresses {
|
||||||
v, err := validator.GetCandidateCap(opts, address)
|
v, err := validator.GetCandidateCap(opts, address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -361,8 +361,8 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
|
||||||
rewardsMap[rwt.key] = rewardResults
|
rewardsMap[rwt.key] = rewardResults
|
||||||
}
|
}
|
||||||
// record the total reward into state db
|
// record the total reward into state db
|
||||||
totalMinted := state.GetTotalMinted(stateBlock).Big()
|
totalMinted := stateBlock.GetTotalMinted().Big()
|
||||||
lastEpochNum := state.GetLastEpochNum(stateBlock)
|
lastEpochNum := stateBlock.GetLastEpochNum()
|
||||||
if lastEpochNum.IsZero() {
|
if lastEpochNum.IsZero() {
|
||||||
// if `lastEpochNum` is zero, the total minted has not included tokens before TIPUpgradeReward
|
// if `lastEpochNum` is zero, the total minted has not included tokens before TIPUpgradeReward
|
||||||
// calculate the tokens before TIPUpgradeReward and set to totalMinted
|
// calculate the tokens before TIPUpgradeReward and set to totalMinted
|
||||||
|
|
@ -377,10 +377,10 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
|
||||||
log.Warn("[HookReward] total minted overflow max u256")
|
log.Warn("[HookReward] total minted overflow max u256")
|
||||||
}
|
}
|
||||||
log.Debug("[HookReward] total minted in hook", "value", totalMinted)
|
log.Debug("[HookReward] total minted in hook", "value", totalMinted)
|
||||||
state.PutTotalMinted(stateBlock, common.BigToHash(totalMinted))
|
stateBlock.PutTotalMinted(common.BigToHash(totalMinted))
|
||||||
state.PutLastEpochNum(stateBlock, common.Uint64ToHash(epochNum))
|
stateBlock.PutLastEpochNum(common.Uint64ToHash(epochNum))
|
||||||
// Increment nonce so that statedb does not treat it as empty account
|
// Increment nonce so that statedb does not treat it as empty account
|
||||||
state.IncrementMintedRecordNonce(stateBlock)
|
stateBlock.IncrementMintedRecordNonce()
|
||||||
}
|
}
|
||||||
log.Debug("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
log.Debug("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
||||||
return rewardsMap, nil
|
return rewardsMap, nil
|
||||||
|
|
@ -433,12 +433,12 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type
|
||||||
nodesToKeep[MasterNodeBeneficiary] = c.GetMasternodesFromCheckpointHeader(h)
|
nodesToKeep[MasterNodeBeneficiary] = c.GetMasternodesFromCheckpointHeader(h)
|
||||||
// in reward upgrade, add protector and observer nodes
|
// in reward upgrade, add protector and observer nodes
|
||||||
if chain.Config().IsTIPUpgradeReward(header.Number) {
|
if chain.Config().IsTIPUpgradeReward(header.Number) {
|
||||||
candidates := state.GetCandidates(parentState)
|
candidates := parentState.GetCandidates()
|
||||||
var ms []utils.Masternode
|
var ms []utils.Masternode
|
||||||
for _, candidate := range candidates {
|
for _, candidate := range candidates {
|
||||||
// ignore "0x0000000000000000000000000000000000000000"
|
// ignore "0x0000000000000000000000000000000000000000"
|
||||||
if !candidate.IsZero() {
|
if !candidate.IsZero() {
|
||||||
v := state.GetCandidateCap(parentState, candidate)
|
v := parentState.GetCandidateCap(candidate)
|
||||||
ms = append(ms, utils.Masternode{Address: candidate, Stake: v})
|
ms = append(ms, utils.Masternode{Address: candidate, Stake: v})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -837,10 +837,10 @@ func (api *BlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAddres
|
||||||
result[fieldSuccess] = false
|
result[fieldSuccess] = false
|
||||||
return result, errors.New("nil statedb in GetCandidateStatus")
|
return result, errors.New("nil statedb in GetCandidateStatus")
|
||||||
}
|
}
|
||||||
candidatesAddresses := state.GetCandidates(statedb)
|
candidatesAddresses := statedb.GetCandidates()
|
||||||
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
|
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
|
||||||
for _, address := range candidatesAddresses {
|
for _, address := range candidatesAddresses {
|
||||||
v := state.GetCandidateCap(statedb, address)
|
v := statedb.GetCandidateCap(address)
|
||||||
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
|
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -996,10 +996,10 @@ func (api *BlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.EpochNumb
|
||||||
result[fieldSuccess] = false
|
result[fieldSuccess] = false
|
||||||
return result, errors.New("nil statedb in GetCandidates")
|
return result, errors.New("nil statedb in GetCandidates")
|
||||||
}
|
}
|
||||||
candidatesAddresses := state.GetCandidates(statedb)
|
candidatesAddresses := statedb.GetCandidates()
|
||||||
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
|
candidates = make([]utils.Masternode, 0, len(candidatesAddresses))
|
||||||
for _, address := range candidatesAddresses {
|
for _, address := range candidatesAddresses {
|
||||||
v := state.GetCandidateCap(statedb, address)
|
v := statedb.GetCandidateCap(address)
|
||||||
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
|
candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2610,8 +2610,8 @@ func (api *BlockChainAPI) GetCurrentTotalMinted(ctx context.Context) (*currentTo
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
totalMinted := state.GetTotalMinted(statedb).Big()
|
totalMinted := statedb.GetTotalMinted().Big()
|
||||||
lastEpochNum := state.GetLastEpochNum(statedb).Big()
|
lastEpochNum := statedb.GetLastEpochNum().Big()
|
||||||
result := ¤tTotalMinted{
|
result := ¤tTotalMinted{
|
||||||
TotalMinted: (*hexutil.Big)(totalMinted),
|
TotalMinted: (*hexutil.Big)(totalMinted),
|
||||||
LastEpochNum: (*hexutil.Big)(lastEpochNum),
|
LastEpochNum: (*hexutil.Big)(lastEpochNum),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue