Skip to content

Commit

Permalink
Merge pull request #123 from hive-engine/crash_debug
Browse files Browse the repository at this point in the history
Crash debug logging using loglevel library
  • Loading branch information
bt-cryptomancer authored Jan 22, 2022
2 parents 360ac34 + 25ceb16 commit 8809d5e
Show file tree
Hide file tree
Showing 7 changed files with 4,763 additions and 56 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
],
"startHiveBlock": 41967000,
"genesisHiveBlock": 41967000,
"witnessEnabled": true
"witnessEnabled": true,
"defaultLogLevel": "warn"
}
12 changes: 7 additions & 5 deletions libs/Block.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const SHA256 = require('crypto-js/sha256');
const enchex = require('crypto-js/enc-hex');
const log = require('loglevel');
const { CONSTANTS } = require('../libs/Constants');

const { SmartContracts } = require('./SmartContracts');
Expand Down Expand Up @@ -115,14 +116,15 @@ class Block {
const allowCommentContract = this.refHiveBlockNumber > 54560500;
for (let i = 0; i < nbTransactions; i += 1) {
const transaction = this.transactions[i];
log.info('Processing tx ', transaction);
await this.processTransaction(database, jsVMTimeout, transaction, currentDatabaseHash); // eslint-disable-line

currentDatabaseHash = transaction.databaseHash;

if ((transaction.contract !== 'comments' || allowCommentContract) || transaction.logs === '{}') {
if (mainBlock && currentDatabaseHash !== mainBlock.transactions[relIndex].databaseHash) {
console.warn(mainBlock.transactions[relIndex]); // eslint-disable-line no-console
console.warn(transaction); // eslint-disable-line no-console
log.warn(mainBlock.transactions[relIndex]); // eslint-disable-line no-console
log.warn(transaction); // eslint-disable-line no-console
throw new Error('tx hash mismatch with api');
}
relIndex += 1;
Expand Down Expand Up @@ -184,8 +186,8 @@ class Block {
this.virtualTransactions.push(transaction);
if (mainBlock && currentDatabaseHash
!== mainBlock.virtualTransactions[relIndex].databaseHash) {
console.warn(mainBlock.virtualTransactions[relIndex]); // eslint-disable-line no-console
console.warn(transaction); // eslint-disable-line no-console
log.warn(mainBlock.virtualTransactions[relIndex]); // eslint-disable-line no-console
log.warn(transaction); // eslint-disable-line no-console
throw new Error('tx hash mismatch with api');
}
relIndex += 1;
Expand Down Expand Up @@ -265,7 +267,7 @@ class Block {
newCurrentDatabaseHash = database.getDatabaseHash();


// console.log('transac logs', results.logs);
log.info('Tx results: ', results);
transaction.addLogs(results.logs);
transaction.executedCodeHash = results.executedCodeHash || ''; // eslint-disable-line
transaction.databaseHash = newCurrentDatabaseHash; // eslint-disable-line
Expand Down
31 changes: 17 additions & 14 deletions libs/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable no-await-in-loop */
const SHA256 = require('crypto-js/sha256');
const enchex = require('crypto-js/enc-hex');
const log = require('loglevel');
const validator = require('validator');
const { MongoClient } = require('mongodb');
const { EJSON } = require('bson');
Expand Down Expand Up @@ -202,8 +203,8 @@ class Database {
this.databaseHash = SHA256(this.databaseHash + contractInDb.tables[table].hash)
.toString(enchex);
if (enableHashLogging) {
console.log(`updated hash of ${table} to ${contractInDb.tables[table].hash}`); // eslint-disable-line no-console
console.log(`updated db hash from ${oldDatabaseHash} to ${this.databaseHash}`); // eslint-disable-line no-console
log.info(`updated hash of ${table} to ${contractInDb.tables[table].hash}`); // eslint-disable-line no-console
log.info(`updated db hash from ${oldDatabaseHash} to ${this.databaseHash}`); // eslint-disable-line no-console
}
}
}
Expand Down Expand Up @@ -260,7 +261,7 @@ class Database {
return latestBlock;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand All @@ -278,7 +279,7 @@ class Database {
return latestBlock;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand All @@ -292,7 +293,7 @@ class Database {
return block;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand Down Expand Up @@ -327,11 +328,11 @@ class Database {
);
} else {
// eslint-disable-next-line no-console
console.error('verifyBlock', blockNumber, 'does not exist');
log.error('verifyBlock', blockNumber, 'does not exist');
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
}
}

Expand Down Expand Up @@ -360,7 +361,7 @@ class Database {
return null;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand Down Expand Up @@ -488,7 +489,7 @@ class Database {
result = true;
}
} else {
console.warn(`Table invalid, was not created, payload: ${JSON.stringify(payload)}`); // eslint-disable-line no-console
log.warn(`Table invalid, was not created, payload: ${JSON.stringify(payload)}`); // eslint-disable-line no-console
}

return result;
Expand Down Expand Up @@ -527,14 +528,14 @@ class Database {
let createIndex = true;
if (typeof index === 'object') {
if (tableIndexes[index.name] !== undefined) {
console.log(`Index with name ${index.name} already exists for ${finalTableName}`); // eslint-disable-line no-console
log.info(`Index with name ${index.name} already exists for ${finalTableName}`); // eslint-disable-line no-console
createIndex = false;
} else {
indexOptions.name = index.name;
finalIndex = index.index;
}
} else if (tableIndexes[`${index}_1`] !== undefined) {
console.log(`Index ${index} already exists for ${finalTableName}`); // eslint-disable-line no-console
log.info(`Index ${index} already exists for ${finalTableName}`); // eslint-disable-line no-console
createIndex = false;
} else {
finalIndex[index] = 1;
Expand Down Expand Up @@ -572,6 +573,7 @@ class Database {
indexes,
} = payload;

log.info('Find payload ', JSON.stringify(payload));
await this.flushCache();

const lim = limit || 1000;
Expand Down Expand Up @@ -629,7 +631,7 @@ class Database {
// This can happen when creating a table and using find with index all in the same transaction
// and should be rare in production. Otherwise, contract code is asking for an index that does
// not exist.
console.log(`Index ${JSON.stringify(ind)} not available for ${finalTableName}`); // eslint-disable-line no-console
log.info(`Index ${JSON.stringify(ind)} not available for ${finalTableName}`); // eslint-disable-line no-console
}
if (sort.findIndex(el => el[0] === '_id') < 0) {
sort.push(['_id', 'asc']);
Expand Down Expand Up @@ -657,7 +659,7 @@ class Database {
return result;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand All @@ -672,6 +674,7 @@ class Database {
async findOne(payload) { // eslint-disable-line no-unused-vars
try {
const { contract, table, query } = payload;
log.info('findOne payload ', payload);
let result = null;
if (contract && typeof contract === 'string'
&& table && typeof table === 'string'
Expand Down Expand Up @@ -713,7 +716,7 @@ class Database {
return result;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
log.error(error);
return null;
}
}
Expand Down
12 changes: 7 additions & 5 deletions libs/SmartContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dhive = require('@hiveio/dhive');
const { Base64 } = require('js-base64');
const { VM, VMScript } = require('vm2');
const BigNumber = require('bignumber.js');
const log = require('loglevel');
const validator = require('validator');
const seedrandom = require('seedrandom');
const { CONSTANTS } = require('../libs/Constants');
Expand Down Expand Up @@ -186,7 +187,7 @@ class SmartContracts {
}
},
random: () => rng(),
debug: log => console.log(log), // eslint-disable-line no-console
debug: logmsg => log.info(logmsg), // eslint-disable-line no-console
// execute a smart contract from the current smart contract
executeSmartContract: async (
contractName, actionName, parameters,
Expand Down Expand Up @@ -302,6 +303,7 @@ class SmartContracts {
refHiveBlockNumber,
} = transaction;

log.info('Execute smart contract ', transaction);
if (RESERVED_ACTIONS.includes(action)) return { logs: { errors: ['you cannot trigger this action'] } };

const payloadObj = payload ? JSON.parse(payload) : {};
Expand Down Expand Up @@ -413,7 +415,7 @@ class SmartContracts {
return false;
}
},
debug: log => console.log(log), // eslint-disable-line no-console
debug: logmsg => log.info(logmsg), // eslint-disable-line no-console
// execute a smart contract from the current smart contract
executeSmartContract: async (
contractName, actionName, parameters,
Expand Down Expand Up @@ -508,8 +510,10 @@ class SmartContracts {

return results;
} catch (e) {
// console.error('ERROR DURING CONTRACT EXECUTION: ', e);
log.error('ERROR DURING CONTRACT EXECUTION: ', e);
return { logs: { errors: [`${e.name}: ${e.message}`] } };
} finally {
log.info('executeSmartContract done');
}
}

Expand Down Expand Up @@ -562,7 +566,6 @@ class SmartContracts {
});
// eslint-disable-next-line no-underscore-dangle
vm.vm._context.done = (error) => {
// console.log('error', error);
vm.inUse = false;
resolve(error);
};
Expand All @@ -572,7 +575,6 @@ class SmartContracts {
resolve('no JS VM available');
}
} catch (err) {
// console.log('error', err);
vm.inUse = false;
resolve(err);
}
Expand Down
Loading

0 comments on commit 8809d5e

Please sign in to comment.