[chore] added more tests and remake blockchain.ts

@types
neeboo 6 years ago
parent 5cd33f64b9
commit 30de65c9d8
  1. 53
      examples/testNode.js
  2. 217
      packages/harmony-core/__test__/blockchain.test.ts
  3. 9
      packages/harmony-core/src/blockchain.ts

@ -15,32 +15,43 @@ const mne =
const acc = harmony.wallet.addByMnemonic(mne, 0);
console.log('--- hint: please write these down ---');
console.log('--- hint: please write these down');
console.log('-------------------------------------');
console.log(`${mne}`);
console.log('-------------------------------------');
console.log(
'--- hint: we use this private key to as default account to test ---',
);
console.log('--- hint: we use this private key to as default account to test');
console.log('-------------------------------------');
console.log(`${acc.privateKey}`);
console.log('-------------------------------------');
harmony.blockchain
.getBlockByNumber({
tag: 'latest',
returnObject: false,
})
.then((result) => {
console.log('--- hint: we test hmy_getBlockNumber ---');
console.log(result);
console.log('--------------------------------------');
// now it is async time
async function main() {
const latestBlock = await harmony.blockchain.getBlockByNumber({
blockNumber: 'latest',
});
console.log('--- testing: hmy_getBlockNumber');
console.log('-------------------------------------');
console.log(latestBlock);
console.log('-------------------------------------');
const sameLatestBlock = await harmony.blockchain.getBlockByHash({
blockHash: latestBlock.hash,
});
console.log('--- testing: hmy_getBlockByHash');
console.log('-------------------------------------');
console.log(sameLatestBlock);
console.log('-------------------------------------');
harmony.blockchain
.getBalance({
const latestBalance = await harmony.blockchain.getBalance({
address: acc.address,
tag: 'latest',
})
.then((result) => {
console.log('--- hint: we test hmy_getBalance ---');
console.log(result);
console.log('-------------------------------------');
blockNumber: latestBlock.number,
});
console.log('--- testing: hmy_getBalance');
console.log('-------------------------------------');
console.log(latestBalance);
console.log('-------------------------------------');
}
main();

@ -6,6 +6,16 @@ import { HttpProvider, Messenger } from '@harmony/network';
const provider = new HttpProvider('https://mock.com');
const messenger = new Messenger(provider);
function runMocks(mockRpcResponse: any, repeat: number): void {
const mocks = [];
for (let i = 0; i < 5; i++) {
mocks.push(mockRpcResponse);
}
// tslint:disable-next-line: no-shadowed-variable
const responses = mocks.map((res) => [JSON.stringify(res)]);
fetch.mockResponses(...responses);
}
describe('test Blockchain', () => {
afterEach(() => {
fetch.resetMocks();
@ -50,15 +60,7 @@ describe('test Blockchain', () => {
},
};
// set mocks to test methods
const mocks = [];
for (let i = 0; i < 5; i++) {
mocks.push(mockRpcResponse);
}
// tslint:disable-next-line: no-shadowed-variable
const responses = mocks.map((res) => [JSON.stringify(res)]);
fetch.mockResponses(...responses);
runMocks(mockRpcResponse, 5);
// should test getBlockByNumber
const res = await bc.getBlockByNumber({ blockNumber: 'latest' });
@ -87,4 +89,201 @@ describe('test Blockchain', () => {
// try some errors
});
it('test get transaction', async () => {
const bc = new Blockchain(messenger);
const mockRpcResponse = {
jsonrpc: '2.0',
id: 1,
result: {
blockHash:
'0x359036996c7ad7fdaa42b18de2fc157ae97d4cd3f32c688af349225f7e8f8fc6',
blockNumber: '0x1',
from: '0x15a128e599b74842bccba860311efa92991bffb5',
gas: '0x81650',
gasPrice: '0x0',
hash:
'0x9a71ea0839511c95b0818bd54a38ab56a05337a8282245d853d0ae3a1aedd7da',
input:
'0x6080604052678ac7230489e80000600155600280546001600160a01b0319163317905561014d806100316000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806327c78c421461003b5780634ddd108a14610063575b600080fd5b6100616004803603602081101561005157600080fd5b50356001600160a01b031661007d565b005b61006b61011c565b60408051918252519081900360200190f35b6002546001600160a01b0316331461009457600080fd5b600154303110156100a457600080fd5b6001600160a01b03811660009081526020819052604090205460ff16156100ca57600080fd5b6001600160a01b038116600081815260208190526040808220805460ff1916600190811790915554905181156108fc0292818181858888f19350505050158015610118573d6000803e3d6000fd5b5050565b30319056fea165627a7a72305820b83c347551d539b44b318fb7b4601a635a7d3a6d7063d11e6e05e4886b6d88050029',
nonce: '0x0',
to: null,
transactionIndex: '0x0',
value: '0x69e10de76676d08000000',
v: '0x1b',
r: '0xcea5384b78461b482531561a2f34198ab2cd6c6b69a1254b4029b46f783317a2',
s: '0x3f37baa890214c9a6167846d91d6dbfdada41401cf79c3f2ad3a064eb432f3a',
},
};
runMocks(mockRpcResponse, 4);
const res1 = await bc.getTransactionByHash({
txnHash:
'0x9a71ea0839511c95b0818bd54a38ab56a05337a8282245d853d0ae3a1aedd7da',
});
expect(res1.responseType).toEqual('result');
expect(res1.blockHash).toEqual(
'0x359036996c7ad7fdaa42b18de2fc157ae97d4cd3f32c688af349225f7e8f8fc6',
);
const res2 = await bc.getTransactionByBlockHashAndIndex({
blockHash:
'0x359036996c7ad7fdaa42b18de2fc157ae97d4cd3f32c688af349225f7e8f8fc6',
index: '0x0',
});
expect(res2.responseType).toEqual('result');
expect(res2.hash).toEqual(
'0x9a71ea0839511c95b0818bd54a38ab56a05337a8282245d853d0ae3a1aedd7da',
);
const res3 = await bc.getTransactionByBlockNumberAndIndex({
blockNumber: '0x1',
index: '0x0',
});
expect(res3.responseType).toEqual('result');
expect(res3.hash).toEqual(
'0x9a71ea0839511c95b0818bd54a38ab56a05337a8282245d853d0ae3a1aedd7da',
);
const res4 = await bc.getTransactionByBlockNumberAndIndex({
index: '0x0',
});
expect(res4.responseType).toEqual('result');
expect(res4.hash).toEqual(
'0x9a71ea0839511c95b0818bd54a38ab56a05337a8282245d853d0ae3a1aedd7da',
);
});
it('test get Block Transaction count', async () => {
const bc = new Blockchain(messenger);
const mockRpcResponse = {
jsonrpc: '2.0',
id: 1,
result: '0x65',
};
// set mocks to test methods
runMocks(mockRpcResponse, 5);
const res1 = await bc.getBlockTransactionCountByHash({
blockHash:
'0x7e1ef610f700805b93cf85b1e55bce84fcbd04373252a968755366a8d2215424',
});
expect(res1).toEqual('0x65');
try {
await bc.getBlockTransactionCountByHash({ blockHash: 'wrong' });
} catch (error) {
expect(error.message).toEqual(
`Validation failed for blockHash,should be validated by isHash`,
);
}
const res2 = await bc.getBlockTransactionCountByNumber({
blockNumber: '0x1',
});
try {
await bc.getBlockTransactionCountByNumber({ blockNumber: 'wrong' });
} catch (error) {
expect(error.message).toEqual(
`Validation failed for blockNumber,should be validated by isBlockNumber`,
);
}
expect(res2).toEqual('0x65');
const res3 = await bc.getBlockTransactionCountByNumber({
blockNumber: 'latest',
});
expect(res3).toEqual('0x65');
const res4 = await bc.getTransactionCount({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
blockNumber: '0x1',
});
expect(res4).toEqual('0x65');
const res5 = await bc.getTransactionCount({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
});
expect(res5).toEqual('0x65');
});
it('test get code', async () => {
const bc = new Blockchain(messenger);
const mockRpcResponse = {
jsonrpc: '2.0',
id: 1,
result: '0x',
};
// set mocks to test methods
runMocks(mockRpcResponse, 2);
const res1 = await bc.getCode({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
blockNumber: '0x1',
});
expect(res1).toEqual('0x');
const res2 = await bc.getCode({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
});
expect(res2).toEqual('0x');
});
it('test get storage at', async () => {
const bc = new Blockchain(messenger);
const mockRpcResponse = {
jsonrpc: '2.0',
id: 1,
result:
'0x0000000000000000000000000000000000000000000000000000000000000000',
};
// set mocks to test methods
runMocks(mockRpcResponse, 2);
const res1 = await bc.getStorageAt({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
position: '0x0',
blockNumber: '0x1',
});
expect(res1).toEqual(
'0x0000000000000000000000000000000000000000000000000000000000000000',
);
const res2 = await bc.getStorageAt({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
position: '0x0',
});
expect(res2).toEqual(
'0x0000000000000000000000000000000000000000000000000000000000000000',
);
});
it('test get balance', async () => {
const bc = new Blockchain(messenger);
const mockRpcResponse = {
jsonrpc: '2.0',
id: 1,
result: '0xd3c21bcecceda1000000',
};
// set mocks to test methods
runMocks(mockRpcResponse, 2);
const res1 = await bc.getBalance({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
blockNumber: '0x1',
});
expect(res1).toEqual('0xd3c21bcecceda1000000');
const res2 = await bc.getBalance({
address: '0x15a128e599b74842bccba860311efa92991bffb5',
});
expect(res2).toEqual('0xd3c21bcecceda1000000');
});
// it('test get transaction receipt', async () => {
// const bc = new Blockchain(messenger);
// const mockRpcResponse = {
// jsonrpc: '2.0',
// id: 1,
// result: '0xd3c21bcecceda1000000',
// };
// // set mocks to test methods
// runMocks(mockRpcResponse, 2);
// const res1 = await bc.getBalance({
// address: '0x15a128e599b74842bccba860311efa92991bffb5',
// blockNumber: '0x1',
// });
// expect(res1).toEqual('0xd3c21bcecceda1000000');
// const res2 = await bc.getBalance({
// address: '0x15a128e599b74842bccba860311efa92991bffb5',
// });
// expect(res2).toEqual('0xd3c21bcecceda1000000');
// });
});

@ -101,16 +101,13 @@ class Blockchain extends HarmonyCore {
return result;
}
/**
*
*/
@assertObject({
blockNumber: ['isBlockNumber', AssertType.optional],
blockNumber: ['isBlockNumber', AssertType.required],
})
async getBlockTransactionCountByNumber({
blockNumber = DefaultBlockParams.latest,
blockNumber,
}: {
blockNumber?: string;
blockNumber: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockTransactionCountByNumber,

Loading…
Cancel
Save