Skip to content

Commit

Permalink
Merge pull request #878 from ainblockchain/feature/liayoo/obj-to-map
Browse files Browse the repository at this point in the history
Change objects to Maps
  • Loading branch information
liayoo authored Jan 4, 2022
2 parents 497bc91 + f7618ff commit 9c4eaa4
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 207 deletions.
213 changes: 110 additions & 103 deletions block-pool/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ app.get('/last_block', (req, res, next) => {

app.get('/tx_pool', (req, res, next) => {
const beginTime = Date.now();
const result = node.tp.transactions;
const result = Object.fromEntries(node.tp.transactions);
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.CLIENT_API_GET, latency);
res.status(200)
Expand All @@ -429,7 +429,7 @@ app.get('/tx_pool', (req, res, next) => {

app.get('/tx_tracker', (req, res, next) => {
const beginTime = Date.now();
const result = node.tp.transactionTracker;
const result = Object.fromEntries(node.tp.transactionTracker);
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.CLIENT_API_GET, latency);
res.status(200)
Expand Down
44 changes: 23 additions & 21 deletions consensus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class Consensus {
logger.error(`[${LOG_HEADER}] Proposal is missing required fields: ${msg.value}`);
return;
}
if (this.node.tp.transactionTracker[proposalTx.hash]) {
if (this.node.tp.transactionTracker.has(proposalTx.hash)) {
logger.debug(`[${LOG_HEADER}] Already have the proposal in my tx tracker`);
return;
}
Expand Down Expand Up @@ -322,7 +322,7 @@ class Consensus {
this.server.client.broadcastConsensusMessage(msg, tags);
this.tryVoteForValidBlock(proposalBlock);
} else if (msg.type === ConsensusMessageTypes.VOTE) {
if (this.node.tp.transactionTracker[msg.value.hash]) {
if (this.node.tp.transactionTracker.has(msg.value.hash)) {
logger.debug(`[${LOG_HEADER}] Already have the vote in my tx tracker`);
return;
}
Expand Down Expand Up @@ -508,7 +508,7 @@ class Consensus {
static getPrevBlockInfo(number, lastHash, lastFinalizedBlock, bp) {
if (number === 0) return { block: null };
const prevBlockInfo = lastFinalizedBlock && number === lastFinalizedBlock.number + 1 ?
{ block: lastFinalizedBlock } : bp.hashToBlockInfo[lastHash];
{ block: lastFinalizedBlock } : bp.hashToBlockInfo.get(lastHash);
if (!prevBlockInfo || !prevBlockInfo.block) {
throw new ConsensusError({
code: ConsensusErrorCode.MISSING_PREV_BLOCK,
Expand Down Expand Up @@ -630,17 +630,18 @@ class Consensus {
for (const vote of lastVotes) {
blockPool.addSeenVote(vote);
}
if (!blockPool.hashToBlockInfo[lastHash] || !blockPool.hashToBlockInfo[lastHash].notarized) {
const lastBlockInfo = blockPool.hashToBlockInfo.get(lastHash);
if (!lastBlockInfo || !lastBlockInfo.notarized) {
throw new ConsensusError({
code: ConsensusErrorCode.INVALID_LAST_VOTES_STAKES,
message: `Block's last_votes don't correctly notarize its previous block of number ` +
`${number - 1} with hash ${lastHash}:\n` +
`${JSON.stringify(blockPool.hashToBlockInfo[lastHash], null, 2)}`,
`${JSON.stringify(lastBlockInfo, null, 2)}`,
level: 'error'
});
}
const prevBlockProposal = ConsensusUtil.filterProposalFromVotes(lastVotes);
if (number > 1 && (!prevBlockProposal || !blockPool.hashToBlockInfo[lastHash].proposal)) { // No proposalTx for the genesis block.
if (number > 1 && (!prevBlockProposal || !lastBlockInfo.proposal)) { // No proposalTx for the genesis block.
throw new ConsensusError({
code: ConsensusErrorCode.MISSING_PROPOSAL_IN_LAST_VOTES,
message: `Proposal block is missing its prev block's proposal in last_votes`,
Expand Down Expand Up @@ -887,8 +888,8 @@ class Consensus {
const blockHash = ConsensusUtil.getBlockHashFromConsensusTx(voteTx);
const isAgainst = ConsensusUtil.isAgainstVoteTx(voteTx);
const voteTimestamp = ConsensusUtil.getTimestampFromVoteTx(voteTx);
const blockInfo = this.node.bp.hashToBlockInfo[blockHash] ||
this.node.bp.hashToInvalidBlockInfo[blockHash];
const blockInfo = this.node.bp.hashToBlockInfo.get(blockHash) ||
this.node.bp.hashToInvalidBlockInfo.get(blockHash);
let block;
if (blockInfo && blockInfo.block) {
block = blockInfo.block;
Expand Down Expand Up @@ -953,7 +954,7 @@ class Consensus {
const epoch = this.epoch;
if (this.votedForEpoch(epoch)) {
logger.debug(
`[${LOG_HEADER}] Already voted for ${this.node.bp.epochToBlock[epoch]} ` +
`[${LOG_HEADER}] Already voted for ${this.node.bp.epochToBlock.get(epoch)} ` +
`at epoch ${epoch} but trying to propose at the same epoch`);
return;
}
Expand Down Expand Up @@ -1095,7 +1096,7 @@ class Consensus {
logger.debug(`[${LOG_HEADER}] longestNotarizedChainTips: ` +
`${JSON.stringify(this.node.bp.longestNotarizedChainTips, null, 2)}`);
this.node.bp.longestNotarizedChainTips.forEach((chainTip) => {
const block = _.get(this.node.bp.hashToBlockInfo[chainTip], 'block');
const block = _.get(this.node.bp.hashToBlockInfo.get(chainTip), 'block');
if (!block) return;
if (block.epoch > candidate.epoch) candidate = block;
});
Expand Down Expand Up @@ -1124,7 +1125,7 @@ class Consensus {
!this.node.bp.hashToDb.has(blockHash)) {
chain.unshift(currBlock);
// previous block of currBlock
currBlock = _.get(this.node.bp.hashToBlockInfo[currBlock.last_hash], 'block');
currBlock = _.get(this.node.bp.hashToBlockInfo.get(currBlock.last_hash), 'block');
if (!currBlock) {
currBlock = this.node.bc.getBlockByHash(blockHash);
}
Expand Down Expand Up @@ -1153,7 +1154,7 @@ class Consensus {

getValidatorsVotedFor(blockHash) {
const LOG_HEADER = 'getValidatorsVotedFor';
const blockInfo = this.node.bp.hashToBlockInfo[blockHash];
const blockInfo = this.node.bp.hashToBlockInfo.get(blockHash);
if (!blockInfo || !blockInfo.votes || !blockInfo.votes.length) {
logger.error(`[${LOG_HEADER}] No validators voted`);
throw Error('No validators voted');
Expand Down Expand Up @@ -1258,16 +1259,17 @@ class Consensus {
}

votedForEpoch(epoch) {
const blockHash = this.node.bp.epochToBlock[epoch];
const blockHash = this.node.bp.epochToBlock.get(epoch);
if (!blockHash) return false;
const blockInfo = this.node.bp.hashToBlockInfo[blockHash];
const blockInfo = this.node.bp.hashToBlockInfo.get(blockHash);
if (!blockInfo || !blockInfo.votes) return false;
const myAddr = this.node.account.address;
return blockInfo.votes.find((vote) => vote.address === myAddr) !== undefined;
}

votedForBlock(blockHash) {
const blockInfo = this.node.bp.hashToBlockInfo[blockHash] || this.node.bp.hashToInvalidBlockInfo[blockHash];
const blockInfo = this.node.bp.hashToBlockInfo.get(blockHash) ||
this.node.bp.hashToInvalidBlockInfo.get(blockHash);
if (!blockInfo || !blockInfo.votes) return false;
const myAddr = this.node.account.address;
return blockInfo.votes.find((vote) => vote.address === myAddr) !== undefined;
Expand Down Expand Up @@ -1325,15 +1327,15 @@ class Consensus {
Object.assign({}, { epoch: this.epoch, proposer: this.proposer }, { state: this.state });
if (this.node.bp) {
result.block_pool = {
hashToBlockInfo: this.node.bp.hashToBlockInfo,
hashToInvalidBlockInfo: this.node.bp.hashToInvalidBlockInfo,
hashToBlockInfo: Object.fromEntries(this.node.bp.hashToBlockInfo),
hashToInvalidBlockInfo: Object.fromEntries(this.node.bp.hashToInvalidBlockInfo),
hashToDb: Array.from(this.node.bp.hashToDb.keys()),
hashToNextBlockSet: Object.keys(this.node.bp.hashToNextBlockSet)
hashToNextBlockSet: Array.from(this.node.bp.hashToNextBlockSet.keys())
.reduce((acc, curr) => {
return Object.assign(acc, {[curr]: [...this.node.bp.hashToNextBlockSet[curr]]})
return Object.assign(acc, {[curr]: [...this.node.bp.hashToNextBlockSet.get(curr)]})
}, {}),
epochToBlock: Object.keys(this.node.bp.epochToBlock),
numberToBlockSet: Object.keys(this.node.bp.numberToBlockSet),
epochToBlock: Array.from(this.node.bp.epochToBlock.keys()),
numberToBlockSet: Array.from(this.node.bp.numberToBlockSet.keys()),
longestNotarizedChainTips: this.node.bp.longestNotarizedChainTips
}
}
Expand Down
4 changes: 2 additions & 2 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class BlockchainNode {

getTransactionByHash(hash) {
const LOG_HEADER = 'getTransactionByHash';
const transactionInfo = this.tp.transactionTracker[hash];
const transactionInfo = this.tp.transactionTracker.get(hash);
if (!transactionInfo) {
return null;
}
Expand All @@ -394,7 +394,7 @@ class BlockchainNode {
transactionInfo.state === TransactionStates.PENDING) {
const address = transactionInfo.address;
transactionInfo.transaction =
_.find(this.tp.transactions[address], (tx) => tx.hash === hash) || null;
_.find(this.tp.transactions.get(address), (tx) => tx.hash === hash) || null;
}
return transactionInfo;
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class P2pServer {
getTxStatus() {
return {
txPoolSize: this.node.tp.getPoolSize(),
txTrackerSize: Object.keys(this.node.tp.transactionTracker).length,
txTrackerSize: this.node.tp.transactionTracker.size,
};
}

Expand Down Expand Up @@ -578,7 +578,7 @@ class P2pServer {
trafficStatsManager.addEvent(TrafficEventTypes.P2P_TAG_TX_LENGTH, txTags.length);
trafficStatsManager.addEvent(
TrafficEventTypes.P2P_TAG_TX_MAX_OCCUR, CommonUtil.countMaxOccurrences(txTags));
if (this.node.tp.transactionTracker[tx.hash]) {
if (this.node.tp.transactionTracker.has(tx.hash)) {
logger.debug(`[${LOG_HEADER}] Already have the transaction in my tx tracker`);
const latency = Date.now() - beginTime;
trafficStatsManager.addEvent(TrafficEventTypes.P2P_MESSAGE_SERVER, latency);
Expand Down
1 change: 1 addition & 0 deletions test/integration/consensus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('Consensus', () => {
ref: `/blockchain_params/consensus/max_num_validators`,
value: MAX_NUM_VALIDATORS
},
gas_price: 0,
timestamp: Date.now(),
nonce: -1
};
Expand Down
7 changes: 7 additions & 0 deletions test/integration/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ describe('Native Function', () => {
ref: `/developers/rest_functions/user_whitelist/${serviceUser}`,
value: true
},
gas_price: 0,
timestamp: 1628255843548,
nonce: -1
};
Expand Down Expand Up @@ -4178,6 +4179,7 @@ describe('Native Function', () => {
ref: `${checkoutHistoryBasePath}/${serviceUser}/1628255843548`,
value: null,
},
gas_price: 0,
timestamp: 1628255843548,
nonce: -1
};
Expand Down Expand Up @@ -4220,6 +4222,7 @@ describe('Native Function', () => {
}
}
},
gas_price: 0,
timestamp: 1628255843548,
nonce: -1
};
Expand Down Expand Up @@ -4368,6 +4371,7 @@ describe('Native Function', () => {
}
}
},
gas_price: 0,
timestamp: 1628255843550,
nonce: -1
};
Expand Down Expand Up @@ -4971,6 +4975,7 @@ describe('Native Function', () => {
ref: `${checkinHistoryBasePath}/${serviceUser}/1628255843548`,
value: null,
},
gas_price: 0,
timestamp,
nonce: -1
};
Expand Down Expand Up @@ -5016,6 +5021,7 @@ describe('Native Function', () => {
}
}
},
gas_price: 0,
timestamp,
nonce: -1
};
Expand Down Expand Up @@ -5199,6 +5205,7 @@ describe('Native Function', () => {
}
}
},
gas_price: 0,
timestamp: 1628255843548,
nonce: -1
};
Expand Down
Loading

0 comments on commit 9c4eaa4

Please sign in to comment.