Skip to content

Commit

Permalink
NMT e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bianyuanop committed Jun 14, 2024
1 parent 47e78d7 commit cc4056a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/AnomalyFi/nodekit-seq
go 1.21.10

require (
github.com/AnomalyFi/hypersdk v0.0.0-20240613074544-863a4b32caa9
github.com/AnomalyFi/hypersdk v0.9.4-alpha.1
github.com/ava-labs/avalanche-network-runner v1.7.4-rc.0
github.com/ava-labs/avalanchego v1.11.6
github.com/ethereum/go-ethereum v1.13.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AnomalyFi/hypersdk v0.0.0-20240613074544-863a4b32caa9 h1:6T52iMmeL101cCWSbteLFKM17eqU2Eo/cHGUGlQNiFs=
github.com/AnomalyFi/hypersdk v0.0.0-20240613074544-863a4b32caa9/go.mod h1:UyhIJT0ViaKCJPYemkV4HuXMnAZfAZfo7rLB5uwrPpw=
github.com/AnomalyFi/hypersdk v0.9.4-alpha.1 h1:2HZYVX/nVSzcMl9kQGsbafD5dbkR6LJU2fUwMa9cv50=
github.com/AnomalyFi/hypersdk v0.9.4-alpha.1/go.mod h1:UyhIJT0ViaKCJPYemkV4HuXMnAZfAZfo7rLB5uwrPpw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
Expand Down
Binary file modified tests/e2e/e2e.test
Binary file not shown.
105 changes: 105 additions & 0 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package e2e_test

import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"os"
Expand All @@ -20,7 +24,9 @@ import (

"github.com/AnomalyFi/hypersdk/chain"
"github.com/AnomalyFi/hypersdk/codec"
hconsts "github.com/AnomalyFi/hypersdk/consts"
"github.com/AnomalyFi/hypersdk/crypto/ed25519"
"github.com/AnomalyFi/hypersdk/pubsub"
"github.com/AnomalyFi/hypersdk/rpc"
"github.com/AnomalyFi/nodekit-seq/actions"
"github.com/AnomalyFi/nodekit-seq/auth"
Expand Down Expand Up @@ -494,6 +500,105 @@ var _ = ginkgo.Describe("[Test]", func() {
})
})

ginkgo.It("submit a sequencer msg", func() {
blk := new(chain.StatefulBlock)
results := make([]*chain.Result, 1)

ginkgo.By("issue SequencerMsg to the first node", func() {
parser, err := instances[0].tcli.Parser(context.TODO())
require.NoError(err)
chainID := 45200
chainIDBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(chainIDBytes, uint64(chainID))
data := make([]byte, 200)
_, err = rand.Read(data)
require.NoError(err)

submit, tx, maxFee, err := instances[0].cli.GenerateTransaction(
context.Background(),
parser,
[]chain.Action{&actions.SequencerMsg{
FromAddress: rsender,
Data: data,
ChainId: chainIDBytes,
}},
factory,
)
require.NoError(err)
hutils.Outf("{{yellow}}generated sequencer message transaction{{/}}\n")
require.NoError(submit(context.Background()))
hutils.Outf("{{yellow}}submitted transaction{{/}}\n")
// listen block
wsCli, err := rpc.NewWebSocketClient(instances[0].uri, rpc.DefaultHandshakeTimeout, pubsub.MaxPendingMessages, pubsub.MaxReadMessageSize)
require.NoError(err)
err = wsCli.RegisterBlocks()
require.NoError(err)
found := false
for {
if found {
break
}

b, rs, _, _, err := wsCli.ListenBlock(context.Background(), parser)
require.NoError(err)
for _, t := range b.Txs {
if t.ID() == tx.ID() {
found = true
hutils.Outf("{{green}}inclusion block found{{/}}\n")
break
}
}

blk = b
results = rs
}

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
success, _, err := instances[0].tcli.WaitForTransaction(ctx, tx.ID())
cancel()
require.NoError(err)
require.True(success)
hutils.Outf("{{yellow}}found transaction{{/}}\n")
hutils.Outf(
"{{yellow}}chainID=%d fee=%d sender=%s{{/}}\n",
chainID,
maxFee,
sender,
)

// check if NMT Proof is correct
nID := tx.Actions[0].NMTNamespace()
require.Equal(chainIDBytes, nID)
root := blk.NMTRoot
require.Equal(hconsts.NMTRootLen, len(root))
proof, ok := blk.NMTProofs[hex.EncodeToString(nID)]
require.True(ok)
hutils.Outf("{{green}}proof:{{/}}%+v \n", proof)
hutils.Outf("{{green}}proofs:{{/}}%+v \n", blk.NMTProofs)
actionData := make([]byte, 0)
// should only contain results of the sequencer msg tx
require.Equal(1, len(results))
// prepare action data to be verified
txID := tx.ID()
txResult := results[0]
actionData = append(actionData, nID...)
actionData = append(actionData, txID[:]...)
// append `result` of Action 0 of Tx 0
for k := 0; k < len(txResult.Outputs[0]); k++ {
actionData = append(actionData, txResult.Outputs[0][k][:]...)
}

hutils.Outf("{{green}}actionData:{{/}}%+v \n", hex.EncodeToString(actionData))

// leaves without prefix namespace ID
leaves := make([][]byte, 0, 1)
leaves = append(leaves, actionData)

verified := proof.VerifyNamespace(sha256.New(), nID, leaves, root)
require.True(verified)
})
})

// TODO: add custom asset test
// TODO: test with only part of sig weight
// TODO: attempt to mint a warp asset
Expand Down

0 comments on commit cc4056a

Please sign in to comment.