Skip to content

Commit

Permalink
introduced dynamic fees for x-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Feb 6, 2024
1 parent 0318ea8 commit 9631ec8
Show file tree
Hide file tree
Showing 18 changed files with 2,668 additions and 97 deletions.
2 changes: 2 additions & 0 deletions scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ github.com/ava-labs/avalanchego/vms/proposervm=PostForkBlock=vms/proposervm/mock
github.com/ava-labs/avalanchego/vms/registry=VMGetter=vms/registry/mock_vm_getter.go
github.com/ava-labs/avalanchego/vms/registry=VMRegistry=vms/registry/mock_vm_registry.go
github.com/ava-labs/avalanchego/vms=Factory,Manager=vms/mock_manager.go
github.com/ava-labs/avalanchego/wallet/chain/x=BuilderBackend=wallet/chain/x/mocks/mock_builder_backend.go
github.com/ava-labs/avalanchego/wallet/chain/x=SignerBackend=wallet/chain/x/mocks/mock_signer_backend.go
github.com/ava-labs/avalanchego/x/sync=Client=x/sync/mock_client.go
github.com/ava-labs/avalanchego/x/sync=NetworkClient=x/sync/mock_network_client.go
13 changes: 10 additions & 3 deletions vms/avm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/avm/block"
"github.com/ava-labs/avalanchego/vms/avm/config"
"github.com/ava-labs/avalanchego/vms/avm/state"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/avm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/components/fees"

blockexecutor "github.com/ava-labs/avalanchego/vms/avm/block/executor"
txexecutor "github.com/ava-labs/avalanchego/vms/avm/txs/executor"
Expand Down Expand Up @@ -91,6 +93,9 @@ func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
blockTxs []*txs.Tx
inputs set.Set[ids.ID]
remainingSize = targetBlockSize

feeCfg = config.EUpgradeDynamicFeesConfig
feeManager = fees.NewManager(feeCfg.UnitFees)
)
for {
tx, exists := b.mempool.Peek()
Expand All @@ -111,9 +116,11 @@ func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
}

err = tx.Unsigned.Visit(&txexecutor.SemanticVerifier{
Backend: b.backend,
State: txDiff,
Tx: tx,
Backend: b.backend,
BlkFeeManager: feeManager,
UnitCaps: feeCfg.BlockUnitsCap,
State: txDiff,
Tx: tx,
})
if err != nil {
txID := tx.ID()
Expand Down
13 changes: 10 additions & 3 deletions vms/avm/block/executor/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/vms/avm/block"
"github.com/ava-labs/avalanchego/vms/avm/config"
"github.com/ava-labs/avalanchego/vms/avm/state"
"github.com/ava-labs/avalanchego/vms/avm/txs/executor"
"github.com/ava-labs/avalanchego/vms/components/fees"
)

const SyncBound = 10 * time.Second
Expand Down Expand Up @@ -130,13 +132,18 @@ func (b *Block) Verify(context.Context) error {
atomicRequests: make(map[ids.ID]*atomic.Requests),
}

feeCfg := config.EUpgradeDynamicFeesConfig
feeManager := fees.NewManager(feeCfg.UnitFees)

