Skip to content

Commit

Permalink
P-chain - introducing fees calculators (#2698)
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Benegiamo <alberto.benegiamo@gmail.com>
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
Co-authored-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
  • Loading branch information
abi87 and joshua-kim committed May 21, 2024
1 parent 34e7b2f commit 066c3a6
Show file tree
Hide file tree
Showing 27 changed files with 640 additions and 201 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
"github.com/ava-labs/avalanchego/vms/proposervm"
)

Expand Down Expand Up @@ -758,9 +759,9 @@ func getStakingConfig(v *viper.Viper, networkID uint32) (node.StakingConfig, err
return config, nil
}

func getTxFeeConfig(v *viper.Viper, networkID uint32) genesis.TxFeeConfig {
func getTxFeeConfig(v *viper.Viper, networkID uint32) fee.StaticConfig {
if networkID != constants.MainnetID && networkID != constants.FujiID {
return genesis.TxFeeConfig{
return fee.StaticConfig{
TxFee: v.GetUint64(TxFeeKey),
CreateAssetTxFee: v.GetUint64(CreateAssetTxFeeKey),
CreateSubnetTxFee: v.GetUint64(CreateSubnetTxFeeKey),
Expand Down Expand Up @@ -1325,7 +1326,7 @@ func GetNodeConfig(v *viper.Viper) (node.Config, error) {
nodeConfig.FdLimit = v.GetUint64(FdLimitKey)

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

// Genesis Data
genesisStakingCfg := nodeConfig.StakingConfig.StakingConfig
Expand Down
3 changes: 2 additions & 1 deletion genesis/genesis_fuji.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
)

var (
Expand All @@ -18,7 +19,7 @@ var (

// FujiParams are the params used for the fuji testnet
FujiParams = Params{
TxFeeConfig: TxFeeConfig{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: 10 * units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
Expand Down
3 changes: 2 additions & 1 deletion genesis/genesis_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
)

// PrivateKey-vmRQiZeXEXYMyJhEiqdC2z5JhuDbxL8ix9UVvjgMu2Er1NepE => P-local1g65uqn6t77p656w64023nh8nd9updzmxyymev2
Expand All @@ -36,7 +37,7 @@ var (

// LocalParams are the params used for local networks
LocalParams = Params{
TxFeeConfig: TxFeeConfig{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: units.MilliAvax,
CreateSubnetTxFee: 100 * units.MilliAvax,
Expand Down
3 changes: 2 additions & 1 deletion genesis/genesis_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
)

var (
Expand All @@ -18,7 +19,7 @@ var (

// MainnetParams are the params used for mainnet
MainnetParams = Params{
TxFeeConfig: TxFeeConfig{
StaticConfig: fee.StaticConfig{
TxFee: units.MilliAvax,
CreateAssetTxFee: 10 * units.MilliAvax,
CreateSubnetTxFee: 1 * units.Avax,
Expand Down
34 changes: 7 additions & 27 deletions genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
)

type StakingConfig struct {
Expand All @@ -33,42 +34,21 @@ type StakingConfig struct {
RewardConfig reward.Config `json:"rewardConfig"`
}

type TxFeeConfig struct {
// Transaction fee
TxFee uint64 `json:"txFee"`
// Transaction fee for create asset transactions
CreateAssetTxFee uint64 `json:"createAssetTxFee"`
// Transaction fee for create subnet transactions
CreateSubnetTxFee uint64 `json:"createSubnetTxFee"`
// Transaction fee for transform subnet transactions
TransformSubnetTxFee uint64 `json:"transformSubnetTxFee"`
// Transaction fee for create blockchain transactions
CreateBlockchainTxFee uint64 `json:"createBlockchainTxFee"`
// Transaction fee for adding a primary network validator
AddPrimaryNetworkValidatorFee uint64 `json:"addPrimaryNetworkValidatorFee"`
// Transaction fee for adding a primary network delegator
AddPrimaryNetworkDelegatorFee uint64 `json:"addPrimaryNetworkDelegatorFee"`
// Transaction fee for adding a subnet validator
AddSubnetValidatorFee uint64 `json:"addSubnetValidatorFee"`
// Transaction fee for adding a subnet delegator
AddSubnetDelegatorFee uint64 `json:"addSubnetDelegatorFee"`
}

type Params struct {
StakingConfig
TxFeeConfig
fee.StaticConfig
}

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

Expand Down
15 changes: 8 additions & 7 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 @@ -122,13 +123,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"`
genesis.TxFeeConfig `json:"txFeeConfig"`
StateSyncConfig `json:"stateSyncConfig"`
BootstrapConfig `json:"bootstrapConfig"`
DatabaseConfig `json:"databaseConfig"`
HTTPConfig `json:"httpConfig"`
IPConfig `json:"ipConfig"`
StakingConfig `json:"stakingConfig"`
fee.StaticConfig `json:"txFeeConfig"`
StateSyncConfig `json:"stateSyncConfig"`
BootstrapConfig `json:"bootstrapConfig"`
DatabaseConfig `json:"databaseConfig"`

// Genesis information
GenesisBytes []byte `json:"-"`
Expand Down
49 changes: 26 additions & 23 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
"github.com/ava-labs/avalanchego/vms/platformvm/upgrade"
"github.com/ava-labs/avalanchego/vms/registry"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
Expand Down Expand Up @@ -1127,29 +1128,31 @@ func (n *Node) initVMs() error {
err := utils.Err(
n.VMManager.RegisterFactory(context.TODO(), constants.PlatformVMID, &platformvm.Factory{
Config: platformconfig.Config{
Chains: n.chainManager,
Validators: vdrs,
UptimeLockedCalculator: n.uptimeCalculator,
SybilProtectionEnabled: n.Config.SybilProtectionEnabled,
PartialSyncPrimaryNetwork: n.Config.PartialSyncPrimaryNetwork,
TrackedSubnets: n.Config.TrackedSubnets,
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,
UptimePercentage: n.Config.UptimeRequirement,
MinValidatorStake: n.Config.MinValidatorStake,
MaxValidatorStake: n.Config.MaxValidatorStake,
MinDelegatorStake: n.Config.MinDelegatorStake,
MinDelegationFee: n.Config.MinDelegationFee,
MinStakeDuration: n.Config.MinStakeDuration,
MaxStakeDuration: n.Config.MaxStakeDuration,
RewardConfig: n.Config.RewardConfig,
Chains: n.chainManager,
Validators: vdrs,
UptimeLockedCalculator: n.uptimeCalculator,
SybilProtectionEnabled: n.Config.SybilProtectionEnabled,
PartialSyncPrimaryNetwork: n.Config.PartialSyncPrimaryNetwork,
TrackedSubnets: n.Config.TrackedSubnets,
StaticFeeConfig: fee.StaticConfig{
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,
},
UptimePercentage: n.Config.UptimeRequirement,
MinValidatorStake: n.Config.MinValidatorStake,
MaxValidatorStake: n.Config.MaxValidatorStake,
MinDelegatorStake: n.Config.MinDelegatorStake,
MinDelegationFee: n.Config.MinDelegationFee,
MinStakeDuration: n.Config.MinStakeDuration,
MaxStakeDuration: n.Config.MaxStakeDuration,
RewardConfig: n.Config.RewardConfig,
UpgradeConfig: upgrade.Config{
ApricotPhase3Time: version.GetApricotPhase3Time(n.Config.NetworkID),
ApricotPhase5Time: version.GetApricotPhase5Time(n.Config.NetworkID),
Expand Down
19 changes: 11 additions & 8 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"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"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/txstest"
"github.com/ava-labs/avalanchego/vms/platformvm/upgrade"
Expand Down Expand Up @@ -308,14 +309,16 @@ func defaultConfig(t *testing.T, f fork) *config.Config {
Chains: chains.TestManager,
UptimeLockedCalculator: uptime.NewLockedCalculator(),
Validators: validators.NewManager(),
TxFee: defaultTxFee,
CreateSubnetTxFee: 100 * defaultTxFee,
CreateBlockchainTxFee: 100 * defaultTxFee,
MinValidatorStake: 5 * units.MilliAvax,
MaxValidatorStake: 500 * units.MilliAvax,
MinDelegatorStake: 1 * units.MilliAvax,
MinStakeDuration: defaultMinStakingDuration,
MaxStakeDuration: defaultMaxStakingDuration,
StaticFeeConfig: fee.StaticConfig{
TxFee: defaultTxFee,
CreateSubnetTxFee: 100 * defaultTxFee,
CreateBlockchainTxFee: 100 * defaultTxFee,
},
MinValidatorStake: 5 * units.MilliAvax,
MaxValidatorStake: 500 * units.MilliAvax,
MinDelegatorStake: 1 * units.MilliAvax,
MinStakeDuration: defaultMinStakingDuration,
MaxStakeDuration: defaultMaxStakingDuration,
RewardConfig: reward.Config{
MaxConsumptionRate: .12 * reward.PercentDenominator,
MinConsumptionRate: .10 * reward.PercentDenominator,
Expand Down
19 changes: 11 additions & 8 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/txstest"
"github.com/ava-labs/avalanchego/vms/platformvm/upgrade"
Expand Down Expand Up @@ -330,14 +331,16 @@ func defaultConfig(t *testing.T, f fork) *config.Config {
Chains: chains.TestManager,
UptimeLockedCalculator: uptime.NewLockedCalculator(),
Validators: validators.NewManager(),
TxFee: defaultTxFee,
CreateSubnetTxFee: 100 * defaultTxFee,
CreateBlockchainTxFee: 100 * defaultTxFee,
MinValidatorStake: 5 * units.MilliAvax,
MaxValidatorStake: 500 * units.MilliAvax,
MinDelegatorStake: 1 * units.MilliAvax,
MinStakeDuration: defaultMinStakingDuration,
MaxStakeDuration: defaultMaxStakingDuration,
StaticFeeConfig: fee.StaticConfig{
TxFee: defaultTxFee,
CreateSubnetTxFee: 100 * defaultTxFee,
CreateBlockchainTxFee: 100 * defaultTxFee,
},
MinValidatorStake: 5 * units.MilliAvax,
MaxValidatorStake: 500 * units.MilliAvax,
MinDelegatorStake: 1 * units.MilliAvax,
MinStakeDuration: defaultMinStakingDuration,
MaxStakeDuration: defaultMaxStakingDuration,
RewardConfig: reward.Config{
MaxConsumptionRate: .12 * reward.PercentDenominator,
MinConsumptionRate: .10 * reward.PercentDenominator,
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
ID: avaxAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: env.config.CreateSubnetTxFee,
Amt: env.config.StaticFeeConfig.CreateSubnetTxFee,
},
}
utxoID := utxo.InputID()
Expand All @@ -158,7 +158,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
In: &secp256k1fx.TransferInput{
Amt: env.config.CreateSubnetTxFee,
Amt: env.config.StaticFeeConfig.CreateSubnetTxFee,
},
}},
}},
Expand Down
45 changes: 4 additions & 41 deletions vms/platformvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
"github.com/ava-labs/avalanchego/vms/platformvm/upgrade"
)

Expand All @@ -30,6 +31,9 @@ type Config struct {
// calling VM.Initialize.
Validators validators.Manager

// All static fees config active before E-upgrade
StaticFeeConfig fee.StaticConfig

// Provides access to the uptime manager as a thread safe data structure
UptimeLockedCalculator uptime.LockedCalculator

Expand All @@ -42,33 +46,6 @@ type Config struct {
// Set of subnets that this node is validating
TrackedSubnets set.Set[ids.ID]

// Fee that is burned by every non-state creating transaction
TxFee uint64

// Fee that must be burned by every state creating transaction before AP3
CreateAssetTxFee uint64

// Fee that must be burned by every subnet creating transaction after AP3
CreateSubnetTxFee uint64

// Fee that must be burned by every transform subnet transaction
TransformSubnetTxFee uint64

// Fee that must be burned by every blockchain creating transaction after AP3
CreateBlockchainTxFee uint64

// Transaction fee for adding a primary network validator
AddPrimaryNetworkValidatorFee uint64

// Transaction fee for adding a primary network delegator
AddPrimaryNetworkDelegatorFee uint64

// Transaction fee for adding a subnet validator
AddSubnetValidatorFee uint64

// Transaction fee for adding a subnet delegator
AddSubnetDelegatorFee uint64

// The minimum amount of tokens one must bond to be a validator
MinValidatorStake uint64

Expand Down Expand Up @@ -106,20 +83,6 @@ type Config struct {
UseCurrentHeight bool
}

func (c *Config) GetCreateBlockchainTxFee(timestamp time.Time) uint64 {
if c.UpgradeConfig.IsApricotPhase3Activated(timestamp) {
return c.CreateBlockchainTxFee
}
return c.CreateAssetTxFee
}

func (c *Config) GetCreateSubnetTxFee(timestamp time.Time) uint64 {
if c.UpgradeConfig.IsApricotPhase3Activated(timestamp) {
return c.CreateSubnetTxFee
}
return c.CreateAssetTxFee
}

// Create the blockchain described in [tx], but only if this node is a member of
// the subnet that validates the chain
func (c *Config) CreateChain(chainID ids.ID, tx *txs.CreateChainTx) {
Expand Down
Loading

0 comments on commit 066c3a6

Please sign in to comment.