Skip to content

Commit

Permalink
Merge pull request #623 from ainblockchain/release/v0.9.2
Browse files Browse the repository at this point in the history
Merge Release/v0.9.2 into Master
  • Loading branch information
minsulee2 committed Oct 20, 2021
2 parents 8042cda + a73d508 commit df6ffd8
Show file tree
Hide file tree
Showing 113 changed files with 16,104 additions and 10,450 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
project_id: ${{ secrets.PERF_TEST_PIPELINE_GCP_PROJECT_ID }}
- name: send test start message to gcp
run: |-
gcloud compute ssh "${{ secrets.PERF_TEST_PIPELINE_GCE_INSTANCE }}" --zone "${{ secrets.PERF_TEST_PIPELINE_GCE_INSTANCE_ZONE }}" -- "cd ~/../workspace/testnet-performance-test-pipeline && nohup node start_performance_test.js ${{ github.event.pull_request.head.ref }} >> test_log.txt 2>&1 &" &
gcloud compute ssh "${{ secrets.PERF_TEST_PIPELINE_GCE_INSTANCE }}" --zone "${{ secrets.PERF_TEST_PIPELINE_GCE_INSTANCE_ZONE }}" -- "cd ~/../workspace/testnet-performance-test-pipeline && nohup node start_performance_test.js ${{ secrets.PERF_TEST_PIPELINE_TEST_BRANCH }} ${{ github.event.pull_request.head.ref }} >> test_log.txt 2>&1 &" &
sleep 60
check_deployment:
if: github.event.pull_request.base.ref == 'master'
Expand Down
43 changes: 27 additions & 16 deletions blockchain/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ const {
GenesisRules,
GenesisOwners,
AccountProperties,
ProofProperties,
StateInfoProperties,
StateVersions,
} = require('../common/constants');
const PathUtil = require('../common/path-util');

