Skip to content

Commit

Permalink
rename schema
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Aug 13, 2024
1 parent 4f425ea commit 1fda425
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 65 deletions.
2 changes: 1 addition & 1 deletion api/api_public_klay.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type TotalSupplyResult struct {
TotalSupply *hexutil.Big `json:"totalSupply"` // The total supply of the native token. i.e. Minted - Burnt.
TotalMinted *hexutil.Big `json:"totalMinted"` // Total minted amount.
TotalBurnt *hexutil.Big `json:"totalBurnt"` // Total burnt amount. Sum of all burnt amounts below.
BurntFee *hexutil.Big `json:"burntFee"` // from tx fee burn. ReadAccReward(num).BurntFee.
BurntFee *hexutil.Big `json:"burntFee"` // from tx fee burn. ReadSupplyCheckpoint(num).BurntFee.
ZeroBurn *hexutil.Big `json:"zeroBurn"` // balance of 0x0 (zero) address.
DeadBurn *hexutil.Big `json:"deadBurn"` // balance of 0xdead (dead) address.
Kip103Burn *hexutil.Big `json:"kip103Burn"` // by KIP103 fork. Read from its memo.
Expand Down
6 changes: 3 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo
}

// Rewind SupplyCheckpoints to highest checkpoint before new head
newLastAccRewardBlockNumber := header.Number.Uint64() - (header.Number.Uint64() % params.SupplyCheckpointInterval)
bc.db.WriteLastAccRewardBlockNumber(newLastAccRewardBlockNumber)
newLastSupplyCheckpointNumber := header.Number.Uint64() - (header.Number.Uint64() % params.SupplyCheckpointInterval)
bc.db.WriteLastSupplyCheckpointNumber(newLastSupplyCheckpointNumber)

return bc.CurrentBlock().Number().Uint64(), nil
}
Expand All @@ -634,7 +634,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo
if bc.Config().Istanbul.ProposerPolicy == params.WeightedRandom && !bc.Config().IsKaiaForkEnabled(new(big.Int).SetUint64(num)) && params.IsStakingUpdateInterval(num) {
bc.db.DeleteStakingInfo(num)
}
bc.db.DeleteAccReward(num)
bc.db.DeleteSupplyCheckpoint(num)
}

// If SetHead was only called as a chain reparation method, try to skip
Expand Down
34 changes: 17 additions & 17 deletions reward/supply_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type TotalSupply struct {
TotalSupply *big.Int // The total supply of the native token. i.e. Minted - Burnt.
TotalMinted *big.Int // Total minted amount.
TotalBurnt *big.Int // Total burnt amount. Sum of all burnt amounts below.
BurntFee *big.Int // from tx fee burn. ReadAccReward(num).BurntFee.
BurntFee *big.Int // from tx fee burn. ReadSupplyCheckpoint(num).BurntFee.
ZeroBurn *big.Int // balance of 0x0 (zero) address.
DeadBurn *big.Int // balance of 0xdead (dead) address.
Kip103Burn *big.Int // by KIP103 fork. Read from its memo.
Expand Down Expand Up @@ -134,9 +134,9 @@ func (sm *supplyManager) Stop() {
}
}

