Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use LoomProvider with the /eth endpoint #299

Merged
merged 39 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1a39fc2
Expose read client
redben Aug 15, 2019
8161b7c
Change reader suffix to /eth
redben Aug 15, 2019
1acee64
Forward rpc static calls as is to /eth endpoint
redben Aug 15, 2019
8b9dcd0
format
redben Aug 15, 2019
5183ef4
Fix _ethCallSupportedMethod return type
redben Aug 15, 2019
62c5f31
e2e wip
redben Aug 18, 2019
8d8ce5c
Don't deploy blueprint contract
enlight Aug 19, 2019
b6918fe
Merge branch 'disable-blueprint' into loom-provider-eth-endpoint
redben Aug 19, 2019
80fa6d0
Keep support for old protocol in client and loomProvider
redben Aug 20, 2019
a5192b6
ws-rpc-client id string/number bug workaround
redben Aug 20, 2019
085b054
revert changes to e2e_tests.ts
redben Aug 21, 2019
bffb4c3
format
redben Aug 21, 2019
5dc2706
Fix loomProvider _ethRPCMethods subscribe/unsubscribe
redben Aug 21, 2019
75a68bc
LoomProvider keep support of old protocol in _onWebSocketMessage
redben Aug 21, 2019
7b84d8f
ws-rpc-client._onEventMessage: add back event handling for old endpoint
redben Aug 21, 2019
8719dcc
Address PR comments
redben Aug 21, 2019
2263779
WSRPCClient keep original interface (protected _getNextRequestId)
redben Aug 22, 2019
d68aa3b
LoomProvider: Remove unused imports
redben Aug 22, 2019
291771d
Address comments on PR
redben Aug 22, 2019
0d728a2
Cleanup
enlight Aug 22, 2019
87994b7
Fix the build?
enlight Aug 22, 2019
6df71f8
Run web3 tests against /queryws & /eth endpoints (where it makes sense)
enlight Aug 23, 2019
d7f7685
Fix .number in loomProvider._createBlockInfo
redben Aug 28, 2019
61e0cff
LoomProvider Web3: Fix events
redben Aug 28, 2019
5a22b37
LoomProvider._onWebSocketMessage fix conditional paths
redben Aug 28, 2019
094d2df
Move call _client.addListenerForTopics()) to the right place
redben Aug 28, 2019
5f2d002
Skip BluePrint contract test
redben Aug 28, 2019
f79c244
Bring back BluePrint
enlight Aug 30, 2019
21f7c13
Cleanup
enlight Sep 24, 2019
a8f2fdb
Fix build error
enlight Sep 24, 2019
a36280e
Make WSRPCClient handle all EVM notifications regardless of the endpoint
enlight Sep 24, 2019
2d8bc4b
/eth endpoint supports eth_getBalance
enlight Sep 24, 2019
cebfaa2
The good old change shit until it works approach
enlight Sep 24, 2019
4fee498
Switch on logging
enlight Sep 24, 2019
50201d7
Upgrade to build 1283
enlight Sep 24, 2019
478f32f
Update loom binary download location
enlight Sep 24, 2019
3f0dcf9
Revert "The good old change shit until it works approach"
enlight Sep 24, 2019
b0d6752
Better naming for tests & some more cleanup
enlight Sep 24, 2019
c466082
Switch off logging
enlight Sep 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions e2e_support/genesis.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"contracts": [
{
"vm": "plugin",
"format": "plugin",
"name": "BluePrint",
"location": "blueprint:0.0.1",
"init": {}
},
{
"vm": "plugin",
"format": "plugin",
Expand Down Expand Up @@ -99,4 +92,4 @@
}
}
]
}
}
10 changes: 5 additions & 5 deletions e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ setup_dappchain() {
$LOOM_BIN init -f
cp -R $REPO_ROOT/e2e_support/* .
cp -R $REPO_ROOT/e2e_support/tm-config/* chaindata/config/
mkdir -p contracts
cp $LOOM_BLUEPRINT_DIR/build/contracts/* contracts
#mkdir -p contracts
#cp $LOOM_BLUEPRINT_DIR/build/contracts/* contracts
}

start_chains() {
Expand All @@ -72,7 +72,7 @@ stop_chains() {
kill -9 $LOOM_PID
LOOM_PID=""
fi
pkill -f "${LOOM_DIR}/contracts/blueprint.0.0.1" || true
#pkill -f "${LOOM_DIR}/contracts/blueprint.0.0.1" || true
}

run_tests() {
Expand All @@ -90,8 +90,8 @@ cleanup() {
if [ "${TRAVIS:-}" ]; then
mkdir -p $LOOM_DIR

setup_weave_blueprint
download_dappchain
#setup_weave_blueprint
#download_dappchain
fi

setup_dappchain
Expand Down
51 changes: 37 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from './crypto-utils'
import { Address, LocalAddress } from './address'
import { WSRPCClient, IJSONRPCEvent } from './internal/ws-rpc-client'
import { RPCClientEvent, IJSONRPCClient } from './internal/json-rpc-client'
import { RPCClientEvent, IJSONRPCClient, IJSONRPCResponse } from './internal/json-rpc-client'
import { sleep, parseUrl } from './helpers'

export interface ITxHandlerResult {
Expand Down Expand Up @@ -81,7 +81,7 @@ export enum ClientEvent {
/**
* Exclusively used by loom-provider
*/
EVMEvent = 'evmEvent',
EVMEvent = 'message', // 'evmEvent',
redben marked this conversation as resolved.
Show resolved Hide resolved
/**
* Emitted when an error occurs that can't be relayed by other means.
* Listener will receive IClientErrorEventArgs.
Expand Down Expand Up @@ -242,6 +242,7 @@ export class Client extends EventEmitter {

private _writeClient: IJSONRPCClient
private _readClient!: IJSONRPCClient
readonly isLegacy: boolean

/** Broadcaster to use to send txs & receive results. */
txBroadcaster: ITxBroadcaster
Expand Down Expand Up @@ -315,6 +316,8 @@ export class Client extends EventEmitter {
)
}

