-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsetup.ts
132 lines (113 loc) · 4.16 KB
/
setup.ts
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
import { expect } from 'bun:test';
import { setupWithServer } from '@acala-network/chopsticks';
import {
type Blockchain,
BuildBlockMode,
connectParachains,
connectVertical,
} from '@acala-network/chopsticks-core';
import { POLIMEC_WASM, bridge_storage, polkadot_hub_storage, polkadot_storage } from '../overrides';
type SetupResult = Awaited<ReturnType<typeof setupWithServer>>;
export class ChainSetup {
private relaychain?: Blockchain;
private polimec?: Blockchain;
private assetHub?: Blockchain;
private bridgeHub?: Blockchain;
// Store setup objects for cleanup
private polimecSetup?: SetupResult;
private assetHubSetup?: SetupResult;
private relaychainSetup?: SetupResult;
private bridgeHubSetup?: SetupResult;
async initialize(polimec_storage?: unknown) {
const [polimecSetup, assetHubSetup, relaychainSetup, bridgeHubSetup] = await Promise.all([
this.setupPolimec(polimec_storage),
this.setupAssetHub(),
this.setupRelaychain(),
this.setupBridgeHub(),
]);
console.log('✅ Local nodes instances are up');
// Store setup objects
this.polimecSetup = polimecSetup;
this.assetHubSetup = assetHubSetup;
this.relaychainSetup = relaychainSetup;
this.bridgeHubSetup = bridgeHubSetup;
// Store chain references
this.polimec = polimecSetup.chain;
this.assetHub = assetHubSetup.chain;
this.relaychain = relaychainSetup.chain;
this.bridgeHub = bridgeHubSetup.chain;
const parachains = [this.polimec, this.assetHub, this.bridgeHub];
for (const parachain of parachains) {
await connectVertical(this.relaychain, parachain);
}
await connectParachains(parachains);
console.log('✅ HRMP channels created');
// Needed to execute storage migrations within the new WASM before running tests.
const head = this.polimec.head;
console.log(`✅ Polimec chain is at block ${head.number}`);
console.log('✅ Producing a new block...');
const new_block = await this.polimec?.newBlock();
console.log(`✅ Polimec chain is at block ${new_block.number}`);
expect(new_block.number === head.number + 1, 'Block number should be incremented by 1');
}
async cleanup() {
await Promise.all([
this.relaychain?.close(),
this.polimec?.close(),
this.assetHub?.close(),
this.bridgeHub?.close(),
]);
await Promise.all([
this.relaychainSetup?.close(),
this.polimecSetup?.close(),
this.assetHubSetup?.close(),
this.bridgeHubSetup?.close(),
]);
console.log('✅ Local nodes instances are down');
}
private async setupPolimec(polimec_storage: unknown) {
const file = Bun.file(POLIMEC_WASM);
// Note: the tests are intended to use a pre-production, locally compiled runtime, that's why we throw an error.
if (!(await file.exists())) {
throw new Error(
'Polimec runtime not found! Please build it by running `cargo b -r -p polimec-runtime` before executing the tests.',
);
}
const hasher = new Bun.CryptoHasher('blake2b256');
hasher.update(await file.bytes());
const runtimeHash = hasher.digest('hex');
console.log(`✅ Polimec runtime used in tests: 0x${runtimeHash}`);
if (polimec_storage !== undefined) console.info('✅ Polimec custom storage provided');
return setupWithServer({
endpoint: 'wss://polimec.ibp.network',
port: 8000,
'wasm-override': POLIMEC_WASM,
'import-storage': polimec_storage,
'build-block-mode': BuildBlockMode.Instant,
});
}
private setupAssetHub() {
return setupWithServer({
endpoint: 'wss://sys.ibp.network/statemint',
port: 8001,
'import-storage': polkadot_hub_storage,
'build-block-mode': BuildBlockMode.Instant,
});
}
private setupRelaychain() {
return setupWithServer({
endpoint: 'wss://rpc.ibp.network/polkadot',
port: 8002,
'import-storage': polkadot_storage,
'build-block-mode': BuildBlockMode.Instant,
});
}
private setupBridgeHub() {
return setupWithServer({
endpoint: 'wss://sys.ibp.network/bridgehub-polkadot',
port: 8003,
'import-storage': bridge_storage,
'build-block-mode': BuildBlockMode.Instant,
});
}
}