|
1 | 1 | import http from "http";
|
2 | 2 | import type { AddressInfo } from "net";
|
3 |
| -import test, { Test } from "tape-promise/tape"; |
4 | 3 | import { v4 as uuidv4 } from "uuid";
|
| 4 | +import "jest-extended"; |
| 5 | + |
5 | 6 | import express from "express";
|
6 | 7 | import bodyParser from "body-parser";
|
7 | 8 | import Web3 from "web3";
|
@@ -40,194 +41,179 @@ const connectorId = uuidv4();
|
40 | 41 | const logLevel: LogLevelDesc = "INFO";
|
41 | 42 |
|
42 | 43 | const testCase = "Test get status";
|
| 44 | +describe(testCase, () => { |
| 45 | + const besuTestLedger = new BesuTestLedger({ logLevel }); |
43 | 46 |
|
44 |
| -test("BEFORE " + testCase, async (t: Test) => { |
45 |
| - const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
46 |
| - await t.doesNotReject(pruning, "Pruning did not throw OK"); |
47 |
| - t.end(); |
48 |
| -}); |
| 47 | + const expressApp = express(); |
| 48 | + expressApp.use(bodyParser.json({ limit: "250mb" })); |
| 49 | + const server = http.createServer(expressApp); |
| 50 | + const listenOptions: IListenOptions = { |
| 51 | + hostname: "0.0.0.0", |
| 52 | + port: 0, |
| 53 | + server, |
| 54 | + }; |
49 | 55 |
|
50 |
| -test(testCase, async (t: Test) => { |
51 |
| - t.comment("Starting Besu Test Ledger"); |
52 |
| - const besuTestLedger = new BesuTestLedger({ logLevel }); |
| 56 | + beforeAll(async () => { |
| 57 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 58 | + await expect(pruning).resolves.toBeTruthy(); |
| 59 | + }); |
53 | 60 |
|
54 |
| - test.onFinish(async () => { |
| 61 | + afterAll(async () => { |
55 | 62 | await besuTestLedger.stop();
|
56 | 63 | await besuTestLedger.destroy();
|
57 | 64 | await pruneDockerAllIfGithubAction({ logLevel });
|
58 | 65 | });
|
59 | 66 |
|
60 |
| - await besuTestLedger.start(); |
61 |
| - |
62 |
| - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
63 |
| - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
64 |
| - const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey(); |
65 |
| - const privateKey = besuTestLedger.getGenesisAccountPrivKey(); |
66 |
| - const web3SigningCredential: Web3SigningCredential = { |
67 |
| - ethAccount: firstHighNetWorthAccount, |
68 |
| - secret: privateKey, |
69 |
| - type: Web3SigningCredentialType.PrivateKeyHex, |
70 |
| - } as Web3SigningCredential; |
71 |
| - |
72 |
| - const keychainId = uuidv4(); |
73 |
| - const keychainPlugin = new PluginKeychainMemory({ |
74 |
| - instanceId: uuidv4(), |
75 |
| - keychainId, |
76 |
| - // pre-provision keychain with mock backend holding the private key of the |
77 |
| - // test account that we'll reference while sending requests with the |
78 |
| - // signing credential pointing to this keychain entry. |
79 |
| - backend: new Map([ |
80 |
| - [DemoHelperJSON.contractName, JSON.stringify(DemoHelperJSON)], |
81 |
| - ]), |
82 |
| - logLevel, |
83 |
| - }); |
84 |
| - keychainPlugin.set( |
85 |
| - HashTimeLockJSON.contractName, |
86 |
| - JSON.stringify(HashTimeLockJSON), |
87 |
| - ); |
| 67 | + afterAll(async () => await Servers.shutdown(server)); |
88 | 68 |
|
89 |
| - const factory = new PluginFactoryLedgerConnector({ |
90 |
| - pluginImportType: PluginImportType.Local, |
| 69 | + beforeAll(async () => { |
| 70 | + await besuTestLedger.start(); |
91 | 71 | });
|
92 | 72 |
|
93 |
| - const pluginRegistry = new PluginRegistry({}); |
94 |
| - const connector: PluginLedgerConnectorBesu = await factory.create({ |
95 |
| - rpcApiHttpHost, |
96 |
| - rpcApiWsHost, |
97 |
| - logLevel, |
98 |
| - instanceId: connectorId, |
99 |
| - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), |
| 73 | + afterAll(async () => { |
| 74 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 75 | + await expect(pruning).resolves.toBeTruthy(); |
100 | 76 | });
|
101 | 77 |
|
102 |
| - pluginRegistry.add(connector); |
103 |
| - const pluginOptions: IPluginHtlcEthBesuOptions = { |
104 |
| - logLevel, |
105 |
| - instanceId: uuidv4(), |
106 |
| - pluginRegistry, |
107 |
| - }; |
108 |
| - |
109 |
| - const factoryHTLC = new PluginFactoryHtlcEthBesu({ |
110 |
| - pluginImportType: PluginImportType.Local, |
111 |
| - }); |
112 |
| - |
113 |
| - const pluginHtlc = await factoryHTLC.create(pluginOptions); |
114 |
| - pluginRegistry.add(pluginHtlc); |
115 |
| - |
116 |
| - const expressApp = express(); |
117 |
| - expressApp.use(bodyParser.json({ limit: "250mb" })); |
118 |
| - const server = http.createServer(expressApp); |
119 |
| - const listenOptions: IListenOptions = { |
120 |
| - hostname: "0.0.0.0", |
121 |
| - port: 0, |
122 |
| - server, |
123 |
| - }; |
124 |
| - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; |
125 |
| - test.onFinish(async () => await Servers.shutdown(server)); |
126 |
| - const { address, port } = addressInfo; |
127 |
| - const apiHost = `http://${address}:${port}`; |
128 |
| - |
129 |
| - const configuration = new Configuration({ basePath: apiHost }); |
130 |
| - const api = new BesuApi(configuration); |
131 |
| - |
132 |
| - await pluginHtlc.getOrCreateWebServices(); |
133 |
| - await pluginHtlc.registerWebServices(expressApp); |
134 |
| - |
135 |
| - const web3 = new Web3(rpcApiHttpHost); |
136 |
| - |
137 |
| - t.comment("Deploys HashTimeLock via .json file on initialize function"); |
138 |
| - const initRequest: InitializeRequest = { |
139 |
| - connectorId, |
140 |
| - keychainId, |
141 |
| - constructorArgs: [], |
142 |
| - web3SigningCredential, |
143 |
| - gas: DataTest.estimated_gas, |
144 |
| - }; |
145 |
| - const deployOut = await pluginHtlc.initialize(initRequest); |
146 |
| - t.ok( |
147 |
| - deployOut.transactionReceipt, |
148 |
| - "pluginHtlc.initialize() output.transactionReceipt is truthy OK", |
149 |
| - ); |
150 |
| - t.ok( |
151 |
| - deployOut.transactionReceipt.contractAddress, |
152 |
| - "pluginHtlc.initialize() output.transactionReceipt.contractAddress is truthy OK", |
153 |
| - ); |
154 |
| - const hashTimeLockAddress = deployOut.transactionReceipt |
155 |
| - .contractAddress as string; |
156 |
| - |
157 |
| - //Deploy DemoHelpers |
158 |
| - t.comment("Deploys DemoHelpers via .json file on deployContract function"); |
159 |
| - const deployOutDemo = await connector.deployContract({ |
160 |
| - contractName: DemoHelperJSON.contractName, |
161 |
| - contractAbi: DemoHelperJSON.abi, |
162 |
| - bytecode: DemoHelperJSON.bytecode, |
163 |
| - web3SigningCredential, |
164 |
| - keychainId, |
165 |
| - constructorArgs: [], |
166 |
| - gas: DataTest.estimated_gas, |
167 |
| - }); |
168 |
| - t.ok(deployOutDemo, "deployContract() output is truthy OK"); |
169 |
| - t.ok( |
170 |
| - deployOutDemo.transactionReceipt, |
171 |
| - "deployContract() output.transactionReceipt is truthy OK", |
172 |
| - ); |
173 |
| - t.ok( |
174 |
| - deployOutDemo.transactionReceipt.contractAddress, |
175 |
| - "deployContract() output.transactionReceipt.contractAddress is truthy OK", |
176 |
| - ); |
177 |
| - |
178 |
| - t.comment("Get account balance"); |
179 |
| - const balance = await web3.eth.getBalance(firstHighNetWorthAccount); |
180 |
| - |
181 |
| - t.comment("Create new contract for HTLC"); |
182 |
| - const bodyObj: NewContractObj = { |
183 |
| - contractAddress: hashTimeLockAddress, |
184 |
| - inputAmount: 10, |
185 |
| - outputAmount: 0x04, |
186 |
| - expiration: DataTest.expiration, |
187 |
| - hashLock: DataTest.hashLock, |
188 |
| - receiver: DataTest.receiver, |
189 |
| - outputNetwork: "BTC", |
190 |
| - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", |
191 |
| - connectorId: connectorId, |
192 |
| - web3SigningCredential, |
193 |
| - keychainId, |
194 |
| - gas: DataTest.estimated_gas, |
195 |
| - }; |
196 |
| - const resp = await api.newContractV1(bodyObj); |
197 |
| - t.ok(resp, "response newContract is OK"); |
198 |
| - t.equal(resp.status, 200, "response status newContract is OK"); |
199 |
| - |
200 |
| - t.comment("Get status of HTLC"); |
201 |
| - const responseTxId = await connector.invokeContract({ |
202 |
| - contractName: DemoHelperJSON.contractName, |
203 |
| - keychainId, |
204 |
| - signingCredential: web3SigningCredential, |
205 |
| - invocationType: EthContractInvocationType.Call, |
206 |
| - methodName: "getTxId", |
207 |
| - params: [ |
208 |
| - firstHighNetWorthAccount, |
209 |
| - DataTest.receiver, |
210 |
| - 10, |
211 |
| - DataTest.hashLock, |
212 |
| - DataTest.expiration, |
213 |
| - ], |
214 |
| - }); |
215 |
| - const balance2 = await web3.eth.getBalance(firstHighNetWorthAccount); |
216 |
| - |
217 |
| - t.equal( |
218 |
| - parseInt(balance), |
219 |
| - parseInt(balance2) - 10, |
220 |
| - "Balance of account is OK", |
221 |
| - ); |
222 |
| - |
223 |
| - const ids = [responseTxId.callOutput as string]; |
224 |
| - const res = await api.getStatusV1({ |
225 |
| - ids, |
226 |
| - web3SigningCredential, |
227 |
| - connectorId, |
228 |
| - keychainId, |
| 78 | + test(testCase, async () => { |
| 79 | + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
| 80 | + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
| 81 | + const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey(); |
| 82 | + const privateKey = besuTestLedger.getGenesisAccountPrivKey(); |
| 83 | + const web3SigningCredential: Web3SigningCredential = { |
| 84 | + ethAccount: firstHighNetWorthAccount, |
| 85 | + secret: privateKey, |
| 86 | + type: Web3SigningCredentialType.PrivateKeyHex, |
| 87 | + } as Web3SigningCredential; |
| 88 | + |
| 89 | + const keychainId = uuidv4(); |
| 90 | + const keychainPlugin = new PluginKeychainMemory({ |
| 91 | + instanceId: uuidv4(), |
| 92 | + keychainId, |
| 93 | + // pre-provision keychain with mock backend holding the private key of the |
| 94 | + // test account that we'll reference while sending requests with the |
| 95 | + // signing credential pointing to this keychain entry. |
| 96 | + backend: new Map([ |
| 97 | + [DemoHelperJSON.contractName, JSON.stringify(DemoHelperJSON)], |
| 98 | + ]), |
| 99 | + logLevel, |
| 100 | + }); |
| 101 | + keychainPlugin.set( |
| 102 | + HashTimeLockJSON.contractName, |
| 103 | + JSON.stringify(HashTimeLockJSON), |
| 104 | + ); |
| 105 | + |
| 106 | + const factory = new PluginFactoryLedgerConnector({ |
| 107 | + pluginImportType: PluginImportType.Local, |
| 108 | + }); |
| 109 | + |
| 110 | + const pluginRegistry = new PluginRegistry({}); |
| 111 | + const connector: PluginLedgerConnectorBesu = await factory.create({ |
| 112 | + rpcApiHttpHost, |
| 113 | + rpcApiWsHost, |
| 114 | + logLevel, |
| 115 | + instanceId: connectorId, |
| 116 | + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), |
| 117 | + }); |
| 118 | + |
| 119 | + pluginRegistry.add(connector); |
| 120 | + const pluginOptions: IPluginHtlcEthBesuOptions = { |
| 121 | + logLevel, |
| 122 | + instanceId: uuidv4(), |
| 123 | + pluginRegistry, |
| 124 | + }; |
| 125 | + |
| 126 | + const factoryHTLC = new PluginFactoryHtlcEthBesu({ |
| 127 | + pluginImportType: PluginImportType.Local, |
| 128 | + }); |
| 129 | + |
| 130 | + const pluginHtlc = await factoryHTLC.create(pluginOptions); |
| 131 | + pluginRegistry.add(pluginHtlc); |
| 132 | + |
| 133 | + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; |
| 134 | + const { address, port } = addressInfo; |
| 135 | + const apiHost = `http://${address}:${port}`; |
| 136 | + |
| 137 | + const configuration = new Configuration({ basePath: apiHost }); |
| 138 | + const api = new BesuApi(configuration); |
| 139 | + |
| 140 | + await pluginHtlc.getOrCreateWebServices(); |
| 141 | + await pluginHtlc.registerWebServices(expressApp); |
| 142 | + |
| 143 | + const web3 = new Web3(rpcApiHttpHost); |
| 144 | + |
| 145 | + const initRequest: InitializeRequest = { |
| 146 | + connectorId, |
| 147 | + keychainId, |
| 148 | + constructorArgs: [], |
| 149 | + web3SigningCredential, |
| 150 | + gas: DataTest.estimated_gas, |
| 151 | + }; |
| 152 | + const deployOut = await pluginHtlc.initialize(initRequest); |
| 153 | + expect(deployOut.transactionReceipt).toBeTruthy(); |
| 154 | + expect(deployOut.transactionReceipt.contractAddress).toBeTruthy(); |
| 155 | + const hashTimeLockAddress = deployOut.transactionReceipt |
| 156 | + .contractAddress as string; |
| 157 | + |
| 158 | + //Deploy DemoHelpers |
| 159 | + const deployOutDemo = await connector.deployContract({ |
| 160 | + contractName: DemoHelperJSON.contractName, |
| 161 | + contractAbi: DemoHelperJSON.abi, |
| 162 | + bytecode: DemoHelperJSON.bytecode, |
| 163 | + web3SigningCredential, |
| 164 | + keychainId, |
| 165 | + constructorArgs: [], |
| 166 | + gas: DataTest.estimated_gas, |
| 167 | + }); |
| 168 | + expect(deployOutDemo).toBeTruthy(); |
| 169 | + expect(deployOutDemo.transactionReceipt).toBeTruthy(); |
| 170 | + expect(deployOutDemo.transactionReceipt.contractAddress).toBeTruthy(); |
| 171 | + const balance = await web3.eth.getBalance(firstHighNetWorthAccount); |
| 172 | + |
| 173 | + const bodyObj: NewContractObj = { |
| 174 | + contractAddress: hashTimeLockAddress, |
| 175 | + inputAmount: 10, |
| 176 | + outputAmount: 0x04, |
| 177 | + expiration: DataTest.expiration, |
| 178 | + hashLock: DataTest.hashLock, |
| 179 | + receiver: DataTest.receiver, |
| 180 | + outputNetwork: "BTC", |
| 181 | + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", |
| 182 | + connectorId: connectorId, |
| 183 | + web3SigningCredential, |
| 184 | + keychainId, |
| 185 | + gas: DataTest.estimated_gas, |
| 186 | + }; |
| 187 | + const resp = await api.newContractV1(bodyObj); |
| 188 | + expect(resp).toBeTruthy(); |
| 189 | + expect(resp.status).toEqual(200); |
| 190 | + |
| 191 | + const responseTxId = await connector.invokeContract({ |
| 192 | + contractName: DemoHelperJSON.contractName, |
| 193 | + keychainId, |
| 194 | + signingCredential: web3SigningCredential, |
| 195 | + invocationType: EthContractInvocationType.Call, |
| 196 | + methodName: "getTxId", |
| 197 | + params: [ |
| 198 | + firstHighNetWorthAccount, |
| 199 | + DataTest.receiver, |
| 200 | + 10, |
| 201 | + DataTest.hashLock, |
| 202 | + DataTest.expiration, |
| 203 | + ], |
| 204 | + }); |
| 205 | + const balance2 = await web3.eth.getBalance(firstHighNetWorthAccount); |
| 206 | + |
| 207 | + expect(parseInt(balance)).toEqual(parseInt(balance2) - 10); |
| 208 | + |
| 209 | + const ids = [responseTxId.callOutput as string]; |
| 210 | + const res = await api.getStatusV1({ |
| 211 | + ids, |
| 212 | + web3SigningCredential, |
| 213 | + connectorId, |
| 214 | + keychainId, |
| 215 | + }); |
| 216 | + expect(res.status).toEqual(200); |
| 217 | + expect(res.data[0]).toEqual("1"); |
229 | 218 | });
|
230 |
| - t.equal(res.status, 200, "response status is 200 OK"); |
231 |
| - t.equal(res.data[0], "1", "the contract status is 1 - Active"); |
232 |
| - t.end(); |
233 | 219 | });
|
0 commit comments