Skip to content

Commit

Permalink
Update block hex to be 8 bytes in length (#156)
Browse files Browse the repository at this point in the history
Our current implementation returns `0x0` for nonce on a block response.
However the expectation is an 8byte

- Update `eth.ts` to use `0x0000000000000000` for nonce
- Update tests

Signed-off-by: Nana-EC <[email protected]>
  • Loading branch information
Nana-EC authored Jun 3, 2022
1 parent 10dca21 commit 139bc12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
3 changes: 2 additions & 1 deletion packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const createHash = require('keccak');
export class EthImpl implements Eth {
static emptyHex = '0x';
static zeroHex = '0x0';
static zeroHex8Byte = '0x0000000000000000';
static emptyArrayHex = '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347';
static zeroAddressHex = '0x0000000000000000000000000000000000000000';
static emptyBloom = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
Expand Down Expand Up @@ -767,7 +768,7 @@ export class EthImpl implements Eth {
logsBloom: EthImpl.emptyBloom, //TODO calculate full block boom in mirror node
miner: EthImpl.zeroAddressHex,
mixHash: EthImpl.emptyArrayHex,
nonce: EthImpl.zeroHex,
nonce: EthImpl.zeroHex8Byte,
number: EthImpl.numberTo0x(blockResponse.number),
parentHash: blockResponse.previous_hash.substring(0, 66),
receiptsRoot: EthImpl.emptyArrayHex,
Expand Down
64 changes: 19 additions & 45 deletions packages/relay/tests/lib/eth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {expectUnsupportedMethod} from '../helpers';
const cache = require('js-cache');

import pino from 'pino';
import { Transaction } from '../../src/lib/model';
import { Block, Transaction } from '../../src/lib/model';
const logger = pino();

const Relay = new RelayImpl(logger);
Expand All @@ -48,6 +48,20 @@ const validateHash = (hash: string, len?: number) => {
return !!hash.match(regex);
};

const verifyBlockConstants = (block: Block) => {
expect(block.baseFeePerGas).equal(EthImpl.zeroHex);
expect(block.difficulty).equal(EthImpl.zeroHex);
expect(block.extraData).equal(EthImpl.emptyHex);
expect(block.miner).equal(EthImpl.zeroAddressHex);
expect(block.mixHash).equal(EthImpl.emptyArrayHex);
expect(block.nonce).equal(EthImpl.zeroHex8Byte);
expect(block.receiptsRoot).equal(EthImpl.emptyArrayHex);
expect(block.sha3Uncles).equal(EthImpl.emptyArrayHex);
expect(block.stateRoot).equal(EthImpl.emptyArrayHex);
expect(block.totalDifficulty).equal(EthImpl.zeroHex);
expect(block.uncles).to.deep.equal([]);
};

describe('Eth calls using MirrorNode', async function () {
this.timeout(10000);

Expand Down Expand Up @@ -334,17 +348,7 @@ describe('Eth calls using MirrorNode', async function () {
expect((result.transactions[1] as string)).equal(contractHash1);

// verify expected constants
expect(result.baseFeePerGas).equal(EthImpl.zeroHex);
expect(result.difficulty).equal(EthImpl.zeroHex);
expect(result.extraData).equal(EthImpl.emptyHex);
expect(result.miner).equal(EthImpl.zeroAddressHex);
expect(result.mixHash).equal(EthImpl.emptyArrayHex);
expect(result.nonce).equal(EthImpl.zeroHex);
expect(result.receiptsRoot).equal(EthImpl.emptyArrayHex);
expect(result.sha3Uncles).equal(EthImpl.emptyArrayHex);
expect(result.stateRoot).equal(EthImpl.emptyArrayHex);
expect(result.totalDifficulty).equal(EthImpl.zeroHex);
expect(result.uncles).to.deep.equal([]);
verifyBlockConstants(result);
});

it('eth_getBlockByNumber with match and details', async function () {
Expand All @@ -369,17 +373,7 @@ describe('Eth calls using MirrorNode', async function () {
expect((result.transactions[1] as Transaction).hash).equal(contractHash1);

// verify expected constants
expect(result.baseFeePerGas).equal(EthImpl.zeroHex);
expect(result.difficulty).equal(EthImpl.zeroHex);
expect(result.extraData).equal(EthImpl.emptyHex);
expect(result.miner).equal(EthImpl.zeroAddressHex);
expect(result.mixHash).equal(EthImpl.emptyArrayHex);
expect(result.nonce).equal(EthImpl.zeroHex);
expect(result.receiptsRoot).equal(EthImpl.emptyArrayHex);
expect(result.sha3Uncles).equal(EthImpl.emptyArrayHex);
expect(result.stateRoot).equal(EthImpl.emptyArrayHex);
expect(result.totalDifficulty).equal(EthImpl.zeroHex);
expect(result.uncles).to.deep.equal([]);
verifyBlockConstants(result);
});

it('eth_getBlockByNumber with no match', async function () {
Expand Down Expand Up @@ -465,17 +459,7 @@ describe('Eth calls using MirrorNode', async function () {
expect((result.transactions[1] as string)).equal(contractHash1);

// verify expected constants
expect(result.baseFeePerGas).equal(EthImpl.zeroHex);
expect(result.difficulty).equal(EthImpl.zeroHex);
expect(result.extraData).equal(EthImpl.emptyHex);
expect(result.miner).equal(EthImpl.zeroAddressHex);
expect(result.mixHash).equal(EthImpl.emptyArrayHex);
expect(result.nonce).equal(EthImpl.zeroHex);
expect(result.receiptsRoot).equal(EthImpl.emptyArrayHex);
expect(result.sha3Uncles).equal(EthImpl.emptyArrayHex);
expect(result.stateRoot).equal(EthImpl.emptyArrayHex);
expect(result.totalDifficulty).equal(EthImpl.zeroHex);
expect(result.uncles).to.deep.equal([]);
verifyBlockConstants(result);
});

it('eth_getBlockByHash with match and details', async function () {
Expand All @@ -501,17 +485,7 @@ describe('Eth calls using MirrorNode', async function () {
expect((result.transactions[1] as Transaction).hash).equal(contractHash1);

// verify expected constants
expect(result.baseFeePerGas).equal(EthImpl.zeroHex);
expect(result.difficulty).equal(EthImpl.zeroHex);
expect(result.extraData).equal(EthImpl.emptyHex);
expect(result.miner).equal(EthImpl.zeroAddressHex);
expect(result.mixHash).equal(EthImpl.emptyArrayHex);
expect(result.nonce).equal(EthImpl.zeroHex);
expect(result.receiptsRoot).equal(EthImpl.emptyArrayHex);
expect(result.sha3Uncles).equal(EthImpl.emptyArrayHex);
expect(result.stateRoot).equal(EthImpl.emptyArrayHex);
expect(result.totalDifficulty).equal(EthImpl.zeroHex);
expect(result.uncles).to.deep.equal([]);
verifyBlockConstants(result);
});

it('eth_getBlockByHash with no match', async function () {
Expand Down

0 comments on commit 139bc12

Please sign in to comment.