Skip to content

Commit df6ffd8

Browse files
authored
Merge pull request #623 from ainblockchain/release/v0.9.2
Merge Release/v0.9.2 into Master
2 parents 8042cda + a73d508 commit df6ffd8

File tree

113 files changed

+16104
-10450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+16104
-10450
lines changed

.github/workflows/github-actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
project_id: ${{ secrets.PERF_TEST_PIPELINE_GCP_PROJECT_ID }}
7979
- name: send test start message to gcp
8080
run: |-
81-
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 &" &
81+
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 &" &
8282
sleep 60
8383
check_deployment:
8484
if: github.event.pull_request.base.ref == 'master'

blockchain/block.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ const {
1616
GenesisRules,
1717
GenesisOwners,
1818
AccountProperties,
19-
ProofProperties,
19+
StateInfoProperties,
2020
StateVersions,
2121
} = require('../common/constants');
2222
const PathUtil = require('../common/path-util');
2323

2424
class Block {
25-
constructor(lastHash, lastVotes, evidence, transactions, number, epoch, timestamp,
25+
constructor(lastHash, lastVotes, evidence, transactions, receipts, number, epoch, timestamp,
2626
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal) {
2727
this.last_votes = lastVotes;
2828
this.evidence = evidence;
2929
this.transactions = Block.sanitizeTransactions(transactions);
30+
this.receipts = receipts;
3031
// Block's header
3132
this.last_hash = lastHash;
3233
this.last_votes_hash = CommonUtil.hashString(stringify(lastVotes));
3334
this.evidence_hash = CommonUtil.hashString(stringify(this.evidence));
3435
this.transactions_hash = CommonUtil.hashString(stringify(this.transactions));
36+
this.receipts_hash = CommonUtil.hashString(stringify(this.receipts));
3537
this.number = number;
3638
this.epoch = epoch;
3739
this.timestamp = timestamp;
@@ -50,6 +52,7 @@ class Block {
5052
last_hash: this.last_hash,
5153
last_votes_hash: this.last_votes_hash,
5254
transactions_hash: this.transactions_hash,
55+
receipts_hash: this.receipts_hash,
5356
evidence_hash: this.evidence_hash,
5457
number: this.number,
5558
epoch: this.epoch,
@@ -67,6 +70,7 @@ class Block {
6770
last_votes: this.last_votes,
6871
evidence: this.evidence,
6972
transactions: this.transactions,
73+
receipts: this.receipts,
7074
};
7175
}
7276

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

95-
static create(lastHash, lastVotes, evidence, transactions, number, epoch,
99+
static create(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
96100
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal, timestamp) {
97-
return new Block(lastHash, lastVotes, evidence, transactions, number, epoch,
101+
return new Block(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
98102
timestamp ? timestamp : Date.now(), stateProofHash, proposer, validators, gasAmountTotal,
99103
gasCostTotal);
100104
}
@@ -103,18 +107,18 @@ class Block {
103107
if (!Block.hasRequiredFields(blockInfo)) return null;
104108
if (blockInfo instanceof Block) return blockInfo;
105109
return new Block(blockInfo.last_hash, blockInfo.last_votes, blockInfo.evidence,
106-
blockInfo.transactions, blockInfo.number, blockInfo.epoch, blockInfo.timestamp,
107-
blockInfo.state_proof_hash, blockInfo.proposer, blockInfo.validators,
110+
blockInfo.transactions, blockInfo.receipts, blockInfo.number, blockInfo.epoch,
111+
blockInfo.timestamp, blockInfo.state_proof_hash, blockInfo.proposer, blockInfo.validators,
108112
blockInfo.gas_amount_total, blockInfo.gas_cost_total);
109113
}
110114

111115
static hasRequiredFields(block) {
112116
return (block && block.last_hash !== undefined && block.last_votes !== undefined &&
113117
block.evidence !== undefined && block.transactions !== undefined &&
114-
block.number !== undefined && block.epoch !== undefined && block.timestamp !== undefined &&
115-
block.state_proof_hash !== undefined && block.proposer !== undefined &&
116-
block.validators !== undefined && block.gas_amount_total !== undefined &&
117-
block.gas_cost_total !== undefined);
118+
block.receipts !== undefined && block.number !== undefined && block.epoch !== undefined &&
119+
block.timestamp !== undefined && block.state_proof_hash !== undefined &&
120+
block.proposer !== undefined && block.validators !== undefined &&
121+
block.gas_amount_total !== undefined && block.gas_cost_total !== undefined);
118122
}
119123

120124
static validateHashes(block) {
@@ -129,6 +133,11 @@ class Block {
129133
`[${LOG_HEADER}] Transactions or transactions_hash is incorrect for block ${block.hash}`);
130134
return false;
131135
}
136+
if (block.receipts_hash !== CommonUtil.hashString(stringify(block.receipts))) {
137+
logger.error(
138+
`[${LOG_HEADER}] Receipts or receipts_hash is incorrect for block ${block.hash}`);
139+
return false;
140+
}
132141
if (block.last_votes_hash !== CommonUtil.hashString(stringify(block.last_votes))) {
133142
logger.error(
134143
`[${LOG_HEADER}] Last votes or last_votes_hash is incorrect for block ${block.hash}`);
@@ -327,7 +336,7 @@ class Block {
327336

328337
static executeGenesisTxsAndGetData(genesisTxs, genesisTime) {
329338
const tempGenesisDb = new DB(
330-
new StateNode(StateVersions.EMPTY), StateVersions.EMPTY, null, null, false, -1, null);
339+
new StateNode(StateVersions.EMPTY), StateVersions.EMPTY, null, -1, null);
331340
tempGenesisDb.initDbStates();
332341
const resList = [];
333342
for (const tx of genesisTxs) {
@@ -341,9 +350,10 @@ class Block {
341350
}
342351
const { gasAmountTotal, gasCostTotal } = CommonUtil.getServiceGasCostTotalFromTxList(genesisTxs, resList);
343352
return {
344-
stateProofHash: tempGenesisDb.getStateProof('/')[ProofProperties.PROOF_HASH],
353+
stateProofHash: tempGenesisDb.getProofHash('/'),
345354
gasAmountTotal,
346-
gasCostTotal
355+
gasCostTotal,
356+
receipts: CommonUtil.txResultsToReceipts(resList),
347357
};
348358
}
349359

@@ -361,9 +371,10 @@ class Block {
361371
const epoch = 0;
362372
const proposer = ownerAddress;
363373
const validators = GENESIS_VALIDATORS;
364-
const { stateProofHash, gasAmountTotal, gasCostTotal } = Block.executeGenesisTxsAndGetData(transactions, genesisTime);
365-
return new Block(lastHash, lastVotes, evidence, transactions, number, epoch, genesisTime,
366-
stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal);
374+
const { stateProofHash, gasAmountTotal, gasCostTotal, receipts } =
375+
Block.executeGenesisTxsAndGetData(transactions, genesisTime);
376+
return new Block(lastHash, lastVotes, evidence, transactions, receipts, number, epoch,
377+
genesisTime, stateProofHash, proposer, validators, gasAmountTotal, gasCostTotal);
367378
}
368379
}
369380

blockchain/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const {
77
CHAINS_DIR,
88
CHAIN_SEGMENT_LENGTH,
99
ON_MEMORY_CHAIN_LENGTH,
10+
GenesisAccounts,
11+
AccountProperties,
1012
} = require('../common/constants');
13+
const { ConsensusConsts } = require('../consensus/constants');
1114
const CommonUtil = require('../common/common-util');
1215

1316
class Blockchain {
@@ -16,6 +19,9 @@ class Blockchain {
1619
this.chain = [];
1720
this.blockchainPath = path.resolve(CHAINS_DIR, basePath);
1821
this.initSnapshotBlockNumber = -1;
22+
23+
// Mapping of a block number to the finalized block's info
24+
this.numberToBlockInfo = {};
1925
}
2026

2127
/**
@@ -98,6 +104,13 @@ class Blockchain {
98104
}
99105
}
100106

107+
getBlockInfoByNumber(number) {
108+
if (number === undefined || number === null) return null;
109+
const blockNumber = CommonUtil.toNumberOrNaN(number);
110+
if (!CommonUtil.isNumber(blockNumber)) return null;
111+
return this.numberToBlockInfo[blockNumber];
112+
}
113+
101114
lastBlock() {
102115
if (this.chain.length === 0) {
103116
return null;
@@ -127,7 +140,7 @@ class Blockchain {
127140
lastBlockTimestamp() {
128141
const lastBlock = this.lastBlock();
129142
if (!lastBlock) {
130-
return -1;
143+
return GenesisAccounts[AccountProperties.TIMESTAMP];
131144
}
132145
return lastBlock.timestamp;
133146
}
@@ -139,6 +152,15 @@ class Blockchain {
139152
logger.info(`[${LOG_HEADER}] Successfully added block ${block.number} to chain.`);
140153
}
141154

155+
updateNumberToBlockInfo(block) {
156+
this.numberToBlockInfo[block.number] = {
157+
finalized_at: Date.now()
158+
};
159+
if (block.number >= ConsensusConsts.MAX_FINALIZED_BLOCK_INFO_ON_MEM) {
160+
delete this.numberToBlockInfo[block.number - ConsensusConsts.MAX_FINALIZED_BLOCK_INFO_ON_MEM];
161+
}
162+
}
163+
142164
addNewBlockToChain(newBlock) {
143165
const LOG_HEADER = 'addNewBlockToChain';
144166

@@ -154,6 +176,7 @@ class Blockchain {
154176
newBlock = Block.parse(newBlock);
155177
}
156178
this.addBlockToChain(newBlock);
179+
this.updateNumberToBlockInfo(newBlock);
157180
this.writeBlock(newBlock);
158181
// Keep up to latest ON_MEMORY_CHAIN_LENGTH blocks
159182
while (this.chain.length > ON_MEMORY_CHAIN_LENGTH) {

0 commit comments

Comments
 (0)