Skip to content

Commit

Permalink
Updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardonunesp committed Aug 28, 2019
1 parent 75b8adb commit f503e0a
Show file tree
Hide file tree
Showing 29 changed files with 1,049 additions and 120 deletions.
50 changes: 36 additions & 14 deletions src/loom-provider-2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import debug from 'debug'
import retry from 'retry'
import { Wallet } from 'ethers'
import { Client as WSClient } from 'rpc-websockets'
import { EthRPCMethod, IEthRPCPayload } from './loom-provider'
Expand All @@ -19,6 +20,19 @@ export class LoomProvider2 {
protected notificationCallbacks: Array<Function> = new Array()
readonly wallets: Map<string, Wallet | null> = new Map<string, Wallet>()

/**
* The retry strategy that should be used to retry some web3 requests.
* By default failed requested won't be resent.
* To understand how to tweak the retry strategy see
* https://github.com/tim-kos/node-retry#retrytimeoutsoptions
*/
retryStrategy: retry.OperationOptions = {
retries: 0,
minTimeout: 1000, // 1s
maxTimeout: 30000, // 30s
randomize: true
}

/**
* Constructs the LoomProvider2 to bridges communication between Web3 and Loom DappChains
*
Expand Down Expand Up @@ -94,21 +108,29 @@ export class LoomProvider2 {
return
}

let result

try {
if (this._ethRPCMethods.has(payload.method)) {
const f: Function = this._ethRPCMethods.get(payload.method)!
result = await f(payload)
} else {
result = await this._wsRPC.call(payload.method, payload.params)
const op = retry.operation(this.retryStrategy)
op.attempt(async currAttempt => {
debugLog(`Current attempt ${currAttempt}`)

let result

try {
if (this._ethRPCMethods.has(payload.method)) {
const f: Function = this._ethRPCMethods.get(payload.method)!
result = await f(payload)
} else {
result = await this._wsRPC.call(payload.method, payload.params)
}

callback(null, this._okResponse(payload.id, result, isArray))
} catch (err) {
if (!op.retry(err)) {
callback(err, null)
} else {
errorLog(err)
}
}

callback(null, this._okResponse(payload.id, result, isArray))
} catch (err) {
errorLog(err)
callback(err, null)
}
})
}

// EVENT HANDLING METHODS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
CryptoUtils,
createDefaultTxMiddleware,
Contracts
} from '../../index'
import { createTestHttpClient } from '../helpers'
import { EthersSigner, getJsonRPCSignerAsync } from '../../solidity-helpers'
import { ethers, Signer } from 'ethers'
} from '../../../index'
import { createTestHttpClient } from '../../helpers'
import { EthersSigner, getJsonRPCSignerAsync } from '../../../solidity-helpers'

async function getClientAndContract(
createClient: () => Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
createDefaultTxMiddleware,
Client,
LocalAddress
} from '../../index'
import { createTestHttpClient } from '../helpers'
import { B64ToUint8Array } from '../../crypto-utils'
} from '../../../index'
import { createTestHttpClient } from '../../helpers'
import { B64ToUint8Array } from '../../../crypto-utils'

const toCoinE18 = (amount: number): BN => {
return new BN(10).pow(new BN(18)).mul(new BN(amount))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
IChainEventArgs,
CryptoUtils,
createDefaultTxMiddleware
} from '../../index'
import { MapEntry } from '../tests_pb'
} from '../../../index'
import { MapEntry } from '../../tests_pb'
import {
createTestClient,
createTestHttpClient,
createTestWSClient,
createTestHttpWSClient
} from '../helpers'
} from '../../helpers'

async function getClientAndContract(
createClient: () => Client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import test from 'tape'
import BN from 'bn.js'
import {
Address,
Contracts,
CryptoUtils,
createDefaultTxMiddleware,
Client,
LocalAddress
} from '../../index'
import { createTestHttpClient, waitForMillisecondsAsync } from '../helpers'
import { B64ToUint8Array } from '../../crypto-utils'
} from '../../../index'
import { createTestHttpClient, waitForMillisecondsAsync } from '../../helpers'
import { B64ToUint8Array } from '../../../crypto-utils'

async function getClientAndContract(
createClient: () => Client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import test from 'tape'

import { CryptoUtils, Client } from '../../index'
import { createTestClient, waitForMillisecondsAsync } from '../helpers'
import { CallTx, VMType, MessageTx, Transaction } from '../../proto/loom_pb'
import { LoomProvider } from '../../loom-provider'
import { deployContract } from '../evm-helpers'
import { bufferToProtobufBytes } from '../../crypto-utils'
import { Address, LocalAddress } from '../../address'
import { createDefaultTxMiddleware } from '../../helpers'
import { CryptoUtils, Client } from '../../../index'
import { createTestClient, waitForMillisecondsAsync } from '../../helpers'
import { CallTx, VMType, MessageTx, Transaction } from '../../../proto/loom_pb'
import { LoomProvider } from '../../../loom-provider'
import { deployContract } from '../../evm-helpers'
import { bufferToProtobufBytes } from '../../../crypto-utils'
import { Address, LocalAddress } from '../../../address'
import { createDefaultTxMiddleware } from '../../../helpers'

/**
* Requires the SimpleStore solidity contract deployed on a loomchain.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import test from 'tape'

import { NonceTxMiddleware, SignedTxMiddleware, CryptoUtils } from '../../index'
import { createTestClient } from '../helpers'
import { CallTx, VMType, MessageTx, Transaction } from '../../proto/loom_pb'
import { LoomProvider } from '../../loom-provider'
import { deployContract } from '../evm-helpers'
import { bufferToProtobufBytes } from '../../crypto-utils'
import { Address, LocalAddress } from '../../address'
import { NonceTxMiddleware, SignedTxMiddleware, CryptoUtils } from '../../../index'
import { createTestClient } from '../../helpers'
import { CallTx, VMType, MessageTx, Transaction } from '../../../proto/loom_pb'
import { LoomProvider } from '../../../loom-provider'
import { deployContract } from '../../evm-helpers'
import { bufferToProtobufBytes } from '../../../crypto-utils'
import { Address, LocalAddress } from '../../../address'

/**
* Requires the SimpleStore solidity contract deployed on a loomchain.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'tape'

import { CryptoUtils } from '../../index'
import { createTestClient, execAndWaitForMillisecondsAsync } from '../helpers'
import { EthBlockHashList, EthBlockInfo } from '../../proto/evm_pb'
import { bytesToHexAddr } from '../../crypto-utils'
import { createDefaultTxMiddleware } from '../../helpers'
import { CryptoUtils } from '../../../index'
import { createTestClient, execAndWaitForMillisecondsAsync } from '../../helpers'
import { EthBlockHashList, EthBlockInfo } from '../../../proto/evm_pb'
import { bytesToHexAddr } from '../../../crypto-utils'
import { createDefaultTxMiddleware } from '../../../helpers'

test('Client EVM test (newBlockEvmFilterAsync)', async t => {
let client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
LocalAddress,
CryptoUtils,
createDefaultTxMiddleware
} from '../../index'
import { createTestWSClient } from '../helpers'
} from '../../../index'
import { createTestWSClient } from '../../helpers'

/**
* Requires the SimpleStore solidity contract deployed on a loomchain.
Expand Down
151 changes: 151 additions & 0 deletions src/tests/e2e/loom-provider-2/loom-provider-eth-filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import test from 'tape'

import { execAndWaitForMillisecondsAsync, getTestUrls } from '../../helpers'
import { deployContract2 } from '../../evm-helpers'
import { LoomProvider2 } from '../../../loom-provider-2'

/**
* Requires the SimpleStore solidity contract deployed on a loomchain.
* go-loom/examples/plugins/evmexample/contract/SimpleStore.sol
*
* pragma solidity ^0.4.22;
*
* contract SimpleStore {
* uint value;
*
* constructor() {
* value = 10;
* }
*
* event NewValueSet(uint _value);
*
* function set(uint _value) public {
* value = _value;
* emit NewValueSet(value);
* }
*
* function get() public view returns (uint) {
* return value;
* }
* }
*
*/

const contractData =
'0x608060405234801561001057600080fd5b50600a600081905550610114806100286000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606c575b600080fd5b606a600480360381019080803590602001909291905050506094565b005b348015607757600080fd5b50607e60df565b6040518082815260200191505060405180910390f35b806000819055507f2afa03c814297ffc234ff967b6f0863d3c358be243103f20217c8d3a4d39f9c060005434604051808381526020018281526020019250505060405180910390a150565b600080549050905600a165627a7a72305820deed812a797567167162d0af3ae5f0528c39bea0620e32b28e243628cd655dc40029'

test('LoomProvider + Filters', async t => {
let loomProvider

try {
const { wsEth } = getTestUrls()
loomProvider = new LoomProvider2(wsEth)

await deployContract2(loomProvider, contractData)

// Transaction receipt in order to obtain the topic of the event NewValueSet
const ethNewFilterResult = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 10,
method: 'eth_newFilter',
params: [{ toBlock: 'latest' }]
})
)

t.assert(/0x.+/.test(ethNewFilterResult.result), 'New id should be created for new filter')

const ethNewBlockFilter = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 11,
method: 'eth_newBlockFilter'
})
)

