forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcursors_internal_test.go
149 lines (125 loc) · 4.34 KB
/
cursors_internal_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
package relayer
import (
"context"
"testing"
"github.com/omni-network/omni/lib/cchain"
"github.com/omni-network/omni/lib/xchain"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
func Test_fromOffsets(t *testing.T) {
t.Parallel()
const dstChain = uint64(4)
const srcChain1 = uint64(1)
const srcChain2 = uint64(2)
chainVer1 := xchain.ChainVersion{ID: srcChain1, ConfLevel: xchain.ConfFinalized}
chainVer2 := xchain.ChainVersion{ID: srcChain2, ConfLevel: xchain.ConfFinalized}
chainVer3 := xchain.ChainVersion{ID: srcChain2, ConfLevel: xchain.ConfLatest}
allChainVers := []xchain.ChainVersion{chainVer1, chainVer2, chainVer3}
streamID := func(chainVer xchain.ChainVersion) xchain.StreamID {
return xchain.StreamID{
SourceChainID: chainVer.ID,
DestChainID: dstChain,
ShardID: xchain.ShardID(chainVer.ConfLevel),
}
}
stream1 := streamID(chainVer1)
stream2 := streamID(chainVer2)
stream3 := streamID(chainVer3)
makeCursors := func(offset1, offset2, offset3 uint64) []xchain.SubmitCursor {
var resp []xchain.SubmitCursor
if offset1 != 0 {
resp = append(resp, xchain.SubmitCursor{StreamID: stream1, AttestOffset: offset1})
}
if offset2 != 0 {
resp = append(resp, xchain.SubmitCursor{StreamID: stream2, AttestOffset: offset2})
}
if offset3 != 0 {
resp = append(resp, xchain.SubmitCursor{StreamID: stream3, AttestOffset: offset3})
}
return resp
}
makeResult := func(offset1, offset2, offset3 uint64) map[xchain.ChainVersion]uint64 {
return map[xchain.ChainVersion]uint64{
chainVer1: offset1,
chainVer2: offset2,
chainVer3: offset3,
}
}
tests := []struct {
name string
onChain []xchain.SubmitCursor
want map[xchain.ChainVersion]uint64
}{
{
name: "on-chain empty",
onChain: makeCursors(0, 0, 0),
want: makeResult(1, 1, 1), // All streams initialize at 1
},
{
name: "on-chain only",
onChain: makeCursors(5, 6, 7),
want: makeResult(5, 6, 7),
},
{
name: "mix",
onChain: makeCursors(1, 6, 0),
want: makeResult(1, 6, 1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := fromChainVersionOffsets(tt.onChain, allChainVers)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
var (
_ cchain.Provider = (*mockProvider)(nil)
_ xchain.Provider = (*mockXChainClient)(nil)
)
type mockXChainClient struct {
GetBlockFn func(context.Context, xchain.ProviderRequest) (xchain.Block, bool, error)
GetSubmittedCursorFn func(context.Context, xchain.StreamID) (xchain.SubmitCursor, bool, error)
GetEmittedCursorFn func(context.Context, xchain.EmitRef, xchain.StreamID) (xchain.EmitCursor, bool, error)
}
func (*mockXChainClient) GetSubmission(context.Context, uint64, common.Hash) (xchain.Submission, error) {
panic("unexpected")
}
func (m *mockXChainClient) StreamAsync(context.Context, xchain.ProviderRequest, xchain.ProviderCallback) error {
panic("unexpected")
}
func (m *mockXChainClient) StreamBlocks(context.Context, xchain.ProviderRequest, xchain.ProviderCallback) error {
panic("unexpected")
}
func (*mockXChainClient) ChainVersionHeight(context.Context, xchain.ChainVersion) (uint64, error) {
panic("unexpected")
}
func (m *mockXChainClient) GetBlock(ctx context.Context, req xchain.ProviderRequest) (xchain.Block, bool, error) {
return m.GetBlockFn(ctx, req)
}
func (m *mockXChainClient) GetSubmittedCursor(ctx context.Context, stream xchain.StreamID,
) (xchain.SubmitCursor, bool, error) {
return m.GetSubmittedCursorFn(ctx, stream)
}
func (m *mockXChainClient) GetEmittedCursor(ctx context.Context, ref xchain.EmitRef, stream xchain.StreamID,
) (xchain.EmitCursor, bool, error) {
return m.GetEmittedCursorFn(ctx, ref, stream)
}
type mockSender struct {
SendTransactionFn func(ctx context.Context, submission xchain.Submission) error
}
func (m *mockSender) SendTransaction(ctx context.Context, submission xchain.Submission) error {
return m.SendTransactionFn(ctx, submission)
}
type mockProvider struct {
cchain.Provider
SubscribeFn func(ctx context.Context, chainVer xchain.ChainVersion, attestOffset uint64, callback cchain.ProviderCallback)
}
func (m *mockProvider) StreamAsync(ctx context.Context, chainVer xchain.ChainVersion, attestOffset uint64,
_ string, callback cchain.ProviderCallback,
) {
m.SubscribeFn(ctx, chainVer, attestOffset, callback)
}