Skip to content

Commit c855800

Browse files
authored
Only return priority rewards for post-galactica (#1081)
1 parent 4d47392 commit c855800

2 files changed

Lines changed: 98 additions & 5 deletions

File tree

api/fees/data.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ func (fd *FeesData) getOrLoadFees(blockID thor.Bytes32, rewardPercentiles []floa
113113
return nil, err
114114
}
115115

116-
var rewards *rewards
117-
// If rewardPercentiles is not empty, we need to calculate the rewards for the block
118-
if len(rewardPercentiles) > 0 {
119-
rewards, err = fd.getRewardsForCache(block)
116+
var priorityRewards *rewards
117+
// Fetch the rewards if rewardPercentiles is set and only for post-Galactica blocks
118+
// The rewards refer to priority fees per gas, which are different from pre-Galactica validator rewards
119+
if len(rewardPercentiles) > 0 && block.Header().BaseFee() != nil {
120+
priorityRewards, err = fd.getRewardsForCache(block)
120121
if err != nil {
121122
return nil, err
122123
}
@@ -126,7 +127,7 @@ func (fd *FeesData) getOrLoadFees(blockID thor.Bytes32, rewardPercentiles []floa
126127
fees = &FeeCacheEntry{
127128
baseFee: getBaseFee(header.BaseFee()),
128129
gasUsedRatio: float64(header.GasUsed()) / float64(header.GasLimit()),
129-
cachedRewards: rewards,
130+
cachedRewards: priorityRewards,
130131
parentBlockID: header.ParentID(),
131132
}
132133
fd.cache.Set(header.ID(), fees, float64(header.Number()))

api/fees/data_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111

1212
"github.com/ethereum/go-ethereum/common/hexutil"
1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
"github.com/vechain/thor/v2/genesis"
16+
"github.com/vechain/thor/v2/test/datagen"
17+
"github.com/vechain/thor/v2/test/testchain"
18+
"github.com/vechain/thor/v2/thor"
19+
"github.com/vechain/thor/v2/tx"
1420
)
1521

1622
func TestCalculateRewards(t *testing.T) {
@@ -113,3 +119,89 @@ func TestCalculateRewards(t *testing.T) {
113119
})
114120
}
115121
}
122+
123+
func TestRewardsBeforeAndAfterGalactica(t *testing.T) {
124+
forkConfig := thor.NoFork
125+
forkConfig.GALACTICA = 2
126+
127+
thorChain, err := testchain.NewWithFork(&forkConfig)
128+
assert.NoError(t, err)
129+
130+
addr := thor.BytesToAddress([]byte("to"))
131+
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
132+
133+
trx1 := tx.NewBuilder(tx.TypeLegacy).
134+
ChainTag(thorChain.Repo().ChainTag()).
135+
GasPriceCoef(100).
136+
Expiration(720).
137+
Gas(21000).
138+
Nonce(datagen.RandUint64()).
139+
Clause(cla).
140+
BlockRef(tx.NewBlockRef(0)).
141+
Build()
142+
trx2 := tx.NewBuilder(tx.TypeLegacy).
143+
ChainTag(thorChain.Repo().ChainTag()).
144+
GasPriceCoef(0).
145+
Expiration(720).
146+
Gas(21000).
147+
Nonce(datagen.RandUint64()).
148+
Clause(cla).
149+
BlockRef(tx.NewBlockRef(0)).
150+
Build()
151+
assert.NoError(t,
152+
thorChain.MintBlock(
153+
genesis.DevAccounts()[0],
154+
tx.MustSign(trx1, genesis.DevAccounts()[0].PrivateKey),
155+
tx.MustSign(trx2, genesis.DevAccounts()[0].PrivateKey),
156+
),
157+
)
158+
159+
trx3 := tx.NewBuilder(tx.TypeLegacy).
160+
ChainTag(thorChain.Repo().ChainTag()).
161+
GasPriceCoef(10).
162+
Expiration(720).
163+
Gas(21000).
164+
Nonce(datagen.RandUint64()).
165+
Clause(cla).
166+
BlockRef(tx.NewBlockRef(0)).
167+
Build()
168+
assert.NoError(t, thorChain.MintBlock(genesis.DevAccounts()[0], tx.MustSign(trx3, genesis.DevAccounts()[0].PrivateKey)))
169+
170+
feesData := newFeesData(thorChain.Repo(), thorChain.Stater(), 10)
171+
172+
bestBlockSummary := thorChain.Repo().BestBlockSummary()
173+
174+
blockCount := 2
175+
oldestBlockID, baseFees, gasUsedRatios, rewards, err := feesData.resolveRange(bestBlockSummary, uint32(blockCount), []float64{25, 50, 75})
176+
assert.NoError(t, err)
177+
assert.NotNil(t, oldestBlockID)
178+
assert.Len(t, baseFees, blockCount)
179+
assert.Len(t, gasUsedRatios, blockCount)
180+
assert.Len(t, rewards, blockCount)
181+
182+
// Before GALACTICA
183+
assert.NotNil(t, baseFees[0])
184+
assert.Equal(t, baseFees[0], (*hexutil.Big)(big.NewInt(0)))
185+
186+
assert.NotNil(t, rewards[0])
187+
assert.Len(t, rewards[0], 3)
188+
for _, reward := range rewards[0] {
189+
assert.NotNil(t, reward)
190+
assert.True(t, big.NewInt(0).Cmp((*big.Int)(reward)) == 0)
191+
}
192+
193+
// After GALACTICA
194+
assert.NotNil(t, baseFees[1])
195+
assert.Equal(t, baseFees[1], (*hexutil.Big)(big.NewInt(thor.InitialBaseFee)))
196+
197+
assert.NotNil(t, rewards[1])
198+
assert.Len(t, rewards[1], 3)
199+
200+
expectedReward, ok := new(big.Int).SetString("1029215686274509", 10)
201+
require.True(t, ok, "failed to parse expected reward")
202+
203+
for _, reward := range rewards[1] {
204+
assert.NotNil(t, reward)
205+
assert.True(t, expectedReward.Cmp((*big.Int)(reward)) == 0)
206+
}
207+
}

0 commit comments

Comments
 (0)