Skip to content

Commit

Permalink
fix: test getBlock(latest)
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Aug 25, 2022
1 parent b208194 commit 0c5e31e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 37 deletions.
11 changes: 0 additions & 11 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ describe('defaultProvider', () => {
test(`getBlock(blockHash=${exampleBlockHash}, blockNumber=undefined)`, () => {
return expect(testProvider.getBlock(exampleBlockHash)).resolves.not.toThrow();
});

test('getBlock(blockIdentifier=latest)', async () => {
expect(exampleBlock).not.toBeNull();

const { block_number, accepted_time } = exampleBlock;

expect(typeof block_number).toEqual('number');

return expect(typeof accepted_time).toEqual('number');
});

test('getBlock() -> { blockNumber }', async () => {
const block = await testProvider.getBlock();
return expect(block).toHaveProperty('block_number');
Expand Down
23 changes: 19 additions & 4 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { RpcProvider } from '../src';
import { GetBlockResponse, RpcProvider } from '../src';
import { describeIfRpc, getTestProvider } from './fixtures';

describeIfRpc('RPCProvider', () => {
let provider: RpcProvider;
let rpcProvider: RpcProvider;

beforeAll(async () => {
provider = getTestProvider() as RpcProvider;
rpcProvider = getTestProvider() as RpcProvider;
});

describe('RPC methods', () => {
test('getChainId', async () => {
const chainId = await provider.getChainId();
const chainId = await rpcProvider.getChainId();
expect(chainId).toBe('0x534e5f474f45524c49');
});
});

describe('divergence from Provider methods', () => {
let exampleBlock: GetBlockResponse;

beforeAll(async () => {
exampleBlock = await rpcProvider.getBlock('latest');
});

test('getBlock(blockIdentifier=latest)', async () => {
expect(exampleBlock).not.toBeNull();
const { block_number, accepted_time } = exampleBlock;
expect(typeof block_number).toEqual('number');
return expect(typeof accepted_time).toEqual('number');
});
});
});
20 changes: 19 additions & 1 deletion __tests__/sequencerProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, Provider, SequencerProvider, stark } from '../src';
import { Contract, GetBlockResponse, Provider, SequencerProvider, stark } from '../src';
import { toBN } from '../src/utils/number';
import {
compiledErc20,
Expand All @@ -7,6 +7,7 @@ import {
getTestProvider,
} from './fixtures';

// This run only if Devnet Sequencer ?
describeIfSequencer('SequencerProvider', () => {
let provider: SequencerProvider;
let customSequencerProvider: Provider;
Expand Down Expand Up @@ -73,3 +74,20 @@ describeIfSequencer('SequencerProvider', () => {
});
});
});

describe('divergence from Provider methods', () => {
let exampleBlock: GetBlockResponse;
let sequencerProvider: SequencerProvider;

beforeAll(async () => {
sequencerProvider = getTestProvider() as SequencerProvider;
exampleBlock = await sequencerProvider.getBlock('latest');
});

test('getBlock(blockIdentifier=latest)', async () => {
expect(exampleBlock).not.toBeNull();
const { block_number, accepted_time } = exampleBlock;
expect(typeof block_number).toEqual('number');
return expect(typeof accepted_time).toEqual('number');
});
});
23 changes: 10 additions & 13 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import { RPC } from '../types/api';
import fetch from '../utils/fetchPonyfill';
import { getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
import {
BigNumberish,
bigNumberishArrayToDecimalStringArray,
toBN,
toHex,
} from '../utils/number';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { parseCalldata, parseContract, wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { randomAddress } from '../utils/stark';
Expand Down Expand Up @@ -90,18 +85,18 @@ export class RpcProvider implements ProviderInterface {

public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getBlockWithTxHashes', [blockIdentifierGetter.getIdentifier()]).then(
this.responseParser.parseGetBlockResponse
);
return this.fetchEndpoint('starknet_getBlockWithTxHashes', [
blockIdentifierGetter.getIdentifier(),
]).then(this.responseParser.parseGetBlockResponse);
}

public async getBlockWithTxs(
blockIdentifier: BlockIdentifier = 'pending'
): Promise<GetBlockResponse> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getBlockWithTxs', [blockIdentifierGetter.getIdentifier()]).then(
this.responseParser.parseGetBlockResponse
);
return this.fetchEndpoint('starknet_getBlockWithTxs', [
blockIdentifierGetter.getIdentifier(),
]).then(this.responseParser.parseGetBlockResponse);
}

public async getNonce(contractAddress: string): Promise<any> {
Expand Down Expand Up @@ -281,7 +276,9 @@ export class RpcProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier
): Promise<RPC.GetTransactionCountResponse> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getBlockTransactionCount', [blockIdentifierGetter.getIdentifier()]);
return this.fetchEndpoint('starknet_getBlockTransactionCount', [
blockIdentifierGetter.getIdentifier(),
]);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/provider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type BlockIdentifierObject =
| { type: 'BLOCK_NUMBER'; data: BlockNumber }
| { type: 'BLOCK_HASH'; data: BigNumberish };


export class BlockIdentifierClass {
blockIdentifier: BlockIdentifier;

Expand Down
4 changes: 2 additions & 2 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StarknetChainId } from '../../constants';
import { Status } from '../lib';
import { BlockIdentifier } from '../../provider/utils';
import { Status } from '../lib';

export namespace RPC {
export type Response = {
Expand Down Expand Up @@ -41,7 +41,7 @@ export namespace RPC {
sequencer: string;
new_root: string;
old_root: string;
accepted_time: number;
timestamp: number;
gas_price: string;
transactions: string[];
};
Expand Down
3 changes: 2 additions & 1 deletion src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import BN from 'bn.js';
import { Abi, CompressedProgram, EntryPointsByType, RawCalldata, Signature, Status } from './lib';

export interface GetBlockResponse {
accepted_time: number;
accepted_time?: number;
timestamp?: number;
block_hash: string;
block_number: number;
gas_price: string;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/ellipticCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ec = new EC(
* The function _truncateToN in lib/elliptic/ec/index.js does a shift-right of 4 bits
* in some cases. This function does the opposite operation so that
* _truncateToN(fixMessage(msg)) == msg.
*
*
* @param msg
*/
function fixMessage(msg: string) {
Expand Down Expand Up @@ -68,7 +68,7 @@ export function getKeyPairFromPublicKey(publicKey: BigNumberish): KeyPair {

/**
* Signs a message using the provided key.
*
*
* @param keyPair should be an KeyPair with a valid private key.
* @returns an Signature.
*/
Expand All @@ -94,7 +94,7 @@ function chunkArray(arr: any[], n: number): any[][] {

/**
* Verifies a message using the provided key.
*
*
* @param keyPair should be an KeyPair with a valid public key.
* @param sig should be an Signature.
* @returns true if the verification succeeds.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ResponseParser } from '.';
export class RPCResponseParser extends ResponseParser {
public parseGetBlockResponse(res: RPC.GetBlockResponse): GetBlockResponse {
return {
accepted_time: res.accepted_time,
timestamp: res.timestamp,
block_hash: res.block_hash,
block_number: res.block_number,
gas_price: res.gas_price,
Expand Down

0 comments on commit 0c5e31e

Please sign in to comment.