this.isLegacy = false === /eth$/.test(this._readClient.url)

const emitContractEvent = (url: string, event: IJSONRPCEvent) =>
this._emitContractEvent(url, event)

Expand Down Expand Up @@ -416,8 +419,8 @@ export class Client extends EventEmitter {
const emitContractEvent = (url: string, event: IJSONRPCEvent) => {
this._emitContractEvent(url, event, true)
}

this._readClient.on(RPCClientEvent.EVMMessage, emitContractEvent)
const eventType = this.isLegacy ? RPCClientEvent.EVMMessage : RPCClientEvent.Message
this._readClient.on(eventType, emitContractEvent)
}

/**
Expand All @@ -442,6 +445,17 @@ export class Client extends EventEmitter {
}
}

/**
* Sends a JSON-RPC message.
* @param method RPC method name.
* @param params Parameter object or array.
* @returns A promise that will be resolved with the value of the result field (if any) in the
* JSON-RPC response message.
*/
async sendWeb3MsgAsync<T>(method: string, params: object | any[]): Promise<IJSONRPCResponse<T>> {
return this._readClient.sendAsync<IJSONRPCResponse<T>>(method, params)
}

/**
* Queries the receipt corresponding to a transaction hash
*
Expand Down Expand Up @@ -555,7 +569,6 @@ export class Client extends EventEmitter {
const envelope: EthFilterEnvelope = EthFilterEnvelope.deserializeBinary(
bufferToProtobufBytes(result)
)

switch (envelope.getMessageCase()) {
case EthFilterEnvelope.MessageCase.ETH_BLOCK_HASH_LIST:
return envelope.getEthBlockHashList() as EthBlockHashList
Expand Down Expand Up @@ -675,12 +688,15 @@ export class Client extends EventEmitter {
* @param method Method selected to the filter, can be "newHeads" or "logs"
* @param filter JSON string of the filter
*/
evmSubscribeAsync(method: string, filterObject: Object): Promise<string> {
const filter = JSON.stringify(filterObject)
return this._readClient.sendAsync<string>('evmsubscribe', {
method,
filter
})
evmSubscribeAsync(method: string, params: Object | any[]): Promise<string> {
if (this.isLegacy) {
const filter = JSON.stringify(params)
return this._readClient.sendAsync<string>('evmsubscribe', {
method,
filter
})
}
return this._readClient.sendAsync<string>('eth_subscribe', params)
}

/**
Expand All @@ -691,9 +707,10 @@ export class Client extends EventEmitter {
* @return boolean If true the subscription is removed with success
*/
evmUnsubscribeAsync(id: string): Promise<boolean> {
return this._readClient.sendAsync<boolean>('evmunsubscribe', {
id
})
if (this.isLegacy) {
return this._readClient.sendAsync<boolean>('evmunsubscribe', { id })
}
return this._readClient.sendAsync<boolean>('eth_unsubscribe', [id])
}

/**
Expand Down Expand Up @@ -755,6 +772,12 @@ export class Client extends EventEmitter {
} else if (result) {
debugLog('Event', event.id, result)

// emit evm events as is (jsonrpc/ethereum)
if (!this.isLegacy && isEVM) {
this.emit(ClientEvent.EVMEvent, event)
return
}

// Ugh, no built-in JSON->Protobuf marshaller apparently
// https://github.com/google/protobuf/issues/1591 so gotta do this manually
const eventArgs: IChainEventArgs = {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function setupProtocolsFromEndpoint(
): { writer: IJSONRPCClient; reader: IJSONRPCClient } {
const protocol = selectProtocol(endpoint)
const writerSuffix = protocol === JSONRPCProtocol.HTTP ? '/rpc' : '/websocket'
const readerSuffix = protocol === JSONRPCProtocol.HTTP ? '/query' : '/queryws'
const readerSuffix = protocol === JSONRPCProtocol.HTTP ? '/eth' : '/eth'
redben marked this conversation as resolved.
Show resolved Hide resolved
redben marked this conversation as resolved.
Show resolved Hide resolved

const writer = createJSONRPCClient({
protocols: [{ url: endpoint + writerSuffix }]
Expand Down
37 changes: 26 additions & 11 deletions src/internal/ws-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ export class WSRPCClient extends EventEmitter {
private _isSubcribed: boolean = false

protected _rpcId: number = 0
protected _getNextRequestId = () => (++this._rpcId).toString()

protected _getNextRequestId(): string {
const id = ++this._rpcId
// @ts-ignore
return this.isLegacy ? id.toString() : id
}

requestTimeout: number

get isSubscribed(): boolean {
return this._isSubcribed
}

get isLegacy() {
return /eth$/.test(this.url) === false
}

/**
*
* @param url
Expand Down Expand Up @@ -206,16 +215,22 @@ export class WSRPCClient extends EventEmitter {
const msgStr = message instanceof ArrayBuffer ? Buffer.from(message).toString() : message
const msg = JSON.parse(msgStr)

// Events from native loomchain have the id equals 0
if (msg.id === '0') {
log('Loom Event arrived', msg)
this.emit(RPCClientEvent.Message, this.url, msg)
}

// Events from EVM have the id from the evmsubscribe command
if (/^0x.+$/.test(msg.id)) {
log('EVM Event arrived', msg)
this.emit(RPCClientEvent.EVMMessage, this.url, msg)
if (this.isLegacy) {
// Events from native loomchain have the id equals 0
if (msg.id === '0') {
log('Loom Event arrived', msg)
this.emit(RPCClientEvent.Message, this.url, msg)
}
// EVM Event old endpoint
else if (/^0x.+$/.test(msg.id)) {
this.emit(RPCClientEvent.EVMMessage, this.url, msg)
}
} else {
// EVM Event eth endpoint
if (msg.method === 'eth_subscription') {
log('EVM Event arrived', msg)
this.emit(RPCClientEvent.EVMMessage, msg)
}
}
}
}
Loading