-
Notifications
You must be signed in to change notification settings - Fork 55
/
e2e_test.go
347 lines (294 loc) · 10.1 KB
/
e2e_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package e2e_test
import (
"context"
"encoding/json"
"os"
"path/filepath"
"sync"
"testing"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/e2e/app"
"github.com/omni-network/omni/e2e/docker"
"github.com/omni-network/omni/e2e/types"
"github.com/omni-network/omni/e2e/vmcompose"
"github.com/omni-network/omni/halo/genutil/evm/predeploys"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/evmchain"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/tutil"
"github.com/omni-network/omni/lib/xchain"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
rpctypes "github.com/cometbft/cometbft/rpc/core/types"
e2e "github.com/cometbft/cometbft/test/e2e/pkg"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
//nolint:gochecknoglobals // This was copied from cometbft/test/e2e/test/e2e_test.go
var (
endopintsCache = map[string]xchain.RPCEndpoints{}
networkCache = map[string]netconf.Network{}
deployInfoCache = map[string]types.DeployInfos{}
testnetCache = map[string]types.Testnet{}
testnetCacheMtx = sync.Mutex{}
blocksCache = map[string][]*cmttypes.Block{}
blocksCacheMtx = sync.Mutex{}
)
// Portal is a struct that contains the chain, client and contract for a portal.
type Portal struct {
Chain netconf.Chain
Client ethclient.Client
Contract *bindings.OmniPortal
}
type testFunc struct {
TestNode func(*testing.T, netconf.Network, *e2e.Node, []Portal)
TestPortal func(*testing.T, netconf.Network, Portal, []Portal)
TestOmniEVM func(*testing.T, ethclient.Client)
TestNetwork func(*testing.T, netconf.Network, xchain.RPCEndpoints)
skipFunc func(types.Manifest) bool
}
func testNode(t *testing.T, fn func(*testing.T, netconf.Network, *e2e.Node, []Portal)) {
t.Helper()
test(t, testFunc{TestNode: fn})
}
func testPortal(t *testing.T, fn func(*testing.T, netconf.Network, Portal, []Portal)) {
t.Helper()
test(t, testFunc{TestPortal: fn})
}
func testOmniEVM(t *testing.T, fn func(*testing.T, ethclient.Client)) {
t.Helper()
test(t, testFunc{TestOmniEVM: fn})
}
func testNetwork(t *testing.T, fn func(*testing.T, netconf.Network, xchain.RPCEndpoints)) {
t.Helper()
test(t, testFunc{TestNetwork: fn})
}
func maybeTestNetwork(
t *testing.T,
skipFunc func(types.Manifest) bool,
fn func(*testing.T, netconf.Network, xchain.RPCEndpoints),
) {
t.Helper()
test(t, testFunc{TestNetwork: fn, skipFunc: skipFunc})
}
// test runs tests for testnet nodes. The callback functions are respectively given a
// single node to test, and a single portal to test, running as a subtest in parallel with other subtests.
//
// The testnet manifest must be given as the envvar E2E_MANIFEST. If not set,
// these tests are skipped so that they're not picked up during normal unit
// test runs. If E2E_NODE is also set, only the specified node is tested,
// otherwise all nodes are tested.
func test(t *testing.T, testFunc testFunc) {
t.Helper()
testnet, network, _, endpoints := loadEnv(t)
nodes := testnet.Nodes
if testFunc.skipFunc != nil && testFunc.skipFunc(testnet.Manifest) {
t.Skip("Skipping test")
return
}
if name := os.Getenv(app.EnvE2ENode); name != "" {
node := testnet.LookupNode(name)
require.NotNil(t, node, "node %q not found in testnet %q", name, testnet.Name)
nodes = []*e2e.Node{node}
}
portals := makePortals(t, network, endpoints)
log.Info(context.Background(), "Running tests for testnet",
"testnet", testnet.Name,
"nodes", len(nodes),
"portals", len(portals),
)
for _, node := range nodes {
if node.Stateless() {
continue
} else if testFunc.TestNode == nil {
continue
}
t.Run(node.Name, func(t *testing.T) {
t.Parallel()
testFunc.TestNode(t, network, node, portals)
})
}
if testFunc.TestPortal != nil {
for _, portal := range portals {
t.Run(portal.Chain.Name, func(t *testing.T) {
t.Parallel()
testFunc.TestPortal(t, network, portal, portals)
})
}
}
if testFunc.TestOmniEVM != nil {
for _, chain := range network.Chains {
if !netconf.IsOmniExecution(network.ID, chain.ID) {
continue
}
rpc, err := endpoints.ByNameOrID(chain.Name, chain.ID)
require.NoError(t, err)
client, err := ethclient.Dial(chain.Name, rpc)
require.NoError(t, err)
t.Run(chain.Name, func(t *testing.T) {
t.Parallel()
testFunc.TestOmniEVM(t, client)
})
}
}
if testFunc.TestNetwork != nil {
t.Run("network", func(t *testing.T) {
t.Parallel()
testFunc.TestNetwork(t, network, endpoints)
})
}
}
// makePortals creates a portal struct for each chain in the network.
func makePortals(t *testing.T, network netconf.Network, endpoints xchain.RPCEndpoints) []Portal {
t.Helper()
resp := make([]Portal, 0, len(network.EVMChains()))
for _, chain := range network.EVMChains() {
if _, ok := evmchain.MetadataByID(chain.ID); !ok {
t.Log("Skipping mock chain", chain.ID)
continue
}
rpc, err := endpoints.ByNameOrID(chain.Name, chain.ID)
tutil.RequireNoError(t, err)
ethClient, err := ethclient.Dial(chain.Name, rpc)
require.NoError(t, err)
// create our Omni Portal Contract
contract, err := bindings.NewOmniPortal(chain.PortalAddress, ethClient)
require.NoError(t, err)
require.NotNil(t, contract, "contract is nil")
resp = append(resp, Portal{
Chain: chain,
Client: ethClient,
Contract: contract,
})
}
return resp
}
// loadEnv loads the testnet and network based on env vars.
//
//nolint:unparam // DeployInfos will be used in future.
func loadEnv(t *testing.T) (types.Testnet, netconf.Network, types.DeployInfos, xchain.RPCEndpoints) {
t.Helper()
manifestFile := os.Getenv(app.EnvE2EManifest)
if manifestFile == "" {
t.Skip(app.EnvE2EManifest + " not set, not an end-to-end test run")
}
if !filepath.IsAbs(manifestFile) {
require.Fail(t, app.EnvE2EManifest+" must be an absolute path", "got", manifestFile)
}
ifdType := os.Getenv(app.EnvInfraType)
ifdFile := os.Getenv(app.EnvInfraFile)
if ifdType != docker.ProviderName && ifdFile == "" {
require.Fail(t, app.EnvInfraFile+" not set while INFRASTRUCTURE_TYPE="+ifdType)
} else if ifdType != docker.ProviderName && !filepath.IsAbs(ifdFile) {
require.Fail(t, app.EnvInfraFile+" must be an absolute path", "got", ifdFile)
}
testnetCacheMtx.Lock()
defer testnetCacheMtx.Unlock()
if testnet, ok := testnetCache[manifestFile]; ok {
return testnet, networkCache[manifestFile], deployInfoCache[manifestFile], endopintsCache[manifestFile]
}
m, err := app.LoadManifest(manifestFile)
require.NoError(t, err)
var ifd types.InfrastructureData
switch ifdType {
case docker.ProviderName:
ifd, err = docker.NewInfraData(m)
case vmcompose.ProviderName:
ifd, err = vmcompose.LoadData(context.Background(), ifdFile)
default:
require.Fail(t, "unsupported infrastructure type", ifdType)
}
require.NoError(t, err)
cfg := app.DefinitionConfig{
ManifestFile: manifestFile,
}
testnet, err := app.TestnetFromManifest(context.Background(), m, ifd, cfg)
require.NoError(t, err)
testnetCache[manifestFile] = testnet
endpointsFile := os.Getenv(app.EnvE2ERPCEndpoints)
if endpointsFile == "" {
t.Fatalf(app.EnvE2ERPCEndpoints + " not set")
}
bz, err := os.ReadFile(endpointsFile)
require.NoError(t, err)
var endpoints xchain.RPCEndpoints
require.NoError(t, json.Unmarshal(bz, &endpoints))
endopintsCache[manifestFile] = endpoints
var deployInfo types.DeployInfos
deployInfoFile := os.Getenv(app.EnvE2EDeployInfo)
if deployInfoFile != "" {
deployInfo, err = types.LoadDeployInfos(deployInfoFile)
require.NoError(t, err)
deployInfoCache[manifestFile] = deployInfo
}
portalReg, err := makePortalRegistry(testnet.Network, endpoints)
require.NoError(t, err)
network, err := netconf.AwaitOnExecutionChain(context.Background(), testnet.Network, portalReg, endpoints.Keys())
require.NoError(t, err)
networkCache[manifestFile] = network
return testnet, network, deployInfo, endpoints
}
// fetchBlockChain fetches a complete, up-to-date block history from
// the freshest testnet archive node.
func fetchBlockChain(ctx context.Context, t *testing.T) []*cmttypes.Block {
t.Helper()
testnet, _, _, _ := loadEnv(t) //nolint:dogsled // Fine for testing.
// Find the freshest archive node
var (
client *rpchttp.HTTP
status *rpctypes.ResultStatus
)
for _, node := range testnet.ArchiveNodes() {
c, err := node.Client()
require.NoError(t, err)
s, err := c.Status(ctx)
require.NoError(t, err)
if status == nil || s.SyncInfo.LatestBlockHeight > status.SyncInfo.LatestBlockHeight {
client = c
status = s
}
}
require.NotNil(t, client, "couldn't find an archive node")
// Fetch blocks. Look for existing block history in the block cache, and
// extend it with any new blocks that have been produced.
blocksCacheMtx.Lock()
defer blocksCacheMtx.Unlock()
from := status.SyncInfo.EarliestBlockHeight
to := status.SyncInfo.LatestBlockHeight
blocks, ok := blocksCache[testnet.Name]
if !ok {
blocks = make([]*cmttypes.Block, 0, to-from+1)
}
if len(blocks) > 0 {
from = blocks[len(blocks)-1].Height + 1
}
for h := from; h <= to; h++ {
resp, err := client.Block(ctx, &(h))
require.NoError(t, ctx.Err(), "Timeout fetching all blocks: %d of %d", h, to)
require.NoError(t, err)
require.NotNil(t, resp.Block)
require.Equal(t, h, resp.Block.Height, "unexpected block height %v", resp.Block.Height)
blocks = append(blocks, resp.Block)
}
require.NotEmpty(t, blocks, "blockchain does not contain any blocks")
blocksCache[testnet.Name] = blocks
return blocks
}
func makePortalRegistry(network netconf.ID, endpoints xchain.RPCEndpoints) (*bindings.PortalRegistry, error) {
meta := netconf.MetadataByID(network, network.Static().OmniExecutionChainID)
rpc, err := endpoints.ByNameOrID(meta.Name, meta.ChainID)
if err != nil {
return nil, err
}
ethCl, err := ethclient.Dial(meta.Name, rpc)
if err != nil {
return nil, err
}
resp, err := bindings.NewPortalRegistry(common.HexToAddress(predeploys.PortalRegistry), ethCl)
if err != nil {
return nil, errors.Wrap(err, "create portal registry")
}
return resp, nil
}