Skip to content

Commit

Permalink
Simplify static fee calculations (#3240)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Jul 30, 2024
1 parent a0f7e42 commit 809081e
Show file tree
Hide file tree
Showing 23 changed files with 333 additions and 448 deletions.
39 changes: 16 additions & 23 deletions api/info/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network"
"github.com/ava-labs/avalanchego/network/peer"
Expand Down Expand Up @@ -46,20 +47,12 @@ type Info struct {
}

type Parameters struct {
Version *version.Application
NodeID ids.NodeID
NodePOP *signer.ProofOfPossession
NetworkID uint32
TxFee uint64
CreateAssetTxFee uint64
CreateSubnetTxFee uint64
TransformSubnetTxFee uint64
CreateBlockchainTxFee uint64
AddPrimaryNetworkValidatorFee uint64
AddPrimaryNetworkDelegatorFee uint64
AddSubnetValidatorFee uint64
AddSubnetDelegatorFee uint64
VMManager vms.Manager
Version *version.Application
NodeID ids.NodeID
NodePOP *signer.ProofOfPossession
NetworkID uint32
TxFeeConfig genesis.TxFeeConfig
VMManager vms.Manager
}

func NewService(
Expand Down Expand Up @@ -404,15 +397,15 @@ func (i *Info) GetTxFee(_ *http.Request, _ *struct{}, reply *GetTxFeeResponse) e
zap.String("method", "getTxFee"),
)

reply.TxFee = json.Uint64(i.TxFee)
reply.CreateAssetTxFee = json.Uint64(i.CreateAssetTxFee)
reply.CreateSubnetTxFee = json.Uint64(i.CreateSubnetTxFee)
reply.TransformSubnetTxFee = json.Uint64(i.TransformSubnetTxFee)
reply.CreateBlockchainTxFee = json.Uint64(i.CreateBlockchainTxFee)
reply.AddPrimaryNetworkValidatorFee = json.Uint64(i.AddPrimaryNetworkValidatorFee)
reply.AddPrimaryNetworkDelegatorFee = json.Uint64(i.AddPrimaryNetworkDelegatorFee)
reply.AddSubnetValidatorFee = json.Uint64(i.AddSubnetValidatorFee)
reply.AddSubnetDelegatorFee = json.Uint64(i.AddSubnetDelegatorFee)
reply.TxFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.TxFee)
reply.CreateAssetTxFee = json.Uint64(i.TxFeeConfig.CreateAssetTxFee)
reply.CreateSubnetTxFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.CreateSubnetTxFee)
reply.TransformSubnetTxFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.TransformSubnetTxFee)
reply.CreateBlockchainTxFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.CreateBlockchainTxFee)
reply.AddPrimaryNetworkValidatorFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.AddPrimaryNetworkValidatorFee)
reply.AddPrimaryNetworkDelegatorFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.AddPrimaryNetworkDelegatorFee)
reply.AddSubnetValidatorFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.AddSubnetValidatorFee)
reply.AddSubnetDelegatorFee = json.Uint64(i.TxFeeConfig.StaticFeeConfig.AddSubnetDelegatorFee)
return nil
}

Expand Down
26 changes: 14 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,18 +762,20 @@ func getStakingConfig(v *viper.Viper, networkID uint32) (node.StakingConfig, err
return config, nil
}

