forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
creator_test.go
151 lines (130 loc) · 4.06 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package relayer_test
import (
"math/rand"
"testing"
"time"
"github.com/omni-network/omni/halo/attest/voter"
"github.com/omni-network/omni/lib/cchain"
"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)
// Ensure msg.LogIndex is increasing
for i := 1; i < len(block.Msgs); i++ {
block.Msgs[i].LogIndex = block.Msgs[i-1].LogIndex + 1 + uint64(rand.Intn(1000))
}
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)
header, err := vote.BlockHeader.ToXChain()
require.NoError(t, err)
require.Equal(t, block.BlockHeader, header)
require.Equal(t, addr, common.Address(vote.Signature.ValidatorAddress))
tree, err := xchain.NewMsgTree(block.Msgs)
require.NoError(t, err)
blockHeader, err := vote.BlockHeader.ToXChain()
require.NoError(t, err)
sig, err := vote.Signature.ToXChain()
require.NoError(t, err)
att := xchain.Attestation{
AttestHeader: vote.AttestHeader.ToXChain(),
BlockHeader: blockHeader,
ValidatorSetID: valSetID,
MsgRoot: [32]byte(vote.MsgRoot),
Signatures: []xchain.SigTuple{sig},
}
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,
ValSet: []cchain.PortalValidator{{Address: addr, Power: 1}},
},
},
}
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
}