t.assert(
/0x.+/.test(ethNewBlockFilter.result),
'New id should be created for new block filter'
)

const ethGetFilterChanges = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 12,
method: 'eth_getFilterChanges',
params: [ethNewBlockFilter.result]
})
)

t.assert(
ethGetFilterChanges.result.length > 0,
'Get filter changes should return array with new blocks'
)

const ethUninstallFilterResult = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 13,
method: 'eth_uninstallFilter',
params: [ethNewBlockFilter.result]
})
)

t.deepEqual(ethUninstallFilterResult.result, true, 'Should uninstall filter and return true')
} catch (err) {
t.error(err)
}

if (loomProvider) {
loomProvider.disconnect()
}

t.end()
})

test('LoomProvider + Filters 2', async t => {
let loomProvider

try {
const { wsEth } = getTestUrls()
loomProvider = new LoomProvider2(wsEth)

await deployContract2(loomProvider, contractData)

const ethNewBlockFilter = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 11,
method: 'eth_newBlockFilter'
})
)

t.assert(
/0x.+/.test(ethNewBlockFilter.result),
'New id should be created for new block filter'
)

const ethGetFilterChanges = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 12,
method: 'eth_getFilterChanges',
params: [ethNewBlockFilter.result]
})
)

t.assert(ethGetFilterChanges.result.length > 0, 'Should return filter changes')

const ethGetBlockByHash = await execAndWaitForMillisecondsAsync(
loomProvider.sendAsync({
id: 13,
method: 'eth_getBlockByHash',
params: [ethGetFilterChanges.result[0], true]
})
)

t.assert(ethGetBlockByHash.result, 'Should return the block requested by hash')
} catch (err) {
t.error(err)
}

if (loomProvider) {
loomProvider.disconnect()
}

t.end()
})
Loading

0 comments on commit f503e0a

Please sign in to comment.