Skip to content

Commit

Permalink
Improve error message of dynamic fee calculations (#3297)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 15, 2024
1 parent 35b58d0 commit 9a2aa85
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions vms/platformvm/txs/fee/dynamic_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
package fee

import (
"errors"
"fmt"

"github.com/ava-labs/avalanchego/vms/components/fee"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)

var _ Calculator = (*dynamicCalculator)(nil)
var (
_ Calculator = (*dynamicCalculator)(nil)

ErrCalculatingComplexity = errors.New("error calculating complexity")
ErrCalculatingGas = errors.New("error calculating gas")
ErrCalculatingCost = errors.New("error calculating cost")
)

func NewDynamicCalculator(
weights fee.Dimensions,
Expand All @@ -28,11 +37,27 @@ type dynamicCalculator struct {
func (c *dynamicCalculator) CalculateFee(tx txs.UnsignedTx) (uint64, error) {
complexity, err := TxComplexity(tx)
if err != nil {
return 0, err
return 0, fmt.Errorf("%w: %w", ErrCalculatingComplexity, err)
}
gas, err := complexity.ToGas(c.weights)
if err != nil {
return 0, err
return 0, fmt.Errorf(
"%w with complexity (%v) and weights (%v): %w",
ErrCalculatingGas,
complexity,
c.weights,
err,
)
}
fee, err := gas.Cost(c.price)
if err != nil {
return 0, fmt.Errorf(
"%w with gas (%d) and price (%d): %w",
ErrCalculatingCost,
gas,
c.price,
err,
)
}
return gas.Cost(c.price)
return fee, nil
}

0 comments on commit 9a2aa85

Please # to comment.