func getTxFeeConfig(v *viper.Viper, networkID uint32) fee.StaticConfig {
func getTxFeeConfig(v *viper.Viper, networkID uint32) genesis.TxFeeConfig {
if networkID != constants.MainnetID && networkID != constants.FujiID {
return fee.StaticConfig{
TxFee: v.GetUint64(TxFeeKey),
CreateAssetTxFee: v.GetUint64(CreateAssetTxFeeKey),
CreateSubnetTxFee: v.GetUint64(CreateSubnetTxFeeKey),
TransformSubnetTxFee: v.GetUint64(TransformSubnetTxFeeKey),
CreateBlockchainTxFee: v.GetUint64(CreateBlockchainTxFeeKey),
AddPrimaryNetworkValidatorFee: v.GetUint64(AddPrimaryNetworkValidatorFeeKey),
AddPrimaryNetworkDelegatorFee: v.GetUint64(AddPrimaryNetworkDelegatorFeeKey),
AddSubnetValidatorFee: v.GetUint64(AddSubnetValidatorFeeKey),
AddSubnetDelegatorFee: v.GetUint64(AddSubnetDelegatorFeeKey),
return genesis.TxFeeConfig{
CreateAssetTxFee: v.GetUint64(CreateAssetTxFeeKey),
StaticFeeConfig: fee.StaticConfig{
TxFee: v.GetUint64(TxFeeKey),
CreateSubnetTxFee: v.GetUint64(CreateSubnetTxFeeKey),
TransformSubnetTxFee: v.GetUint64(TransformSubnetTxFeeKey),
CreateBlockchainTxFee: v.GetUint64(CreateBlockchainTxFeeKey),
AddPrimaryNetworkValidatorFee: v.GetUint64(AddPrimaryNetworkValidatorFeeKey),
AddPrimaryNetworkDelegatorFee: v.GetUint64(AddPrimaryNetworkDelegatorFeeKey),
AddSubnetValidatorFee: v.GetUint64(AddSubnetValidatorFeeKey),
AddSubnetDelegatorFee: v.GetUint64(AddSubnetDelegatorFeeKey),
},
}
}
return genesis.GetTxFeeConfig(networkID)
Expand Down Expand Up @@ -1329,7 +1331,7 @@ func GetNodeConfig(v *viper.Viper) (node.Config, error) {
nodeConfig.FdLimit = v.GetUint64(FdLimitKey)

// Tx Fee
nodeConfig.StaticConfig = getTxFeeConfig(v, nodeConfig.NetworkID)
nodeConfig.TxFeeConfig = getTxFeeConfig(v, nodeConfig.NetworkID)

// Genesis Data
genesisStakingCfg := nodeConfig.StakingConfig.StakingConfig
Expand Down
16 changes: 8 additions & 8 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.IntSlice(ACPObjectKey, nil, "ACPs to object adoption")

// AVAX fees
fs.Uint64(TxFeeKey, genesis.LocalParams.TxFee, "Transaction fee, in nAVAX")
fs.Uint64(TxFeeKey, genesis.LocalParams.StaticFeeConfig.TxFee, "Transaction fee, in nAVAX")
fs.Uint64(CreateAssetTxFeeKey, genesis.LocalParams.CreateAssetTxFee, "Transaction fee, in nAVAX, for transactions that create new assets")
fs.Uint64(CreateSubnetTxFeeKey, genesis.LocalParams.CreateSubnetTxFee, "Transaction fee, in nAVAX, for transactions that create new subnets")
fs.Uint64(TransformSubnetTxFeeKey, genesis.LocalParams.TransformSubnetTxFee, "Transaction fee, in nAVAX, for transactions that transform subnets")
fs.Uint64(CreateBlockchainTxFeeKey, genesis.LocalParams.CreateBlockchainTxFee, "Transaction fee, in nAVAX, for transactions that create new blockchains")
fs.Uint64(AddPrimaryNetworkValidatorFeeKey, genesis.LocalParams.AddPrimaryNetworkValidatorFee, "Transaction fee, in nAVAX, for transactions that add new primary network validators")
fs.Uint64(AddPrimaryNetworkDelegatorFeeKey, genesis.LocalParams.AddPrimaryNetworkDelegatorFee, "Transaction fee, in nAVAX, for transactions that add new primary network delegators")
fs.Uint64(AddSubnetValidatorFeeKey, genesis.LocalParams.AddSubnetValidatorFee, "Transaction fee, in nAVAX, for transactions that add new subnet validators")
fs.Uint64(AddSubnetDelegatorFeeKey, genesis.LocalParams.AddSubnetDelegatorFee, "Transaction fee, in nAVAX, for transactions that add new subnet delegators")
fs.Uint64(CreateSubnetTxFeeKey, genesis.LocalParams.StaticFeeConfig.CreateSubnetTxFee, "Transaction fee, in nAVAX, for transactions that create new subnets")
fs.Uint64(TransformSubnetTxFeeKey, genesis.LocalParams.StaticFeeConfig.TransformSubnetTxFee, "Transaction fee, in nAVAX, for transactions that transform subnets")
fs.Uint64(CreateBlockchainTxFeeKey, genesis.LocalParams.StaticFeeConfig.CreateBlockchainTxFee, "Transaction fee, in nAVAX, for transactions that create new blockchains")
fs.Uint64(AddPrimaryNetworkValidatorFeeKey, genesis.LocalParams.StaticFeeConfig.AddPrimaryNetworkValidatorFee, "Transaction fee, in nAVAX, for transactions that add new primary network validators")
fs.Uint64(AddPrimaryNetworkDelegatorFeeKey, genesis.LocalParams.StaticFeeConfig.AddPrimaryNetworkDelegatorFee, "Transaction fee, in nAVAX, for transactions that add new primary network delegators")
fs.Uint64(AddSubnetValidatorFeeKey, genesis.LocalParams.StaticFeeConfig.AddSubnetValidatorFee, "Transaction fee, in nAVAX, for transactions that add new subnet validators")
fs.Uint64(AddSubnetDelegatorFeeKey, genesis.LocalParams.StaticFeeConfig.AddSubnetDelegatorFee, "Transaction fee, in nAVAX, for transactions that add new subnet delegators")

// Database
fs.String(DBTypeKey, leveldb.Name, fmt.Sprintf("Database type to use. Must be one of {%s, %s, %s}", leveldb.Name, memdb.Name, pebbledb.Name))
Expand Down
22 changes: 12 additions & 10 deletions genesis/genesis_fuji.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ var (

// FujiParams are the params used for the fuji testnet
FujiParams = Params{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: 10 * units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
TransformSubnetTxFee: 1 * units.Avax,
CreateBlockchainTxFee: 100 * units.MilliAvax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
TxFeeConfig: TxFeeConfig{
CreateAssetTxFee: 10 * units.MilliAvax,
StaticFeeConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
TransformSubnetTxFee: 1 * units.Avax,
CreateBlockchainTxFee: 100 * units.MilliAvax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
},
},
StakingConfig: StakingConfig{
UptimeRequirement: .8, // 80%
Expand Down
22 changes: 12 additions & 10 deletions genesis/genesis_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ var (

// LocalParams are the params used for local networks
LocalParams = Params{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
TransformSubnetTxFee: 100 * units.MilliAvax,
CreateBlockchainTxFee: 100 * units.MilliAvax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
TxFeeConfig: TxFeeConfig{
CreateAssetTxFee: units.MilliAvax,
StaticFeeConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
TransformSubnetTxFee: 100 * units.MilliAvax,
CreateBlockchainTxFee: 100 * units.MilliAvax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
},
},
StakingConfig: StakingConfig{
UptimeRequirement: .8, // 80%
Expand Down
22 changes: 12 additions & 10 deletions genesis/genesis_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ var (

// MainnetParams are the params used for mainnet
MainnetParams = Params{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: 10 * units.MilliAvax,
CreateSubnetTxFee: 1 * units.Avax,
TransformSubnetTxFee: 10 * units.Avax,
CreateBlockchainTxFee: 1 * units.Avax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
TxFeeConfig: TxFeeConfig{
CreateAssetTxFee: 10 * units.MilliAvax,
StaticFeeConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateSubnetTxFee: 1 * units.Avax,
TransformSubnetTxFee: 10 * units.Avax,
CreateBlockchainTxFee: 1 * units.Avax,
AddPrimaryNetworkValidatorFee: 0,
AddPrimaryNetworkDelegatorFee: 0,
AddSubnetValidatorFee: units.MilliAvax,
AddSubnetDelegatorFee: units.MilliAvax,
},
},
StakingConfig: StakingConfig{
UptimeRequirement: .8, // 80%
Expand Down
17 changes: 11 additions & 6 deletions genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ type StakingConfig struct {
RewardConfig reward.Config `json:"rewardConfig"`
}

type TxFeeConfig struct {
CreateAssetTxFee uint64 `json:"createAssetTxFee"`
StaticFeeConfig fee.StaticConfig `json:"staticFeeConfig"`
}

type Params struct {
StakingConfig
fee.StaticConfig
TxFeeConfig
}

func GetTxFeeConfig(networkID uint32) fee.StaticConfig {
func GetTxFeeConfig(networkID uint32) TxFeeConfig {
switch networkID {
case constants.MainnetID:
return MainnetParams.StaticConfig
return MainnetParams.TxFeeConfig
case constants.FujiID:
return FujiParams.StaticConfig
return FujiParams.TxFeeConfig
case constants.LocalID:
return LocalParams.StaticConfig
return LocalParams.TxFeeConfig
default:
return LocalParams.StaticConfig
return LocalParams.TxFeeConfig
}
}

Expand Down
15 changes: 7 additions & 8 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
)

type APIIndexerConfig struct {
Expand Down Expand Up @@ -123,13 +122,13 @@ type DatabaseConfig struct {

// Config contains all of the configurations of an Avalanche node.
type Config struct {
HTTPConfig `json:"httpConfig"`
IPConfig `json:"ipConfig"`
StakingConfig `json:"stakingConfig"`
fee.StaticConfig `json:"txFeeConfig"`
StateSyncConfig `json:"stateSyncConfig"`
BootstrapConfig `json:"bootstrapConfig"`
DatabaseConfig `json:"databaseConfig"`
HTTPConfig `json:"httpConfig"`
IPConfig `json:"ipConfig"`
StakingConfig `json:"stakingConfig"`
genesis.TxFeeConfig `json:"txFeeConfig"`
StateSyncConfig `json:"stateSyncConfig"`
BootstrapConfig `json:"bootstrapConfig"`
DatabaseConfig `json:"databaseConfig"`

// Genesis information
GenesisBytes []byte `json:"-"`
Expand Down
25 changes: 9 additions & 16 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,8 @@ func (n *Node) initVMs() error {
SybilProtectionEnabled: n.Config.SybilProtectionEnabled,
PartialSyncPrimaryNetwork: n.Config.PartialSyncPrimaryNetwork,
TrackedSubnets: n.Config.TrackedSubnets,
StaticFeeConfig: n.Config.StaticConfig,
CreateAssetTxFee: n.Config.CreateAssetTxFee,
StaticFeeConfig: n.Config.StaticFeeConfig,
UptimePercentage: n.Config.UptimeRequirement,
MinValidatorStake: n.Config.MinValidatorStake,
MaxValidatorStake: n.Config.MaxValidatorStake,
Expand All @@ -1249,7 +1250,7 @@ func (n *Node) initVMs() error {
}),
n.VMManager.RegisterFactory(context.TODO(), constants.AVMID, &avm.Factory{
Config: avmconfig.Config{
TxFee: n.Config.TxFee,
TxFee: n.Config.StaticFeeConfig.TxFee,
CreateAssetTxFee: n.Config.CreateAssetTxFee,
EUpgradeTime: eUpgradeTime,
},
Expand Down Expand Up @@ -1421,20 +1422,12 @@ func (n *Node) initInfoAPI() error {

service, err := info.NewService(
info.Parameters{
Version: version.CurrentApp,
NodeID: n.ID,
NodePOP: signer.NewProofOfPossession(n.Config.StakingSigningKey),
NetworkID: n.Config.NetworkID,
TxFee: n.Config.TxFee,
CreateAssetTxFee: n.Config.CreateAssetTxFee,
CreateSubnetTxFee: n.Config.CreateSubnetTxFee,
TransformSubnetTxFee: n.Config.TransformSubnetTxFee,
CreateBlockchainTxFee: n.Config.CreateBlockchainTxFee,
AddPrimaryNetworkValidatorFee: n.Config.AddPrimaryNetworkValidatorFee,
AddPrimaryNetworkDelegatorFee: n.Config.AddPrimaryNetworkDelegatorFee,
AddSubnetValidatorFee: n.Config.AddSubnetValidatorFee,
AddSubnetDelegatorFee: n.Config.AddSubnetDelegatorFee,
VMManager: n.VMManager,
Version: version.CurrentApp,
NodeID: n.ID,
NodePOP: signer.NewProofOfPossession(n.Config.StakingSigningKey),
NetworkID: n.Config.NetworkID,
TxFeeConfig: n.Config.TxFeeConfig,
VMManager: n.VMManager,
},
n.Log,
n.vdrs,
Expand Down
4 changes: 2 additions & 2 deletions tests/antithesis/avalanchego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (w *workload) issueXToPTransfer(ctx context.Context) {
xBaseTxFee = xContext.BaseTxFee
pBuilder = pWallet.Builder()
pContext = pBuilder.Context()
pBaseTxFee = pContext.BaseTxFee
pBaseTxFee = pContext.StaticFeeConfig.TxFee
txFees = xBaseTxFee + pBaseTxFee
neededBalance = txFees + units.Avax
)
Expand Down Expand Up @@ -484,7 +484,7 @@ func (w *workload) issuePToXTransfer(ctx context.Context) {
pContext = pBuilder.Context()
avaxAssetID = pContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
pBaseTxFee = pContext.BaseTxFee
pBaseTxFee = pContext.StaticFeeConfig.TxFee
xBaseTxFee = xContext.BaseTxFee
txFees = pBaseTxFee + xBaseTxFee
neededBalance = txFees + units.Schmeckle
Expand Down
14 changes: 5 additions & 9 deletions vms/platformvm/block/executor/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ func (v *verifier) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
}

var (
staticFeeConfig = v.txExecutorBackend.Config.StaticFeeConfig
upgradeConfig = v.txExecutorBackend.Config.UpgradeConfig
timestamp = onCommitState.GetTimestamp() // Equal to parent timestamp
feeCalculator = fee.NewStaticCalculator(staticFeeConfig, upgradeConfig, timestamp)
timestamp = onCommitState.GetTimestamp() // Equal to parent timestamp
feeCalculator = state.NewStaticFeeCalculator(v.txExecutorBackend.Config, timestamp)
)
return v.proposalBlock(b, nil, onCommitState, onAbortState, feeCalculator, nil, nil, nil)
}
Expand All @@ -181,10 +179,8 @@ func (v *verifier) ApricotStandardBlock(b *block.ApricotStandardBlock) error {
}

var (
staticFeeConfig = v.txExecutorBackend.Config.StaticFeeConfig
upgradeConfig = v.txExecutorBackend.Config.UpgradeConfig
timestamp = onAcceptState.GetTimestamp() // Equal to parent timestamp
feeCalculator = fee.NewStaticCalculator(staticFeeConfig, upgradeConfig, timestamp)
timestamp = onAcceptState.GetTimestamp() // Equal to parent timestamp
feeCalculator = state.NewStaticFeeCalculator(v.txExecutorBackend.Config, timestamp)
)
return v.standardBlock(b, feeCalculator, onAcceptState)
}
Expand All @@ -208,7 +204,7 @@ func (v *verifier) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
)
}

feeCalculator := fee.NewStaticCalculator(v.txExecutorBackend.Config.StaticFeeConfig, v.txExecutorBackend.Config.UpgradeConfig, currentTimestamp)
feeCalculator := state.NewStaticFeeCalculator(v.txExecutorBackend.Config, currentTimestamp)
atomicExecutor := executor.AtomicTxExecutor{
Backend: v.txExecutorBackend,
FeeCalculator: feeCalculator,
Expand Down
3 changes: 2 additions & 1 deletion vms/platformvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Config struct {
Validators validators.Manager

// All static fees config active before E-upgrade
StaticFeeConfig fee.StaticConfig
CreateAssetTxFee uint64 // Override for CreateSubnet and CreateChain before AP3
StaticFeeConfig fee.StaticConfig

// Provides access to the uptime manager as a thread safe data structure
UptimeLockedCalculator uptime.LockedCalculator
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func TestGetBlock(t *testing.T) {
service, _, factory := defaultService(t)
service.vm.ctx.Lock.Lock()

service.vm.StaticFeeConfig.CreateAssetTxFee = 100 * defaultTxFee
service.vm.Config.CreateAssetTxFee = 100 * defaultTxFee

builder, signer := factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1])
utx, err := builder.NewCreateChainTx(
Expand Down
Loading

0 comments on commit 809081e

Please sign in to comment.