class Block {
constructor(lastHash, lastVotes, evidence, transactions, number, epoch, timestamp,
constructor(lastHash, lastVotes, evidence, transactions, receipts, number, epoch, timestamp,
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal) {
this.last_votes = lastVotes;
this.evidence = evidence;
this.transactions = Block.sanitizeTransactions(transactions);
this.receipts = receipts;
// Block's header
this.last_hash = lastHash;
this.last_votes_hash = CommonUtil.hashString(stringify(lastVotes));
this.evidence_hash = CommonUtil.hashString(stringify(this.evidence));
this.transactions_hash = CommonUtil.hashString(stringify(this.transactions));
this.receipts_hash = CommonUtil.hashString(stringify(this.receipts));
this.number = number;
this.epoch = epoch;
this.timestamp = timestamp;
Expand All @@ -50,6 +52,7 @@ class Block {
last_hash: this.last_hash,
last_votes_hash: this.last_votes_hash,
transactions_hash: this.transactions_hash,
receipts_hash: this.receipts_hash,
evidence_hash: this.evidence_hash,
number: this.number,
epoch: this.epoch,
Expand All @@ -67,6 +70,7 @@ class Block {
last_votes: this.last_votes,
evidence: this.evidence,
transactions: this.transactions,
receipts: this.receipts,
};
}

Expand All @@ -92,9 +96,9 @@ class Block {
return sizeof({...block.header, ...block.body});
}

static create(lastHash, lastVotes, evidence, transactions, number, epoch,
static create(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal, timestamp) {
return new Block(lastHash, lastVotes, evidence, transactions, number, epoch,
return new Block(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
timestamp ? timestamp : Date.now(), stateProofHash, proposer, validators, gasAmountTotal,
gasCostTotal);
}
Expand All @@ -103,18 +107,18 @@ class Block {
if (!Block.hasRequiredFields(blockInfo)) return null;
if (blockInfo instanceof Block) return blockInfo;
return new Block(blockInfo.last_hash, blockInfo.last_votes, blockInfo.evidence,
blockInfo.transactions, blockInfo.number, blockInfo.epoch, blockInfo.timestamp,
blockInfo.state_proof_hash, blockInfo.proposer, blockInfo.validators,
blockInfo.transactions, blockInfo.receipts, blockInfo.number, blockInfo.epoch,
blockInfo.timestamp, blockInfo.state_proof_hash, blockInfo.proposer, blockInfo.validators,
blockInfo.gas_amount_total, blockInfo.gas_cost_total);
}

static hasRequiredFields(block) {
return (block && block.last_hash !== undefined && block.last_votes !== undefined &&
block.evidence !== undefined && block.transactions !== undefined &&
block.number !== undefined && block.epoch !== undefined && block.timestamp !== undefined &&
block.state_proof_hash !== undefined && block.proposer !== undefined &&
block.validators !== undefined && block.gas_amount_total !== undefined &&
block.gas_cost_total !== undefined);
block.receipts !== undefined && block.number !== undefined && block.epoch !== undefined &&
block.timestamp !== undefined && block.state_proof_hash !== undefined &&
block.proposer !== undefined && block.validators !== undefined &&
block.gas_amount_total !== undefined && block.gas_cost_total !== undefined);
}

static validateHashes(block) {
Expand All @@ -129,6 +133,11 @@ class Block {
`[${LOG_HEADER}] Transactions or transactions_hash is incorrect for block ${block.hash}`);
return false;
}
if (block.receipts_hash !== CommonUtil.hashString(stringify(block.receipts))) {
logger.error(
`[${LOG_HEADER}] Receipts or receipts_hash is incorrect for block ${block.hash}`);
return false;
}
if (block.last_votes_hash !== CommonUtil.hashString(stringify(block.last_votes))) {
logger.error(
`[${LOG_HEADER}] Last votes or last_votes_hash is incorrect for block ${block.hash}`);
Expand Down Expand Up @@ -327,7 +336,7 @@ class Block {

static executeGenesisTxsAndGetData(genesisTxs, genesisTime) {
const tempGenesisDb = new DB(
new StateNode(StateVersions.EMPTY), StateVersions.EMPTY, null, null, false, -1, null);
new StateNode(StateVersions.EMPTY), StateVersions.EMPTY, null, -1, null);
tempGenesisDb.initDbStates();
const resList = [];
for (const tx of genesisTxs) {
Expand All @@ -341,9 +350,10 @@ class Block {
}
const { gasAmountTotal, gasCostTotal } = CommonUtil.getServiceGasCostTotalFromTxList(genesisTxs, resList);
return {
stateProofHash: tempGenesisDb.getStateProof('/')[ProofProperties.PROOF_HASH],
stateProofHash: tempGenesisDb.getProofHash('/'),
gasAmountTotal,
gasCostTotal
gasCostTotal,
receipts: CommonUtil.txResultsToReceipts(resList),
};
}

Expand All @@ -361,9 +371,10 @@ class Block {
const epoch = 0;
const proposer = ownerAddress;
const validators = GENESIS_VALIDATORS;
const { stateProofHash, gasAmountTotal, gasCostTotal } = Block.executeGenesisTxsAndGetData(transactions, genesisTime);
return new Block(lastHash, lastVotes, evidence, transactions, number, epoch, genesisTime,
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal);
const { stateProofHash, gasAmountTotal, gasCostTotal, receipts } =
Block.executeGenesisTxsAndGetData(transactions, genesisTime);
return new Block(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
genesisTime, stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal);
}
}

Expand Down
25 changes: 24 additions & 1 deletion blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const {
CHAINS_DIR,
CHAIN_SEGMENT_LENGTH,
ON_MEMORY_CHAIN_LENGTH,
GenesisAccounts,
AccountProperties,
} = require('../common/constants');
const { ConsensusConsts } = require('../consensus/constants');
const CommonUtil = require('../common/common-util');

class Blockchain {
Expand All @@ -16,6 +19,9 @@ class Blockchain {
this.chain = [];
this.blockchainPath = path.resolve(CHAINS_DIR, basePath);
this.initSnapshotBlockNumber = -1;

// Mapping of a block number to the finalized block's info
this.numberToBlockInfo = {};
}

/**
Expand Down Expand Up @@ -98,6 +104,13 @@ class Blockchain {
}
}

getBlockInfoByNumber(number) {
if (number === undefined || number === null) return null;
const blockNumber = CommonUtil.toNumberOrNaN(number);
if (!CommonUtil.isNumber(blockNumber)) return null;
return this.numberToBlockInfo[blockNumber];
}

lastBlock() {
if (this.chain.length === 0) {
return null;
Expand Down Expand Up @@ -127,7 +140,7 @@ class Blockchain {
lastBlockTimestamp() {
const lastBlock = this.lastBlock();
if (!lastBlock) {
return -1;
return GenesisAccounts[AccountProperties.TIMESTAMP];
}
return lastBlock.timestamp;
}
Expand All @@ -139,6 +152,15 @@ class Blockchain {
logger.info(`[${LOG_HEADER}] Successfully added block ${block.number} to chain.`);
}

updateNumberToBlockInfo(block) {
this.numberToBlockInfo[block.number] = {
finalized_at: Date.now()
};
if (block.number >= ConsensusConsts.MAX_FINALIZED_BLOCK_INFO_ON_MEM) {
delete this.numberToBlockInfo[block.number - ConsensusConsts.MAX_FINALIZED_BLOCK_INFO_ON_MEM];
}
}

addNewBlockToChain(newBlock) {
const LOG_HEADER = 'addNewBlockToChain';

Expand All @@ -154,6 +176,7 @@ class Blockchain {
newBlock = Block.parse(newBlock);
}
this.addBlockToChain(newBlock);
this.updateNumberToBlockInfo(newBlock);
this.writeBlock(newBlock);
// Keep up to latest ON_MEMORY_CHAIN_LENGTH blocks
while (this.chain.length > ON_MEMORY_CHAIN_LENGTH) {
Expand Down
Loading

0 comments on commit df6ffd8

Please sign in to comment.