Skip to content

Commit 5cc4a47

Browse files
authored
Merge pull request #152 from ainblockchain/develop
Release 0.4.0
2 parents e3b0505 + 884569e commit 5cc4a47

37 files changed

+1825
-2538
lines changed

blockchain/block.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ainUtil = require('@ainblockchain/ain-util');
55
const logger = require('../logger')('BLOCK');
66
const ChainUtil = require('../chain-util');
77
const Transaction = require('../tx-pool/transaction');
8+
const StateNode = require('../db/state-node');
89
const DB = require('../db');
910
const {
1011
PredefinedDbPaths,
@@ -16,6 +17,7 @@ const {
1617
GenesisOwners,
1718
AccountProperties,
1819
ProofProperties,
20+
StateVersions,
1921
} = require('../constants');
2022
const BlockFilePatterns = require('./block-file-patterns');
2123

@@ -243,18 +245,20 @@ class Block {
243245
}
244246

245247
static getGenesisStateProofHash() {
246-
const tempGenesisState = new DB(null, null, false, -1);
248+
const tempGenesisDb =
249+
new DB(new StateNode(StateVersions.EMPTY), StateVersions.EMPTY, null, null, false, -1);
250+
tempGenesisDb.initDbStates();
247251
const genesisTransactions = Block.getGenesisBlockData(
248252
GenesisAccounts[AccountProperties.TIMESTAMP]);
249253
for (const tx of genesisTransactions) {
250-
const res = tempGenesisState.executeTransaction(tx);
254+
const res = tempGenesisDb.executeTransaction(tx);
251255
if (ChainUtil.transactionFailed(res)) {
252256
logger.error(`Genesis transaction failed:\n${JSON.stringify(tx, null, 2)}` +
253257
`\nRESULT: ${JSON.stringify(res)}`)
254258
return null;
255259
}
256260
}
257-
return tempGenesisState.getProof('/')[ProofProperties.PROOF_HASH];
261+
return tempGenesisDb.getProof('/')[ProofProperties.PROOF_HASH];
258262
}
259263

260264
static genesis() {

blockchain/index.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Blockchain {
1616
// Finalized chain
1717
this.chain = [];
1818
this.blockchainDir = blockchainDir;
19-
this.backupDb = null;
2019
this.syncedAfterStartup = false;
2120
}
2221

@@ -101,13 +100,6 @@ class Blockchain {
101100
}
102101
}
103102

104-
setBackupDb(backupDb) {
105-
if (this.backupDb !== null) {
106-
throw Error('Already set backupdb');
107-
}
108-
this.backupDb = backupDb;
109-
}
110-
111103
lastBlock() {
112104
if (this.chain.length === 0) {
113105
return null;
@@ -139,7 +131,7 @@ class Blockchain {
139131
return lastBlock.timestamp;
140132
}
141133

142-
addNewBlockToChain(newBlock) {
134+
addNewBlockToChain(newBlock, db) {
143135
if (!newBlock) {
144136
logger.error('[blockchain.addNewBlockToChain] Block is null');
145137
return false;
@@ -151,15 +143,17 @@ class Blockchain {
151143
if (!(newBlock instanceof Block)) {
152144
newBlock = Block.parse(newBlock);
153145
}
154-
if (!this.backupDb.executeTransactionList(newBlock.last_votes)) {
155-
logger.error('[blockchain.addNewBlockToChain] Failed to execute last_votes of block' +
156-
`${JSON.stringify(newBlock, null, 2)}`);
157-
return false;
158-
}
159-
if (!this.backupDb.executeTransactionList(newBlock.transactions)) {
160-
logger.error('[blockchain.addNewBlockToChain] Failed to execute transactions of block' +
161-
`${JSON.stringify(newBlock, null, 2)}`);
162-
return false;
146+
if (db) {
147+
if (!db.executeTransactionList(newBlock.last_votes)) {
148+
logger.error('[blockchain.addNewBlockToChain] Failed to execute last_votes of block' +
149+
`${JSON.stringify(newBlock, null, 2)}`);
150+
return false;
151+
}
152+
if (!db.executeTransactionList(newBlock.transactions)) {
153+
logger.error('[blockchain.addNewBlockToChain] Failed to execute transactions of block' +
154+
`${JSON.stringify(newBlock, null, 2)}`);
155+
return false;
156+
}
163157
}
164158
this.chain.push(newBlock);
165159
this.writeChain();
@@ -266,7 +260,7 @@ class Blockchain {
266260
return chainSubSection.length > 0 ? chainSubSection : [];
267261
}
268262

269-
merge(chainSubSection) {
263+
merge(chainSubSection, db) {
270264
// Call to shift here is important as it removes the first element from the list !!
271265
logger.info(`Last block number before merge: ${this.lastBlockNumber()}`);
272266
if (!chainSubSection || chainSubSection.length === 0) {
@@ -323,7 +317,7 @@ class Blockchain {
323317
continue;
324318
}
325319
// TODO(lia): validate the state proof of each block
326-
if (!this.addNewBlockToChain(block)) {
320+
if (!this.addNewBlockToChain(block, db)) {
327321
logger.error(`Failed to add block ` + block);
328322
return false;
329323
}

chain-util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class ChainUtil {
5656
return ruleUtil.isDict(value);
5757
}
5858

59-
static isEmptyNode(value) {
60-
return ruleUtil.isEmptyNode(value);
59+
static isEmpty(value) {
60+
return ruleUtil.isEmpty(value);
6161
}
6262

6363
static isValAddr(value) {

client/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ app.get('/pending_nonce_tracker', (req, res, next) => {
290290
.end();
291291
});
292292

293+
app.get('/state_versions', (req, res) => {
294+
const result = {
295+
version_list: node.stateManager.getVersionList(),
296+
finalized_version: node.stateManager.getFinalizedVersion(),
297+
};
298+
res.status(200)
299+
.set('Content-Type', 'application/json')
300+
.send({code: 0, result})
301+
.end();
302+
});
303+
293304
app.get('/get_transaction', (req, res, next) => {
294305
const transactionInfo = node.tp.transactionTracker[req.query.hash];
295306
if (transactionInfo) {

client/protocol_versions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
},
55
"0.3.3": {
66
"min": "0.3.2"
7+
},
8+
"0.4.0": {
9+
"min": "0.3.2"
710
}
811
}

consensus/block-pool.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BlockPool {
1313
// e.g. { block_hash: { block, proposal, votes: { address: stake } } }
1414
this.hashToBlockInfo = {};
1515
// Mapping of a block hash to the new db state
16-
this.hashToState = new Map();
16+
this.hashToDb = new Map();
1717
// Mapping of a block hash to a set of block hashes that extend the block.
1818
// e.g. { block_hash: Set<block_hash> }
1919
this.hashToNextBlockSet = {};
@@ -240,7 +240,7 @@ class BlockPool {
240240
this.hashToBlockInfo[blockHash] = {};
241241
}
242242
const blockInfo = this.hashToBlockInfo[blockHash];
243-
if (ChainUtil.isEmptyNode(blockInfo.block)) {
243+
if (ChainUtil.isEmpty(blockInfo.block)) {
244244
this.hashToBlockInfo[blockHash].block = block;
245245
this.hashToBlockInfo[blockHash].proposal = proposalTx;
246246
// We might have received some votes before the block itself
@@ -372,7 +372,11 @@ class BlockPool {
372372
delete this.hashToBlockInfo[blockHash];
373373
delete this.numberToBlock[number];
374374
delete this.hashToNextBlockSet[blockHash];
375-
this.hashToState.delete(blockHash);
375+
if (this.hashToDb.has(blockHash)) {
376+
const db = this.hashToDb.get(blockHash);
377+
this.node.destroyDb(db);
378+
this.hashToDb.delete(blockHash);
379+
}
376380
}
377381
});
378382
Object.keys(this.numberToBlock).forEach((key) => {

0 commit comments

Comments
 (0)