-
Notifications
You must be signed in to change notification settings - Fork 680
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ACP-103 fee package (#3203)
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
1 parent
9a6418c
commit 38fb295
Showing
8 changed files
with
1,030 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.