From 9b2c9bd091444369987fea8b565598630522053f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Fri, 13 Dec 2024 11:52:50 +0100 Subject: [PATCH 1/2] removed unused json files for wallet template --- .../wallet/golden/SpawnArguments.json | 8 ----- vm/templates/wallet/golden/SpawnPayload.json | 7 ---- .../wallet/golden/SpendArguments.json | 32 ------------------- vm/templates/wallet/golden/SpendPayload.json | 7 ---- 4 files changed, 54 deletions(-) delete mode 100644 vm/templates/wallet/golden/SpawnArguments.json delete mode 100644 vm/templates/wallet/golden/SpawnPayload.json delete mode 100644 vm/templates/wallet/golden/SpendArguments.json delete mode 100644 vm/templates/wallet/golden/SpendPayload.json diff --git a/vm/templates/wallet/golden/SpawnArguments.json b/vm/templates/wallet/golden/SpawnArguments.json deleted file mode 100644 index 16c8e12fa1..0000000000 --- a/vm/templates/wallet/golden/SpawnArguments.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Object": {"PublicKey": "0x0000000000000000000000000000000000000000000000000000000012345678"}, - "Hex": "0000000000000000000000000000000000000000000000000000000012345678" -} -{ - "Object": {"PublicKey": "0x1234000000000000000000000000000000000000000000000000000000000001"}, - "Hex": "1234000000000000000000000000000000000000000000000000000000000001" -} diff --git a/vm/templates/wallet/golden/SpawnPayload.json b/vm/templates/wallet/golden/SpawnPayload.json deleted file mode 100644 index 04cbd1cfe5..0000000000 --- a/vm/templates/wallet/golden/SpawnPayload.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Object": { - "Nonce": 1543, - "GasPrice": 256 - }, - "Hex": "1d180104" -} diff --git a/vm/templates/wallet/golden/SpendArguments.json b/vm/templates/wallet/golden/SpendArguments.json deleted file mode 100644 index e6b009a6b0..0000000000 --- a/vm/templates/wallet/golden/SpendArguments.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "Object": { - "Destination": [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 3, - 51, - 51 - ], - "Amount": 351 - }, - "Hex": "0000000000000000000000000000000000000000000333337d05" -} diff --git a/vm/templates/wallet/golden/SpendPayload.json b/vm/templates/wallet/golden/SpendPayload.json deleted file mode 100644 index c151c467bc..0000000000 --- a/vm/templates/wallet/golden/SpendPayload.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Object": { - "Nonce": 1543, - "GasPrice": 111222333 - }, - "Hex": "1d18f678841a" -} From 7703d8be101ad5d61591fb66e77fff805650fdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Fri, 13 Dec 2024 12:38:08 +0100 Subject: [PATCH 2/2] support multisig in transactions api --- api/grpcserver/v2alpha1/transaction.go | 118 ++++++++++------- api/grpcserver/v2alpha1/transaction_test.go | 136 +++++++++++--------- vm/sdk/multisig/tx.go | 16 +-- vm/sdk/wallet/tx.go | 3 +- vm/templates/multisig/methods.go | 52 ++++++++ vm/templates/multisig/program.go | 13 +- vm/templates/selectors.go | 23 ++++ vm/templates/wallet/methods.go | 25 +--- vm/vm_test.go | 26 ++-- 9 files changed, 253 insertions(+), 159 deletions(-) create mode 100644 vm/templates/multisig/methods.go create mode 100644 vm/templates/selectors.go diff --git a/api/grpcserver/v2alpha1/transaction.go b/api/grpcserver/v2alpha1/transaction.go index b3653ddc47..bc2a42f7c8 100644 --- a/api/grpcserver/v2alpha1/transaction.go +++ b/api/grpcserver/v2alpha1/transaction.go @@ -27,6 +27,7 @@ import ( "github.com/spacemeshos/go-spacemesh/sql/transactions" "github.com/spacemeshos/go-spacemesh/system" "github.com/spacemeshos/go-spacemesh/vm/core" + "github.com/spacemeshos/go-spacemesh/vm/templates/multisig" "github.com/spacemeshos/go-spacemesh/vm/templates/wallet" ) @@ -174,7 +175,7 @@ func (s *TransactionService) ParseTransaction( t.MaxGas = header.MaxGas t.GasPrice = header.GasPrice t.MaxSpend = header.MaxSpend - contents, txType, err := toTxContents(raw.Raw) + contents, txType, err := toTxContents(raw.Raw, header) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -327,7 +328,7 @@ func (s *TransactionService) toTx( t.GasPrice = tx.GasPrice t.MaxSpend = tx.MaxSpend - contents, txType, err := toTxContents(tx.Raw) + contents, txType, err := toTxContents(tx.Raw, tx.TxHeader) if err != nil { return nil } @@ -403,61 +404,88 @@ func (s *TransactionService) convertTxState( } } -func decodeTxArgs(decoder *scale.Decoder) (any, *core.Address, error) { +func toTxContents(rawTx []byte, header *types.TxHeader) ( + *spacemeshv2alpha1.TransactionContents, spacemeshv2alpha1.Transaction_TransactionType, error, +) { + res := &spacemeshv2alpha1.TransactionContents{} + txType := spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_UNSPECIFIED + var tx core.Tx - _, err := tx.DecodeScale(decoder) + _, err := tx.DecodeScale(scale.NewDecoder(bytes.NewReader(rawTx))) if err != nil { - return nil, nil, fmt.Errorf("%w: decoding TX: %w", core.ErrMalformed, err) + return nil, txType, fmt.Errorf("%w: decoding TX: %w", core.ErrMalformed, err) } var payload athcon.Payload err = gossamerScale.Unmarshal(tx.Payload, &payload) if err != nil { - return nil, nil, fmt.Errorf("%w: tx payload: %w", core.ErrMalformed, err) + return nil, txType, fmt.Errorf("%w: tx payload: %w", core.ErrMalformed, err) } - txArgs, err := wallet.ParseArgs(payload) - if err != nil { - return nil, nil, fmt.Errorf("%w: decoding TX args: %w", core.ErrMalformed, err) - } - return txArgs, &wallet.TemplateAddress, nil -} - -func toTxContents(rawTx []byte) (*spacemeshv2alpha1.TransactionContents, - spacemeshv2alpha1.Transaction_TransactionType, error, -) { - res := &spacemeshv2alpha1.TransactionContents{} - txType := spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_UNSPECIFIED - - r := bytes.NewReader(rawTx) - txArgs, _, err := decodeTxArgs(scale.NewDecoder(r)) - if err != nil { - return res, txType, err - } - - switch args := txArgs.(type) { - case *wallet.SpawnArgs: - res.Contents = &spacemeshv2alpha1.TransactionContents_SingleSigSpawn{ - SingleSigSpawn: &spacemeshv2alpha1.ContentsSingleSigSpawn{ - Pubkey: signing.NewPublicKey(args.Pubkey[:]).String(), - }, + switch header.TemplateAddress { + case wallet.TemplateAddress: + txArgs, err := wallet.ParseArgs(payload) + if err != nil { + return nil, txType, fmt.Errorf("%w: decoding TX args: %w", core.ErrMalformed, err) + } + switch args := txArgs.(type) { + case *wallet.SpawnArgs: + res.Contents = &spacemeshv2alpha1.TransactionContents_SingleSigSpawn{ + SingleSigSpawn: &spacemeshv2alpha1.ContentsSingleSigSpawn{ + Pubkey: signing.NewPublicKey(args.Pubkey[:]).String(), + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SPAWN + case *wallet.SpendArgs: + res.Contents = &spacemeshv2alpha1.TransactionContents_Send{ + Send: &spacemeshv2alpha1.ContentsSend{ + Destination: args.To.String(), + Amount: args.Amount, + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SEND + case *wallet.DeployArgs: + res.Contents = &spacemeshv2alpha1.TransactionContents_Deploy{ + Deploy: &spacemeshv2alpha1.ContentsDeploy{ + Template: core.TemplateAddress(args.Code).String(), + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_DEPLOY } - txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SPAWN - case *wallet.SpendArgs: - res.Contents = &spacemeshv2alpha1.TransactionContents_Send{ - Send: &spacemeshv2alpha1.ContentsSend{ - Destination: args.To.String(), - Amount: args.Amount, - }, + case multisig.TemplateAddress: + txArgs, err := multisig.ParseArgs(payload) + if err != nil { + return nil, txType, fmt.Errorf("%w: decoding TX args: %w", core.ErrMalformed, err) } - txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SEND - case *wallet.DeployArgs: - res.Contents = &spacemeshv2alpha1.TransactionContents_Deploy{ - Deploy: &spacemeshv2alpha1.ContentsDeploy{ - Template: core.TemplateAddress(args.Code).String(), - }, + switch args := txArgs.(type) { + case *multisig.SpawnArguments: + pubs := make([]string, 0, len(args.PublicKeys)) + for _, pub := range args.PublicKeys { + pubs = append(pubs, pub.String()) + } + res.Contents = &spacemeshv2alpha1.TransactionContents_MultiSigSpawn{ + MultiSigSpawn: &spacemeshv2alpha1.ContentsMultiSigSpawn{ + Required: uint32(args.Required), + Pubkey: pubs, + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SPAWN + case *multisig.SpendArguments: + res.Contents = &spacemeshv2alpha1.TransactionContents_Send{ + Send: &spacemeshv2alpha1.ContentsSend{ + Destination: args.To.String(), + Amount: args.Amount, + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SEND + case *multisig.DeployArguments: + res.Contents = &spacemeshv2alpha1.TransactionContents_Deploy{ + Deploy: &spacemeshv2alpha1.ContentsDeploy{ + Template: core.TemplateAddress(args.Code).String(), + }, + } + txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_DEPLOY } - txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_DEPLOY } return res, txType, nil diff --git a/api/grpcserver/v2alpha1/transaction_test.go b/api/grpcserver/v2alpha1/transaction_test.go index 911cdbe03a..bdf0d64c73 100644 --- a/api/grpcserver/v2alpha1/transaction_test.go +++ b/api/grpcserver/v2alpha1/transaction_test.go @@ -28,7 +28,9 @@ import ( "github.com/spacemeshos/go-spacemesh/vm" "github.com/spacemeshos/go-spacemesh/vm/core" "github.com/spacemeshos/go-spacemesh/vm/sdk" + sdkmultisig "github.com/spacemeshos/go-spacemesh/vm/sdk/multisig" "github.com/spacemeshos/go-spacemesh/vm/sdk/wallet" + "github.com/spacemeshos/go-spacemesh/vm/templates/multisig" walletTemplate "github.com/spacemeshos/go-spacemesh/vm/templates/wallet" ) @@ -580,7 +582,7 @@ func TestToTxContents(t *testing.T) { require.NoError(t, err) tx := newTx(t, 0, types.Address{}, signer) - contents, txType, err := toTxContents(tx.Raw) + contents, txType, err := toTxContents(tx.Raw, &types.TxHeader{TemplateAddress: walletTemplate.TemplateAddress}) require.NoError(t, err) require.NotNil(t, contents.GetSingleSigSpawn()) require.Nil(t, contents.GetSend()) @@ -594,7 +596,7 @@ func TestToTxContents(t *testing.T) { require.NoError(t, err) tx := newTx(t, 1, types.Address{}, signer) - contents, txType, err := toTxContents(tx.Raw) + contents, txType, err := toTxContents(tx.Raw, &types.TxHeader{TemplateAddress: walletTemplate.TemplateAddress}) require.NoError(t, err) require.NotNil(t, contents.GetSend()) require.Nil(t, contents.GetSingleSigSpawn()) @@ -602,76 +604,82 @@ func TestToTxContents(t *testing.T) { }) t.Run("multisig spawn", func(t *testing.T) { - t.Skip("multisig spawn is not supported yet") - // t.Parallel() - - // var pubs []ed25519.PublicKey - // pks := make([]ed25519.PrivateKey, 0, 3) - // for i := 0; i < 3; i++ { - // pub, pk, err := ed25519.GenerateKey(nil) - // require.NoError(t, err) - // pubs = append(pubs, pub) - // pks = append(pks, pk) - // } + t.Parallel() - // var agg *multisig2.Aggregator - // for i := 0; i < len(pks); i++ { - // part := multisig2.SelfSpawn(uint8(i), pks[i], multisig.TemplateAddress, 1, pubs, types.Nonce(1)) - // if agg == nil { - // agg = part - // } else { - // agg.Add(*part.Part(uint8(i))) - // } - // } - // rawTx := agg.Raw() + var ( + pubs []core.PublicKey + pubStrs []string + pks []ed25519.PrivateKey + ) + + for i := 0; i < 3; i++ { + pub, pk, err := ed25519.GenerateKey(nil) + require.NoError(t, err) + pks = append(pks, pk) + p := core.PublicKey(pub) + pubs = append(pubs, p) + pubStrs = append(pubStrs, p.String()) + } - // contents, txType, err := toTxContents(rawTx) - // require.NoError(t, err) - // require.NotNil(t, contents.GetMultiSigSpawn()) - // require.Nil(t, contents.GetSend()) - // require.Nil(t, contents.GetSingleSigSpawn()) - // require.Nil(t, contents.GetVestingSpawn()) - // require.Nil(t, contents.GetVaultSpawn()) - // require.Nil(t, contents.GetDrainVault()) - // require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SPAWN, txType) + tx, err := sdkmultisig.Spawn(multisig.TemplateAddress, 2, pubs, 0) + require.NoError(t, err) + agg := sdkmultisig.NewSignatureAggregator(tx) + for i := range 2 { + sig := core.SignRawTx(tx, types.Hash20{}, pks[i]) + agg.Add(uint8(i), core.Signature(sig)) + } + rawTx := agg.Raw() + contents, txType, err := toTxContents(rawTx, &types.TxHeader{TemplateAddress: multisig.TemplateAddress}) + require.NoError(t, err) + require.NotNil(t, contents.GetMultiSigSpawn()) + require.Equal(t, &spacemeshv2alpha1.ContentsMultiSigSpawn{ + Required: 2, + Pubkey: pubStrs, + }, contents.GetMultiSigSpawn()) + require.Nil(t, contents.GetSend()) + require.Nil(t, contents.GetSingleSigSpawn()) + require.Nil(t, contents.GetVestingSpawn()) + require.Nil(t, contents.GetVaultSpawn()) + require.Nil(t, contents.GetDrainVault()) + require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SPAWN, txType) }) t.Run("multisig send", func(t *testing.T) { - t.Skip("multisig send is not supported yet") - // t.Parallel() - - // var pubs []ed25519.PublicKey - // pks := make([]ed25519.PrivateKey, 0, 3) - // for i := 0; i < 3; i++ { - // pub, pk, err := ed25519.GenerateKey(nil) - // require.NoError(t, err) - // pubs = append(pubs, pub) - // pks = append(pks, pk) - // } + t.Parallel() - // to, err := wallet.Address(*signing.NewPublicKey(pubs[0])) - // require.NoError(t, err) + var ( + pks []ed25519.PrivateKey + to = types.RandomAddress(t) + from = types.RandomAddress(t) + ) + for i := 0; i < 3; i++ { + _, pk, err := ed25519.GenerateKey(nil) + require.NoError(t, err) + pks = append(pks, pk) + } - // var agg *multisig2.Aggregator - // for i := 0; i < len(pks); i++ { - // part := multisig2.Spend(uint8(i), pks[i], multisig.TemplateAddress, to, 100, types.Nonce(1)) - // if agg == nil { - // agg = part - // } else { - // agg.Add(*part.Part(uint8(i))) - // } - // } - // rawTx := agg.Raw() + tx, err := sdkmultisig.Spend(from, to, 100, 1) + require.NoError(t, err) + agg := sdkmultisig.NewSignatureAggregator(tx) + for i := range 2 { + sig := core.SignRawTx(tx, types.Hash20{}, pks[i]) + agg.Add(uint8(i), core.Signature(sig)) + } + rawTx := agg.Raw() - // contents, txType, err := toTxContents(rawTx) - // require.NoError(t, err) - // require.NotNil(t, contents.GetSend()) - // require.Nil(t, contents.GetMultiSigSpawn()) - // require.Nil(t, contents.GetSingleSigSpawn()) - // require.Nil(t, contents.GetVestingSpawn()) - // require.Nil(t, contents.GetVaultSpawn()) - // require.Nil(t, contents.GetDrainVault()) - // require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SEND, txType) + contents, txType, err := toTxContents(rawTx, &types.TxHeader{TemplateAddress: multisig.TemplateAddress}) + require.NoError(t, err) + require.NotNil(t, contents.GetSend()) + require.Equal(t, &spacemeshv2alpha1.ContentsSend{ + Destination: to.String(), + Amount: 100, + }, contents.GetSend()) + require.Nil(t, contents.GetMultiSigSpawn()) + require.Nil(t, contents.GetSingleSigSpawn()) + require.Nil(t, contents.GetVestingSpawn()) + require.Nil(t, contents.GetVaultSpawn()) + require.Nil(t, contents.GetDrainVault()) + require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_MULTI_SIG_SEND, txType) }) t.Run("vault spawn", func(t *testing.T) { diff --git a/vm/sdk/multisig/tx.go b/vm/sdk/multisig/tx.go index a20eebe2e1..d3636c9e31 100644 --- a/vm/sdk/multisig/tx.go +++ b/vm/sdk/multisig/tx.go @@ -13,19 +13,9 @@ import ( "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/vm/core" "github.com/spacemeshos/go-spacemesh/vm/sdk" + "github.com/spacemeshos/go-spacemesh/vm/templates/multisig" ) -// SpawnArguments contains a collection with PublicKeys. -type SpawnArguments struct { - Required uint8 - PublicKeys []core.PublicKey -} - -type SpendArguments struct { - To types.Address - Amount uint64 -} - // part contains a reference to public key and signature from private key counterpart. type part struct { Ref uint8 @@ -72,7 +62,7 @@ func (a *SignatureAggregator) Raw() []byte { } func EncodeSpawnArgs(required uint8, pubkeys []core.PublicKey) []byte { - args := SpawnArguments{ + args := multisig.SpawnArguments{ Required: required, PublicKeys: pubkeys, } @@ -80,7 +70,7 @@ func EncodeSpawnArgs(required uint8, pubkeys []core.PublicKey) []byte { } func EncodeSpendArgs(to types.Address, amount uint64) []byte { - args := SpendArguments{ + args := multisig.SpendArguments{ To: to, Amount: amount, } diff --git a/vm/sdk/wallet/tx.go b/vm/sdk/wallet/tx.go index 94d710faa1..8670605f9f 100644 --- a/vm/sdk/wallet/tx.go +++ b/vm/sdk/wallet/tx.go @@ -13,6 +13,7 @@ import ( "github.com/spacemeshos/go-spacemesh/vm/core" "github.com/spacemeshos/go-spacemesh/vm/host" "github.com/spacemeshos/go-spacemesh/vm/sdk" + "github.com/spacemeshos/go-spacemesh/vm/templates" "github.com/spacemeshos/go-spacemesh/vm/templates/wallet" ) @@ -27,7 +28,7 @@ func Deploy(pk signing.PrivateKey, nonce core.Nonce, blob []byte, opts ...sdk.Op } athPayload := athcon.Payload{ - Selector: &wallet.DeploySelector, + Selector: &templates.DeploySelector, Input: blobEncoded.Bytes(), } payload, err := gossamerScale.Marshal(athPayload) diff --git a/vm/templates/multisig/methods.go b/vm/templates/multisig/methods.go new file mode 100644 index 0000000000..360b69863f --- /dev/null +++ b/vm/templates/multisig/methods.go @@ -0,0 +1,52 @@ +package multisig + +import ( + "errors" + "fmt" + + gossamerScale "github.com/ChainSafe/gossamer/pkg/scale" + athcon "github.com/athenavm/athena/ffi/athcon/bindings/go" + + "github.com/spacemeshos/go-spacemesh/common/types" + "github.com/spacemeshos/go-spacemesh/vm/core" + "github.com/spacemeshos/go-spacemesh/vm/templates" +) + +// SpawnArguments contains a collection with PublicKeys. +type SpawnArguments struct { + Required uint8 + PublicKeys []core.PublicKey +} + +type SpendArguments struct { + To types.Address + Amount uint64 +} + +type DeployArguments struct { + Code []byte +} + +func ParseArgs(payload athcon.Payload) (any, error) { + if payload.Selector == nil { + return nil, errors.New("nil method selector") + } + var txArgs any + + switch *payload.Selector { + case templates.DeploySelector: + txArgs = new(DeployArguments) + case templates.SpawnSelector: + txArgs = new(SpawnArguments) + case templates.SpendSelector: + txArgs = new(SpendArguments) + default: + return nil, fmt.Errorf("unknown method selector %q", payload.Selector.String()) + } + err := gossamerScale.Unmarshal(payload.Input, txArgs) + if err != nil { + return nil, fmt.Errorf("malformed tx arguments payload: %w", err) + } + + return txArgs, nil +} diff --git a/vm/templates/multisig/program.go b/vm/templates/multisig/program.go index 96538c013c..dd99f6e6f8 100644 --- a/vm/templates/multisig/program.go +++ b/vm/templates/multisig/program.go @@ -1,6 +1,17 @@ package multisig -import _ "embed" +import ( + _ "embed" + + "github.com/spacemeshos/go-spacemesh/common/types" + "github.com/spacemeshos/go-spacemesh/vm/core" +) //go:embed elf/multisig var PROGRAM []byte + +var TemplateAddress types.Address + +func init() { + TemplateAddress = core.TemplateAddress(PROGRAM) +} diff --git a/vm/templates/selectors.go b/vm/templates/selectors.go new file mode 100644 index 0000000000..244ce925dd --- /dev/null +++ b/vm/templates/selectors.go @@ -0,0 +1,23 @@ +package templates + +import ( + athcon "github.com/athenavm/athena/ffi/athcon/bindings/go" +) + +var DeploySelector, SpawnSelector, SpendSelector athcon.MethodSelector + +func init() { + var err error + DeploySelector, err = athcon.FromString("athexp_deploy") + if err != nil { + panic(err.Error()) + } + SpawnSelector, err = athcon.FromString("athexp_spawn") + if err != nil { + panic(err.Error()) + } + SpendSelector, err = athcon.FromString("athexp_spend") + if err != nil { + panic(err.Error()) + } +} diff --git a/vm/templates/wallet/methods.go b/vm/templates/wallet/methods.go index 204a548bc7..51924ee743 100644 --- a/vm/templates/wallet/methods.go +++ b/vm/templates/wallet/methods.go @@ -8,26 +8,9 @@ import ( athcon "github.com/athenavm/athena/ffi/athcon/bindings/go" "github.com/spacemeshos/go-spacemesh/common/types" + "github.com/spacemeshos/go-spacemesh/vm/templates" ) -var DeploySelector, spawnSelector, spendSelector athcon.MethodSelector - -func init() { - var err error - DeploySelector, err = athcon.FromString("athexp_deploy") - if err != nil { - panic(err.Error()) - } - spawnSelector, err = athcon.FromString("athexp_spawn") - if err != nil { - panic(err.Error()) - } - spendSelector, err = athcon.FromString("athexp_spend") - if err != nil { - panic(err.Error()) - } -} - type DeployArgs struct { Code []byte } @@ -48,11 +31,11 @@ func ParseArgs(payload athcon.Payload) (any, error) { var txArgs any switch *payload.Selector { - case DeploySelector: + case templates.DeploySelector: txArgs = new(DeployArgs) - case spawnSelector: + case templates.SpawnSelector: txArgs = new(SpawnArgs) - case spendSelector: + case templates.SpendSelector: txArgs = new(SpendArgs) default: return nil, fmt.Errorf("unknown method selector %q", payload.Selector.String()) diff --git a/vm/vm_test.go b/vm/vm_test.go index c100b99d70..71807915c0 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -271,10 +271,8 @@ func (t *tester) addWalletTemplate() *tester { return t.addTemplate(wallet.TemplateAddress, wallet.PROGRAM) } -var multiSigWalletTemplateAddress = types.Address{2} - func (t *tester) addMultiSigWalletTemplate() *tester { - return t.addTemplate(multiSigWalletTemplateAddress, multisig.PROGRAM) + return t.addTemplate(multisig.TemplateAddress, multisig.PROGRAM) } func (t *tester) addSingleSig(n int) *tester { @@ -1411,37 +1409,36 @@ func TestWallets(t *testing.T) { }) }) t.Run("MultiSig13", func(t *testing.T) { - testWallet(t, defaultGasPrice, multiSigWalletTemplateAddress, func(t *testing.T) *tester { + testWallet(t, defaultGasPrice, multisig.TemplateAddress, func(t *testing.T) *tester { return newTester(t). addMultiSigWalletTemplate(). - addMultisig(multiSigWalletTemplateAddress, funded, 1, 3). + addMultisig(multisig.TemplateAddress, funded, 1, 3). applyGenesisWithBalance(). - addMultisig(multiSigWalletTemplateAddress, total-funded, 1, 3) + addMultisig(multisig.TemplateAddress, total-funded, 1, 3) }) }) t.Run("MultiSig23", func(t *testing.T) { - testWallet(t, defaultGasPrice, multiSigWalletTemplateAddress, func(t *testing.T) *tester { + testWallet(t, defaultGasPrice, multisig.TemplateAddress, func(t *testing.T) *tester { return newTester(t). addMultiSigWalletTemplate(). - addMultisig(multiSigWalletTemplateAddress, funded, 2, 3). + addMultisig(multisig.TemplateAddress, funded, 2, 3). applyGenesisWithBalance(). - addMultisig(multiSigWalletTemplateAddress, total-funded, 2, 3) + addMultisig(multisig.TemplateAddress, total-funded, 2, 3) }) }) t.Run("MultiSig57", func(t *testing.T) { - testWallet(t, defaultGasPrice, multiSigWalletTemplateAddress, func(t *testing.T) *tester { + testWallet(t, defaultGasPrice, multisig.TemplateAddress, func(t *testing.T) *tester { return newTester(t). addMultiSigWalletTemplate(). - addMultisig(multiSigWalletTemplateAddress, funded, 5, 7). + addMultisig(multisig.TemplateAddress, funded, 5, 7). applyGenesisWithBalance(). - addMultisig(multiSigWalletTemplateAddress, total-funded, 5, 7) + addMultisig(multisig.TemplateAddress, total-funded, 5, 7) }) }) } func TestSingleSigWalletDeploy(t *testing.T) { - code := multisig.PROGRAM - templateAddress := core.TemplateAddress(code) + templateAddress := multisig.TemplateAddress tt := newTester( t, ).addWalletTemplate(). @@ -1462,6 +1459,7 @@ func TestSingleSigWalletDeploy(t *testing.T) { require.NoError(t, err) // 2. Deploy a new contract using it + code := multisig.PROGRAM rawDeployTx := types.NewRawTx(account.deploy(tt, 1, code)) skipped, results, err := tt.Apply(types.GetEffectiveGenesis()+1, []types.Transaction{{RawTx: rawDeployTx}}, nil) require.NoError(t, err)