Skip to content

Commit

Permalink
Merge branch 'main' into 312-truffle-relay-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
natanasow committed Jul 26, 2022
2 parents 088ae1c + 5af00f9 commit 6f7a449
Show file tree
Hide file tree
Showing 15 changed files with 873 additions and 268 deletions.
25 changes: 13 additions & 12 deletions docs/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
"items": {
"title": "rewardPercentile",
"description": "Floating point value between 0 and 100.",
"type": "number"
"type": "number",
"pattern": "^[0-9][0-9]?$|^100$"
}
}
}
Expand Down Expand Up @@ -159,7 +160,9 @@
"description": "An array of gas used ratio.",
"type": "array",
"items": {
"$ref": "#/components/schemas/uint"
"title": "hex encoded unsigned integer",
"type": "string",
"pattern": "^0x([1-9a-f]+[0-9a-f]*|0|0.8)$"
}
},
"baseFeePerGas": {
Expand Down Expand Up @@ -358,11 +361,7 @@
"result": {
"name": "Log objects",
"schema": {
"title": "Filter results",
"type": "array",
"items": {
"$ref": "#/components/schemas/FilterResults"
}
"$ref": "#/components/schemas/FilterResults"
}
}
},
Expand Down Expand Up @@ -460,10 +459,7 @@
"name": "Transaction count",
"schema": {
"title": "Transaction count",
"type": "array",
"items": {
"$ref": "#/components/schemas/uint"
}
"$ref": "#/components/schemas/uint"
}
}
},
Expand Down Expand Up @@ -683,7 +679,7 @@
"name": "The current client version.",
"schema": {
"type": "string",
"pattern": "relay/<semanticVersion>"
"pattern": "relay\/[0-9]\\.[0-9]\\.[0-9]"
}
}
}
Expand Down Expand Up @@ -1378,6 +1374,11 @@
"title": "Error message",
"type": "string",
"pattern": "Unsupported JSON-RPC method"
},
"name": {
"title": "Error name",
"type": "string",
"pattern": "Method not found"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"start": "npx lerna exec --scope @hashgraph/json-rpc-server -- npm run start",
"start:docker": "docker run --name hedera-relay -d -p 7546:7546 ${npm_package_name}:latest",
"test": "npx lerna run test",
"openrpctest": "ts-mocha packages/server/tests/integration/openrpc.spec.ts",
"openrpctest": "ts-mocha packages/relay/tests/lib/openrpc.spec.ts --exit",
"integration:prerequisite": "ts-node packages/server/tests/helpers/prerequisite.ts"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export interface Eth {

getBlockByNumber(blockNum: string, showDetails: boolean): Promise<Block | null>;

getBlockTransactionCountByHash(hash: string): Promise<number | null>;
getBlockTransactionCountByHash(hash: string): Promise<string | null>;

getBlockTransactionCountByNumber(blockNum: string): Promise<number | null>
getBlockTransactionCountByNumber(blockNum: string): Promise<string | null>

getCode(address: string, blockNumber: string | null): Promise<string>;

Expand Down
6 changes: 5 additions & 1 deletion packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class SDKClient {
.setEthereumData(transactionBuffer), callerName);
}

async submitContractCallQuery(to: string, data: string, gas: number, callerName: string): Promise<ContractFunctionResult> {
async submitContractCallQuery(to: string, data: string, gas: number, from: string, callerName: string): Promise<ContractFunctionResult> {
const contract = SDKClient.prune0x(to);
const contractId = contract.startsWith("00000000000")
? ContractId.fromSolidityAddress(contract)
Expand All @@ -209,6 +209,10 @@ export class SDKClient {
contractCallQuery.setFunctionParameters(Buffer.from(SDKClient.prune0x(data), 'hex'));
}

if (from) {
contractCallQuery.setSenderAccountId(AccountId.fromEvmAddress(0,0, from))
}

if (this.clientMain.operatorAccountId !== null) {
contractCallQuery
.setPaymentTransactionId(TransactionId.generate(this.clientMain.operatorAccountId));
Expand Down
59 changes: 18 additions & 41 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import { Eth } from '../index';
import { ContractId, Status, Hbar, EthereumTransaction } from '@hashgraph/sdk';
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
import { Logger } from 'pino';
import { Block, CachedBlock, Transaction, Log } from './model';
import { MirrorNode } from './mirrorNode';
import { Block, Transaction, Log } from './model';
import { MirrorNodeClient, SDKClient } from './clients';
import { JsonRpcError, predefined } from './errors';
import constants from './constants';
Expand Down Expand Up @@ -76,12 +75,6 @@ export class EthImpl implements Eth {
*/
private readonly sdkClient: SDKClient;

/**
* The mirror node mock
* @private
*/
private readonly mirrorNode: MirrorNode;

/**
* The interface through which we interact with the mirror node
* @private
Expand Down Expand Up @@ -109,20 +102,17 @@ export class EthImpl implements Eth {
/**
* Create a new Eth implementation.
* @param nodeClient
* @param mirrorNode
* @param mirrorNodeClient
* @param logger
* @param chain
*/
constructor(
nodeClient: SDKClient,
mirrorNode: MirrorNode,
mirrorNodeClient: MirrorNodeClient,
logger: Logger,
chain: string
) {
this.sdkClient = nodeClient;
this.mirrorNode = mirrorNode;
this.mirrorNodeClient = mirrorNodeClient;
this.logger = logger;
this.chain = chain;
Expand Down Expand Up @@ -540,7 +530,7 @@ export class EthImpl implements Eth {
*
* @param hash
*/
async getBlockTransactionCountByHash(hash: string): Promise<number | null> {
async getBlockTransactionCountByHash(hash: string): Promise<string | null> {
this.logger.trace('getBlockTransactionCountByHash(hash=%s, showDetails=%o)', hash);
return this.mirrorNodeClient
.getBlock(hash)
Expand All @@ -555,7 +545,7 @@ export class EthImpl implements Eth {
* Gets the number of transaction in a block by its block number.
* @param blockNumOrTag
*/
async getBlockTransactionCountByNumber(blockNumOrTag: string): Promise<number | null> {
async getBlockTransactionCountByNumber(blockNumOrTag: string): Promise<string | null> {
this.logger.trace('getBlockTransactionCountByNumber(blockNum=%s, showDetails=%o)', blockNumOrTag);
const blockNum = await this.translateBlockTag(blockNumOrTag);
return this.mirrorNodeClient
Expand Down Expand Up @@ -669,24 +659,7 @@ export class EthImpl implements Eth {
if (record.ethereumHash == null) {
throw new Error('The ethereumHash can never be null for an ethereum transaction, and yet it was!!');
}
const txHash = EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex'));

// If the transaction succeeded, create a new block for the transaction.
const mostRecentBlock = await this.mirrorNode.getMostRecentBlock();
this.logger.debug('mostRecentBlock=%o', mostRecentBlock);
let block = mostRecentBlock;
if (record.receipt.status == Status.Success) {
block = new CachedBlock(mostRecentBlock, txHash);
this.mirrorNode.storeBlock(block);
}

// Create a receipt. Register the receipt in the cache and return the tx hash
if (block == null) {
this.logger.error('Failed to get a block for transaction');
return '';
}

return txHash;
return EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex'));
} catch (e) {
this.logger.error(e,
'Failed sendRawTransaction during record retrieval for transaction %s, returning computed hash', transaction);
Expand Down Expand Up @@ -730,10 +703,10 @@ export class EthImpl implements Eth {
gas = call.gas;
}
}

// Execute the call and get the response
this.logger.debug('Making eth_call on contract %o with gas %d and call data "%s"', call.to, gas, call.data);
const contractCallResponse = await this.sdkClient.submitContractCallQuery(call.to, call.data, gas, EthImpl.ethCall);
this.logger.debug('Making eth_call on contract %o with gas %d and call data "%s" from "%s"', call.to, gas, call.data, call.from);
const contractCallResponse = await this.sdkClient.submitContractCallQuery(call.to, call.data, gas, call.from, EthImpl.ethCall);

// FIXME Is this right? Maybe so?
return EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));
Expand Down Expand Up @@ -778,13 +751,13 @@ export class EthImpl implements Eth {
input: contractResult.function_parameters,
maxPriorityFeePerGas: maxPriorityFee,
maxFeePerGas: maxFee,
nonce: contractResult.nonce,
nonce: EthImpl.nullableNumberTo0x(contractResult.nonce),
r: rSig,
s: sSig,
to: contractResult.to?.substring(0, 42),
transactionIndex: EthImpl.numberTo0x(contractResult.transaction_index),
type: contractResult.type,
v: contractResult.v,
type: EthImpl.nullableNumberTo0x(contractResult.type),
v: EthImpl.nullableNumberTo0x(contractResult.v),
value: EthImpl.numberTo0x(contractResult.amount),
});
}
Expand Down Expand Up @@ -863,6 +836,10 @@ export class EthImpl implements Eth {
return EthImpl.emptyHex + input.toString(16);
}

static nullableNumberTo0x(input: number | BigNumber): string | null {
return input === null ? null : EthImpl.numberTo0x(input);
}

static toHash32(value: string): string {
return value.substring(0, 66);
}
Expand Down Expand Up @@ -997,7 +974,7 @@ export class EthImpl implements Eth {
return null;
}

return block.count;
return EthImpl.numberTo0x(block.count);
}

private getTransactionFromContractResults(contractResults: any) {
Expand Down Expand Up @@ -1033,13 +1010,13 @@ export class EthImpl implements Eth {
input: contractResultDetails.function_parameters,
maxPriorityFeePerGas: EthImpl.toNullIfEmptyHex(contractResultDetails.max_priority_fee_per_gas),
maxFeePerGas: EthImpl.toNullIfEmptyHex(contractResultDetails.max_fee_per_gas),
nonce: contractResultDetails.nonce,
nonce: EthImpl.nullableNumberTo0x(contractResultDetails.nonce),
r: rSig,
s: sSig,
to: contractResultDetails.to.substring(0, 42),
transactionIndex: EthImpl.numberTo0x(contractResultDetails.transaction_index),
type: contractResultDetails.type,
v: contractResultDetails.v,
type: EthImpl.nullableNumberTo0x(contractResultDetails.type),
v: EthImpl.nullableNumberTo0x(contractResultDetails.v),
value: EthImpl.numberTo0x(contractResultDetails.amount),
});
})
Expand Down
Loading

0 comments on commit 6f7a449

Please sign in to comment.