Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add P-chain dynamic fees execution #3251

Merged
merged 143 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 138 commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
3741923
Add P-chain fee APIs
StephenButtolph Aug 10, 2024
01aff13
nit
StephenButtolph Aug 10, 2024
c75957f
Merge branch 'master' into dynamic-fees-api
StephenButtolph Aug 13, 2024
db2dff9
Add P-chain dynamic fees execution
StephenButtolph Aug 13, 2024
f25e199
Pack all etna txs
StephenButtolph Aug 13, 2024
350f65d
fix lint
StephenButtolph Aug 13, 2024
423ae82
wip fix e2e
StephenButtolph Aug 13, 2024
7bd5779
nit
StephenButtolph Aug 13, 2024
c971ef5
nit
StephenButtolph Aug 13, 2024
f043cfa
nit
StephenButtolph Aug 13, 2024
4265bde
wip
StephenButtolph Aug 14, 2024
a112095
fix staking rewards
StephenButtolph Aug 14, 2024
d678a9a
nits
StephenButtolph Aug 14, 2024
d63b26b
nits
StephenButtolph Aug 14, 2024
34d5e66
merged
StephenButtolph Aug 14, 2024
02c817f
Merge branch 'master' into dynamic-fees-api
StephenButtolph Aug 15, 2024
d08173f
merged
StephenButtolph Aug 15, 2024
1f35db3
Merge branch 'master' into dynamic-fees-api
StephenButtolph Aug 15, 2024
d6cedfd
merged
StephenButtolph Aug 15, 2024
574d265
remove debug error
StephenButtolph Aug 15, 2024
75716b4
add simple tests
StephenButtolph Aug 15, 2024
e578bdc
nit
StephenButtolph Aug 15, 2024
b7f1cef
Merge branch 'dynamic-fees-api' into dynamic-fees-execution
StephenButtolph Aug 15, 2024
d60d97f
nits
StephenButtolph Aug 15, 2024
7ddc52d
nit
StephenButtolph Aug 15, 2024
dcac5fc
nit
StephenButtolph Aug 15, 2024
d256171
nit
StephenButtolph Aug 15, 2024
ac172e5
Merge branch 'dynamic-fees-api' into dynamic-fees-execution
StephenButtolph Aug 15, 2024
6a00c00
nit
StephenButtolph Aug 15, 2024
1e79201
merged
StephenButtolph Aug 15, 2024
fdbd5d3
merged
StephenButtolph Aug 15, 2024
edfddbc
Remove duplicate fork definitions
StephenButtolph Aug 15, 2024
700b9af
nits
StephenButtolph Aug 15, 2024
eecb49e
more cleanup
StephenButtolph Aug 16, 2024
f685c70
comments
StephenButtolph Aug 16, 2024
9443588
add comment
StephenButtolph Aug 16, 2024
80a7f2f
nit
StephenButtolph Aug 16, 2024
39d48f6
fix comment
StephenButtolph Aug 16, 2024
2cd4f45
merged
StephenButtolph Aug 16, 2024
f91f3be
wip
StephenButtolph Aug 16, 2024
348e8de
merged
StephenButtolph Aug 16, 2024
f0b7459
functions over methods
StephenButtolph Aug 19, 2024
05fccff
Refactor
StephenButtolph Aug 19, 2024
38ce20b
fix test
StephenButtolph Aug 19, 2024
caf3b60
fix state initialization
StephenButtolph Aug 19, 2024
f4ef32f
Only test initialized states
StephenButtolph Aug 19, 2024
11bcdef
nit
StephenButtolph Aug 19, 2024
c69540a
Merge branch 'refactor-state-tests' into dynamic-fees-execution
StephenButtolph Aug 19, 2024
6f2010d
merged
StephenButtolph Aug 19, 2024
ffbee12
Add test
StephenButtolph Aug 19, 2024
179f557
Simplify tests
StephenButtolph Aug 19, 2024
9918ac8
nit
StephenButtolph Aug 19, 2024
894e121
Use reasonable values
StephenButtolph Aug 19, 2024
9353d05
nit
StephenButtolph Aug 19, 2024
afac039
nit
StephenButtolph Aug 19, 2024
420d3ca
nit
StephenButtolph Aug 19, 2024
3925f36
Expose wallet backend
StephenButtolph Aug 19, 2024
972baf8
Merge branch 'expose-wallet-backend' into dynamic-fees-execution
StephenButtolph Aug 19, 2024
2d295d6
Add verifier tests
StephenButtolph Aug 20, 2024
191d13d
wip
StephenButtolph Aug 20, 2024
50c9109
reduce diff
StephenButtolph Aug 20, 2024
fce8bed
merged
StephenButtolph Aug 20, 2024
a50f75a
wip
StephenButtolph Aug 20, 2024
77664c8
Allow P-chain wallet to be used by the platformvm
StephenButtolph Aug 20, 2024
eefd6c3
save
StephenButtolph Aug 20, 2024
3e27a8e
nit
StephenButtolph Aug 20, 2024
9db6d87
deadcode
StephenButtolph Aug 20, 2024
348be19
nit
StephenButtolph Aug 20, 2024
a0772ba
merged
StephenButtolph Aug 20, 2024
7e4b007
reduce diff
StephenButtolph Aug 20, 2024
f6bbbe4
reduce diff
StephenButtolph Aug 20, 2024
b7c1b1e
reduce diff
StephenButtolph Aug 20, 2024
d68b2ce
Meter complexity first
StephenButtolph Aug 20, 2024
5a0c28b
fix unit tests
StephenButtolph Aug 21, 2024
1b61a31
Merge branch 'master' into dynamic-fees-execution
StephenButtolph Aug 21, 2024
4671917
nit
StephenButtolph Aug 21, 2024
92ede3c
Merge branch 'dynamic-fees-execution' of github.com:ava-labs/avalanch…
StephenButtolph Aug 21, 2024
d75e73c
Add .String() to Fork testing utility
StephenButtolph Aug 21, 2024
f516281
Merge branch 'add-fork-stringer' into dynamic-fees-execution
StephenButtolph Aug 21, 2024
4b4cc5a
Merge branch 'master' into dynamic-fees-execution
StephenButtolph Aug 21, 2024
2014103
Add comment
StephenButtolph Aug 21, 2024
78c8d93
nit
StephenButtolph Aug 21, 2024
8c0050c
nit
StephenButtolph Aug 21, 2024
a9f3bd1
nit
StephenButtolph Aug 21, 2024
8ad5f82
merged
StephenButtolph Aug 21, 2024
3df46f4
merged
StephenButtolph Aug 23, 2024
40a637d
cleanup
StephenButtolph Aug 23, 2024
d39ab69
reduce diff
StephenButtolph Aug 23, 2024
4381899
nit
StephenButtolph Aug 23, 2024
67a81ee
wip
StephenButtolph Aug 23, 2024
ef1d345
Simplify
StephenButtolph Aug 23, 2024
7048e0f
Update TestAddDelegatorTxOverDelegatedRegression
StephenButtolph Aug 23, 2024
e851cfd
Update TestAddDelegatorTxHeapCorruption
StephenButtolph Aug 23, 2024
4c34378
nit
StephenButtolph Aug 23, 2024
825dbd5
Update TestRejectedStateRegressionInvalidValidatorTimestamp
StephenButtolph Aug 23, 2024
902caa0
Update TestRejectedStateRegressionInvalidValidatorTimestamp
StephenButtolph Aug 23, 2024
b44988d
Update TestAddDelegatorTxAddBeforeRemove
StephenButtolph Aug 23, 2024
071bca3
Update TestRemovePermissionedValidatorDuringPendingToCurrentTransitio…
StephenButtolph Aug 23, 2024
06fd267
Update TestRemovePermissionedValidatorDuringPendingToCurrentTransitio…
StephenButtolph Aug 23, 2024
86740fd
Update TestSubnetValidatorBLSKeyDiffAfterExpiry
StephenButtolph Aug 23, 2024
e0f7f8b
Update TestPrimaryNetworkValidatorPopulatedToEmptyBLSKeyDiff
StephenButtolph Aug 23, 2024
38c7458
Update TestSubnetValidatorPopulatedToEmptyBLSKeyDiff
StephenButtolph Aug 23, 2024
7ec4950
Update TestSubnetValidatorSetAfterPrimaryNetworkValidatorRemoval
StephenButtolph Aug 23, 2024
cdd06ee
Update TestAddValidatorCommit
StephenButtolph Aug 23, 2024
348d927
Update TestInvalidAddValidatorCommit
StephenButtolph Aug 23, 2024
24e7268
Update TestAddValidatorReject
StephenButtolph Aug 23, 2024
d3bba26
Update TestAddValidatorInvalidNotReissued
StephenButtolph Aug 23, 2024
4daa47a
Update TestAddSubnetValidatorAccept
StephenButtolph Aug 23, 2024
23f9722
Update TestAddSubnetValidatorReject
StephenButtolph Aug 23, 2024
afc4287
Update TestCreateChain
StephenButtolph Aug 23, 2024
faa2b1e
Update TestCreateSubnet
StephenButtolph Aug 23, 2024
4e2f005
Update TestAtomicImport
StephenButtolph Aug 23, 2024
2b6e501
Update TestRemovePermissionedValidatorDuringAddPending
StephenButtolph Aug 23, 2024
37b81ad
Update TestTransferSubnetOwnershipTx
StephenButtolph Aug 23, 2024
994eb89
Update TestBaseTx
StephenButtolph Aug 23, 2024
abbc394
Remove return value
StephenButtolph Aug 23, 2024
832db70
implement helper
StephenButtolph Aug 23, 2024
b415d91
reduce diff
StephenButtolph Aug 23, 2024
d0cf8d5
fix flaky test
StephenButtolph Aug 23, 2024
762678c
nits
StephenButtolph Aug 23, 2024
b15c103
Reduce diff
StephenButtolph Aug 23, 2024
9a0c1b8
finish first pass
StephenButtolph Aug 24, 2024
82cdaa4
merged
StephenButtolph Aug 24, 2024
d57f852
nit
StephenButtolph Aug 26, 2024
fbba8b6
nit
StephenButtolph Aug 26, 2024
5982340
nit
StephenButtolph Aug 26, 2024
0bb0459
nit
StephenButtolph Aug 26, 2024
4703496
nit
StephenButtolph Aug 26, 2024
3e3e9cd
Delete moar
StephenButtolph Aug 26, 2024
241abad
Delete moar
StephenButtolph Aug 26, 2024
946f522
Finish first pass
StephenButtolph Aug 26, 2024
f9fb261
nit
StephenButtolph Aug 26, 2024
b8edfba
Merge branch 'improve-p-tx-creation-2' into improve-p-tx-creation-3
StephenButtolph Aug 26, 2024
b9f44e6
merged
StephenButtolph Aug 26, 2024
5128d6d
Merge branch 'master' into improve-p-tx-creation-2
StephenButtolph Aug 26, 2024
1e59af9
Merge branch 'improve-p-tx-creation-2' into improve-p-tx-creation-3
StephenButtolph Aug 26, 2024
e56c072
merged
StephenButtolph Aug 26, 2024
32fc1f2
Merge branch 'master' into dynamic-fees-execution
StephenButtolph Aug 27, 2024
93d0397
reduce diff
StephenButtolph Aug 27, 2024
7e880a4
reduce diff
StephenButtolph Aug 28, 2024
e7d651e
Remove bad comment
StephenButtolph Aug 28, 2024
9a4bd4c
Use global configs rather than custom configs
StephenButtolph Aug 28, 2024
9d3bbf5
Merge branch 'master' into dynamic-fees-execution
StephenButtolph Aug 28, 2024
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
227 changes: 188 additions & 39 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/gas"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"

blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
Expand Down Expand Up @@ -244,14 +246,39 @@ func (b *builder) PackAllBlockTxs() ([]*txs.Tx, error) {
return nil, fmt.Errorf("%w: %s", errMissingPreferredState, preferredID)
}

return packBlockTxs(
timestamp, _, err := state.NextBlockTime(preferredState, b.txExecutorBackend.Clk)
if err != nil {
return nil, fmt.Errorf("could not calculate next staker change time: %w", err)
}

if !b.txExecutorBackend.Config.UpgradeConfig.IsEtnaActivated(timestamp) {
return packDurangoBlockTxs(
preferredID,
preferredState,
b.Mempool,
b.txExecutorBackend,
b.blkManager,
timestamp,
math.MaxInt,
)
}

stateDiff, err := state.NewDiffOn(preferredState)
if err != nil {
return nil, err
}

if _, err := txexecutor.AdvanceTimeTo(b.txExecutorBackend, stateDiff, timestamp); err != nil {
return nil, err
}

return packEtnaBlockTxsOn(
preferredID,
preferredState,
stateDiff,
b.Mempool,
b.txExecutorBackend,
b.blkManager,
b.txExecutorBackend.Clk.Time(),
math.MaxInt,
math.MaxUint64,
)
}

Expand All @@ -264,15 +291,30 @@ func buildBlock(
forceAdvanceTime bool,
parentState state.Chain,
) (block.Block, error) {
blockTxs, err := packBlockTxs(
parentID,
parentState,
builder.Mempool,
builder.txExecutorBackend,
builder.blkManager,
timestamp,
targetBlockSize,
var (
blockTxs []*txs.Tx
err error
)
if builder.txExecutorBackend.Config.UpgradeConfig.IsEtnaActivated(timestamp) {
blockTxs, err = packEtnaBlockTxs(
parentID,
parentState,
builder.Mempool,
builder.txExecutorBackend,
builder.blkManager,
timestamp,
)
} else {
blockTxs, err = packDurangoBlockTxs(
parentID,
parentState,
builder.Mempool,
builder.txExecutorBackend,
builder.blkManager,
timestamp,
targetBlockSize,
)
}
if err != nil {
return nil, fmt.Errorf("failed to pack block txs: %w", err)
}
Expand Down Expand Up @@ -314,7 +356,7 @@ func buildBlock(
)
}

func packBlockTxs(
func packDurangoBlockTxs(
parentID ids.ID,
parentState state.Chain,
mempool mempool.Mempool,
Expand Down Expand Up @@ -346,55 +388,162 @@ func packBlockTxs(
if txSize > remainingSize {
break
}
mempool.Remove(tx)

// Invariant: [tx] has already been syntactically verified.

txDiff, err := state.NewDiffOn(stateDiff)
shouldAdd, err := executeTx(
parentID,
stateDiff,
mempool,
backend,
manager,
&inputs,
feeCalculator,
tx,
)
if err != nil {
return nil, err
}
if !shouldAdd {
continue
}

remainingSize -= txSize
blockTxs = append(blockTxs, tx)
}

return blockTxs, nil
}

func packEtnaBlockTxs(
parentID ids.ID,
parentState state.Chain,
mempool mempool.Mempool,
backend *txexecutor.Backend,
manager blockexecutor.Manager,
timestamp time.Time,
) ([]*txs.Tx, error) {
stateDiff, err := state.NewDiffOn(parentState)
if err != nil {
return nil, err
}

if _, err := txexecutor.AdvanceTimeTo(backend, stateDiff, timestamp); err != nil {
return nil, err
}

executor := &txexecutor.StandardTxExecutor{
Backend: backend,
State: txDiff,
FeeCalculator: feeCalculator,
Tx: tx,
feeState := stateDiff.GetFeeState()
return packEtnaBlockTxsOn(parentID, stateDiff, mempool, backend, manager, feeState.Capacity)
}

func packEtnaBlockTxsOn(
parentID ids.ID,
stateDiff state.Diff,
mempool mempool.Mempool,
backend *txexecutor.Backend,
manager blockexecutor.Manager,
capacity gas.Gas,
) ([]*txs.Tx, error) {
var (
blockTxs []*txs.Tx
inputs set.Set[ids.ID]
blockComplexity gas.Dimensions
feeCalculator = state.PickFeeCalculator(backend.Config, stateDiff)
)
for {
tx, exists := mempool.Peek()
if !exists {
break
}

err = tx.Unsigned.Visit(executor)
txComplexity, err := fee.TxComplexity(tx.Unsigned)
if err != nil {
txID := tx.ID()
mempool.MarkDropped(txID, err)
continue
return nil, err
}

if inputs.Overlaps(executor.Inputs) {
txID := tx.ID()
mempool.MarkDropped(txID, blockexecutor.ErrConflictingBlockTxs)
continue
newBlockComplexity, err := blockComplexity.Add(&txComplexity)
if err != nil {
return nil, err
}
err = manager.VerifyUniqueInputs(parentID, executor.Inputs)
newBlockGas, err := newBlockComplexity.ToGas(backend.Config.DynamicFeeConfig.Weights)
if err != nil {
txID := tx.ID()
mempool.MarkDropped(txID, err)
continue
return nil, err
}
if newBlockGas > capacity {
break
}
inputs.Union(executor.Inputs)

txDiff.AddTx(tx, status.Committed)
err = txDiff.Apply(stateDiff)
shouldAdd, err := executeTx(
parentID,
stateDiff,
mempool,
backend,
manager,
&inputs,
feeCalculator,
tx,
)
if err != nil {
return nil, err
}
if !shouldAdd {
continue
}

remainingSize -= txSize
blockComplexity = newBlockComplexity
blockTxs = append(blockTxs, tx)
}

return blockTxs, nil
}

func executeTx(
parentID ids.ID,
stateDiff state.Diff,
mempool mempool.Mempool,
backend *txexecutor.Backend,
manager blockexecutor.Manager,
inputs *set.Set[ids.ID],
feeCalculator fee.Calculator,
tx *txs.Tx,
) (bool, error) {
mempool.Remove(tx)

// Invariant: [tx] has already been syntactically verified.

txDiff, err := state.NewDiffOn(stateDiff)
if err != nil {
return false, err
}

executor := &txexecutor.StandardTxExecutor{
Backend: backend,
State: txDiff,
FeeCalculator: feeCalculator,
Tx: tx,
}

err = tx.Unsigned.Visit(executor)
if err != nil {
txID := tx.ID()
mempool.MarkDropped(txID, err)
return false, nil
}

if inputs.Overlaps(executor.Inputs) {
txID := tx.ID()
mempool.MarkDropped(txID, blockexecutor.ErrConflictingBlockTxs)
return false, nil
}
err = manager.VerifyUniqueInputs(parentID, executor.Inputs)
if err != nil {
txID := tx.ID()
mempool.MarkDropped(txID, err)
return false, nil
}
inputs.Union(executor.Inputs)

txDiff.AddTx(tx, status.Committed)
return true, txDiff.Apply(stateDiff)
}

// getNextStakerToReward returns the next staker txID to remove from the staking
// set with a RewardValidatorTx rather than an AdvanceTimeTx. [chainTimestamp]
// is the timestamp of the chain at the time this validator would be getting
Expand Down
43 changes: 42 additions & 1 deletion vms/platformvm/block/executor/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/components/gas"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
Expand Down Expand Up @@ -454,12 +455,52 @@ func (v *verifier) standardBlock(
return nil
}

func (v *verifier) processStandardTxs(txs []*txs.Tx, feeCalculator fee.Calculator, state state.Diff, parentID ids.ID) (
func (v *verifier) processStandardTxs(
txs []*txs.Tx,
feeCalculator fee.Calculator,
state state.Diff,
parentID ids.ID,
) (
set.Set[ids.ID],
map[ids.ID]*atomic.Requests,
func(),
error,
) {
// Complexity is limited first to avoid processing too large of a block.
if timestamp := state.GetTimestamp(); v.txExecutorBackend.Config.UpgradeConfig.IsEtnaActivated(timestamp) {
var blockComplexity gas.Dimensions
for _, tx := range txs {
txComplexity, err := fee.TxComplexity(tx.Unsigned)
if err != nil {
txID := tx.ID()
v.MarkDropped(txID, err) // cache tx as dropped
return nil, nil, nil, err
}

blockComplexity, err = blockComplexity.Add(&txComplexity)
if err != nil {
return nil, nil, nil, err
}
}

blockGas, err := blockComplexity.ToGas(v.txExecutorBackend.Config.DynamicFeeConfig.Weights)
if err != nil {
return nil, nil, nil, err
}

// If this block exceeds the available capacity, ConsumeGas will return
// an error.
feeState := state.GetFeeState()
feeState, err = feeState.ConsumeGas(blockGas)
if err != nil {
return nil, nil, nil, err
}

// Updating the fee state prior to executing the transactions is fine
// because the fee calculator was already created.
state.SetFeeState(feeState)
}

var (
onAcceptFunc func()
inputs set.Set[ids.ID]
Expand Down
Loading
Loading