func (sm *supplyManager) GetAccReward(num uint64) (*database.AccReward, error) {
func (sm *supplyManager) GetAccReward(num uint64) (*database.SupplyCheckpoint, error) {
if accReward, ok := sm.accRewardCache.Get(num); ok {
return accReward.(*database.AccReward), nil
return accReward.(*database.SupplyCheckpoint), nil
}

accReward, err := sm.getAccRewardUncached(num)
Expand Down Expand Up @@ -279,12 +279,12 @@ func (sm *supplyManager) catchup() {

var (
headNum = sm.chain.CurrentBlock().NumberU64()
lastNum = sm.db.ReadLastAccRewardBlockNumber()
lastNum = sm.db.ReadLastSupplyCheckpointNumber()
)

if lastNum > 0 && sm.db.ReadAccReward(lastNum) == nil {
if lastNum > 0 && sm.db.ReadSupplyCheckpoint(lastNum) == nil {
logger.Error("Last accumulated reward not found. Restarting supply catchup", "last", lastNum, "head", headNum)
sm.db.WriteLastAccRewardBlockNumber(0) // soft reset to genesis
sm.db.WriteLastSupplyCheckpointNumber(0) // soft reset to genesis
lastNum = 0
}

Expand All @@ -296,17 +296,17 @@ func (sm *supplyManager) catchup() {
logger.Error("totalSupplyFromState failed", "number", 0, "err", err)
return
}
sm.db.WriteAccReward(0, &database.AccReward{
sm.db.WriteSupplyCheckpoint(0, &database.SupplyCheckpoint{
Minted: genesisTotalSupply,
BurntFee: big.NewInt(0),
})
sm.db.WriteLastAccRewardBlockNumber(0)
sm.db.WriteLastSupplyCheckpointNumber(0)
lastNum = 0
logger.Info("Stored genesis total supply", "supply", genesisTotalSupply)
}

lastNum = sm.db.ReadLastAccRewardBlockNumber()
lastAccReward := sm.db.ReadAccReward(lastNum)
lastNum = sm.db.ReadLastSupplyCheckpointNumber()
lastAccReward := sm.db.ReadSupplyCheckpoint(lastNum)

// Big-step catchup; accumulate until the head block as of now.
// The head block can be obsolete by the time catchup finished, so the big-step can end up being a bit short.
Expand Down Expand Up @@ -378,18 +378,18 @@ func (sm *supplyManager) totalSupplyFromState(num uint64) (*big.Int, error) {
return totalSupply, nil
}

func (sm *supplyManager) getAccRewardUncached(num uint64) (*database.AccReward, error) {
accReward := sm.db.ReadAccReward(num)
func (sm *supplyManager) getAccRewardUncached(num uint64) (*database.SupplyCheckpoint, error) {
accReward := sm.db.ReadSupplyCheckpoint(num)
if accReward != nil {
return accReward, nil
}

// Trace back to the last stored accumulated reward.
var fromNum uint64
var fromAcc *database.AccReward
var fromAcc *database.SupplyCheckpoint

// Fast path using checkpointInterval
if accReward := sm.db.ReadAccReward(num - num%sm.checkpointInterval); accReward != nil {
if accReward := sm.db.ReadSupplyCheckpoint(num - num%sm.checkpointInterval); accReward != nil {
logger.Trace("fast path reaccumulating rewards", "from", num-num%sm.checkpointInterval, "to", num)
fromNum = num - num%sm.checkpointInterval
fromAcc = accReward
Expand All @@ -407,7 +407,7 @@ func (sm *supplyManager) getAccRewardUncached(num uint64) (*database.AccReward,
// If `write` is true, the result will be written to the database.
// If `write` is false, the result will not be written to the database,
// to prevent overwriting LastAccRewardBlockNumber (essentially rollback) and to keep the disk size small (only store at checkpointInterval).
func (sm *supplyManager) accumulateReward(from, to uint64, fromAcc *database.AccReward, write bool) (*database.AccReward, error) {
func (sm *supplyManager) accumulateReward(from, to uint64, fromAcc *database.SupplyCheckpoint, write bool) (*database.SupplyCheckpoint, error) {
accReward := fromAcc.Copy() // make a copy because we're updating it in-place.

for num := from + 1; num <= to; num++ {
Expand Down Expand Up @@ -437,8 +437,8 @@ func (sm *supplyManager) accumulateReward(from, to uint64, fromAcc *database.Acc
// Store to database, print progress log.
sm.accRewardCache.Add(num, accReward.Copy())
if write && (num%sm.checkpointInterval) == 0 {
sm.db.WriteAccReward(num, accReward)
sm.db.WriteLastAccRewardBlockNumber(num)
sm.db.WriteSupplyCheckpoint(num, accReward)
sm.db.WriteLastSupplyCheckpointNumber(num)
}
if (num % supplyLogInterval) == 0 {
logger.Info("Accumulated block rewards", "number", num, "minted", accReward.Minted.String(), "burntFee", accReward.BurntFee.String())
Expand Down
8 changes: 4 additions & 4 deletions reward/supply_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ func (s *SupplyTestSuite) TestBlockRewind() {
}

s.chain.SetHead(200)
assert.Equal(t, uint64(128), s.db.ReadLastAccRewardBlockNumber())
assert.NotNil(t, s.db.ReadAccReward(128))
assert.Nil(t, s.db.ReadAccReward(256))
assert.Equal(t, uint64(128), s.db.ReadLastSupplyCheckpointNumber())
assert.NotNil(t, s.db.ReadSupplyCheckpoint(128))
assert.Nil(t, s.db.ReadSupplyCheckpoint(256))

s.insertBlocks(201)

Expand Down Expand Up @@ -337,7 +337,7 @@ func (s *SupplyTestSuite) TestTotalSupplyPartialInfo() {
assert.Equal(t, expected.Kip160Burn, ts.Kip160Burn)

// No AccReward
s.db.DeleteAccReward(num - (num % 128))
s.db.DeleteSupplyCheckpoint(num - (num % 128))
s.sm.accRewardCache.Purge()
ts, err = s.sm.GetTotalSupply(num)
assert.ErrorIs(t, err, errNoAccReward)
Expand Down
42 changes: 21 additions & 21 deletions storage/database/db_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ type DBManager interface {
DeleteStakingInfo(blockNum uint64)

// Accumulated block rewards functions
ReadAccReward(blockNum uint64) *AccReward
WriteAccReward(blockNum uint64, accReward *AccReward)
DeleteAccReward(blockNum uint64)
ReadLastAccRewardBlockNumber() uint64
WriteLastAccRewardBlockNumber(blockNum uint64)
ReadSupplyCheckpoint(blockNum uint64) *SupplyCheckpoint
WriteSupplyCheckpoint(blockNum uint64, accReward *SupplyCheckpoint)
DeleteSupplyCheckpoint(blockNum uint64)
ReadLastSupplyCheckpointNumber() uint64
WriteLastSupplyCheckpointNumber(blockNum uint64)

// DB migration related function
StartDBMigration(DBManager) error
Expand Down Expand Up @@ -2876,11 +2876,11 @@ func (dbm *databaseManager) ReadGovernanceState() ([]byte, error) {
return db.Get(governanceStateKey)
}

// ReadAccReward retrieves the accumulated reward (minted, burntFee) up to a specific block number.
// ReadSupplyCheckpoint retrieves the accumulated reward (minted, burntFee) up to a specific block number.
// Returns nil if the accumulated reward is not stored.
func (dbm *databaseManager) ReadAccReward(blockNum uint64) *AccReward {
func (dbm *databaseManager) ReadSupplyCheckpoint(blockNum uint64) *SupplyCheckpoint {
db := dbm.getDatabase(MiscDB)
data, err := db.Get(accRewardKey(blockNum))
data, err := db.Get(supplyCheckpointKey(blockNum))
if len(data) == 0 || err != nil {
return nil
}
Expand All @@ -2891,14 +2891,14 @@ func (dbm *databaseManager) ReadAccReward(blockNum uint64) *AccReward {
if err := rlp.DecodeBytes(data, &stored); err != nil {
logger.Crit("Corrupt accumulated reward", "err", err)
}
return &AccReward{
return &SupplyCheckpoint{
Minted: new(big.Int).SetBytes(stored.Minted),
BurntFee: new(big.Int).SetBytes(stored.BurntFee),
}
}

// WriteAccReward stores the accumulated reward (minted, burntFee) up to a specific block number.
func (dbm *databaseManager) WriteAccReward(blockNum uint64, accReward *AccReward) {
// WriteSupplyCheckpoint stores the accumulated reward (minted, burntFee) up to a specific block number.
func (dbm *databaseManager) WriteSupplyCheckpoint(blockNum uint64, accReward *SupplyCheckpoint) {
db := dbm.getDatabase(MiscDB)
stored := struct {
Minted []byte
Expand All @@ -2911,35 +2911,35 @@ func (dbm *databaseManager) WriteAccReward(blockNum uint64, accReward *AccReward
if err != nil {
logger.Crit("Failed to write accumulated reward", "err", err)
}
if err := db.Put(accRewardKey(blockNum), data); err != nil {
if err := db.Put(supplyCheckpointKey(blockNum), data); err != nil {
logger.Crit("Failed to write accumulated reward", "err", err)
}
}

// DeleteAccReward removes the accumulated reward at a specific block number.
func (dbm *databaseManager) DeleteAccReward(blockNum uint64) {
// DeleteSupplyCheckpoint removes the accumulated reward at a specific block number.
func (dbm *databaseManager) DeleteSupplyCheckpoint(blockNum uint64) {
db := dbm.getDatabase(MiscDB)
if err := db.Delete(accRewardKey(blockNum)); err != nil {
if err := db.Delete(supplyCheckpointKey(blockNum)); err != nil {
logger.Crit("Failed to delete accumulated reward", "err", err)
}
}

// ReadLastAccRewardBlockNumber retrieves the last block number for which the accumulated reward is stored.
func (dbm *databaseManager) ReadLastAccRewardBlockNumber() uint64 {
// ReadLastSupplyCheckpointNumber retrieves the last block number for which the accumulated reward is stored.
func (dbm *databaseManager) ReadLastSupplyCheckpointNumber() uint64 {
db := dbm.getDatabase(MiscDB)
data, err := db.Get(lastAccRewardBlockNumberKey)
data, err := db.Get(lastCheckpointNumberKey)
if len(data) == 0 || err != nil {
return 0
} else {
return binary.BigEndian.Uint64(data)
}
}

// WriteLastAccRewardBlockNumber stores the last block number for which the accumulated reward is stored.
func (dbm *databaseManager) WriteLastAccRewardBlockNumber(blockNum uint64) {
// WriteLastSupplyCheckpointNumber stores the last block number for which the accumulated reward is stored.
func (dbm *databaseManager) WriteLastSupplyCheckpointNumber(blockNum uint64) {
db := dbm.getDatabase(MiscDB)
data := common.Int64ToByteBigEndian(blockNum)
if err := db.Put(lastAccRewardBlockNumberKey, data); err != nil {
if err := db.Put(lastCheckpointNumberKey, data); err != nil {
logger.Crit("Failed to write last accumulated reward block number", "err", err)
}
}
Expand Down
22 changes: 11 additions & 11 deletions storage/database/db_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,24 +827,24 @@ func TestDBManager_AccReward(t *testing.T) {
// AccReward
testcases := []struct {
Number uint64
AccReward *AccReward
AccReward *SupplyCheckpoint
}{
{1000, &AccReward{big.NewInt(1111), big.NewInt(99)}},
{2000, &AccReward{big.NewInt(0), big.NewInt(88)}},
{3000, &AccReward{big.NewInt(2222), big.NewInt(0)}},
{4000, &AccReward{big.NewInt(0), big.NewInt(0)}},
{1000, &SupplyCheckpoint{big.NewInt(1111), big.NewInt(99)}},
{2000, &SupplyCheckpoint{big.NewInt(0), big.NewInt(88)}},
{3000, &SupplyCheckpoint{big.NewInt(2222), big.NewInt(0)}},
{4000, &SupplyCheckpoint{big.NewInt(0), big.NewInt(0)}},
}
for _, tc := range testcases {
assert.Nil(t, dbm.ReadAccReward(tc.Number))
dbm.WriteAccReward(tc.Number, tc.AccReward)
assert.Equal(t, tc.AccReward, dbm.ReadAccReward(tc.Number))
assert.Nil(t, dbm.ReadSupplyCheckpoint(tc.Number))
dbm.WriteSupplyCheckpoint(tc.Number, tc.AccReward)
assert.Equal(t, tc.AccReward, dbm.ReadSupplyCheckpoint(tc.Number))
}

// LastAccRewardBlockNumber
lastNum := uint64(54321)
assert.Zero(t, dbm.ReadLastAccRewardBlockNumber())
dbm.WriteLastAccRewardBlockNumber(lastNum)
assert.Equal(t, lastNum, dbm.ReadLastAccRewardBlockNumber())
assert.Zero(t, dbm.ReadLastSupplyCheckpointNumber())
dbm.WriteLastSupplyCheckpointNumber(lastNum)
assert.Equal(t, lastNum, dbm.ReadLastSupplyCheckpointNumber())
}
}

Expand Down
15 changes: 7 additions & 8 deletions storage/database/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ var (

stakingInfoPrefix = []byte("stakingInfo")

accRewardPrefix = []byte("accReward")
lastAccRewardBlockNumberKey = []byte("lastAccRewardBlockNumber")
supplyCheckpointPrefix = []byte("supplyCheckpoint")
lastCheckpointNumberKey = []byte("lastSupplyCheckpointNumber")

chaindatafetcherCheckpointKey = []byte("chaindatafetcherCheckpoint")
)
Expand Down Expand Up @@ -300,19 +300,18 @@ func parsePruningMarkKey(key []byte) PruningMark {
}
}

type AccReward struct {
type SupplyCheckpoint struct {
Minted *big.Int
BurntFee *big.Int
}

func (ar *AccReward) Copy() *AccReward {
return &AccReward{
func (ar *SupplyCheckpoint) Copy() *SupplyCheckpoint {
return &SupplyCheckpoint{
Minted: new(big.Int).Set(ar.Minted),
BurntFee: new(big.Int).Set(ar.BurntFee),
}
}

// AccRewardKey = accRewardPrefix + blockNumber
func accRewardKey(blockNumber uint64) []byte {
return append(accRewardPrefix, common.Int64ToByteBigEndian(blockNumber)...)
func supplyCheckpointKey(blockNumber uint64) []byte {
return append(supplyCheckpointPrefix, common.Int64ToByteBigEndian(blockNumber)...)
}

0 comments on commit 1fda425

Please sign in to comment.