This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from OpenSTFoundation/sarvesh/merkel-patricia…
…-proof-generator Sarvesh/merkel patricia proof generator
- Loading branch information
Showing
33 changed files
with
1,149 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const leveldown = require('leveldown') | ||
, levelup = require('levelup') | ||
; | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
function LevelDBFactory() { | ||
this.instanceMap = {}; | ||
} | ||
|
||
LevelDBFactory.prototype = { | ||
/** | ||
* Returns leveldb instance, it creates new in not already exists otherwise returns existing instance | ||
* @param dbPath | ||
* @return leveldb instance | ||
*/ | ||
getInstance: function (dbPath) { | ||
const oThis = this; | ||
|
||
let lowerCasePath = dbPath.toLowerCase(); | ||
|
||
if (!oThis.instanceMap[lowerCasePath]) { | ||
oThis.instanceMap[lowerCasePath] = oThis.create(lowerCasePath); | ||
} | ||
return oThis.instanceMap[lowerCasePath]; | ||
}, | ||
|
||
/** | ||
* | ||
* @param dbPath | ||
* @return leveldb instance | ||
*/ | ||
create: function (dbPath) { | ||
return levelup(leveldown(dbPath)); | ||
}, | ||
}; | ||
module.exports = new LevelDBFactory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const Trie = require('merkle-patricia-tree'); | ||
|
||
const rootPrefix = "../.." | ||
, logger = require(rootPrefix + '/helpers/custom_console_logger') | ||
, responseHelper = require(rootPrefix + '/lib/formatter/response') | ||
, proof = require(rootPrefix + "/lib/proof/proof") | ||
, basicHelper = require(rootPrefix + '/helpers/basic_helper') | ||
; | ||
|
||
/** | ||
* @constructor | ||
* @param stateRoot | ||
* @param db | ||
*/ | ||
function AccountProof(stateRoot, db) { | ||
const oThis = this; | ||
|
||
oThis.trie = new Trie(db, stateRoot); | ||
} | ||
|
||
AccountProof.prototype = { | ||
|
||
/** | ||
* Validate and _build proof | ||
* @param address | ||
* @return {Promise<proof>} | ||
*/ | ||
perform: async function (address) { | ||
const oThis = this; | ||
|
||
await oThis._validate(address); | ||
return oThis._build(address); | ||
}, | ||
/** | ||
* Delegates call to _build account proof to lib | ||
* @param address | ||
* @return {Promise<proof>} | ||
*/ | ||
_build: async function (address) { | ||
const oThis = this; | ||
|
||
return proof.accountProof(address, oThis.trie); | ||
}, | ||
/** | ||
* Validate input | ||
* @param address | ||
* @private | ||
*/ | ||
_validate: async function (address) { | ||
const oThis = this; | ||
|
||
let errorConf = { | ||
internal_error_identifier: "s_p_ap_validate_2", | ||
debug_options: {}, | ||
error_config: basicHelper.fetchErrorConfig() | ||
}; | ||
|
||
if (!oThis.trie || oThis.trie.root === oThis.trie.EMPTY_TRIE_ROOT) { | ||
errorConf.api_error_identifier = "tree_not_initialized"; | ||
let errorResponse = responseHelper.error(errorConf); | ||
return Promise.reject(errorResponse); | ||
} | ||
if (address === undefined) { | ||
logger.error("Account address is invalid"); | ||
errorConf.api_error_identifier = "account_address_undefined"; | ||
let errorResponse = responseHelper.error(errorConf); | ||
return Promise.reject(errorResponse); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = AccountProof; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const ethUtils = require('ethereumjs-util'); | ||
|
||
const rootPrefix = '../..' | ||
, AccountProof = require(rootPrefix + '/lib/proof/account_proof') | ||
; | ||
|
||
/** | ||
* Constructor for helper methods class - ProofHelperKlass | ||
* @constructor | ||
*/ | ||
|
||
const ProofHelperKlass = function () { | ||
}; | ||
|
||
ProofHelperKlass.prototype = { | ||
|
||
/** | ||
* @notice left-pad value to make length 32 bytes | ||
* @param value | ||
* @return {string} padded value of length 32 bytes i.e. 64 nibbles | ||
* @private | ||
*/ | ||
_leftPad: function (value) { | ||
return ("0000000000000000000000000000000000000000000000000000000000000000" + value).substring(value.length) | ||
}, | ||
/** | ||
*@notice generates storagePath of a variable in the storage | ||
* @param storageIndex | ||
* @param mappings, key of mapping variable | ||
* @return {Buffer2} | ||
*/ | ||
storagePath: function (storageIndex, mappings) { | ||
|
||
let path = Buffer.from(this._leftPad(storageIndex), 'hex'); | ||
if (mappings && mappings.length > 0) { | ||
mappings.map(mapping => { | ||
path = Buffer.concat([Buffer.from(this._leftPad(mapping), 'hex'), path]) | ||
}); | ||
path = Buffer.from(ethUtils.sha3(path), 'hex') | ||
} | ||
path = Buffer.from(ethUtils.sha3(path), 'hex'); | ||
return path; | ||
}, | ||
|
||
/** | ||
* @notice generates storage root of a contract | ||
* @param stateRoot | ||
* @param contractAddress | ||
* @param db level db instace | ||
* @return {Promise<string>} | ||
* @private | ||
*/ | ||
fetchStorageRoot: async function (stateRoot, contractAddress, db) { | ||
|
||
let accountProofInstance = new AccountProof(stateRoot, db) | ||
, accountProof = await accountProofInstance.perform(contractAddress) | ||
, accountValue = accountProof.toHash().data.value | ||
, decodedValue = ethUtils.rlp.decode('0x' + accountValue); | ||
|
||
return '0x' + decodedValue[2].toString('hex'); | ||
} | ||
}; | ||
|
||
module.exports = new ProofHelperKlass(); |
Oops, something went wrong.