Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X- chain semantic fee check #3187

Open
wants to merge 3 commits into
base: x-chain_introducing-fees-calculators
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions vms/avm/txs/executor/semantic_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/avm/state"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/avm/txs/fee"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
Expand All @@ -31,36 +32,15 @@ type SemanticVerifier struct {
}

func (v *SemanticVerifier) BaseTx(tx *txs.BaseTx) error {
for i, in := range tx.Ins {
// Note: Verification of the length of [t.tx.Creds] happens during
// syntactic verification, which happens before semantic verification.
cred := v.Tx.Creds[i].Credential
if err := v.verifyTransfer(tx, in, cred); err != nil {
return err
}
}

for _, out := range tx.Outs {
fxIndex, err := v.getFx(out.Out)
if err != nil {
return err
}

assetID := out.AssetID()
if err := v.verifyFxUsage(fxIndex, assetID); err != nil {
return err
}
}

return nil
return v.verifyBaseTx(tx, nil, nil)
}

func (v *SemanticVerifier) CreateAssetTx(tx *txs.CreateAssetTx) error {
return v.BaseTx(&tx.BaseTx)
return v.verifyBaseTx(&tx.BaseTx, nil, nil)
}

func (v *SemanticVerifier) OperationTx(tx *txs.OperationTx) error {
if err := v.BaseTx(&tx.BaseTx); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, nil, nil); err != nil {
return err
}

Expand All @@ -81,7 +61,7 @@ func (v *SemanticVerifier) OperationTx(tx *txs.OperationTx) error {
}

func (v *SemanticVerifier) ImportTx(tx *txs.ImportTx) error {
if err := v.BaseTx(&tx.BaseTx); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, tx.ImportedIns, nil); err != nil {
return err
}

Expand Down Expand Up @@ -122,7 +102,7 @@ func (v *SemanticVerifier) ImportTx(tx *txs.ImportTx) error {
}

func (v *SemanticVerifier) ExportTx(tx *txs.ExportTx) error {
if err := v.BaseTx(&tx.BaseTx); err != nil {
if err := v.verifyBaseTx(&tx.BaseTx, nil, tx.ExportedOuts); err != nil {
return err
}

Expand All @@ -146,6 +126,52 @@ func (v *SemanticVerifier) ExportTx(tx *txs.ExportTx) error {
return nil
}

func (v *SemanticVerifier) verifyBaseTx(
tx *txs.BaseTx,
importedIns []*avax.TransferableInput,
exportedOuts []*avax.TransferableOutput,
) error {
feeCalculator := fee.NewStaticCalculator(v.Backend.Config.StaticConfig)
fee, err := feeCalculator.CalculateFee(&txs.Tx{Unsigned: tx})
if err != nil {
return err
}

err = avax.VerifyTx(
fee,
v.FeeAssetID,
[][]*avax.TransferableInput{tx.Ins, importedIns},
[][]*avax.TransferableOutput{tx.Outs, exportedOuts},
v.Codec,
)
if err != nil {
return err
}

for i, in := range tx.Ins {
// Note: Verification of the length of [t.tx.Creds] happens during
// syntactic verification, which happens before semantic verification.
cred := v.Tx.Creds[i].Credential
if err := v.verifyTransfer(tx, in, cred); err != nil {
return err
}
}

for _, out := range tx.Outs {
fxIndex, err := v.getFx(out.Out)
if err != nil {
return err
}

assetID := out.AssetID()
if err := v.verifyFxUsage(fxIndex, assetID); err != nil {
return err
}
}

return nil
}

func (v *SemanticVerifier) verifyTransfer(
tx txs.UnsignedTx,
in *avax.TransferableInput,
Expand Down
Loading
Loading