for _, tx := range txs {
// Verify that the tx is valid according to the current state of the
// chain.
err := tx.Unsigned.Visit(&executor.SemanticVerifier{
Backend: b.manager.backend,
State: stateDiff,
Tx: tx,
Backend: b.manager.backend,
BlkFeeManager: feeManager,
UnitCaps: feeCfg.BlockUnitsCap,
State: stateDiff,
Tx: tx,
})
if err != nil {
txID := tx.ID()
Expand Down
13 changes: 10 additions & 3 deletions vms/avm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/avm/block"
"github.com/ava-labs/avalanchego/vms/avm/config"
"github.com/ava-labs/avalanchego/vms/avm/metrics"
"github.com/ava-labs/avalanchego/vms/avm/state"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/avm/txs/executor"
"github.com/ava-labs/avalanchego/vms/avm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/components/fees"
)

var (
Expand Down Expand Up @@ -160,10 +162,15 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
return err
}

feeCfg := config.EUpgradeDynamicFeesConfig
feeManager := fees.NewManager(feeCfg.UnitFees)

err = tx.Unsigned.Visit(&executor.SemanticVerifier{
Backend: m.backend,
State: stateDiff,
Tx: tx,
Backend: m.backend,
BlkFeeManager: feeManager,
UnitCaps: feeCfg.BlockUnitsCap,
State: stateDiff,
Tx: tx,
})
if err != nil {
return err
Expand Down
45 changes: 45 additions & 0 deletions vms/avm/config/dynamic_fees_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package config

import (
"math"

commonfees "github.com/ava-labs/avalanchego/vms/components/fees"
)

// Dynamic fees configs become relevant with dynamic fees introduction in E-fork
// We cannot easily include then in Config since they do not come from genesis
// They don't feel like an execution config either, since we need a fork upgrade
// to update them (testing is a different story).
// I am setting them in a separate config object, but will access it via Config
// so to have fork control over which dynamic fees is picked

// EUpgradeDynamicFeesConfig to be tuned TODO ABENEGIA
var EUpgradeDynamicFeesConfig = DynamicFeesConfig{
UnitFees: commonfees.Dimensions{
1,
2,
3,
4,
},

BlockUnitsCap: commonfees.Dimensions{
math.MaxUint64,
math.MaxUint64,
math.MaxUint64,
math.MaxUint64,
},
}

type DynamicFeesConfig struct {
// UnitFees contains, per each fee dimension, the
// unit fees valid as soon as fork introducing dynamic fees
// activates. Unit fees will be then updated by the dynamic fees algo.
UnitFees commonfees.Dimensions

// BlockUnitsCap contains, per each fee dimension, the
// maximal complexity a valid P-chain block can host
BlockUnitsCap commonfees.Dimensions
}
27 changes: 19 additions & 8 deletions vms/avm/txs/executor/semantic_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"reflect"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/avm/state"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/avm/txs/fees"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"

commonfees "github.com/ava-labs/avalanchego/vms/components/fees"
)

var (
Expand All @@ -27,20 +30,22 @@ var (

type SemanticVerifier struct {
*Backend
State state.ReadOnlyChain
Tx *txs.Tx
BlkFeeManager *commonfees.Manager
UnitCaps commonfees.Dimensions
State state.ReadOnlyChain
Tx *txs.Tx
}

func (v *SemanticVerifier) BaseTx(tx *txs.BaseTx) error {
return v.verifyBaseTx(tx, nil, nil)
return v.verifyBaseTx(tx, nil, nil, v.Tx.Creds)
}

func (v *SemanticVerifier) CreateAssetTx(tx *txs.CreateAssetTx) error {
return v.verifyBaseTx(&tx.BaseTx, nil, nil)
return v.verifyBaseTx(&tx.BaseTx, nil, nil, v.Tx.Creds)
}

func (v *SemanticVerifier) OperationTx(tx *txs.OperationTx) error {
if err := v.verifyBaseTx(&tx.BaseTx, nil, nil); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, nil, nil, v.Tx.Creds); err != nil {
return err
}

Expand All @@ -61,7 +66,7 @@ func (v *SemanticVerifier) OperationTx(tx *txs.OperationTx) error {
}

func (v *SemanticVerifier) ImportTx(tx *txs.ImportTx) error {
if err := v.verifyBaseTx(&tx.BaseTx, tx.ImportedIns, nil); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, tx.ImportedIns, nil, v.Tx.Creds); err != nil {
return err
}

Expand Down Expand Up @@ -102,7 +107,7 @@ func (v *SemanticVerifier) ImportTx(tx *txs.ImportTx) error {
}

func (v *SemanticVerifier) ExportTx(tx *txs.ExportTx) error {
if err := v.verifyBaseTx(&tx.BaseTx, nil, tx.ExportedOuts); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, nil, tx.ExportedOuts, v.Tx.Creds); err != nil {
return err
}

Expand Down Expand Up @@ -130,9 +135,15 @@ func (v *SemanticVerifier) verifyBaseTx(
tx *txs.BaseTx,
importedIns []*avax.TransferableInput,
exportedOuts []*avax.TransferableOutput,
creds []*fxs.FxCredential,
) error {
feeCalculator := fees.Calculator{
Config: v.Config,
IsEUpgradeActive: v.Config.IsEUpgradeActivated(v.State.GetTimestamp()),
Config: v.Config,
FeeManager: v.BlkFeeManager,
ConsumedUnitsCap: v.UnitCaps,
Codec: v.Codec,
Credentials: creds,
}
if err := tx.Visit(&feeCalculator); err != nil {
return err
Expand Down
Loading

0 comments on commit 9631ec8

Please sign in to comment.