我想使用LevelDB访问以太坊交易树,但它只适用于Block 0,对于任何其他块,它都没有给我任何数据。只是想知道如何web3.eth.getTransaction()内部工作:下面是代码:
const Trie1 = require ('merkle-patricia-tree').SecureTrie
const { Account, BN, bufferToHex, rlp } = require ('ethereumjs-util') ;
var levelup = require('levelup');
var leveldown = require('leveldown');
const gethDbPath = '/Users/AK/gethDataDir/geth/chaindata'
const db = new levelup(leveldown(gethDbPath))
async function test() {
const txnRoot = '0x51736d42e0e8fe95c3e609e5e5fb70da476613d24b5cd0d710d68190649c39f4'
const txnRootBuffer = Buffer.from(txnRoot.slice(2), 'hex')
const trie1 = new Trie1(db,txnRootBuffer)
const stream = trie1.createReadStream()
stream.on('data', (data) => {
console.log(`key: ${bufferToHex(data.key)}`)
console.log(`Value: ${bufferToHex(rlp.decode(data.value))}`)
})
.on('end', () => {
console.log('1111--->>> Finished')
})
}
test() ;
下面是我试图直接访问levelDB的事务。我尝试了几乎所有的组合,如TxnRoot, TxnHash,有或没有Kecak256等
{难度:149904,extraData:"0 xd983010a10846765746888676f312e31372e368664617277696e"gasLimit:2770332, gasUsed: 21000, hash:"0 xa98255eed7b9a16b30013b8cabf8adf07def8cb772ba9d3110f72a97fd351ad7",logsBloom:"0 x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",miner: 0x3f457dd6c71534bc7955c29b6a097cf39b96bfd7", mixHash:"0 x366ff733fc9e2768595a25299edec785688deb4c8fe2d53543b860e8ee0724fc",nonce: "0x24bc70e55ec61f1d",编号:284,parentHash:"0 x17d0dee586ef5064cfcd835e4c40d34f6a5683449cd3a1a1f0f705a2bab6a938",receiptsRoot:"0 x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",sha3Uncles:"0 x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",size: 647, stateRoot:"0 xb2ae3c859e0873667bdc088217be87cab1fed2a068e9e6baa124922915c83419",timestamp: 1654920279, total难度:39858713,transactions:("0 x50a949ef9cab9d8edad24d8b6b33b3023ef499a6f43aabd8db1a7db85daf68f7",transactionsRoot:"0 x51736d42e0e8fe95c3e609e5e5fb70da476613d24b5cd0d710d68190649c39f4",[]}
我相信这是您使用SecureTrie
而不是Trie
的转换树。Transaction和Receipts不像State和Storage那样散列。我有同样的问题去看看我的代码https://ethereum.stackexchange.com/questions/130716/prove-transactionhash-of-block-using-ethereumjs-tx-and-merkle-patricia-tree