Skip to content

Commit

Permalink
Merge pull request #61 from hive-engine/feature/useBlockApi
Browse files Browse the repository at this point in the history
Allow for using block_api.get_block API
  • Loading branch information
bt-cryptomancer authored Feb 19, 2024
2 parents 5bdd7c7 + bef0053 commit fdd05ec
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 92 deletions.
7 changes: 7 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@
"rpcWebsockets" : {
"enabled" : true,
"port" : 5002
},
"streamerConfig": {
"antiForkBufferMaxSize": 2,
"maxQps": 1,
"lookaheadBufferSize": 5,
"hashVerificationNode": false,
"useBlockApi": false
}
}
8 changes: 5 additions & 3 deletions libs/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ class Block {
currentDatabaseHash = transaction.databaseHash;

if ((transaction.contract !== 'comments' || allowCommentContract) || transaction.logs === '{}') {
if (mainBlock && currentDatabaseHash !== mainBlock.transactions[relIndex].databaseHash) {
log.warn(mainBlock.transactions[relIndex]); // eslint-disable-line no-console
log.warn(transaction); // eslint-disable-line no-console
if (mainBlock && (
currentDatabaseHash !== mainBlock.transactions[relIndex].databaseHash
|| transaction.payload !== mainBlock.transactions[relIndex].payload)) {
log.warn(mainBlock.transactions[relIndex]);
log.warn(transaction);
throw new Error('tx hash mismatch with api');
}
relIndex += 1;
Expand Down
81 changes: 55 additions & 26 deletions plugins/Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let database = null;
let javascriptVMTimeout = 0;
let producing = false;
let stopRequested = false;
let enableHashVerification = false;
let hashVerificationNode = false;

const createGenesisBlock = async (payload) => {
// check if genesis block hasn't been generated already
Expand Down Expand Up @@ -50,6 +50,34 @@ function getRefBlockNumber(block) {
return block.refHiveBlockNumber;
}

let gbid = 1;
async function getBlock(node, blockNumber, tries = 1) {
gbid += 1;
try {
const block = (await axios({
url: node,
method: 'POST',
headers: {
'content-type': 'application/json',
},
data: {
jsonrpc: '2.0', id: gbid, method: 'getBlockInfo', params: { blockNumber },
},
})).data.result;
if (block) {
return block;
}
} catch (error) {
if (tries >= 3) {
console.error(error);
return null;
}
}
console.log(`Attempt #${tries} failed, retrying...`);
await new Promise(r => setTimeout(() => r(), 500));
return await getBlock(node, blockNumber, tries + 1);
}

// produce all the pending transactions, that will result in the creation of a block
async function producePendingTransactions(
refHiveBlockNumber, refHiveBlockId, prevRefHiveBlockId, transactions, timestamp,
Expand All @@ -74,40 +102,41 @@ async function producePendingTransactions(
previousBlock.hash,
previousBlock.databaseHash,
);

log.info(`Start session for block ${newBlock.blockNumber}`);
const session = database.startSession();

const mainBlock = !enableHashVerification ? null : (await axios({
url: 'https://api.hive-engine.com/rpc/blockchain',
method: 'POST',
headers: {
'content-type': 'application/json',
},
data: {
jsonrpc: '2.0', id: 10, method: 'getBlockInfo', params: { blockNumber: newBlock.blockNumber },
},
})).data.result;
const mainBlock = !hashVerificationNode ? null : (await getBlock(hashVerificationNode, newBlock.blockNumber));
try {
await session.withTransaction(async () => {
await newBlock.produceBlock(database, javascriptVMTimeout, mainBlock);

if (newBlock.transactions.length > 0 || newBlock.virtualTransactions.length > 0) {
if (mainBlock && newBlock.hash) {
console.log(`Sidechain Block ${mainBlock.blockNumber}, Main db hash: ${mainBlock.databaseHash}, Main block hash: ${mainBlock.hash}, This db hash: ${newBlock.databaseHash}, This block hash: ${newBlock.hash}`); // eslint-disable-line no-console

if (mainBlock.databaseHash !== newBlock.databaseHash
|| mainBlock.hash !== newBlock.hash) {
throw new Error(`Block mismatch with api \nMain: ${JSON.stringify(mainBlock, null, 2)}, \nThis: ${JSON.stringify(newBlock, null, 2)}`);
await session.withTransaction(async (liveSession) => {
log.info(`WithTransaction invoked for block ${newBlock.blockNumber}`);
try {
await newBlock.produceBlock(database, javascriptVMTimeout, mainBlock);
log.info(`ProduceBlock finished for block ${newBlock.blockNumber}`);

if (newBlock.transactions.length > 0 || newBlock.virtualTransactions.length > 0) {
if (mainBlock && newBlock.hash) {
console.log(`Sidechain Block ${mainBlock.blockNumber}, Main db hash: ${mainBlock.databaseHash}, Main block hash: ${mainBlock.hash}, This db hash: ${newBlock.databaseHash}, This block hash: ${newBlock.hash}`); // eslint-disable-line no-console

if (mainBlock.databaseHash !== newBlock.databaseHash
|| mainBlock.hash !== newBlock.hash) {
throw new Error(`Block mismatch with api \nMain: ${JSON.stringify(mainBlock, null, 2)}, \nThis: ${JSON.stringify(newBlock, null, 2)}`);
}
}
}

await addBlock(newBlock);
await addBlock(newBlock);
} else if (mainBlock.refHiveBlockNumber === newBlock.refHiveBlockNumber) {
throw new Error(`Block mismatch with api \nMain: ${JSON.stringify(mainBlock, null, 2)}, \nThis: ${JSON.stringify(newBlock, null, 2)}`);
}
} catch (e) {
throw(e);
}
log.info(`Transaction Callback finished for block ${newBlock.blockNumber}`);
});
} catch (e) {
console.error(e); // eslint-disable-line no-console
log.warn(e);
throw e;
} finally {
log.info(`End session for block ${newBlock.blockNumber}`);
await database.endSession();
}
} else {
Expand Down Expand Up @@ -168,7 +197,7 @@ const init = async (conf, callback) => {
databaseName,
} = conf;
javascriptVMTimeout = conf.javascriptVMTimeout; // eslint-disable-line prefer-destructuring
enableHashVerification = conf.enableHashVerification; // eslint-disable-line prefer-destructuring
hashVerificationNode = conf.hashVerificationNode; // eslint-disable-line prefer-destructuring
log.setDefaultLevel(conf.defaultLogLevel ? conf.defaultLogLevel : 'warn');

database = new Database();
Expand Down
Loading

0 comments on commit fdd05ec

Please sign in to comment.