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

Upgrade version to 1.7.0 (obsolete) #161

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
044ddad
Merge pull request #150 from ainblockchain/release/v1.6.3
platfowner Sep 27, 2023
7ddeea4
Add countDecimals() util function
platfowner Oct 19, 2023
58b09d7
Allow up to 6 decimals in transfer values
platfowner Oct 19, 2023
0bb8135
Add more comments to decimal test cases
platfowner Oct 20, 2023
1d2fb2f
Merge pull request #152 from ainblockchain/feature/platfowner/feature
platfowner Oct 20, 2023
bbc38c7
Allow only positive transfer values
platfowner Oct 23, 2023
4deee26
Merge pull request #153 from ainblockchain/feature/platfowner/feature
platfowner Oct 23, 2023
76e7c5a
Extend countDecimals() for negative values
platfowner Oct 24, 2023
36d2f66
Tweak comments and names
platfowner Oct 25, 2023
dcb8cb2
Add a todo for hd derivation path
platfowner Oct 26, 2023
3a0b9da
Merge pull request #154 from ainblockchain/feature/platfowner/feature
platfowner Oct 26, 2023
e45c044
Add getStateProof() method
platfowner Feb 22, 2024
5936543
Add getProofHash() method
platfowner Feb 22, 2024
762b885
Merge pull request #155 from ainblockchain/feature/platfowner/feature
platfowner Feb 23, 2024
c162f76
Add getStateInfo() method
platfowner Feb 23, 2024
8fcf83d
Move getNonce() to wallet from signer
platfowner Feb 23, 2024
c66214e
Add getTimestamp() method
platfowner Feb 23, 2024
1f450f9
Add getAddress() method
platfowner Feb 23, 2024
5188c68
Merge pull request #156 from ainblockchain/bugfix/platfowner/bugfix
platfowner Feb 26, 2024
e4062a0
Rename: getTransaction -> getTransactionByHash
platfowner Feb 26, 2024
ffed4f5
Add getTransactionByBlockHashAndIndex() method
platfowner Feb 26, 2024
5f4cac8
Add getTransactionByBlockNumberAndIndex() method
platfowner Feb 26, 2024
876094d
Add getPendingTransactions() method
platfowner Feb 26, 2024
99bd536
Add getTransactionPoolSizeUtilization() method
platfowner Feb 26, 2024
bd8d90d
Update test with getBlock() method call
platfowner Feb 26, 2024
56ba075
Merge pull request #157 from ainblockchain/feature/platfowner/feature
platfowner Feb 26, 2024
aaa4385
Add getLastBlock() method
platfowner Feb 26, 2024
f34ac27
Add getLastBlockNumber() method
platfowner Feb 27, 2024
143c44b
Split getBlock() method to getBlockByNumber() and getBlockByHash()
platfowner Feb 27, 2024
6bfbc8c
Add getBlockList() method
platfowner Feb 27, 2024
ca03856
Add getBlockHeadersList() method
platfowner Feb 28, 2024
70aaafb
Add getBlockTransactionCountByNumber() method
platfowner Feb 28, 2024
d3b6ab0
Add getBlockTransactionCountByHash() method
platfowner Feb 28, 2024
9461cd2
Merge pull request #158 from ainblockchain/feature/platfowner/feature
platfowner Feb 29, 2024
250a214
Split getProposer() to getProposerByNumber() and getProposerByHash()
platfowner Mar 4, 2024
b734f7a
Split getValidators() to getValidatorsByNumber() and getValidatorsByH…
platfowner Mar 4, 2024
bad198a
Add getValidatorInfo() method
platfowner Mar 4, 2024
c1fd6a9
Tweak comments
platfowner Mar 4, 2024
ad139d6
Merge pull request #159 from ainblockchain/feature/platfowner/feature
platfowner Mar 5, 2024
f31a4e8
Upgrade version to 1.7.0
platfowner Mar 22, 2024
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
14 changes: 14 additions & 0 deletions __tests__/__snapshots__/ain.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ Object {
}
`;

exports[`ain-js Database getProofHash 1`] = `"0x88496dfee3566db91f487aa4cbf69a0c42a3e2a5d0a65bfd4897d699e8734785"`;

exports[`ain-js Database getStateInfo 1`] = `
Object {
"#num_children": 1,
"#state_ph": "0x985a1f057d5047b1dee392127eb776571fbbe79da7ae6114f8f8f18c4f786135",
"#tree_bytes": 1840,
"#tree_height": 2,
"#tree_max_siblings": 1,
"#tree_size": 3,
"#version": "erased",
}
`;

exports[`ain-js Database matchFunction 1`] = `
Object {
"matched_config": Object {
Expand Down
323 changes: 305 additions & 18 deletions __tests__/ain.test.ts

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions __tests__/test_util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { set } from 'lodash';

export declare function fail(error?: any): never;

export function eraseProtoVer(retVal) {
retVal.protoVer = 'erased';
return retVal;
export function eraseProtoVer(result) {
const erased = JSON.parse(JSON.stringify(result));
set(erased, 'protoVer', 'erased');
return erased;
}

export function eraseStateVersion(result) {
const erased = JSON.parse(JSON.stringify(result));
set(erased, '#version', 'erased');
return erased;
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ainblockchain/ain-js",
"version": "1.6.3",
"version": "1.7.0",
"description": "",
"main": "lib/ain.js",
"scripts": {
Expand Down
37 changes: 37 additions & 0 deletions src/ain-db/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EvalRuleInput,
EvalOwnerInput,
MatchInput,
StateInfoInput,
GetOptions,
} from '../types';
import Ain from '../ain';
Expand Down Expand Up @@ -347,6 +348,42 @@ export default class Reference {
return this._ain.provider.send('ain_matchOwner', request);
}

/**
* Fetches the state proof of a global blockchain state path.
* @param {StateInfoInput} params The state info input.
* @returns {Promise<any>} The return value of the blockchain API.
*/
getStateProof(params?: StateInfoInput): Promise<any> {
const request = {
ref: Reference.extendPath(this.path, params ? params.ref : undefined)
}
return this._ain.provider.send('ain_getStateProof', request);
}

/**
* Fetches the proof hash of a global blockchain state path.
* @param {StateInfoInput} params The state info input.
* @returns {Promise<any>} The return value of the blockchain API.
*/
getProofHash(params?: StateInfoInput): Promise<any> {
const request = {
ref: Reference.extendPath(this.path, params ? params.ref : undefined)
}
return this._ain.provider.send('ain_getProofHash', request);
}

/**
* Fetches the state information of a global blockchain state path.
* @param {StateInfoInput} params The state info input.
* @returns {Promise<any>} The return value of the blockchain API.
*/
getStateInfo(params?: StateInfoInput): Promise<any> {
const request = {
ref: Reference.extendPath(this.path, params ? params.ref : undefined)
}
return this._ain.provider.send('ain_getStateInfo', request);
}

// TODO(liayoo): Add this function.
///**
// * Attaches an listener for database events.
Expand Down
171 changes: 146 additions & 25 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,54 +85,175 @@ export default class Ain {
}

/**
* Fetches a block with a block hash or block number.
* @param {string | number} blockHashOrBlockNumber The block hash or block number.
* Fetches the last block.
* @returns {Promise<Block>}
*/
getLastBlock(): Promise<Block> {
return this.provider.send('ain_getLastBlock', {});
}

/**
* Fetches the last block number.
* @returns {Promise<Number>}
*/
getLastBlockNumber(): Promise<Block> {
return this.provider.send('ain_getLastBlockNumber', {});
}

/**
* Fetches a block with a block number.
* @param {number} blockNumber The block number.
* @param {boolean} returnTransactionObjects If it's true, returns a block with full transaction objects.
* Otherwise, returns a block with only transaction hashes.
* @returns {Promise<Block>}
*/
getBlock(blockHashOrBlockNumber: string | number, returnTransactionObjects?: boolean): Promise<Block> {
const byHash = typeof blockHashOrBlockNumber === 'string'
const rpcMethod = byHash ? 'ain_getBlockByHash' : 'ain_getBlockByNumber';
const data = Object.assign({},
{ getFullTransactions: !!returnTransactionObjects,
[byHash ? 'hash' : 'number']: blockHashOrBlockNumber });
return this.provider.send(rpcMethod, data);
getBlockByNumber(blockNumber: number, returnTransactionObjects?: boolean): Promise<Block> {
const data =
Object.assign({}, { getFullTransactions: !!returnTransactionObjects, number: blockNumber });
return this.provider.send('ain_getBlockByNumber', data);
}

/**
* Fetches a block with a block hash.
* @param {string} blockHash The block hash.
* @param {boolean} returnTransactionObjects If it's true, returns a block with full transaction objects.
* Otherwise, returns a block with only transaction hashes.
* @returns {Promise<Block>}
*/
getBlockByHash(blockHash: string, returnTransactionObjects?: boolean): Promise<Block> {
const data =
Object.assign({}, { getFullTransactions: !!returnTransactionObjects, hash: blockHash });
return this.provider.send('ain_getBlockByHash', data);
}

/**
* Fetches blocks with a block number range.
* @param {number} from The begining block number (inclusive).
* @param {number} to The ending block number (exclusive).
* @returns {Promise<Array<Block>>}
*/
getBlockList(from: number, to: number): Promise<Array<Block>> {
return this.provider.send('ain_getBlockList', { from, to });
}

/**
* Fetches the forger's address of a block with a block hash or block number.
* @param {string | number} blockHashOrBlockNumber The block hash or block number.
* Fetches block headers with a block number range.
* @param {number} from The begining block number (inclusive).
* @param {number} to The ending block number (exclusive).
* @returns {Promise<Array<Block>>}
*/
getBlockHeadersList(from: number, to: number): Promise<Array<Block>> {
return this.provider.send('ain_getBlockHeadersList', { from, to });
}

/**
* Fetches block transaction count with a block number.
* @param {number} number The block number.
* @returns {Promise<Number>}
*/
getBlockTransactionCountByNumber(number: number): Promise<Number> {
return this.provider.send('ain_getBlockTransactionCountByNumber', { number });
}

/**
* Fetches block transaction count with a block hash.
* @param {string} hash The block hash.
* @returns {Promise<Number>}
*/
getBlockTransactionCountByHash(hash: string): Promise<Number> {
return this.provider.send('ain_getBlockTransactionCountByHash', { hash });
}

/**
* Fetches the information of the given validator address.
* @param {string} address The validator address.
* @returns {Promise<any>}
*/
getValidatorInfo(address: string): Promise<any> {
return this.provider.send('ain_getValidatorInfo', { address });
}

/**
* Fetches the validator list of a block with a block number.
* @param {number} blockNumber The block number.
* @returns {Promise<any>}
*/
getValidatorsByNumber(blockNumber: number): Promise<any> {
return this.provider.send('ain_getValidatorsByNumber', { number: blockNumber });
}

/**
* Fetches the validator list of a block with a block hash.
* @param {string} blockHash The block hash.
* @returns {Promise<any>}
*/
getValidatorsByHash(blockHash: string): Promise<any> {
return this.provider.send('ain_getValidatorsByHash', { hash: blockHash });
}

/**
* Fetches the block proproser's address of a block with a block number.
* @param {number} blockNumber The block number.
* @returns {Promise<string>}
*/
getProposer(blockHashOrBlockNumber: string | number): Promise<string> {
const byHash = typeof blockHashOrBlockNumber === 'string'
const rpcMethod = byHash ? 'ain_getProposerByHash' : 'ain_getProposerByNumber';
return this.provider.send(rpcMethod,
{[byHash ? 'hash' : 'number']: blockHashOrBlockNumber});
getProposerByNumber(blockNumber: number): Promise<string> {
return this.provider.send('ain_getProposerByNumber', { number: blockNumber });
}

/**
* Fetches the validator list of a block with a block hash or block number.
* @param {string | number} blockHashOrBlockNumber The block hash or block number.
* @returns {Promise<string[]>}
* Fetches the block proproser's address of a block with a block hash.
* @param {string} blockHash The block hash.
* @returns {Promise<string>}
*/
getValidators(blockHashOrBlockNumber: string | number): Promise<string[]> {
const byHash = typeof blockHashOrBlockNumber === 'string'
const rpcMethod = byHash ? 'ain_getValidatorsByHash' : 'ain_getValidatorsByNumber';
return this.provider.send(rpcMethod,
{[byHash ? 'hash' : 'number']: blockHashOrBlockNumber});
getProposerByHash(blockHash: string): Promise<string> {
return this.provider.send('ain_getProposerByHash', { hash: blockHash });
}

/**
* Fetches pending transaction.
* @returns {Promise<any>}
*/
getPendingTransactions(): Promise<any> {
return this.provider.send('ain_getPendingTransactions', {});
}

/**
* Fetches transaction pool size utilization.
* @returns {Promise<any>}
*/
getTransactionPoolSizeUtilization(): Promise<any> {
return this.provider.send('ain_getTransactionPoolSizeUtilization', {});
}

/**
* Fetches a transaction's information with a transaction hash.
* @param {string} transactionHash The transaction hash.
* @returns {Promise<TransactionInfo>}
*/
getTransaction(transactionHash: string): Promise<TransactionInfo> {
getTransactionByHash(transactionHash: string): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByHash', { hash: transactionHash });
}

/**
* Fetches a transaction's information with a block hash and an index.
* @param {string} blockHash The block hash.
* @param {number} index The transaction index in the block
* @returns {Promise<TransactionInfo>}
*/
getTransactionByBlockHashAndIndex(blockHash: string, index: Number): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByBlockHashAndIndex', { block_hash: blockHash, index });
}

/**
* Fetches a transaction's information with a block hash and an index.
* @param {string} blockNumber The block number.
* @param {number} index The transaction index in the block
* @returns {Promise<TransactionInfo>}
*/
getTransactionByBlockNumberAndIndex(blockNumber: Number, index: Number): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByBlockNumberAndIndex', { block_number: blockNumber, index });
}

/**
* Fetches a blockchain app's state usage information with an app name.
* @param {string} appName The blockchain app name.
Expand Down
8 changes: 8 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export default class Provider {
this.httpClient = axios.create(axiosConfig);
}

/**
* Fetches the blockchain node's address.
* @returns {Promise<number>} The address value.
*/
getAddress(): Promise<number> {
return this.send('ain_getAddress', {})
}

/**
* Creates a JSON-RPC payload and sends it to the network.
* @param {string} rpcMethod The JSON-RPC method.
Expand Down
24 changes: 2 additions & 22 deletions src/signer/default-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,11 @@ export class DefaultSigner implements Signer {
}
let nonce = transactionInput.nonce;
if (nonce === undefined) {
nonce = await this.getNonce({ address, from: "pending" });
nonce = await this.wallet.getNonce({ address, from: "pending" });
}
const timestamp = transactionInput.timestamp ? transactionInput.timestamp : Date.now();
const gasPrice = transactionInput.gas_price || 0;
const billing = transactionInput.billing;
return Object.assign(tx, { nonce, timestamp, gas_price: gasPrice, billing });
}

/**
* Fetches an account's nonce value, which is the current transaction count of the account.
* @param {object} args The ferch options.
* It may contain a string 'address' value and a string 'from' value.
* The 'address' is the address of the account to get the nonce of,
* and the 'from' is the source of the data.
* It could be either the pending transaction pool ("pending") or
* the committed blocks ("committed"). The default value is "committed".
* @returns {Promise<number>} The nonce value.
*/
getNonce(args: { address?: string, from?: string }): Promise<number> {
if (!args) { args = {}; }
const address = args.address ? Ain.utils.toChecksumAddress(args.address)
: this.getAddress(args.address);
if (args.from !== undefined && args.from !== 'pending' && args.from !== 'committed') {
throw Error("'from' should be either 'pending' or 'committed'");
}
return this.provider.send('ain_getNonce', { address, from: args.from })
}
}
};
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ export interface MatchInput {
is_global?: boolean;
}

/**
* An interface for state information input.
*/
export interface StateInfoInput {
ref?: string;
}

/**
* An interface for state usage information.
*/
Expand Down
Loading
Loading