Skip to content

Commit

Permalink
Implement ACP-103 fee package (#3203)
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Benegiamo <[email protected]>
Signed-off-by: Joshua Kim <[email protected]>
Co-authored-by: Alberto Benegiamo <[email protected]>
Co-authored-by: Joshua Kim <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent 9a6418c commit 38fb295
Show file tree
Hide file tree
Showing 8 changed files with 1,030 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/holiman/uint256 v1.2.4
github.com/huin/goupnp v1.3.0
github.com/jackpal/gateway v1.0.6
github.com/jackpal/go-nat-pmp v1.0.2
Expand Down Expand Up @@ -117,7 +118,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down
22 changes: 22 additions & 0 deletions vms/components/fee/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// The fee package implements dynamic gas pricing specified in ACP-103:
// https://github.com/avalanche-foundation/ACPs/tree/main/ACPs/103-dynamic-fees
package fee

type Config struct {
// Weights to merge fee dimensions into a single gas value.
Weights Dimensions `json:"weights"`
// Maximum amount of gas the chain is allowed to store for future use.
MaxGasCapacity Gas `json:"maxGasCapacity"`
// Maximum amount of gas the chain is allowed to consume per second.
MaxGasPerSecond Gas `json:"maxGasPerSecond"`
// Target amount of gas the chain should consume per second to keep the fees
// stable.
TargetGasPerSecond Gas `json:"targetGasPerSecond"`
// Minimum price per unit of gas.
MinGasPrice GasPrice `json:"minGasPrice"`
// Constant used to convert excess gas to a gas price.
ExcessConversionConstant Gas `json:"excessConversionConstant"`
}
70 changes: 70 additions & 0 deletions vms/components/fee/dimensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package fee

import "github.com/ava-labs/avalanchego/utils/math"

const (
Bandwidth Dimension = iota
DBRead
DBWrite // includes deletes
Compute

NumDimensions = iota
)

type (
Dimension uint
Dimensions [NumDimensions]uint64
)

// Add returns d + sum(os...).
//
// If overflow occurs, an error is returned.
func (d Dimensions) Add(os ...Dimensions) (Dimensions, error) {
var err error
for _, o := range os {
for i := range o {
d[i], err = math.Add(d[i], o[i])
if err != nil {
return d, err
}
}
}
return d, nil
}

// Sub returns d - sum(os...).
//
// If underflow occurs, an error is returned.
func (d Dimensions) Sub(os ...Dimensions) (Dimensions, error) {
var err error
for _, o := range os {
for i := range o {
d[i], err = math.Sub(d[i], o[i])
if err != nil {
return d, err
}
}
}
return d, nil
}

// ToGas returns d · weights.
//
// If overflow occurs, an error is returned.
func (d Dimensions) ToGas(weights Dimensions) (Gas, error) {
var res uint64
for i := range d {
v, err := math.Mul(d[i], weights[i])
if err != nil {
return 0, err
}
res, err = math.Add(res, v)
if err != nil {
return 0, err
}
}
return Gas(res), nil
}
Loading

0 comments on commit 38fb295

Please sign in to comment.