Skip to content

Commit

Permalink
Merge pull request #428 from ainblockchain/release/v0.7.6
Browse files Browse the repository at this point in the history
Merge Release/v0.7.6 into master
  • Loading branch information
minsulee2 authored Jun 2, 2021
2 parents 843363e + f15ea3a commit 8a85c7f
Show file tree
Hide file tree
Showing 41 changed files with 3,527 additions and 935 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ MIN_NUM_VALIDATORS=3 ACCOUNT_INDEX=2 DEBUG=false STAKE=250 CONSOLE_LOG=true ENAB
You can override default port numbering system by setting `PORT` and `P2P_PORT` environment variables.
Before starting node jobs, remove existing blockchain files and logs if necessary:
```
rm -rf chains logs
rm -rf /path/to/data/dir logs
```
The default blockchain data directory is ~/ain_blockchain_data (e.g. chain data will be at ~/ain_blockchain_data/chains). You can use a different directory by specifying the `BLOCKCHAIN_DATA_DIR` environment variable.

The default minimum size of the validator whitelist is 3. Change MIN_NUM_VALIDATORS parameter in
the genesis-configs/base/genesis.json to change this value. You may also need to modify the GENESIS_WHITELIST and GENESIS_VALIDATORS accordingly.
The genesis configs directory used is `genesis-configs/base` by default and it can be altered using `GENESIS_CONFIGS_DIR` env variable. For example, afan shard cluster can use the following command line:
Expand Down
1 change: 0 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const process = require('process');
const express = require('express');
const jayson = require('jayson');
const _ = require('lodash');
Expand Down
9 changes: 9 additions & 0 deletions client/protocol_versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@
},
"0.7.3": {
"min": "0.7.0"
},
"0.7.4": {
"min": "0.7.0"
},
"0.7.5": {
"min": "0.7.0"
},
"0.7.6": {
"min": "0.7.0"
}
}
14 changes: 13 additions & 1 deletion common/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
Expand Down Expand Up @@ -63,7 +64,11 @@ if (!semver.valid(CONSENSUS_PROTOCOL_VERSION)) {
throw Error('Wrong data version format is specified for CONSENSUS_PROTOCOL_VERSION');
}
const LOGS_DIR = path.resolve(__dirname, '../logs');
const CHAINS_DIR = path.resolve(__dirname, '../chains');
const BLOCKCHAIN_DATA_DIR = process.env.BLOCKCHAIN_DATA_DIR || path.resolve(__dirname, '../ain_blockchain_data');
if (!fs.existsSync(BLOCKCHAIN_DATA_DIR)) {
fs.mkdirSync(BLOCKCHAIN_DATA_DIR, { recursive: true });
}
const CHAINS_DIR = path.resolve(BLOCKCHAIN_DATA_DIR, 'chains');
const CHAINS_N2B_DIR_NAME = 'n2b'; // NOTE: Block number to block.
const CHAINS_H2N_DIR_NAME = 'h2n'; // NOTE: Block hash to block number.
const HASH_DELIMITER = '#';
Expand Down Expand Up @@ -237,6 +242,7 @@ const AccountProperties = {
const OwnerProperties = {
ANYONE: '*',
BRANCH_OWNER: 'branch_owner',
FID_PREFIX: 'fid:',
OWNER: '.owner',
OWNERS: 'owners',
WRITE_FUNCTION: 'write_function',
Expand Down Expand Up @@ -312,6 +318,7 @@ const NativeFunctionIds = {
PAY: '_pay',
RELEASE: '_release',
SAVE_LAST_TX: '_saveLastTx',
SET_OWNER_CONFIG: '_setOwnerConfig',
STAKE: '_stake',
TRANSFER: '_transfer',
UNSTAKE: '_unstake',
Expand Down Expand Up @@ -645,6 +652,10 @@ function buildOwnerPermissions(branchOwner, writeFunction, writeOwner, writeRule
};
}

function buildRulePermission(rule) {
return { [RuleProperties.WRITE]: rule };
}

module.exports = {
FeatureFlags,
CURRENT_PROTOCOL_VERSION,
Expand Down Expand Up @@ -700,6 +711,7 @@ module.exports = {
GenesisOwners,
GasFeeConstants,
buildOwnerPermissions,
buildRulePermission,
...GenesisParams.blockchain,
...GenesisParams.consensus,
...GenesisParams.resource,
Expand Down
92 changes: 92 additions & 0 deletions common/network-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const _ = require('lodash');
const axios = require('axios');
const logger = require('../logger')('NETWORK-UTIL');
const {
CURRENT_PROTOCOL_VERSION
} = require('../common/constants');
const ChainUtil = require('../common/chain-util');


async function _waitUntilTxFinalize(endpoint, txHash) {
while (true) {
const confirmed = await sendGetRequest(
endpoint,
'ain_getTransactionByHash',
{ hash: txHash }
)
.then((resp) => {
return (_.get(resp, 'data.result.result.is_finalized', false) === true);
})
.catch((err) => {
logger.error(`Failed to confirm transaction: ${err}`);
return false;
});
if (confirmed) {
return true;
}
await ChainUtil.sleep(1000);
}
}

async function sendTxAndWaitForFinalization(endpoint, tx, privateKey) {
const res = await signAndSendTx(endpoint, tx, privateKey);
if (_.get(res, 'errMsg', false) || !_.get(res, 'success', false)) {
throw Error(`Failed to sign and send tx: ${res.errMsg}`);
}
if (!(await _waitUntilTxFinalize(endpoint, _.get(res, 'txHash', null)))) {
throw Error('Transaction did not finalize in time.' +
'Try selecting a different parent_chain_poc.');
}
}

async function sendSignedTx(endpoint, params) {
return await axios.post(
endpoint,
{
method: 'ain_sendSignedTransaction',
params,
jsonrpc: '2.0',
id: 0
}
).then((resp) => {
const result = _.get(resp, 'data.result.result.result', {});
const success = !ChainUtil.isFailedTx(result);
return { success, errMsg: result.error_message };
}).catch((err) => {
logger.error(`Failed to send transaction: ${err}`);
return { success: false, errMsg: err.message };
});
}

// FIXME(minsulee2): this is duplicated function see: ./tools/util.js
async function signAndSendTx(endpoint, tx, privateKey) {
const { txHash, signedTx } = ChainUtil.signTransaction(tx, privateKey);
const result = await sendSignedTx(endpoint, signedTx);
return Object.assign(result, { txHash });
}

function sendGetRequest(endpoint, method, params) {
// NOTE(platfowner): .then() was used here to avoid some unexpected behavior of axios.post()
// (see https://github.com/ainblockchain/ain-blockchain/issues/101)
return axios.post(
endpoint,
{
method,
params: Object.assign(params, { protoVer: CURRENT_PROTOCOL_VERSION }),
jsonrpc: '2.0',
id: 0
}
).then((resp) => {
return resp;
}).catch((err) => {
logger.error(`Failed to send get request: ${err}`);
return null;
});
}

module.exports = {
sendTxAndWaitForFinalization,
sendSignedTx,
signAndSendTx,
sendGetRequest
};
4 changes: 4 additions & 0 deletions common/path-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class PathUtil {
return ChainUtil.formatPath([PredefinedDbPaths.MANAGE_APP, appName, PredefinedDbPaths.MANAGE_APP_CONFIG]);
}

static getAppPath(appName) {
return ChainUtil.formatPath([PredefinedDbPaths.APPS, appName]);
}

static getAppAdminPathFromServiceAccountName(accountName) {
return ruleUtil.getAppAdminPath(accountName);
}
Expand Down
8 changes: 8 additions & 0 deletions common/version-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const {
} = require('../common/constants');

class VersionUtil {
static isValidProtocolVersion(version) {
if (!version || !semver.valid(version)) {
return false;
} else {
return true;
}
}

static isValidVersionMatch(ver) {
return ver && semver.valid(semver.coerce(ver.min)) &&
(!ver.max || semver.valid(semver.coerce(ver.max)));
Expand Down
2 changes: 1 addition & 1 deletion consensus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
const {
signAndSendTx,
sendGetRequest
} = require('../p2p/util');
} = require('../common/network-util');
const PathUtil = require('../common/path-util');
const DB = require('../db');
const VersionUtil = require('../common/version-util');
Expand Down
Loading

0 comments on commit 8a85c7f

Please sign in to comment.