-
Notifications
You must be signed in to change notification settings - Fork 848
sae: Implement custom tx AsOp
#5308
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
Merged
Merged
Changes from 95 commits
Commits
Show all changes
111 commits
Select commit
Hold shift + click to select a range
eb8c0a0
sae: Add C-chain custom tx serialization
StephenButtolph e248997
sae: Implement AsOp
StephenButtolph 253c255
wip
StephenButtolph 3cca8aa
comments
StephenButtolph 6346d2e
reduce exported identifiers
StephenButtolph ab83fe6
wip
StephenButtolph 8f3f888
wip
StephenButtolph 83cc38b
wip
StephenButtolph d606762
Extend fuzz tests
StephenButtolph 0a82734
nit
StephenButtolph 5b86b6b
wip
StephenButtolph 3d92bd5
wip
StephenButtolph 893e17e
wip
StephenButtolph 83b0ec0
wip
StephenButtolph c46bcc2
nit
StephenButtolph e92638f
nit
StephenButtolph f2faffb
lint
StephenButtolph 7447b08
lint
StephenButtolph 8c3b6f0
nits
StephenButtolph 0369966
fix bazel
StephenButtolph 85ae854
ci: free runner disk before publishing Antithesis images (#5310)
JonathanOppenheimer 88a234a
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph 4e261c6
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph 7073c57
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph d63857d
merged
StephenButtolph 06d2a41
nit
StephenButtolph c28b2d2
fuzz full op
StephenButtolph aba7590
mark as compatibility
StephenButtolph ca2a5ea
nit
StephenButtolph 4be05cf
nit
StephenButtolph 9b4c83f
nit
StephenButtolph 5d9dd0d
merged
StephenButtolph 7e58c8e
nit
StephenButtolph 8573565
nit
StephenButtolph 2f01ee8
Unblock myself for sae progress
StephenButtolph b28d132
sae: Smarter tx fuzzing
StephenButtolph f12a1b4
nit
StephenButtolph 258df6f
nit
StephenButtolph 8cefe95
nit
StephenButtolph fa25c9a
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph 95e7256
nit
StephenButtolph 5d2ef23
want got
StephenButtolph 46814f4
want got
StephenButtolph 58306f5
Merge branch 'StephenButtolph/atomic-tx-rewrite.1' of github.com:ava-…
StephenButtolph 88c09d1
merged
StephenButtolph 271658d
nit
StephenButtolph 9da6af8
nit
StephenButtolph 7bcb962
ci
StephenButtolph 34b1457
nosec
StephenButtolph 692f899
merged
StephenButtolph eedd62e
nit
StephenButtolph e1ece39
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 9e3bb98
ci
StephenButtolph a4bad9b
remove duplicate code
StephenButtolph 56850ce
nit
StephenButtolph a41e9a6
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph cefd09f
nit
StephenButtolph 2ed8fcb
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 05ddba9
oops
StephenButtolph 192a266
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph fca7808
no moar recursion pls
StephenButtolph f47cf40
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 1ede276
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph 01cc4cf
Merge branch 'StephenButtolph/atomic-tx-rewrite.1' into StephenButtol…
StephenButtolph bc6aed5
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 0de4a41
nit
StephenButtolph 87d29d2
nit
StephenButtolph 6e9545c
Use the identifier rather than where the identifier is used
StephenButtolph 47fe30d
Update visability
StephenButtolph adeaa57
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph 6350008
merged
StephenButtolph 05bb2f4
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph ba1afe1
nit
StephenButtolph 3e3e6aa
ci
StephenButtolph 9ffcd6c
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 963c295
remove jank, use cmp
StephenButtolph 937c6f8
merged
StephenButtolph 8ae1a82
fix
StephenButtolph 3d11e3b
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph d39ed4d
nit
StephenButtolph 18c3c2b
merged
StephenButtolph 05930e4
missed one
StephenButtolph 9e0ffe5
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 99f52e1
nit
StephenButtolph 18a656e
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 8bb04f6
bazel
StephenButtolph e6a1d35
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 149b1cb
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.1
StephenButtolph a781d21
Merge branch 'StephenButtolph/atomic-tx-rewrite.1' into StephenButtol…
StephenButtolph 23b9916
Add comment
StephenButtolph c1b9fa8
nit
StephenButtolph b4d98b5
nit
StephenButtolph 81c3c02
nit
StephenButtolph 9e36643
nit
StephenButtolph 61dcfbd
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph ca7c26b
merged
StephenButtolph 268f6ed
Merge branch 'StephenButtolph/smart-atomic-tx-fuzzing' into StephenBu…
StephenButtolph 40c3ba6
cleanup
StephenButtolph 3ed6547
cleanup
StephenButtolph f0515ec
Add more cases
StephenButtolph 6dbe653
add comments
StephenButtolph 9ae76cc
Add interface check
StephenButtolph 6838983
nit
StephenButtolph d44b9e6
grammar
StephenButtolph 5370371
more succinct
StephenButtolph cfd18da
merged
StephenButtolph a5c21d1
pedantic
StephenButtolph eb701ee
ok
StephenButtolph 1ad0654
Document codec weirdness
StephenButtolph ac82445
simplify
StephenButtolph cab3300
Merge branch 'master' into StephenButtolph/atomic-tx-rewrite.2
StephenButtolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| # gazelle:default_visibility //vms/saevm/cchain:__subpackages__,//vms/saevm/cchain:external_consumers | ||
|
|
||
| package(default_visibility = [ | ||
| "//vms/saevm/cchain:__subpackages__", | ||
| "//vms/saevm/cchain:external_consumers", | ||
| ]) | ||
|
|
||
| package_group( | ||
| name = "external_consumers", | ||
| packages = [], | ||
| ) |
This file contains hidden or 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,64 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
| load("//.bazel:defs.bzl", "go_test") | ||
|
|
||
| package(default_visibility = [ | ||
| "//vms/saevm/cchain:__subpackages__", | ||
| "//vms/saevm/cchain:external_consumers", | ||
| ]) | ||
|
|
||
| go_library( | ||
| name = "tx", | ||
| srcs = [ | ||
| "codec.go", | ||
| "export.go", | ||
| "import.go", | ||
| "tx.go", | ||
| ], | ||
| importpath = "github.com/ava-labs/avalanchego/vms/saevm/cchain/tx", | ||
| deps = [ | ||
| "//codec", | ||
| "//codec/linearcodec", | ||
| "//graft/coreth/plugin/evm/atomic", | ||
| "//graft/coreth/plugin/evm/upgrade/ap5", | ||
| "//ids", | ||
| "//utils/hashing", | ||
| "//utils/math", | ||
| "//utils/wrappers", | ||
| "//vms/components/avax", | ||
| "//vms/components/gas", | ||
| "//vms/saevm/hook", | ||
| "//vms/secp256k1fx", | ||
| "@com_github_ava_labs_libevm//common", | ||
| "@com_github_holiman_uint256//:uint256", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "tx_test", | ||
| srcs = [ | ||
| "compatibility_test.go", | ||
| "tx_test.go", | ||
| ], | ||
| data = glob(["testdata/**"]), | ||
| embed = [":tx"], | ||
| deps = [ | ||
| "//graft/coreth/plugin/evm/atomic", | ||
| "//graft/coreth/plugin/evm/atomic/vm", | ||
| "//ids", | ||
| "//snow", | ||
| "//utils/math", | ||
| "//vms/components/avax", | ||
| "//vms/components/gas", | ||
| "//vms/components/verify", | ||
| "//vms/saevm/cchain/tx/txtest", | ||
| "//vms/saevm/cmputils", | ||
| "//vms/saevm/hook", | ||
| "//vms/secp256k1fx", | ||
| "@com_github_ava_labs_libevm//common", | ||
| "@com_github_google_go_cmp//cmp", | ||
| "@com_github_google_go_cmp//cmp/cmpopts", | ||
| "@com_github_holiman_uint256//:uint256", | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| ], | ||
| ) |
This file contains hidden or 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,75 @@ | ||
| // Copyright (C) 2019, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package tx | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/ava-labs/avalanchego/codec" | ||
| "github.com/ava-labs/avalanchego/codec/linearcodec" | ||
| "github.com/ava-labs/avalanchego/utils/wrappers" | ||
| "github.com/ava-labs/avalanchego/vms/secp256k1fx" | ||
| ) | ||
|
|
||
| const codecVersion uint16 = 0 | ||
|
|
||
| var c codec.Manager | ||
|
|
||
| func init() { | ||
| c = codec.NewDefaultManager() | ||
|
|
||
| // Registration order impacts the typeID included in the canonical format. | ||
| // We skip registrations in specific locations so that UTXOs in shared | ||
| // memory share the same serialized format as on the P-Chain and X-Chain. | ||
| var ( | ||
| lc = linearcodec.NewDefault() | ||
| errs = wrappers.Errs{} | ||
| ) | ||
| errs.Add( | ||
| lc.RegisterType(&Import{}), | ||
| lc.RegisterType(&Export{}), | ||
| ) | ||
| lc.SkipRegistrations(3) | ||
| errs.Add( | ||
| lc.RegisterType(&secp256k1fx.TransferInput{}), | ||
| ) | ||
| lc.SkipRegistrations(1) | ||
| errs.Add( | ||
| lc.RegisterType(&secp256k1fx.TransferOutput{}), | ||
| ) | ||
| lc.SkipRegistrations(1) | ||
| errs.Add( | ||
| lc.RegisterType(&secp256k1fx.Credential{}), | ||
| c.RegisterCodec(codecVersion, lc), | ||
| ) | ||
| if errs.Errored() { | ||
| panic(errs.Err) | ||
| } | ||
| } | ||
|
|
||
| // MarshalSlice returns the canonical binary format of a slice of transactions. | ||
| func MarshalSlice(txs []*Tx) ([]byte, error) { | ||
| if len(txs) == 0 { | ||
| return nil, nil | ||
| } | ||
| return c.Marshal(codecVersion, txs) | ||
| } | ||
|
|
||
| var errInefficientSlicePacking = errors.New("inefficient slice packing: empty slices should be packed as nil") | ||
|
|
||
| // ParseSlice deserializes a slice of [Tx] from its canonical binary format. | ||
| func ParseSlice(b []byte) ([]*Tx, error) { | ||
| if len(b) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| var txs []*Tx | ||
| if _, err := c.Unmarshal(b, &txs); err != nil { | ||
| return nil, err | ||
| } | ||
| if len(txs) == 0 { | ||
| return nil, errInefficientSlicePacking | ||
| } | ||
| return txs, nil | ||
| } |
This file contains hidden or 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,154 @@ | ||
| // Copyright (C) 2019, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package tx_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "math/big" | ||
| "testing" | ||
|
|
||
| "github.com/ava-labs/libevm/common" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/holiman/uint256" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ava-labs/avalanchego/graft/coreth/plugin/evm/atomic" | ||
| "github.com/ava-labs/avalanchego/snow" | ||
| "github.com/ava-labs/avalanchego/vms/components/gas" | ||
| "github.com/ava-labs/avalanchego/vms/saevm/cchain/tx/txtest" | ||
| "github.com/ava-labs/avalanchego/vms/saevm/hook" | ||
|
|
||
| . "github.com/ava-labs/avalanchego/vms/saevm/cchain/tx" | ||
| ) | ||
|
|
||
| // fuzz seeds f with [NewTxs] and fuzzes the test. | ||
| func fuzz(f *testing.F, ff func(t *testing.T, tx *Tx)) { | ||
| fuzzer := &txtest.F{ | ||
| F: f, | ||
| } | ||
| for _, tx := range NewTxs { | ||
| fuzzer.Add(tx) | ||
| } | ||
| fuzzer.Fuzz(ff) | ||
| } | ||
|
|
||
| func FuzzParseRoundTrip(f *testing.F) { | ||
| fuzz(f, func(t *testing.T, want *Tx) { | ||
| bytes, err := want.Bytes() | ||
| require.NoErrorf(t, err, "%T.Bytes()", want) | ||
|
|
||
| got, err := Parse(bytes) | ||
| require.NoError(t, err, "Parse()") | ||
| if diff := cmp.Diff(want, got, CmpOpt()); diff != "" { | ||
| t.Errorf("Parse() diff (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func FuzzJSONCompatibility(f *testing.F) { | ||
| fuzz(f, func(t *testing.T, newTx *Tx) { | ||
| oldTx := ToOldTx(t, newTx) | ||
| want, err := json.Marshal(oldTx) | ||
| require.NoErrorf(t, err, "json.Marshal(%T)", oldTx) | ||
|
|
||
| got, err := json.Marshal(newTx) | ||
| require.NoErrorf(t, err, "json.Marshal(%T)", newTx) | ||
| assert.JSONEq(t, string(want), string(got)) | ||
| }) | ||
| } | ||
|
|
||
| func FuzzAsOpCompatibility(f *testing.F) { | ||
| fuzz(f, func(t *testing.T, newTx *Tx) { | ||
| got, err := newTx.AsOp(AVAXAssetID) | ||
| if err != nil { | ||
| t.Skip("invalid tx") | ||
| } | ||
|
StephenButtolph marked this conversation as resolved.
|
||
|
|
||
| oldTx := ToOldTx(t, newTx) | ||
| gasUsed, err := oldTx.UnsignedAtomicTx.GasUsed(true) | ||
| require.NoErrorf(t, err, "%T.GasUsed(true)", oldTx.UnsignedAtomicTx) | ||
|
|
||
| gasPrice, err := atomic.EffectiveGasPrice(oldTx.UnsignedAtomicTx, AVAXAssetID, true) | ||
| require.NoErrorf(t, err, "atomic.EffectiveGasPrice(%T, avaxAssetID, true)", oldTx) | ||
|
|
||
| state := newAsOpStateDB() | ||
| if export, ok := oldTx.UnsignedAtomicTx.(*atomic.UnsignedExportTx); ok { | ||
| for _, in := range export.Ins { | ||
| state.initialNonces[in.Address] = in.Nonce | ||
| } | ||
| } | ||
|
|
||
| ctx := &snow.Context{AVAXAssetID: AVAXAssetID} | ||
| require.NoErrorf(t, oldTx.UnsignedAtomicTx.EVMStateTransfer(ctx, state), "%T.EVMStateTransfer()", oldTx.UnsignedAtomicTx) | ||
|
|
||
| want := hook.Op{ | ||
| ID: oldTx.ID(), | ||
| Gas: gas.Gas(gasUsed), | ||
| GasFeeCap: gasPrice, | ||
| Burn: state.op.Burn, | ||
| Mint: state.op.Mint, | ||
| } | ||
| if diff := cmp.Diff(want, got, cmpopts.EquateEmpty()); diff != "" { | ||
| t.Errorf("%T.AsOp() diff (-want +got):\n%s", newTx, diff) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // asOpStateDB is an in-memory [atomic.StateDB] for [FuzzAsOpCompatibility]. It | ||
| // constructs a [hook.Op] from [atomic.UnsignedAtomicTx.EVMStateTransfer]. | ||
| type asOpStateDB struct { | ||
| initialNonces map[common.Address]uint64 | ||
| op hook.Op | ||
| } | ||
|
|
||
| func newAsOpStateDB() *asOpStateDB { | ||
| return &asOpStateDB{ | ||
| initialNonces: make(map[common.Address]uint64), | ||
| op: hook.Op{ | ||
| Burn: make(map[common.Address]hook.AccountDebit), | ||
| Mint: make(map[common.Address]uint256.Int), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (s *asOpStateDB) AddBalance(addr common.Address, amount *uint256.Int) { | ||
| b := s.op.Mint[addr] | ||
| b.Add(&b, amount) | ||
| s.op.Mint[addr] = b | ||
| } | ||
|
|
||
| func (s *asOpStateDB) SubBalance(addr common.Address, amount *uint256.Int) { | ||
| d := s.op.Burn[addr] | ||
| d.Amount.Add(&d.Amount, amount) | ||
| d.MinBalance = d.Amount | ||
| s.op.Burn[addr] = d | ||
| } | ||
|
|
||
| func (*asOpStateDB) GetBalance(common.Address) *uint256.Int { | ||
| // Large enough to never underflow, but small enough to never overflow. | ||
| return new(uint256.Int).Lsh(uint256.NewInt(1), 128) | ||
| } | ||
|
|
||
| func (*asOpStateDB) AddBalanceMultiCoin(common.Address, common.Hash, *big.Int) {} | ||
|
|
||
| func (*asOpStateDB) SubBalanceMultiCoin(common.Address, common.Hash, *big.Int) {} | ||
|
|
||
| func (*asOpStateDB) GetBalanceMultiCoin(common.Address, common.Hash) *big.Int { | ||
| // Large enough to never underflow, but small enough to never overflow. | ||
| return new(big.Int).Lsh(big.NewInt(1), 128) | ||
| } | ||
|
|
||
| func (s *asOpStateDB) SetNonce(addr common.Address, nonce uint64) { | ||
| d := s.op.Burn[addr] | ||
| // The op specifies what nonce is being consumed, not the next nonce. So we | ||
| // need to subtract 1. | ||
| d.Nonce = nonce - 1 | ||
| s.op.Burn[addr] = d | ||
| } | ||
|
|
||
| func (s *asOpStateDB) GetNonce(addr common.Address) uint64 { | ||
| return s.initialNonces[addr] | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.