Skip to content

Conversation

@wgr523
Copy link
Collaborator

@wgr523 wgr523 commented Nov 12, 2025

Proposed changes

Record total minted API with additional parameter epoch number. Record both minted and burned. Renamed to getTokenSupply.

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

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)
Copy link
Collaborator

@gzliudan gzliudan Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add 0x prefix in string ? I think it's more clear

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.HexToHash can. done.
new(big.Int).SetString cannot.

Copy link
Collaborator

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:

func init() {
	// See SEC 2 section 2.7.1
	// curve parameters taken from:
	// http://www.secg.org/sec2-v2.pdf
	theCurve.P, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 0)
	theCurve.N, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 0)
	theCurve.B, _ = new(big.Int).SetString("0x0000000000000000000000000000000000000000000000000000000000000007", 0)
	theCurve.Gx, _ = new(big.Int).SetString("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 0)
	theCurve.Gy, _ = new(big.Int).SetString("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 0)
	theCurve.BitSize = 256
}

// calculate the tokens before TIPUpgradeReward and set to totalMinted
// for now no-do
totalMinted := big.NewInt(0)
totalBurned := big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new(big.Int) is better than big.NewInt(0)

for epochNumIter > 0 {
totalMinted = state.GetPostTotalMinted(stateBlock, epochNumIter-1).Big()
totalBurned = state.GetPostTotalBurned(stateBlock, epochNumIter-1).Big()
if totalMinted.BitLen() != 0 || totalBurned.BitLen() != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use Sign() to compare with 0

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error string should started with lowercase, use undefined instead of Undefined

call: 'eth_getCurrentTotalMinted',
params: 0,
name: 'getTokenSupply',
call: 'eth_getTokenSupply',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is not precise, which token ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XDC token. I think token is enough, no need xdc token.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use return statedb.GetState(common.MintedRecordAddressBinary, hash)

}
totalMinted.Add(totalMinted, rewardSum)
bigPower256 := new(big.Int).Lsh(big.NewInt(1), 256)
bigMaxU256 := new(big.Int).Sub(bigPower256, big.NewInt(1))
Copy link
Collaborator

@gzliudan gzliudan Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use MaxUint256 in abi package, or define a package level variable:

MaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1)

No need to create bigPower256 and bigMaxU256 each time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or use math.MaxBig256

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error strings should not be capitalized

// if overflow, set to maxU256 and log a warning
if totalMinted.Cmp(bigMaxU256) >= 0 {
if totalMinted.Cmp(bigMaxU256) > 0 {
totalMinted.Set(bigMaxU256)
Copy link
Collaborator

@gzliudan gzliudan Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think totalMinted and totalBurned will be greater than bigMaxU256 one day.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will be very long time in the future. this PR can skip that.

preEpochMinted := new(big.Int).Mul(new(big.Int).SetUint64(config.Reward), new(big.Int).SetUint64(params.Ether))
onsetEpochMinus := onsetEpoch
if onsetEpochMinus > 0 {
onsetEpochMinus -= 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onsetEpochMinus--

if onsetEpochMinus > 0 {
onsetEpochMinus -= 1
} else {
log.Warn("onsetEpoch is 0 which could not happen", epochNum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log message string should be capitalized

call: 'eth_getTokenSupply',
params: 1,
}),
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we define command in gethclient ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is useful, then we shall do it

PostUpgradeTotalMinted *hexutil.Big `json:"postUpgradeTotalMinted"`
PreUpgradeTotalMinted *hexutil.Big `json:"preUpgradeTotalMinted"`
PostUpgradeTotalBurned *hexutil.Big `json:"postUpgradeTotalBurned"`
TotalMinted *hexutil.Big `json:"totalMinted"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you

  1. remove Total for all variables as it always represents total
  2. create like sub object like
  • rewardV2.minted
  • rewardV2.burned
  • rewardV1.minted

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostUpgrade and PreUpgrade. I think reward v1 v2 is more easier understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants