Skip to content

Commit

Permalink
Add SoV Excess to P-chain state (ava-labs#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored and yacovm committed Oct 25, 2024
1 parent 7e05311 commit fdd28c7
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 0 deletions.
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 @@ -91,6 +91,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().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()

onParentAccept.EXPECT().GetCurrentStakerIterator().Return(
Expand Down Expand Up @@ -162,6 +163,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().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetCurrentSupply(constants.PrimaryNetworkID).Return(uint64(1000), nil).AnyTimes()

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().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()

// wrong height
Expand Down Expand Up @@ -134,6 +135,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {

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

txID := ids.GenerateTestID()
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().GetSoVExcess().Return(gas.Gas(0)).Times(2)
parentOnAcceptState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(2)

backend := &backend{
Expand Down Expand Up @@ -335,6 +336,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().GetSoVExcess().Return(gas.Gas(0)).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 @@ -597,6 +599,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().GetSoVExcess().Return(gas.Gas(0)).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
Expand Down Expand Up @@ -695,6 +698,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().GetSoVExcess().Return(gas.Gas(0)).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
Expand Down Expand Up @@ -811,6 +815,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
parentState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)
parentStatelessBlk.EXPECT().Parent().Return(grandParentID).Times(1)

Expand Down
11 changes: 11 additions & 0 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type diff struct {

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

// Subnet ID --> supply of native asset of the subnet
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewDiff(
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
feeState: parentState.GetFeeState(),
sovExcess: parentState.GetSoVExcess(),
accruedFees: parentState.GetAccruedFees(),
expiryDiff: newExpiryDiff(),
subnetOwners: make(map[ids.ID]fx.Owner),
Expand Down Expand Up @@ -117,6 +119,14 @@ func (d *diff) SetFeeState(feeState gas.State) {
d.feeState = feeState
}

func (d *diff) GetSoVExcess() gas.Gas {
return d.sovExcess
}

func (d *diff) SetSoVExcess(excess gas.Gas) {
d.sovExcess = excess
}

func (d *diff) GetAccruedFees() uint64 {
return d.accruedFees
}
Expand Down Expand Up @@ -482,6 +492,7 @@ func (d *diff) DeleteUTXO(utxoID ids.ID) {
func (d *diff) Apply(baseState Chain) error {
baseState.SetTimestamp(d.timestamp)
baseState.SetFeeState(d.feeState)
baseState.SetSoVExcess(d.sovExcess)
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 @@ -68,6 +68,24 @@ func TestDiffFeeState(t *testing.T) {
assertChainsEqual(t, state, d)
}

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

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

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

initialExcess := state.GetSoVExcess()
newExcess := initialExcess + 1
d.SetSoVExcess(newExcess)
require.Equal(newExcess, d.GetSoVExcess())
require.Equal(initialExcess, state.GetSoVExcess())

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

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

Expand Down Expand Up @@ -272,6 +290,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
Expand Down Expand Up @@ -307,6 +326,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
Expand Down Expand Up @@ -348,6 +368,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
Expand Down Expand Up @@ -392,6 +413,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
Expand Down Expand Up @@ -530,6 +552,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

d, err := NewDiffOn(state)
Expand Down Expand Up @@ -628,6 +651,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().GetSoVExcess().Return(gas.Gas(0)).Times(1)
state.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)

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

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

expectedCurrentSupply, err := expected.GetCurrentSupply(constants.PrimaryNetworkID)
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.

27 changes: 27 additions & 0 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var (

TimestampKey = []byte("timestamp")
FeeStateKey = []byte("fee state")
SoVExcessKey = []byte("sov excess")
AccruedFeesKey = []byte("accrued fees")
CurrentSupplyKey = []byte("current supply")
LastAcceptedKey = []byte("last accepted")
Expand All @@ -110,6 +111,9 @@ type Chain interface {
GetFeeState() gas.State
SetFeeState(f gas.State)

GetSoVExcess() gas.Gas
SetSoVExcess(e gas.Gas)

GetAccruedFees() uint64
SetAccruedFees(f uint64)

Expand Down Expand Up @@ -289,6 +293,7 @@ type stateBlk struct {
* |-- blocksReindexedKey -> nil
* |-- timestampKey -> timestamp
* |-- feeStateKey -> feeState
* |-- sovExcessKey -> sovExcess
* |-- accruedFeesKey -> accruedFees
* |-- currentSupplyKey -> currentSupply
* |-- lastAcceptedKey -> lastAccepted
Expand Down Expand Up @@ -386,6 +391,7 @@ type state struct {
// The persisted fields represent the current database value
timestamp, persistedTimestamp time.Time
feeState, persistedFeeState gas.State
sovExcess, persistedSOVExcess gas.Gas
accruedFees, persistedAccruedFees uint64
currentSupply, persistedCurrentSupply uint64
// [lastAccepted] is the most recently accepted block.
Expand Down Expand Up @@ -1091,6 +1097,14 @@ func (s *state) SetFeeState(feeState gas.State) {
s.feeState = feeState
}

func (s *state) GetSoVExcess() gas.Gas {
return s.sovExcess
}

func (s *state) SetSoVExcess(e gas.Gas) {
s.sovExcess = e
}

func (s *state) GetAccruedFees() uint64 {
return s.accruedFees
}
Expand Down Expand Up @@ -1391,6 +1405,13 @@ func (s *state) loadMetadata() error {
s.persistedFeeState = feeState
s.SetFeeState(feeState)

sovExcess, err := database.WithDefault(database.GetUInt64, s.singletonDB, SoVExcessKey, 0)
if err != nil {
return err
}
s.persistedSOVExcess = gas.Gas(sovExcess)
s.SetSoVExcess(gas.Gas(sovExcess))

accruedFees, err := database.WithDefault(database.GetUInt64, s.singletonDB, AccruedFeesKey, 0)
if err != nil {
return err
Expand Down Expand Up @@ -2439,6 +2460,12 @@ func (s *state) writeMetadata() error {
}
s.persistedFeeState = s.feeState
}
if s.sovExcess != s.persistedSOVExcess {
if err := database.PutUInt64(s.singletonDB, SoVExcessKey, uint64(s.sovExcess)); err != nil {
return fmt.Errorf("failed to write sov excess: %w", err)
}
s.persistedSOVExcess = s.sovExcess
}
if s.accruedFees != s.persistedAccruedFees {
if err := database.PutUInt64(s.singletonDB, AccruedFeesKey, s.accruedFees); err != nil {
return fmt.Errorf("failed to write accrued fees: %w", err)
Expand Down
Loading

0 comments on commit fdd28c7

Please sign in to comment.