Last active
June 7, 2017 22:30
-
-
Save daragao/cda5d96d4d4248826762992badb0f96d to your computer and use it in GitHub Desktop.
playing around to check how to read a levelDB
This file contains hidden or 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
const level = require('level'); | |
const levelup = require('levelup'); | |
const RLP = require('rlp'); | |
const Trie = require('merkle-patricia-tree') | |
const DATADIR_PATH = '../../simple_geth_launcher/data_node1'; | |
/* | |
const nodeDb = level('/home/da/.ethereum/geth/nodes'); | |
const nodeDataArr = []; | |
nodeDb.createReadStream({ keys: true, values: true }) | |
.on('data',(data) => {nodeDataArr.push(data)}) | |
const testRead = () => { | |
const keyObj = {}; | |
const valueObj = {}; | |
const parsedData = nodeDataArr.map((data) => { | |
const key = data.key; | |
const value = data.value; | |
const splitKey = key.split(':'); | |
const keyBytes = new Buffer(splitKey[1]); | |
const keyBytesHex = keyBytes.toString('hex'); | |
const valueBytes = new Buffer(value,'binary'); | |
const valueBytesHex = valueBytes.toString('hex'); | |
//keyObj[keyBytesHex] = (keyObj[keyBytesHex] + 1) || 1; | |
//valueObj[keyBytesHex] = (valueObj[valueBytesHex] + 1) || 1; | |
return {splitKey,value,keyBytes,valueBytes,keyBytesHex,valueBytesHex}; | |
}); | |
parsedData | |
.filter((d)=>d.splitKey[3]==='lastping') | |
.forEach((d)=>{ | |
const bytes = d.keyBytes; | |
console.log(d.splitKey[3],bytes.toString('hex')) | |
}) | |
// .forEach((d)=>console.log(d.decodedBytes)); | |
//.forEach((d)=>console.log(d)); | |
//console.log(Object.keys(keyObj)); | |
//console.log(Object.keys(valueObj)); | |
}; | |
setTimeout(testRead , 100); | |
*/ | |
//const chainDb = levelup('/home/da/.ethereum/geth/chaindata'); | |
const chainDb = levelup(DATADIR_PATH+'/chaindata'); | |
//Note: we are doing everything using binary encoding. | |
//setTimeout(console.log,5000); | |
const enc = 'binary'; | |
//const blockHash = new Buffer('137a6bfeba9dc887389382f35dfb5a81e94038021f76e834a6b6032cdbb76967','hex'); | |
const blockHash = new Buffer('ff3ea59a41767cff82aa49be5e8188b97245802105a769cff829e0d55b8ae057','hex'); | |
//ff3ea59a41767cff82aa49be5e8188b97245802105a769cff829e0d55b8ae057 | |
const options = { encoding: enc, valueEncoding: enc, keyEncoding: enc }; | |
//chainDb.get('block-'+blockHash+'-body', options, (err, value) => { console.log('get()',value,err); }) | |
chainDb.get(blockHash, options, (err, value) => { | |
const decodedBytes = RLP.decode(value); | |
console.log('get()',decodedBytes,err); | |
const mapStr = decodedBytes.map(data=>data.toString('hex')); | |
console.log(mapStr); | |
}); | |
/* | |
chainDb | |
.createReadStream({ encoding: enc, valueEncoding: enc, keyEncoding: enc, keys: true, values: false }) | |
.on('data',(data) => { | |
const keyBin = new Buffer(data,enc); | |
const keyStrMatch = keyBin.toString('ascii').match(/^((receipts-)?block-)(num-)?/); | |
if(keyStrMatch) { | |
const firstIdx = keyStrMatch[0].length; | |
const result = {}; | |
if(keyStrMatch[0] === 'receipts-block-') { | |
const blockNumBin = keyBin.slice(firstIdx); | |
result.type = keyStrMatch[0].slice(0,-1); | |
result.value = blockNumBin.toString('hex'); | |
} else if(keyStrMatch[0] === 'block-num-') { | |
//caught 'block-num-' | |
const blockNumBin = keyBin.slice(firstIdx); | |
result.type = keyStrMatch[0].slice(0,-1); | |
result.value = blockNumBin.toString('hex'); | |
} else { | |
// caught 'block-*-(td|header|body)' | |
const terminator = keyBin.slice(firstIdx+32).toString('ascii'); | |
const blockAddrBin = keyBin.slice(firstIdx,firstIdx+32); | |
result.type = keyStrMatch[0]+terminator.slice(1); | |
result.value = blockAddrBin.toString('hex'); | |
} | |
//console.log(result); | |
} else console.log(data) | |
}); | |
*/ | |
/* | |
chainDb | |
.createReadStream({ keys: true, values: false, keyEncoding: enc }) | |
.on('data',(keyBin) => { | |
const key = new Buffer(keyBin,enc); | |
const keyStr = key.toString('utf-8').split('-'); | |
if(keyStr.length === 3 && keyStr[2] === 'body') { | |
const addr = new Buffer(keyStr[1]); | |
console.log(addr.toString('hex'), addr.toString('hex').length) | |
} | |
}); | |
*/ | |
/* | |
const chainDataArr = []; | |
chainDb.createReadStream({ keys: true, values: true }) | |
.on('data',(data) => {chainDataArr.push(data)}) | |
const chainRead = () => { | |
const keyObj = {}; | |
const valueObj = {}; | |
const parsedData = chainDataArr.map((data) => { | |
const key = new Buffer(data.key); | |
const value = new Buffer(data.value); | |
console.log(key.toString('hex'),value.toString('hex')); | |
}); | |
}; | |
setTimeout(chainRead , 100); | |
*/ |
This file contains hidden or 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
import plyvel | |
import binascii | |
#db = plyvel.DB("/Users/doart3/simple_geth_launcher/data_node1/chaindata") | |
db = plyvel.DB("/Users/doart3/simple_geth_launcher/data_node1/geth/nodes") | |
#db.put("key", "value") | |
#print db.get("key") | |
for key, value in db: | |
#print(binascii.b2a_hex(key)) | |
print(key) | |
#print(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment