Skip to content

Commit f503e0a

Browse files
committed
Updating tests
1 parent 75b8adb commit f503e0a

29 files changed

+1049
-120
lines changed

src/loom-provider-2.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import debug from 'debug'
2+
import retry from 'retry'
23
import { Wallet } from 'ethers'
34
import { Client as WSClient } from 'rpc-websockets'
45
import { EthRPCMethod, IEthRPCPayload } from './loom-provider'
@@ -19,6 +20,19 @@ export class LoomProvider2 {
1920
protected notificationCallbacks: Array<Function> = new Array()
2021
readonly wallets: Map<string, Wallet | null> = new Map<string, Wallet>()
2122

23+
/**
24+
* The retry strategy that should be used to retry some web3 requests.
25+
* By default failed requested won't be resent.
26+
* To understand how to tweak the retry strategy see
27+
* https://github.com/tim-kos/node-retry#retrytimeoutsoptions
28+
*/
29+
retryStrategy: retry.OperationOptions = {
30+
retries: 0,
31+
minTimeout: 1000, // 1s
32+
maxTimeout: 30000, // 30s
33+
randomize: true
34+
}
35+
2236
/**
2337
* Constructs the LoomProvider2 to bridges communication between Web3 and Loom DappChains
2438
*
@@ -94,21 +108,29 @@ export class LoomProvider2 {
94108
return
95109
}
96110

97-
let result
98-
99-
try {
100-
if (this._ethRPCMethods.has(payload.method)) {
101-
const f: Function = this._ethRPCMethods.get(payload.method)!
102-
result = await f(payload)
103-
} else {
104-
result = await this._wsRPC.call(payload.method, payload.params)
111+
const op = retry.operation(this.retryStrategy)
112+
op.attempt(async currAttempt => {
113+
debugLog(`Current attempt ${currAttempt}`)
114+
115+
let result
116+
117+
try {
118+
if (this._ethRPCMethods.has(payload.method)) {
119+
const f: Function = this._ethRPCMethods.get(payload.method)!
120+
result = await f(payload)
121+
} else {
122+
result = await this._wsRPC.call(payload.method, payload.params)
123+
}
124+
125+
callback(null, this._okResponse(payload.id, result, isArray))
126+
} catch (err) {
127+
if (!op.retry(err)) {
128+
callback(err, null)
129+
} else {
130+
errorLog(err)
131+
}
105132
}
106-
107-
callback(null, this._okResponse(payload.id, result, isArray))
108-
} catch (err) {
109-
errorLog(err)
110-
callback(err, null)
111-
}
133+
})
112134
}
113135

114136
// EVENT HANDLING METHODS

src/tests/e2e/address-mapper-tests.ts renamed to src/tests/e2e/contracts/address-mapper-tests.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
CryptoUtils,
88
createDefaultTxMiddleware,
99
Contracts
10-
} from '../../index'
11-
import { createTestHttpClient } from '../helpers'
12-
import { EthersSigner, getJsonRPCSignerAsync } from '../../solidity-helpers'
13-
import { ethers, Signer } from 'ethers'
10+
} from '../../../index'
11+
import { createTestHttpClient } from '../../helpers'
12+
import { EthersSigner, getJsonRPCSignerAsync } from '../../../solidity-helpers'
1413

1514
async function getClientAndContract(
1615
createClient: () => Client

src/tests/e2e/coin-tests.ts renamed to src/tests/e2e/contracts/coin-tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
createDefaultTxMiddleware,
88
Client,
99
LocalAddress
10-
} from '../../index'
11-
import { createTestHttpClient } from '../helpers'
12-
import { B64ToUint8Array } from '../../crypto-utils'
10+
} from '../../../index'
11+
import { createTestHttpClient } from '../../helpers'
12+
import { B64ToUint8Array } from '../../../crypto-utils'
1313

1414
const toCoinE18 = (amount: number): BN => {
1515
return new BN(10).pow(new BN(18)).mul(new BN(amount))

src/tests/e2e/contract-tests.ts renamed to src/tests/e2e/contracts/contract-tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import {
88
IChainEventArgs,
99
CryptoUtils,
1010
createDefaultTxMiddleware
11-
} from '../../index'
12-
import { MapEntry } from '../tests_pb'
11+
} from '../../../index'
12+
import { MapEntry } from '../../tests_pb'
1313
import {
1414
createTestClient,
1515
createTestHttpClient,
1616
createTestWSClient,
1717
createTestHttpWSClient
18-
} from '../helpers'
18+
} from '../../helpers'
1919

2020
async function getClientAndContract(
2121
createClient: () => Client

src/tests/e2e/dpos-tests.ts renamed to src/tests/e2e/contracts/dpos-tests.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import test from 'tape'
2-
import BN from 'bn.js'
32
import {
43
Address,
54
Contracts,
65
CryptoUtils,
76
createDefaultTxMiddleware,
87
Client,
98
LocalAddress
10-
} from '../../index'
11-
import { createTestHttpClient, waitForMillisecondsAsync } from '../helpers'
12-
import { B64ToUint8Array } from '../../crypto-utils'
9+
} from '../../../index'
10+
import { createTestHttpClient, waitForMillisecondsAsync } from '../../helpers'
11+
import { B64ToUint8Array } from '../../../crypto-utils'
1312

1413
async function getClientAndContract(
1514
createClient: () => Client

src/tests/e2e/client-evm-event-tests-2.ts renamed to src/tests/e2e/evm/client-evm-event-tests-2.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import test from 'tape'
22

3-
import { CryptoUtils, Client } from '../../index'
4-
import { createTestClient, waitForMillisecondsAsync } from '../helpers'
5-
import { CallTx, VMType, MessageTx, Transaction } from '../../proto/loom_pb'
6-
import { LoomProvider } from '../../loom-provider'
7-
import { deployContract } from '../evm-helpers'
8-
import { bufferToProtobufBytes } from '../../crypto-utils'
9-
import { Address, LocalAddress } from '../../address'
10-
import { createDefaultTxMiddleware } from '../../helpers'
3+
import { CryptoUtils, Client } from '../../../index'
4+
import { createTestClient, waitForMillisecondsAsync } from '../../helpers'
5+
import { CallTx, VMType, MessageTx, Transaction } from '../../../proto/loom_pb'
6+
import { LoomProvider } from '../../../loom-provider'
7+
import { deployContract } from '../../evm-helpers'
8+
import { bufferToProtobufBytes } from '../../../crypto-utils'
9+
import { Address, LocalAddress } from '../../../address'
10+
import { createDefaultTxMiddleware } from '../../../helpers'
1111

1212
/**
1313
* Requires the SimpleStore solidity contract deployed on a loomchain.

src/tests/e2e/client-evm-event-tests.ts renamed to src/tests/e2e/evm/client-evm-event-tests.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import test from 'tape'
22

3-
import { NonceTxMiddleware, SignedTxMiddleware, CryptoUtils } from '../../index'
4-
import { createTestClient } from '../helpers'
5-
import { CallTx, VMType, MessageTx, Transaction } from '../../proto/loom_pb'
6-
import { LoomProvider } from '../../loom-provider'
7-
import { deployContract } from '../evm-helpers'
8-
import { bufferToProtobufBytes } from '../../crypto-utils'
9-
import { Address, LocalAddress } from '../../address'
3+
import { NonceTxMiddleware, SignedTxMiddleware, CryptoUtils } from '../../../index'
4+
import { createTestClient } from '../../helpers'
5+
import { CallTx, VMType, MessageTx, Transaction } from '../../../proto/loom_pb'
6+
import { LoomProvider } from '../../../loom-provider'
7+
import { deployContract } from '../../evm-helpers'
8+
import { bufferToProtobufBytes } from '../../../crypto-utils'
9+
import { Address, LocalAddress } from '../../../address'
1010

1111
/**
1212
* Requires the SimpleStore solidity contract deployed on a loomchain.

src/tests/e2e/client-evm-tests.ts renamed to src/tests/e2e/evm/client-evm-tests.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import test from 'tape'
22

3-
import { CryptoUtils } from '../../index'
4-
import { createTestClient, execAndWaitForMillisecondsAsync } from '../helpers'
5-
import { EthBlockHashList, EthBlockInfo } from '../../proto/evm_pb'
6-
import { bytesToHexAddr } from '../../crypto-utils'
7-
import { createDefaultTxMiddleware } from '../../helpers'
3+
import { CryptoUtils } from '../../../index'
4+
import { createTestClient, execAndWaitForMillisecondsAsync } from '../../helpers'
5+
import { EthBlockHashList, EthBlockInfo } from '../../../proto/evm_pb'
6+
import { bytesToHexAddr } from '../../../crypto-utils'
7+
import { createDefaultTxMiddleware } from '../../../helpers'
88

99
test('Client EVM test (newBlockEvmFilterAsync)', async t => {
1010
let client

src/tests/e2e/evm-contract-tests.ts renamed to src/tests/e2e/evm/evm-contract-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
LocalAddress,
77
CryptoUtils,
88
createDefaultTxMiddleware
9-
} from '../../index'
10-
import { createTestWSClient } from '../helpers'
9+
} from '../../../index'
10+
import { createTestWSClient } from '../../helpers'
1111

1212
/**
1313
* Requires the SimpleStore solidity contract deployed on a loomchain.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import test from 'tape'
2+
3+
import { execAndWaitForMillisecondsAsync, getTestUrls } from '../../helpers'
4+
import { deployContract2 } from '../../evm-helpers'
5+
import { LoomProvider2 } from '../../../loom-provider-2'
6+
7+
/**
8+
* Requires the SimpleStore solidity contract deployed on a loomchain.
9+
* go-loom/examples/plugins/evmexample/contract/SimpleStore.sol
10+
*
11+
* pragma solidity ^0.4.22;
12+
*
13+
* contract SimpleStore {
14+
* uint value;
15+
*
16+
* constructor() {
17+
* value = 10;
18+
* }
19+
*
20+
* event NewValueSet(uint _value);
21+
*
22+
* function set(uint _value) public {
23+
* value = _value;
24+
* emit NewValueSet(value);
25+
* }
26+
*
27+
* function get() public view returns (uint) {
28+
* return value;
29+
* }
30+
* }
31+
*
32+
*/
33+
34+
const contractData =
35+
'0x608060405234801561001057600080fd5b50600a600081905550610114806100286000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606c575b600080fd5b606a600480360381019080803590602001909291905050506094565b005b348015607757600080fd5b50607e60df565b6040518082815260200191505060405180910390f35b806000819055507f2afa03c814297ffc234ff967b6f0863d3c358be243103f20217c8d3a4d39f9c060005434604051808381526020018281526020019250505060405180910390a150565b600080549050905600a165627a7a72305820deed812a797567167162d0af3ae5f0528c39bea0620e32b28e243628cd655dc40029'
36+
37+
test('LoomProvider + Filters', async t => {
38+
let loomProvider
39+
40+
try {
41+
const { wsEth } = getTestUrls()
42+
loomProvider = new LoomProvider2(wsEth)
43+
44+
await deployContract2(loomProvider, contractData)
45+
46+
// Transaction receipt in order to obtain the topic of the event NewValueSet
47+
const ethNewFilterResult = await execAndWaitForMillisecondsAsync(
48+
loomProvider.sendAsync({
49+
id: 10,
50+
method: 'eth_newFilter',
51+
params: [{ toBlock: 'latest' }]
52+
})
53+
)
54+
55+
t.assert(/0x.+/.test(ethNewFilterResult.result), 'New id should be created for new filter')
56+
57+
const ethNewBlockFilter = await execAndWaitForMillisecondsAsync(
58+
loomProvider.sendAsync({
59+
id: 11,
60+
method: 'eth_newBlockFilter'
61+
})
62+
)
63+
64+
t.assert(
65+
/0x.+/.test(ethNewBlockFilter.result),
66+
'New id should be created for new block filter'
67+
)
68+
69+
const ethGetFilterChanges = await execAndWaitForMillisecondsAsync(
70+
loomProvider.sendAsync({
71+
id: 12,
72+
method: 'eth_getFilterChanges',
73+
params: [ethNewBlockFilter.result]
74+
})
75+
)
76+
77+
t.assert(
78+
ethGetFilterChanges.result.length > 0,
79+
'Get filter changes should return array with new blocks'
80+
)
81+
82+
const ethUninstallFilterResult = await execAndWaitForMillisecondsAsync(
83+
loomProvider.sendAsync({
84+
id: 13,
85+
method: 'eth_uninstallFilter',
86+
params: [ethNewBlockFilter.result]
87+
})
88+
)
89+
90+
t.deepEqual(ethUninstallFilterResult.result, true, 'Should uninstall filter and return true')
91+
} catch (err) {
92+
t.error(err)
93+
}
94+
95+
if (loomProvider) {
96+
loomProvider.disconnect()
97+
}
98+
99+
t.end()
100+
})
101+
102+
test('LoomProvider + Filters 2', async t => {
103+
let loomProvider
104+
105+
try {
106+
const { wsEth } = getTestUrls()
107+
loomProvider = new LoomProvider2(wsEth)
108+
109+
await deployContract2(loomProvider, contractData)
110+
111+
const ethNewBlockFilter = await execAndWaitForMillisecondsAsync(
112+
loomProvider.sendAsync({
113+
id: 11,
114+
method: 'eth_newBlockFilter'
115+
})
116+
)
117+
118+
t.assert(
119+
/0x.+/.test(ethNewBlockFilter.result),
120+
'New id should be created for new block filter'
121+
)
122+
123+
const ethGetFilterChanges = await execAndWaitForMillisecondsAsync(
124+
loomProvider.sendAsync({
125+
id: 12,
126+
method: 'eth_getFilterChanges',
127+
params: [ethNewBlockFilter.result]
128+
})
129+
)
130+
131+
t.assert(ethGetFilterChanges.result.length > 0, 'Should return filter changes')
132+
133+
const ethGetBlockByHash = await execAndWaitForMillisecondsAsync(
134+
loomProvider.sendAsync({
135+
id: 13,
136+
method: 'eth_getBlockByHash',
137+
params: [ethGetFilterChanges.result[0], true]
138+
})
139+
)
140+
141+
t.assert(ethGetBlockByHash.result, 'Should return the block requested by hash')
142+
} catch (err) {
143+
t.error(err)
144+
}
145+
146+
if (loomProvider) {
147+
loomProvider.disconnect()
148+
}
149+
150+
t.end()
151+
})

0 commit comments

Comments
 (0)