Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add AccruedFees to state.Chain interface #3371

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
// setup state to validate proposal block transaction
onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()

currentStakersIt := iteratormock.NewIterator[*state.Staker](ctrl)
currentStakersIt.EXPECT().Next().Return(true)
Expand Down Expand Up @@ -161,6 +162,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
onParentAccept := state.NewMockDiff(ctrl)
onParentAccept.EXPECT().GetTimestamp().Return(parentTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetCurrentSupply(constants.PrimaryNetworkID).Return(uint64(1000), nil).AnyTimes()

env.blkManager.(*manager).blkIDToState[parentID] = &blockState{
Expand Down
2 changes: 2 additions & 0 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestApricotStandardBlockTimeVerification(t *testing.T) {
chainTime := env.clk.Time().Truncate(time.Second)
onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()

// wrong height
apricotChildBlk, err := block.NewApricotStandardBlock(
Expand Down Expand Up @@ -136,6 +137,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {

onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()

txID := ids.GenerateTestID()
utxo := &avax.UTXO{
Expand Down
5 changes: 5 additions & 0 deletions vms/platformvm/block/executor/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestVerifierVisitProposalBlock(t *testing.T) {
// One call for each of onCommitState and onAbortState.
parentOnAcceptState.EXPECT().GetTimestamp().Return(timestamp).Times(2)
parentOnAcceptState.EXPECT().GetFeeState().Return(gas.State{}).Times(2)
parentOnAcceptState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(2)

backend := &backend{
lastAccepted: parentID,
Expand Down Expand Up @@ -334,6 +335,7 @@ func TestVerifierVisitStandardBlock(t *testing.T) {
timestamp := time.Now()
parentState.EXPECT().GetTimestamp().Return(timestamp).Times(1)
parentState.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
parentState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)
parentStatelessBlk.EXPECT().Height().Return(uint64(1)).Times(1)
mempool.EXPECT().Remove(apricotBlk.Txs()).Times(1)

Expand Down Expand Up @@ -595,6 +597,7 @@ func TestBanffAbortBlockTimestampChecks(t *testing.T) {
s.EXPECT().GetLastAccepted().Return(parentID).Times(3)
s.EXPECT().GetTimestamp().Return(parentTime).Times(3)
s.EXPECT().GetFeeState().Return(gas.State{}).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
require.NoError(err)
Expand Down Expand Up @@ -692,6 +695,7 @@ func TestBanffCommitBlockTimestampChecks(t *testing.T) {
s.EXPECT().GetLastAccepted().Return(parentID).Times(3)
s.EXPECT().GetTimestamp().Return(parentTime).Times(3)
s.EXPECT().GetFeeState().Return(gas.State{}).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
require.NoError(err)
Expand Down Expand Up @@ -807,6 +811,7 @@ func TestVerifierVisitStandardBlockWithDuplicateInputs(t *testing.T) {
parentStatelessBlk.EXPECT().Height().Return(uint64(1)).Times(1)
parentState.EXPECT().GetTimestamp().Return(timestamp).Times(1)
parentState.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
parentState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)
parentStatelessBlk.EXPECT().Parent().Return(grandParentID).Times(1)

err = verifier.ApricotStandardBlock(blk)
Expand Down
15 changes: 13 additions & 2 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type diff struct {
parentID ids.ID
stateVersions Versions

timestamp time.Time
feeState gas.State
timestamp time.Time
feeState gas.State
accruedFees uint64

// Subnet ID --> supply of native asset of the subnet
currentSupply map[ids.ID]uint64
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewDiff(
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
feeState: parentState.GetFeeState(),
accruedFees: parentState.GetAccruedFees(),
subnetOwners: make(map[ids.ID]fx.Owner),
subnetManagers: make(map[ids.ID]chainIDAndAddr),
}, nil
Expand Down Expand Up @@ -112,6 +114,14 @@ func (d *diff) SetFeeState(feeState gas.State) {
d.feeState = feeState
}

func (d *diff) GetAccruedFees() uint64 {
return d.accruedFees
}

func (d *diff) SetAccruedFees(accruedFees uint64) {
d.accruedFees = accruedFees
}

func (d *diff) GetCurrentSupply(subnetID ids.ID) (uint64, error) {
supply, ok := d.currentSupply[subnetID]
if ok {
Expand Down Expand Up @@ -437,6 +447,7 @@ func (d *diff) DeleteUTXO(utxoID ids.ID) {
func (d *diff) Apply(baseState Chain) error {
baseState.SetTimestamp(d.timestamp)
baseState.SetFeeState(d.feeState)
baseState.SetAccruedFees(d.accruedFees)
for subnetID, supply := range d.currentSupply {
baseState.SetCurrentSupply(subnetID, supply)
}
Expand Down
25 changes: 25 additions & 0 deletions vms/platformvm/state/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ func TestDiffFeeState(t *testing.T) {
assertChainsEqual(t, state, d)
}

func TestDiffAccruedFees(t *testing.T) {
require := require.New(t)

state := newTestState(t, memdb.New())

d, err := NewDiffOn(state)
require.NoError(err)

initialAccruedFees := state.GetAccruedFees()
newAccruedFees := initialAccruedFees + 1
d.SetAccruedFees(newAccruedFees)
require.Equal(newAccruedFees, d.GetAccruedFees())
require.Equal(initialAccruedFees, state.GetAccruedFees())

require.NoError(d.Apply(state))
assertChainsEqual(t, state, d)
}

func TestDiffCurrentSupply(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -101,6 +119,7 @@ func TestDiffCurrentValidator(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -135,6 +154,7 @@ func TestDiffPendingValidator(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -175,6 +195,7 @@ func TestDiffCurrentDelegator(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -221,6 +242,7 @@ func TestDiffPendingDelegator(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -361,6 +383,7 @@ func TestDiffTx(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -458,6 +481,7 @@ func TestDiffUTXO(t *testing.T) {
// Called in NewDiffOn
state.EXPECT().GetTimestamp().Return(time.Now()).Times(1)
state.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
require.NoError(err)
Expand Down Expand Up @@ -518,6 +542,7 @@ func assertChainsEqual(t *testing.T, expected, actual Chain) {

require.Equal(expected.GetTimestamp(), actual.GetTimestamp())
require.Equal(expected.GetFeeState(), actual.GetFeeState())
require.Equal(expected.GetAccruedFees(), actual.GetAccruedFees())

expectedCurrentSupply, err := expected.GetCurrentSupply(constants.PrimaryNetworkID)
require.NoError(err)
Expand Down
26 changes: 26 additions & 0 deletions vms/platformvm/state/mock_chain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vms/platformvm/state/mock_diff.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vms/platformvm/state/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading