Skip to content

Commit

Permalink
Add tests & apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Jul 10, 2024
1 parent c492684 commit 48ba73d
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 3 deletions.
7 changes: 4 additions & 3 deletions api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,15 @@ func decodeTxArgs(decoder *scale.Decoder) (uint8, *core.Address, scale.Encodable

var templateAddress *core.Address
var handler core.Handler
if method == core.MethodSpawn {
switch method {
case core.MethodSpawn:
templateAddress = &core.Address{}
if _, err := templateAddress.DecodeScale(decoder); err != nil {
return 0, nil, nil, fmt.Errorf("%w failed to decode template address %w", core.ErrMalformed, err)
}
} else if method == vesting.MethodDrainVault {
case vesting.MethodDrainVault:
templateAddress = &vesting.TemplateAddress
} else {
default:
templateAddress = &wallet.TemplateAddress
}

Expand Down
226 changes: 226 additions & 0 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package v2alpha1
import (
"context"
"errors"
multisig2 "github.com/spacemeshos/go-spacemesh/genvm/sdk/multisig"
"github.com/spacemeshos/go-spacemesh/genvm/sdk/vesting"
"github.com/spacemeshos/go-spacemesh/genvm/templates/multisig"
"github.com/spacemeshos/go-spacemesh/genvm/templates/vault"
vesting2 "github.com/spacemeshos/go-spacemesh/genvm/templates/vesting"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -474,3 +479,224 @@ func newTx(nonce uint64, recipient types.Address, signer *signing.EdSigner) *typ
}
return &tx
}

func TestToTxContents(t *testing.T) {
t.Parallel()

t.Run("singlesig spawn", func(t *testing.T) {
t.Parallel()

signer, err := signing.NewEdSigner()
require.NoError(t, err)
tx := newTx(0, types.Address{}, signer)

contents, txType, err := toTxContents(tx.Raw)
require.NoError(t, err)
require.NotNil(t, contents.GetSingleSigSpawn())
require.Nil(t, contents.GetSend())
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SPAWN, txType)
})

t.Run("singlesig send", func(t *testing.T) {
t.Parallel()

signer, err := signing.NewEdSigner()
require.NoError(t, err)
tx := newTx(1, types.Address{}, signer)

contents, txType, err := toTxContents(tx.Raw)
require.NoError(t, err)
require.NotNil(t, contents.GetSend())
require.Nil(t, contents.GetSingleSigSpawn())
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SEND, txType)
})

t.Run("multisig spawn", func(t *testing.T) {
t.Parallel()

var pubs []ed25519.PublicKey
pks := make([]ed25519.PrivateKey, 0, 3)
for i := 0; i < 3; i++ {
pub, pk, err := ed25519.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())))
require.NoError(t, err)
pubs = append(pubs, pub)
pks = append(pks, pk)
}

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()

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)
})

t.Run("multisig send", func(t *testing.T) {
t.Parallel()

var pubs []ed25519.PublicKey
pks := make([]ed25519.PrivateKey, 0, 3)
for i := 0; i < 3; i++ {
pub, pk, err := ed25519.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())))
require.NoError(t, err)
pubs = append(pubs, pub)
pks = append(pks, pk)
}

to := wallet.Address(pubs[0])

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()

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)
})

t.Run("vault spawn", func(t *testing.T) {
t.Parallel()

var pubs []ed25519.PublicKey
pks := make([]ed25519.PrivateKey, 0, 3)
for i := 0; i < 3; i++ {
pub, pk, err := ed25519.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())))
require.NoError(t, err)
pubs = append(pubs, pub)
pks = append(pks, pk)
}

owner := wallet.Address(pubs[0])
vaultArgs := &vault.SpawnArguments{
Owner: owner,
InitialUnlockAmount: uint64(1000),
TotalAmount: uint64(1001),
VestingStart: 105120,
VestingEnd: 4 * 105120,
}
vaultAddr := core.ComputePrincipal(vault.TemplateAddress, vaultArgs)

var agg *multisig2.Aggregator
for i := 0; i < len(pks); i++ {
part := multisig2.Spawn(uint8(i), pks[i], vaultAddr, vault.TemplateAddress, vaultArgs, types.Nonce(0))
if agg == nil {
agg = part
} else {
agg.Add(*part.Part(uint8(i)))
}
}
rawTx := agg.Raw()

contents, txType, err := toTxContents(rawTx)
require.NoError(t, err)
require.NotNil(t, contents.GetVaultSpawn())
require.Nil(t, contents.GetMultiSigSpawn())
require.Nil(t, contents.GetSingleSigSpawn())
require.Nil(t, contents.GetVestingSpawn())
require.Nil(t, contents.GetSend())
require.Nil(t, contents.GetDrainVault())
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_VAULT_SPAWN, txType)
})

t.Run("drain vault", func(t *testing.T) {
t.Parallel()

var pubs [][]byte
pks := make([]ed25519.PrivateKey, 0, 3)
for i := 0; i < 3; i++ {
pub, pk, err := ed25519.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())))
require.NoError(t, err)
pubs = append(pubs, pub)
pks = append(pks, pk)
}

principal := multisig2.Address(multisig.TemplateAddress, 3, pubs...)
to := wallet.Address(pubs[1])
vaultAddr := wallet.Address(pubs[2])

agg := vesting.DrainVault(
0,
pks[0],
principal,
vaultAddr,
to,
100,
types.Nonce(1))
for i := 1; i < len(pks); i++ {
part := vesting.DrainVault(uint8(i), pks[i], principal, vaultAddr, to, 100, types.Nonce(1))
agg.Add(*part.Part(uint8(i)))
}
rawTx := agg.Raw()

contents, txType, err := toTxContents(rawTx)
require.NoError(t, err)
require.NotNil(t, contents.GetDrainVault())
require.Nil(t, contents.GetMultiSigSpawn())
require.Nil(t, contents.GetSingleSigSpawn())
require.Nil(t, contents.GetVestingSpawn())
require.Nil(t, contents.GetSend())
require.Nil(t, contents.GetVaultSpawn())
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_DRAIN_VAULT, txType)
})

t.Run("multisig spawn", func(t *testing.T) {
t.Parallel()

var pubs []ed25519.PublicKey
pks := make([]ed25519.PrivateKey, 0, 3)
for i := 0; i < 3; i++ {
pub, pk, err := ed25519.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())))
require.NoError(t, err)
pubs = append(pubs, pub)
pks = append(pks, pk)
}

var agg *multisig2.Aggregator
for i := 0; i < len(pks); i++ {
part := multisig2.SelfSpawn(uint8(i), pks[i], vesting2.TemplateAddress, 1, pubs, types.Nonce(1))
if agg == nil {
agg = part
} else {
agg.Add(*part.Part(uint8(i)))
}
}
rawTx := agg.Raw()

contents, txType, err := toTxContents(rawTx)
require.NoError(t, err)
require.NotNil(t, contents.GetVestingSpawn())
require.Nil(t, contents.GetSend())
require.Nil(t, contents.GetSingleSigSpawn())
require.Nil(t, contents.GetMultiSigSpawn())
require.Nil(t, contents.GetVaultSpawn())
require.Nil(t, contents.GetDrainVault())
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_VESTING_SPAWN, txType)
})
}

0 comments on commit 48ba73d

Please sign in to comment.