diff --git a/consensus/tests/engine_v2_tests/reward_test.go b/consensus/tests/engine_v2_tests/reward_test.go index 961209feb882..4879587dbaa6 100644 --- a/consensus/tests/engine_v2_tests/reward_test.go +++ b/consensus/tests/engine_v2_tests/reward_test.go @@ -145,21 +145,22 @@ func TestHookRewardV2SplitReward(t *testing.T) { assert.Equal(t, 2, len(result)) // two signing account, 3 txs, reward is split by 1:2 (total reward is 250...000) for addr, x := range result { - if addr == acc1Addr { + switch addr { + case acc1Addr: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, 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 { + case signer: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, addr) a, _ := big.NewInt(0).SetString("74999999999999999999", 10) assert.Zero(t, a.Cmp(r[owner])) b, _ := big.NewInt(0).SetString("8333333333333333333", 10) assert.Zero(t, b.Cmp(r[config.XDPoS.FoudationWalletAddr])) - } else { + default: assert.Fail(t, "wrong reward") } } @@ -227,21 +228,22 @@ func TestHookRewardAfterUpgrade(t *testing.T) { assert.Equal(t, 2, len(result)) // two signing account, both get fixed reward for addr, x := range result { - if addr == acc1Addr { + switch addr { + case acc1Addr: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, 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 { + case signer: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, 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 { + default: assert.Fail(t, "wrong reward") } } @@ -266,21 +268,22 @@ func TestHookRewardAfterUpgrade(t *testing.T) { // 2 protector both get fixed reward assert.Equal(t, 2, len(resultProtector)) for addr, x := range resultProtector { - if addr == protector1Addr { + switch addr { + case protector1Addr: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, 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 { + case protector2Addr: r := x.(map[common.Address]*big.Int) owner := state.GetCandidateOwner(parentState, 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 { + default: assert.Fail(t, "wrong reward") } @@ -297,11 +300,17 @@ func TestHookRewardAfterUpgrade(t *testing.T) { 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() - totalExpect, _ := big.NewInt(0).SetString("2100125000000000000000", 10) - assert.Zero(t, totalMinted.Cmp(totalExpect), "statedb records wrong total minted") - lastEpochNum := state.GetLastEpochNum(statedb).Big().Int64() - assert.Equal(t, 3, int(lastEpochNum)) + epochNum := uint64(3) + totalMinted := state.GetPostMinted(statedb, epochNum).Big() + expectMinted, _ := big.NewInt(0).SetString("2100125000000000000000", 10) + assert.Zero(t, totalMinted.Cmp(expectMinted), "statedb records wrong total minted") + blockNum := state.GetPostRewardBlock(statedb, epochNum).Big().Int64() + assert.Equal(t, 2700, int(blockNum)) + onsetBlock := state.GetMintedRecordOnsetBlock(statedb).Big().Int64() + assert.Equal(t, 2700, int(onsetBlock)) + totalBurned := state.GetPostBurned(statedb, epochNum).Big().Int64() + // since no EIP 1559, so no burned + assert.Zero(t, totalBurned, "statedb records wrong total burned") common.TIPUpgradeReward = backup } @@ -366,9 +375,9 @@ func TestFinalizeAfterUpgrade(t *testing.T) { assert.Nil(t, err) // the recorded reward cannot be zero - minted := state.GetTotalMinted(statedbAfterFinalize) + epochNum := uint64(3) + minted := state.GetPostMinted(statedbAfterFinalize, epochNum) assert.False(t, minted.IsZero()) - t.Log("total minted", minted) common.TIPUpgradeReward = backup } diff --git a/core/state/statedb_utils.go b/core/state/statedb_utils.go index 8eb4b817ce5e..d2e338fd9843 100644 --- a/core/state/statedb_utils.go +++ b/core/state/statedb_utils.go @@ -153,34 +153,62 @@ func GetVoterCap(statedb *StateDB, candidate, voter common.Address) *big.Int { return ret.Big() } +func IncrementMintedRecordNonce(statedb *StateDB) { + nonce := statedb.GetNonce(common.MintedRecordAddressBinary) + statedb.SetNonce(common.MintedRecordAddressBinary, nonce+1) +} + var ( - slotMintedRecordTotalMinted uint64 = 0 - slotMintedRecordLastEpochNum uint64 = 1 + // Storage slot locations (32-byte keys) within MintedRecord SMC + slotMintedRecordOnsetEpoch = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001") + slotMintedRecordOnsetBlock = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002") + slotMintedRecordPostMintedBase, _ = new(big.Int).SetString("0100000000000000000000000000000000000000000000000000000000000000", 16) + slotMintedRecordPostBurnedBase, _ = new(big.Int).SetString("0200000000000000000000000000000000000000000000000000000000000000", 16) + slotMintedRecordPostRewardBlockBase, _ = new(big.Int).SetString("0300000000000000000000000000000000000000000000000000000000000000", 16) ) -func GetTotalMinted(statedb *StateDB) common.Hash { - hash := GetLocSimpleVariable(slotMintedRecordTotalMinted) - totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash) - return totalMinted +func GetMintedRecordOnsetEpoch(statedb *StateDB) common.Hash { + return statedb.GetState(common.MintedRecordAddressBinary, slotMintedRecordOnsetEpoch) } -func PutTotalMinted(statedb *StateDB, value common.Hash) { - hash := GetLocSimpleVariable(slotMintedRecordTotalMinted) +func PutMintedRecordOnsetEpoch(statedb *StateDB, value common.Hash) { + statedb.SetState(common.MintedRecordAddressBinary, slotMintedRecordOnsetEpoch, value) +} + +func GetMintedRecordOnsetBlock(statedb *StateDB) common.Hash { + return statedb.GetState(common.MintedRecordAddressBinary, slotMintedRecordOnsetBlock) +} + +func PutMintedRecordOnsetBlock(statedb *StateDB, value common.Hash) { + statedb.SetState(common.MintedRecordAddressBinary, slotMintedRecordOnsetBlock, value) +} + +func GetPostMinted(statedb *StateDB, epoch uint64) common.Hash { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostMintedBase, new(big.Int).SetUint64(epoch))) + return statedb.GetState(common.MintedRecordAddressBinary, hash) +} + +func PutPostMinted(statedb *StateDB, epoch uint64, value common.Hash) { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostMintedBase, new(big.Int).SetUint64(epoch))) statedb.SetState(common.MintedRecordAddressBinary, hash, value) } -func GetLastEpochNum(statedb *StateDB) common.Hash { - hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum) - totalMinted := statedb.GetState(common.MintedRecordAddressBinary, hash) - return totalMinted +func GetPostBurned(statedb *StateDB, epoch uint64) common.Hash { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostBurnedBase, new(big.Int).SetUint64(epoch))) + return statedb.GetState(common.MintedRecordAddressBinary, hash) } -func PutLastEpochNum(statedb *StateDB, value common.Hash) { - hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum) +func PutPostBurned(statedb *StateDB, epoch uint64, value common.Hash) { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostBurnedBase, new(big.Int).SetUint64(epoch))) statedb.SetState(common.MintedRecordAddressBinary, hash, value) } -func IncrementMintedRecordNonce(statedb *StateDB) { - nonce := statedb.GetNonce(common.MintedRecordAddressBinary) - statedb.SetNonce(common.MintedRecordAddressBinary, nonce+1) +func GetPostRewardBlock(statedb *StateDB, epoch uint64) common.Hash { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostRewardBlockBase, new(big.Int).SetUint64(epoch))) + return statedb.GetState(common.MintedRecordAddressBinary, hash) +} + +func PutPostRewardBlock(statedb *StateDB, epoch uint64, value common.Hash) { + hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostRewardBlockBase, new(big.Int).SetUint64(epoch))) + statedb.SetState(common.MintedRecordAddressBinary, hash, value) } diff --git a/eth/hooks/engine_v2_hooks.go b/eth/hooks/engine_v2_hooks.go index e3f76fd64886..d7a99bccd2d2 100644 --- a/eth/hooks/engine_v2_hooks.go +++ b/eth/hooks/engine_v2_hooks.go @@ -8,6 +8,7 @@ import ( "time" "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" @@ -285,8 +286,8 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf return nil, err } currentConfig := chain.Config().XDPoS.V2.Config(uint64(round)) - // Get signers/signing tx count - signers, err := GetSigningTxCount(adaptor, chain, header, parentState, currentConfig) + // Get signers/signing tx count, and burned tokens in one epoch + signers, burnedInOneEpoch, err := GetSigningTxCount(adaptor, chain, header, parentState, currentConfig) log.Debug("Time Get Signers", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start))) if err != nil { @@ -361,24 +362,43 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf rewardsMap[rwt.key] = rewardResults } // record the total reward into state db - totalMinted := state.GetTotalMinted(stateBlock).Big() - lastEpochNum := state.GetLastEpochNum(stateBlock) - if lastEpochNum.IsZero() { - // if `lastEpochNum` is zero, the total minted has not included tokens before TIPUpgradeReward - // calculate the tokens before TIPUpgradeReward and set to totalMinted - // for now no-do + totalMinted := new(big.Int) + totalBurned := new(big.Int) + + nonce := stateBlock.GetNonce(common.MintedRecordAddressBinary) + if nonce == 0 { + // initialize MintedRecordAddress + state.PutMintedRecordOnsetEpoch(stateBlock, common.Uint64ToHash(epochNum)) + state.PutMintedRecordOnsetBlock(stateBlock, common.Uint64ToHash(number)) + } else { + epochNumIter := epochNum + for epochNumIter > 0 { + totalMinted = state.GetPostMinted(stateBlock, epochNumIter-1).Big() + totalBurned = state.GetPostBurned(stateBlock, epochNumIter-1).Big() + if totalMinted.Sign() != 0 || totalBurned.Sign() != 0 { + // if previous epoch has non-zero total minted or non-zero total burned, break the loop + break + } + epochNumIter-- + } } totalMinted.Add(totalMinted, rewardSum) - bigPower256 := new(big.Int).Lsh(big.NewInt(1), 256) - bigMaxU256 := new(big.Int).Sub(bigPower256, big.NewInt(1)) // if overflow, set to maxU256 and log a warning - if totalMinted.Cmp(bigMaxU256) >= 0 { - totalMinted.Set(bigMaxU256) + if totalMinted.Cmp(math.MaxBig256) > 0 { + totalMinted.Set(math.MaxBig256) log.Warn("[HookReward] total minted overflow max u256") } log.Debug("[HookReward] total minted in hook", "value", totalMinted) - state.PutTotalMinted(stateBlock, common.BigToHash(totalMinted)) - state.PutLastEpochNum(stateBlock, common.Uint64ToHash(epochNum)) + state.PutPostMinted(stateBlock, epochNum, common.BigToHash(totalMinted)) + state.PutPostRewardBlock(stateBlock, epochNum, common.Uint64ToHash(number)) + // Record total burned into statedb + totalBurned.Add(totalBurned, burnedInOneEpoch) + // if overflow, set to maxU256 and log a warning + if totalBurned.Cmp(math.MaxBig256) > 0 { + totalBurned.Set(math.MaxBig256) + log.Warn("[HookReward] total burned overflow max u256") + } + state.PutPostBurned(stateBlock, epochNum, common.BigToHash(totalBurned)) // Increment nonce so that statedb does not treat it as empty account state.IncrementMintedRecordNonce(stateBlock) } @@ -388,7 +408,7 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf } // get signing transaction sender count -func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *types.Header, parentState *state.StateDB, currentConfig *params.V2Config) (map[Beneficiary]map[common.Address]*RewardLog, error) { +func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *types.Header, parentState *state.StateDB, currentConfig *params.V2Config) (map[Beneficiary]map[common.Address]*RewardLog, *big.Int, error) { // header should be a new epoch switch block number := header.Number.Uint64() rewardEpochCount := 2 @@ -400,9 +420,11 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type mapBlkHash := map[uint64]common.Hash{} + burnedInOneEpoch := new(big.Int) + // prevent overflow if number == 0 { - return signers, nil + return signers, burnedInOneEpoch, nil } data := make(map[common.Hash][]common.Address) @@ -417,11 +439,15 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type h = chain.GetHeader(parentHash, i) if h == nil { log.Error("[GetSigningTxCount] fail to get header", "number", i, "hash", parentHash) - return nil, fmt.Errorf("fail to get header in GetSigningTxCount at number: %v, hash: %v", i, parentHash) + return nil, burnedInOneEpoch, fmt.Errorf("fail to get header in GetSigningTxCount at number: %v, hash: %v", i, parentHash) + } + if epochCount == 0 && h.BaseFee != nil { + // add burned for the first epoch during loop + burnedInOneEpoch.Add(burnedInOneEpoch, new(big.Int).Mul(h.BaseFee, new(big.Int).SetUint64(h.GasUsed))) } isEpochSwitch, _, err := c.IsEpochSwitch(h) if err != nil { - return nil, err + return nil, burnedInOneEpoch, err } if isEpochSwitch && i != chain.Config().XDPoS.V2.SwitchBlock.Uint64()+1 { epochCount += 1 @@ -490,7 +516,7 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type } // prevent overflow if i == 0 { - return signers, nil + return signers, burnedInOneEpoch, nil } } @@ -535,7 +561,7 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber) - return signers, nil + return signers, burnedInOneEpoch, nil } // Calculate reward for signers. diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index e76ca0505403..887f1f9a9887 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2606,25 +2606,85 @@ func (api *BlockChainAPI) GetStakerROIMasternode(masternode common.Address) floa return 100.0 / float64(totalCap.Div(totalCap, voterRewardAYear).Uint64()) } -type currentTotalMinted struct { - TotalMinted *hexutil.Big `json:"totalMinted"` - LastEpochNum *hexutil.Big `json:"lastEpochNum"` - BlockHash common.Hash `json:"blockHash"` - BlockNumber *hexutil.Big `json:"blockNumber"` +type supplyV1 struct { + Minted *hexutil.Big `json:"minted"` } -func (api *BlockChainAPI) GetCurrentTotalMinted(ctx context.Context) (*currentTotalMinted, error) { - statedb, header, err := api.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber) +type supplyV2 struct { + Minted *hexutil.Big `json:"minted"` + Burned *hexutil.Big `json:"burned"` +} + +type tokenSupply struct { + V1 *supplyV1 `json:"v1"` + V2 *supplyV2 `json:"v2"` + Minted *hexutil.Big `json:"minted"` + UpgradeEpochNum *hexutil.Big `json:"upgradeEpochNum"` + EpochNum *hexutil.Big `json:"epochNum"` + BlockHash common.Hash `json:"blockHash"` + BlockNumber *hexutil.Big `json:"blockNumber"` +} + +func (s *BlockChainAPI) GetTokenSupply(ctx context.Context, epochNr rpc.EpochNumber) (*tokenSupply, error) { + engine, ok := s.b.Engine().(*XDPoS.XDPoS) + if !ok { + return nil, errors.New("undefined XDPoS consensus engine") + } + statedb, header, err := s.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber) + nonce := statedb.GetNonce(common.MintedRecordAddressBinary) + if nonce == 0 { + return nil, errors.New("mintedRecordAddress is not initialized due to Reward Upgrade is not applied") + } + currentRound, err := engine.EngineV2.GetRoundNumber(header) + currentEpoch := s.b.ChainConfig().XDPoS.V2.SwitchEpoch + uint64(currentRound)/s.b.ChainConfig().XDPoS.Epoch + if err != nil { + return nil, err + } + onsetEpoch := state.GetMintedRecordOnsetEpoch(statedb).Big().Uint64() + if epochNr >= 0 { + if uint64(epochNr) < onsetEpoch { + return nil, errors.New("epoch number is before reward upgrade") + } + if uint64(epochNr) > currentEpoch { + return nil, errors.New("epoch number is after current epoch") + } + } + epochNum := uint64(epochNr) + if epochNr == rpc.LatestEpochNumber { + epochNum = currentEpoch + } + postMinted := state.GetPostMinted(statedb, epochNum).Big() + number := state.GetPostRewardBlock(statedb, epochNum).Big() + targetHeader, err := s.b.HeaderByNumber(ctx, rpc.BlockNumber(number.Int64())) if err != nil { return nil, err } - totalMinted := state.GetTotalMinted(statedb).Big() - lastEpochNum := state.GetLastEpochNum(statedb).Big() - result := ¤tTotalMinted{ - TotalMinted: (*hexutil.Big)(totalMinted), - LastEpochNum: (*hexutil.Big)(lastEpochNum), - BlockHash: header.Hash(), - BlockNumber: (*hexutil.Big)(header.Number), + config := s.b.ChainConfig().XDPoS + if config == nil { + return nil, errors.New("xdpos config is nil") + } + preEpochMinted := new(big.Int).Mul(new(big.Int).SetUint64(config.Reward), new(big.Int).SetUint64(params.Ether)) + onsetEpochMinus := onsetEpoch + if onsetEpochMinus > 0 { + onsetEpochMinus-- + } else { + log.Warn("OnsetEpoch is 0 which could not happen", epochNum) + } + preMinted := new(big.Int).Mul(preEpochMinted, new(big.Int).SetUint64(onsetEpochMinus)) + postBurned := state.GetPostBurned(statedb, epochNum).Big() + result := &tokenSupply{ + V1: &supplyV1{ + Minted: (*hexutil.Big)(preMinted), + }, + V2: &supplyV2{ + Minted: (*hexutil.Big)(postMinted), + Burned: (*hexutil.Big)(postBurned), + }, + Minted: (*hexutil.Big)(new(big.Int).Add(postMinted, preMinted)), + UpgradeEpochNum: (*hexutil.Big)(new(big.Int).SetUint64(onsetEpoch)), + EpochNum: (*hexutil.Big)(new(big.Int).SetUint64(epochNum)), + BlockHash: targetHeader.Hash(), + BlockNumber: (*hexutil.Big)(number), } return result, nil } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 519f6858d9a3..63daaf26469e 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -606,9 +606,9 @@ web3._extend({ params: 1, }), new web3._extend.Method({ - name: 'getCurrentTotalMinted', - call: 'eth_getCurrentTotalMinted', - params: 0, + name: 'getTokenSupply', + call: 'eth_getTokenSupply', + params: 1, }), ], properties: [