Skip to content

Commit

Permalink
Merge pull request #638 from ainblockchain/develop
Browse files Browse the repository at this point in the history
Merge toJsObject updates into release/v0.9.2
  • Loading branch information
minsulee2 authored Oct 20, 2021
2 parents 5d805b0 + 1e6681d commit a73d508
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 54 deletions.
4 changes: 3 additions & 1 deletion db/state-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class StateNode {
const obj = {};
for (const label of this.getChildLabels()) {
const childNode = this.getChild(label);
obj[label] = isShallow ? true : childNode.toJsObject(options);
obj[label] = (isShallow && !childNode.getIsLeaf()) ?
{ [`${StateInfoProperties.STATE_PROOF_HASH}`]: childNode.getProofHash() } :
childNode.toJsObject(options);
if (childNode.getIsLeaf()) {
if (includeVersion) {
obj[`${StateInfoProperties.VERSION}:${label}`] = childNode.getVersion();
Expand Down
2 changes: 1 addition & 1 deletion deploy_blockchain_genesis_gcp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function inject_account() {
}

FILES_FOR_TRACKER="blockchain/ client/ common/ consensus/ db/ genesis-configs/ logger/ tracker-server/ traffic/ package.json setup_blockchain_ubuntu.sh start_tracker_genesis_gcp.sh start_tracker_incremental_gcp.sh restart_tracker_gcp.sh"
FILES_FOR_NODE="blockchain/ client/ common/ consensus/ db/ genesis-configs/ json_rpc/ logger/ node/ p2p/ traffic/ tx-pool/ package.json setup_blockchain_ubuntu.sh start_node_genesis_gcp.sh start_node_incremental_gcp.sh restart_node_gcp.sh wait_until_node_sync_gcp.sh $KEYSTORE_DIR"
FILES_FOR_NODE="blockchain/ client/ common/ consensus/ db/ genesis-configs/ json_rpc/ logger/ node/ p2p/ tools/ traffic/ tx-pool/ package.json setup_blockchain_ubuntu.sh start_node_genesis_gcp.sh start_node_incremental_gcp.sh restart_node_gcp.sh wait_until_node_sync_gcp.sh $KEYSTORE_DIR"

TRACKER_TARGET_ADDR="${GCP_USER}@${SEASON}-tracker-taiwan"
NODE_0_TARGET_ADDR="${GCP_USER}@${SEASON}-node-0-taiwan"
Expand Down
2 changes: 1 addition & 1 deletion deploy_blockchain_incremental_gcp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ if [[ "$KEYSTORE_OPTION" != "" ]]; then
fi

FILES_FOR_TRACKER="blockchain/ client/ common/ consensus/ db/ genesis-configs/ logger/ tracker-server/ traffic/ package.json setup_blockchain_ubuntu.sh start_tracker_genesis_gcp.sh start_tracker_incremental_gcp.sh restart_tracker_gcp.sh"
FILES_FOR_NODE="blockchain/ client/ common/ consensus/ db/ genesis-configs/ json_rpc/ logger/ node/ p2p/ traffic/ tx-pool/ package.json setup_blockchain_ubuntu.sh start_node_genesis_gcp.sh start_node_incremental_gcp.sh restart_node_gcp.sh wait_until_node_sync_gcp.sh $KEYSTORE_DIR"
FILES_FOR_NODE="blockchain/ client/ common/ consensus/ db/ genesis-configs/ json_rpc/ logger/ node/ p2p/ tools/ traffic/ tx-pool/ package.json setup_blockchain_ubuntu.sh start_node_genesis_gcp.sh start_node_incremental_gcp.sh restart_node_gcp.sh wait_until_node_sync_gcp.sh $KEYSTORE_DIR"

NUM_PARENT_NODES=5
NUM_SHARD_NODES=3
Expand Down
4 changes: 2 additions & 2 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BlockchainNode {
logger.error(`[${LOG_HEADER}] ${err.stack}`);
}
}
logger.info(`[${LOG_HEADER}] Fast mode sync done!`);
logger.info(`[${LOG_HEADER}] Fast mode DB snapshot loading done!`);
} else {
logger.info(`[${LOG_HEADER}] Initializing node in 'full' mode..`);
}
Expand Down Expand Up @@ -259,7 +259,7 @@ class BlockchainNode {
}

updateSnapshots(blockNumber) {
if (blockNumber > 0 && blockNumber % SNAPSHOTS_INTERVAL_BLOCK_NUMBER === 0) {
if (blockNumber % SNAPSHOTS_INTERVAL_BLOCK_NUMBER === 0) {
const snapshot = this.dumpFinalDbStates();
FileUtil.writeSnapshot(this.snapshotDir, blockNumber, snapshot);
FileUtil.writeSnapshot(
Expand Down
83 changes: 47 additions & 36 deletions tools/proof-hash/verifyBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const Blockchain = require('../../blockchain');
const StateManager = require('../../db/state-manager');
const DB = require('../../db');

async function verifyBlock(snapshotFile, blockFile) {
async function verifyBlock(snapshotFile, blockFileList) {
console.log(`\n<< [0]: ${snapshotFile} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n`);
console.log(`* Reading snapshot file: ${snapshotFile}...`);
const snapshot = FileUtil.isCompressedFile(snapshotFile) ?
FileUtil.readCompressedJson(snapshotFile) : FileUtil.readJson(snapshotFile);
Expand All @@ -14,60 +15,70 @@ async function verifyBlock(snapshotFile, blockFile) {
process.exit(0)
}
console.log(` Done.`);
console.log(`* Reading block file: ${blockFile}...`);
const block = FileUtil.isCompressedFile(blockFile) ?
FileUtil.readCompressedJson(blockFile) : FileUtil.readJson(blockFile);
if (block === null) {
console.log(` Failed to read block file: ${blockFile}`);
process.exit(0);
}
console.log(` Done.`);

console.log(`* Initializing db states with snapshot...`);
const bc = new Blockchain(String(8888));
const stateManager = new StateManager();
const db = DB.create(
StateVersions.EMPTY, 'verifyBlock', bc, false, bc.lastBlockNumber(), stateManager);
console.log(`* Initializing db states with snapshot...`);
db.initDbStates(snapshot);
console.log(` Done.`);
console.log(`* Executing block on db...`);
if (block.number > 0) {
if (!db.executeTransactionList(block.last_votes, true, false, block.number, block.timestamp)) {
logger.error(` Failed to execute last_votes (${block.number})`);

for (let i = 0; i < blockFileList.length; i++) {
const blockFile = blockFileList[i];
console.log(`\n<< [${i + 1}]: ${blockFile} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n`);
console.log(`* Reading block file: ${blockFile}...`);
const block = FileUtil.isCompressedFile(blockFile) ?
FileUtil.readCompressedJson(blockFile) : FileUtil.readJson(blockFile);
if (block === null) {
console.log(` Failed to read block file: ${blockFile}`);
process.exit(0);
}
console.log(` Done.`);
console.log(`* Executing block on db...`);
if (block.number > 0) {
if (!db.executeTransactionList(block.last_votes, true, false, block.number, block.timestamp)) {
logger.error(` Failed to execute last_votes (${block.number})`);
process.exit(0);
}
}
if (!db.executeTransactionList(block.transactions, block.number === 0, false, block.number, block.timestamp)) {
logger.error(` Failed to execute transactions (${block.number})`)
process.exit(0);
}
console.log(` Done.`);
console.log(`* Comparing root proof hashes...`);
console.log(` > Root proof hash from block header: ${block.state_proof_hash}`);
console.log(` > Root proof hash from recomputation: ${db.stateRoot.getProofHash()}`);
if (db.stateRoot.getProofHash() === block.state_proof_hash) {
console.log(` *************`);
console.log(` * VERIFIED! *`);
console.log(` *************`);
} else {
console.log(` *****************`);
console.log(` * NOT-VERIFIED! *`);
console.log(` *****************`);
console.log(` Halting verification...`);
break;
}
console.log(` Done.`);
}
if (!db.executeTransactionList(block.transactions, block.number === 0, false, block.number, block.timestamp)) {
logger.error(` Failed to execute transactions (${block.number})`)
process.exit(0);
}
console.log(` Done.`);
console.log(`* Comparing root proof hashes...`);
console.log(` > Root proof hash from block header: ${block.state_proof_hash}`);
console.log(` > Root proof hash from recomputation: ${db.stateRoot.getProofHash()}`);
if (db.stateRoot.getProofHash() === block.state_proof_hash) {
console.log(` *************`);
console.log(` * VERIFIED! *`);
console.log(` *************`);
} else {
console.log(` *****************`);
console.log(` * NOT-VERIFIED! *`);
console.log(` *****************`);
}
console.log(` Done.`);
}

async function processArguments() {
if (process.argv.length !== 4) {
if (process.argv.length < 4) {
usage();
}
await verifyBlock(process.argv[2], process.argv[3]);
await verifyBlock(process.argv[2], process.argv.slice(3));
}

function usage() {
console.log('\nUsage:\n node verifyBlock.js <snapshot file> <block file>\n')
console.log('\nUsage:\n node verifyBlock.js <snapshot file> <block file 1> [<block file 2> ... <block file k>\n')
console.log('Examples:')
console.log(' node verifyBlock.js samples/snapshot-100.json samples/block-101.json')
console.log(' node verifyBlock.js samples/snapshot-100.json.gz samples/block-101.json.gz\n')
console.log(' node verifyBlock.js samples/snapshot-100.json.gz samples/block-101.json.gz')
console.log(' node verifyBlock.js samples/snapshot-100.json samples/block-101.json samples/block-102.json')
console.log(' node verifyBlock.js samples/snapshot-100.json.gz samples/block-101.json.gz samples/block-102.json.gz\n')
process.exit(0)
}

Expand Down
32 changes: 24 additions & 8 deletions unittest/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,21 @@ describe("DB operations", () => {

it('when retrieving value near top of database with is_shallow', () => {
assert.deepEqual(node.db.getValue('/apps/test', { isShallow: true }), {
'ai': true,
'increment': true,
'decrement': true,
'nested': true,
'shards': true,
'ai': {
"#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec"
},
'increment': {
"#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8"
},
'decrement': {
"#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8"
},
'nested': {
"#state_ph": "0x8763e301c728729e38c1f5500a2af7163783bdf0948a7baf7bc87b35f33b347f"
},
'shards': {
"#state_ph": "0xbe0fbf9fec28b21de391ebb202517a420f47ee199aece85153e8fb4d9453f223"
},
})
});

Expand Down Expand Up @@ -584,7 +594,9 @@ describe("DB operations", () => {

it("when retrieving existing function config with is_shallow", () => {
assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isShallow: true }), {
some: true,
some: {
"#state_ph": "0x14df539ce39f11f6f049adf3013eae1197a71a4ce0bdbfd66d3f8adb9d97f61c"
},
});
})
})
Expand Down Expand Up @@ -612,7 +624,9 @@ describe("DB operations", () => {

it('when retrieving existing rule config with is_shallow', () => {
assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isShallow: true }), {
some: true,
some: {
"#state_ph": "0x65d1d444e7f35a54ae9c196d83fda0ffbf93f91341a2470b83d0d512419aaf28"
},
});
});
})
Expand Down Expand Up @@ -665,7 +679,9 @@ describe("DB operations", () => {

it("when retrieving existing owner config with is_shallow", () => {
assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isShallow: true }), {
some: true,
some: {
"#state_ph": "0x5086ccf28e98a15e4d1de16b1f78e3b429e3049baeb39ea22041d75dd16f5800"
},
})
})
})
Expand Down
12 changes: 7 additions & 5 deletions unittest/state-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ describe("state-node", () => {
it("internal node", () => {
assert.deepEqual(StateNode.fromJsObject({ a: 1, b: 2, c: 3 }).toJsObject({ isShallow: true }),
{
a: true,
b: true,
c: true,
a: 1,
b: 2,
c: 3,
},
);
assert.deepEqual(StateNode.fromJsObject({ a: { aa: 11 }, b: 2 }).toJsObject({ isShallow: true }),
{
a: true,
b: true,
a: {
"#state_ph": null
},
b: 2,
},
);
})
Expand Down

0 comments on commit a73d508

Please sign in to comment.