-
Notifications
You must be signed in to change notification settings - Fork 68
Record total minted API v2 #1769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-upgrade
Are you sure you want to change the base?
Changes from 4 commits
f2ba9a4
0206cca
f300935
fe408d6
cd0542d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,34 +153,65 @@ 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("0000000000000000000000000000000000000000000000000000000000000001") | ||
| slotMintedRecordOnsetBlock = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000002") | ||
| slotMintedRecordPostTotalMintedBase, _ = new(big.Int).SetString("0100000000000000000000000000000000000000000000000000000000000000", 16) | ||
| slotMintedRecordPostTotalBurnedBase, _ = 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 GetPostTotalMinted(statedb *StateDB, epoch uint64) common.Hash { | ||
| hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostTotalMintedBase, new(big.Int).SetUint64(epoch))) | ||
| v := statedb.GetState(common.MintedRecordAddressBinary, hash) | ||
| return v | ||
|
||
| } | ||
|
|
||
| func PutPostTotalMinted(statedb *StateDB, epoch uint64, value common.Hash) { | ||
| hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostTotalMintedBase, 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 GetPostTotalBurned(statedb *StateDB, epoch uint64) common.Hash { | ||
| hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostTotalBurnedBase, new(big.Int).SetUint64(epoch))) | ||
| v := statedb.GetState(common.MintedRecordAddressBinary, hash) | ||
| return v | ||
| } | ||
|
|
||
| func PutLastEpochNum(statedb *StateDB, value common.Hash) { | ||
| hash := GetLocSimpleVariable(slotMintedRecordLastEpochNum) | ||
| func PutPostTotalBurned(statedb *StateDB, epoch uint64, value common.Hash) { | ||
| hash := common.BigToHash(new(big.Int).Add(slotMintedRecordPostTotalBurnedBase, 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))) | ||
| v := statedb.GetState(common.MintedRecordAddressBinary, hash) | ||
| return v | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,8 +285,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 +361,45 @@ 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 := big.NewInt(0) | ||
| totalBurned := big.NewInt(0) | ||
|
||
|
|
||
| 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.GetPostTotalMinted(stateBlock, epochNumIter-1).Big() | ||
| totalBurned = state.GetPostTotalBurned(stateBlock, epochNumIter-1).Big() | ||
| if totalMinted.BitLen() != 0 || totalBurned.BitLen() != 0 { | ||
|
||
| // if previous epoch has non-zero total minted or non-zero total burned, break the loop | ||
| break | ||
| } | ||
| epochNumIter-- | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can decrease for epochNumIter > 0 {
epochNumIter--
totalMinted = state.GetPostMinted(stateBlock, epochNumIter).Big()
totalBurned = state.GetPostBurned(stateBlock, epochNumIter).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
}
} |
||
| } | ||
| 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 { | ||
| if totalMinted.Cmp(bigMaxU256) > 0 { | ||
| totalMinted.Set(bigMaxU256) | ||
|
||
| 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.PutPostTotalMinted(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(bigMaxU256) > 0 { | ||
| totalBurned.Set(bigMaxU256) | ||
| log.Warn("[HookReward] total burned overflow max u256") | ||
| } | ||
| state.PutPostTotalBurned(stateBlock, epochNum, common.BigToHash(totalBurned)) | ||
| // Increment nonce so that statedb does not treat it as empty account | ||
| state.IncrementMintedRecordNonce(stateBlock) | ||
| } | ||
|
|
@@ -388,7 +409,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 +421,11 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type | |
|
|
||
| mapBlkHash := map[uint64]common.Hash{} | ||
|
|
||
| burnedInOneEpoch := big.NewInt(0) | ||
|
|
||
| // prevent overflow | ||
| if number == 0 { | ||
| return signers, nil | ||
| return signers, burnedInOneEpoch, nil | ||
| } | ||
|
|
||
| data := make(map[common.Hash][]common.Address) | ||
|
|
@@ -417,11 +440,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 +517,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 +562,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. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add
0xprefix in string ? I think it's more clearThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common.HexToHashcan. done.new(big.Int).SetStringcannot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ethereum/go-ethereum/blob/master/crypto/secp256k1/curve.go#L267-L271: