Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions consensus/tests/engine_v2_tests/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/eth/hooks"
"github.com/XinFinOrg/XDPoSChain/eth/util"
Expand Down Expand Up @@ -52,7 +51,7 @@ func TestHookRewardV2(t *testing.T) {
parentState = statedb.Copy()
reward, err = adaptor.EngineV2.HookReward(blockchain, statedb, parentState, header2700)
assert.Nil(t, err)
owner := state.GetCandidateOwner(parentState, signer)
owner := parentState.GetCandidateOwner(signer)
result := reward["rewards"].(map[common.Address]interface{})
assert.Equal(t, 1, len(result))
for _, x := range result {
Expand Down Expand Up @@ -147,14 +146,14 @@ func TestHookRewardV2SplitReward(t *testing.T) {
for addr, x := range result {
if addr == acc1Addr {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("149999999999999999999", 10)
assert.Zero(t, a.Cmp(r[owner]))
b, _ := big.NewInt(0).SetString("16666666666666666666", 10)
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]))
} else if addr == signer {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("74999999999999999999", 10)
assert.Zero(t, a.Cmp(r[owner]))
b, _ := big.NewInt(0).SetString("8333333333333333333", 10)
Expand Down Expand Up @@ -229,14 +228,14 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
for addr, x := range result {
if addr == acc1Addr {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
} else if addr == signer {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("450000000000000000000", 10)
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
b, _ := big.NewInt(0).SetString("50000000000000000000", 10)
Expand Down Expand Up @@ -268,14 +267,14 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
for addr, x := range resultProtector {
if addr == protector1Addr {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr]), "real reward is", r[config.XDPoS.FoudationWalletAddr])
} else if addr == protector2Addr {
r := x.(map[common.Address]*big.Int)
owner := state.GetCandidateOwner(parentState, addr)
owner := parentState.GetCandidateOwner(addr)
a, _ := big.NewInt(0).SetString("360000000000000000000", 10)
assert.Zero(t, a.Cmp(r[owner]), "real reward is", r[owner])
b, _ := big.NewInt(0).SetString("40000000000000000000", 10)
Expand All @@ -291,16 +290,16 @@ func TestHookRewardAfterUpgrade(t *testing.T) {
for addr, x := range resultObserver {
assert.Equal(t, addr, observer1Addr)
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
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
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)
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))
common.TIPUpgradeReward = backup
}
Expand Down Expand Up @@ -366,7 +365,7 @@ func TestFinalizeAfterUpgrade(t *testing.T) {
assert.Nil(t, err)

// the recorded reward cannot be zero
minted := state.GetTotalMinted(statedbAfterFinalize)
minted := statedbAfterFinalize.GetTotalMinted()
assert.False(t, minted.IsZero())
t.Log("total minted", minted)

Expand Down
9 changes: 4 additions & 5 deletions contracts/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func BuildTxOpeningRandomize(nonce uint64, randomizeAddr common.Address, randomi

// Get signers signed for blockNumber from blockSigner contract.
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.
Expand Down Expand Up @@ -412,8 +412,7 @@ func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*

// Get candidate owner by address.
func GetCandidatesOwnerBySigner(statedb *state.StateDB, signerAddr common.Address) common.Address {
owner := state.GetCandidateOwner(statedb, signerAddr)
return owner
return statedb.GetCandidateOwner(signerAddr)
}

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

if len(voters) > 0 {
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent))
Expand All @@ -443,7 +442,7 @@ func GetRewardBalancesRate(foundationWalletAddr common.Address, statedb *state.S
if _, ok := voterCaps[voteAddr]; ok && common.TIP2019Block.Uint64() <= blockNumber {
continue
}
voterCap := state.GetVoterCap(statedb, masterAddr, voteAddr)
voterCap := statedb.GetVoterCap(masterAddr, voteAddr)
totalCap.Add(totalCap, voterCap)
voterCaps[voteAddr] = voterCap
}
Expand Down
11 changes: 5 additions & 6 deletions contracts/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
contractValidator "github.com/XinFinOrg/XDPoSChain/contracts/validator/contract"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
Expand Down Expand Up @@ -318,7 +317,7 @@ func TestStatedbUtils(t *testing.T) {
if err != nil {
t.Fatalf("can't get candidates: %v", err)
}
candidates_statedb := state.GetCandidates(statedb)
candidates_statedb := statedb.GetCandidates()
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)
}
Expand All @@ -330,7 +329,7 @@ func TestStatedbUtils(t *testing.T) {
if err != nil {
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 {
t.Fatalf("cap not equal, statedb utils is wrong")
}
Expand All @@ -341,7 +340,7 @@ func TestStatedbUtils(t *testing.T) {
if err != nil {
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) {
t.Fatalf("owner not equal, statedb utils is wrong")
}
Expand All @@ -350,7 +349,7 @@ func TestStatedbUtils(t *testing.T) {
if err != nil {
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) {
t.Fatalf("voters not equal, statedb utils is wrong,\nbind calling result\n%v\nstatedb result\n%v", voters, voters_statedb)
}
Expand All @@ -362,7 +361,7 @@ func TestStatedbUtils(t *testing.T) {
if err != nil {
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 {
t.Fatalf("cap not equal, statedb utils is wrong")
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2788,7 +2788,7 @@ func (bc *BlockChain) UpdateM1() error {
} else if stateDB == nil {
return errors.New("nil stateDB in UpdateM1")
} else {
candidates = state.GetCandidates(stateDB)
candidates = stateDB.GetCandidates()
}

var ms []utils.Masternode
Expand Down
67 changes: 33 additions & 34 deletions core/state/statedb_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"

"github.com/XinFinOrg/XDPoSChain/crypto"
)

Expand All @@ -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"]
keys := []common.Hash{}
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()
for i := uint64(0); i < arrLength; i++ {
for i := range arrLength {
key := GetLocDynamicArrAtElement(common.BigToHash(keyArrSlot), i, 1)
keys = append(keys, key)
}
rets := []common.Address{}
for _, key := range keys {
ret := statedb.GetState(common.BlockSignersBinary, key)
ret := s.GetState(common.BlockSignersBinary, key)
rets = append(rets, common.HexToAddress(ret.Hex()))
}

Expand All @@ -42,27 +41,27 @@ var (
}
)

func GetSecret(statedb *StateDB, address common.Address) [][32]byte {
func (s *StateDB) GetSecret(address common.Address) [][32]byte {
slot := slotRandomizeMapping["randomSecret"]
locSecret := GetLocMappingAtKey(address.Hash(), slot)
arrLength := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locSecret))
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 := statedb.GetState(common.RandomizeSMCBinary, key)
ret := s.GetState(common.RandomizeSMCBinary, key)
rets = append(rets, ret)
}
return rets
}

func GetOpening(statedb *StateDB, address common.Address) [32]byte {
func (s *StateDB) GetOpening(address common.Address) [32]byte {
slot := slotRandomizeMapping["randomOpening"]
locOpening := GetLocMappingAtKey(address.Hash(), slot)
ret := statedb.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
ret := s.GetState(common.RandomizeSMCBinary, common.BigToHash(locOpening))
return ret
}

Expand All @@ -89,16 +88,16 @@ var (
}
)

func GetCandidates(statedb *StateDB) []common.Address {
func (s *StateDB) GetCandidates() []common.Address {
slot := slotValidatorMapping["candidates"]
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, slotHash)
arrLength := s.GetState(common.MasternodeVotingSMCBinary, slotHash)
count := arrLength.Big().Uint64()
rets := make([]common.Address, 0, count)

for i := uint64(0); i < count; i++ {
for i := range count {
key := GetLocDynamicArrAtElement(slotHash, i, 1)
ret := statedb.GetState(common.MasternodeVotingSMCBinary, key)
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
if !ret.IsZero() {
rets = append(rets, common.HexToAddress(ret.Hex()))
}
Expand All @@ -107,49 +106,49 @@ func GetCandidates(statedb *StateDB) []common.Address {
return rets
}

func GetCandidateOwner(statedb *StateDB, candidate common.Address) common.Address {
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 := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locCandidateOwner))
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"]
// validatorsState[_candidate].cap;
locValidatorsState := GetLocMappingAtKey(candidate.Hash(), slot)
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()
}

func GetVoters(statedb *StateDB, candidate common.Address) []common.Address {
func (s *StateDB) GetVoters(candidate common.Address) []common.Address {
//mapping(address => address[]) voters;
slot := slotValidatorMapping["voters"]
locVoters := GetLocMappingAtKey(candidate.Hash(), slot)
arrLength := statedb.GetState(common.MasternodeVotingSMCBinary, common.BigToHash(locVoters))
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 := statedb.GetState(common.MasternodeVotingSMCBinary, key)
ret := s.GetState(common.MasternodeVotingSMCBinary, key)
rets = append(rets, common.HexToAddress(ret.Hex()))
}

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"]
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 := statedb.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
ret := s.GetState(common.MasternodeVotingSMCBinary, common.BytesToHash(retByte))
return ret.Big()
}

Expand All @@ -158,29 +157,29 @@ var (
slotMintedRecordLastEpochNum uint64 = 1
)

func GetTotalMinted(statedb *StateDB) common.Hash {
func (s *StateDB) GetTotalMinted() common.Hash {
hash := GetLocSimpleVariable(slotMintedRecordTotalMinted)
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
return totalMinted
}

func PutTotalMinted(statedb *StateDB, value common.Hash) {
func (s *StateDB) PutTotalMinted(value common.Hash) {
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)
totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash)
totalMinted := s.GetState(common.MintedRecordAddressBinary, hash)
return totalMinted
}

func PutLastEpochNum(statedb *StateDB, value common.Hash) {
func (s *StateDB) PutLastEpochNum(value common.Hash) {
hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum)
statedb.SetState(common.MintedRecordAddressBinary, hash, value)
s.SetState(common.MintedRecordAddressBinary, hash, value)
}

func IncrementMintedRecordNonce(statedb *StateDB) {
nonce := statedb.GetNonce(common.MintedRecordAddressBinary)
statedb.SetNonce(common.MintedRecordAddressBinary, nonce+1)
func (s *StateDB) IncrementMintedRecordNonce() {
nonce := s.GetNonce(common.MintedRecordAddressBinary)
s.SetNonce(common.MintedRecordAddressBinary, nonce+1)
}
Loading