forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
creator_test.go
137 lines (118 loc) · 3.64 KB
/
creator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package relayer_test
import (
"math/rand"
"testing"
"time"
"github.com/omni-network/omni/halo/attest/voter"
"github.com/omni-network/omni/lib/k1util"
"github.com/omni-network/omni/lib/xchain"
relayer "github.com/omni-network/omni/relayer/app"
"github.com/cometbft/cometbft/crypto"
k1 "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/ethereum/go-ethereum/common"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"
)
func TestCreatorService_CreateSubmissions(t *testing.T) {
t.Parallel()
var (
SourceChainID = uint64(randomBetween(1, 5))
DestChainID = uint64(randomBetween(1, 5))
)
privKey := k1.GenPrivKey()
addr, err := k1util.PubKeyToAddress(privKey.PubKey())
require.NoError(t, err)
fuzzer := fuzz.New().NilChance(0).Funcs(
func(e *xchain.Msg, c fuzz.Continue) {
e.DestChainID = DestChainID
e.SourceChainID = SourceChainID
e.DestAddress = common.Address(crypto.CRandBytes(20))
e.SourceMsgSender = common.Address(crypto.CRandBytes(20))
e.Data = crypto.CRandBytes(100)
},
)
var block xchain.Block
fuzzer.NilChance(0).NumElements(2, 64).Fuzz(&block)
require.NotEmpty(t, block.Msgs)
var attestHeader xchain.AttestHeader
fuzzer.Fuzz(&attestHeader)
attestHeader.ChainVersion.ID = block.ChainID // Align headers
var valSetID uint64
fuzzer.Fuzz(&valSetID)
// make all msg offset sequential
for i := range block.Msgs {
block.Msgs[i].StreamOffset = uint64(i)
}
vote, err := voter.CreateVote(privKey, attestHeader, block)
require.NoError(t, err)
require.Equal(t, block.BlockHeader, vote.BlockHeader.ToXChain())
require.Equal(t, addr, common.Address(vote.Signature.ValidatorAddress))
tree, err := xchain.NewMsgTree(block.Msgs)
require.NoError(t, err)
att := xchain.Attestation{
AttestHeader: vote.AttestHeader.ToXChain(),
BlockHeader: vote.BlockHeader.ToXChain(),
ValidatorSetID: valSetID,
MsgRoot: [32]byte(vote.MsgRoot),
Signatures: []xchain.SigTuple{vote.Signature.ToXChain()},
}
ensureNoDuplicates := func(t *testing.T, msgs []xchain.Msg) {
t.Helper()
msgSet := make(map[xchain.MsgID]struct{})
for _, msg := range msgs {
if _, exists := msgSet[msg.MsgID]; exists {
// Fail the test if a duplicate message is found
require.Fail(t, "Duplicate message found", msg)
}
msgSet[msg.MsgID] = struct{}{}
}
}
tests := []struct {
name string
streamUpdate relayer.StreamUpdate
}{
{
name: "ok",
streamUpdate: relayer.StreamUpdate{
StreamID: xchain.StreamID{
SourceChainID: SourceChainID,
DestChainID: DestChainID,
},
Attestation: att,
Msgs: block.Msgs,
MsgTree: tree,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
subs, err := relayer.CreateSubmissions(tt.streamUpdate)
require.NoError(t, err)
msgCount := 0
msgs := make([]xchain.Msg, 0, len(tt.streamUpdate.Msgs))
for _, sub := range subs {
require.EqualValues(t, valSetID, sub.ValidatorSetID)
require.NotNil(t, sub.AttestationRoot)
attRoot, err := att.AttestationRoot()
require.NoError(t, err)
require.Equal(t, sub.AttestationRoot.Bytes(), attRoot[:])
require.NotEmpty(t, sub.ProofFlags)
require.NotEmpty(t, sub.Signatures)
for _, msg := range sub.Msgs {
require.Equal(t, msg.DestChainID, sub.DestChainID)
}
msgCount += len(sub.Msgs)
msgs = append(msgs, sub.Msgs...)
}
// ensure no msgs were dropped
require.Len(t, msgs, len(tt.streamUpdate.Msgs))
// check for duplicates
ensureNoDuplicates(t, msgs)
})
}
}
func randomBetween(min, max int) int {
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Intn(max-min+1) + min
}