Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Tries #331

Merged
merged 8 commits into from
Feb 27, 2019
5 changes: 3 additions & 2 deletions lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,12 @@ BlockchainDouble.prototype.processBlock = function(vm, block, commit, callback)
receipt.gasUsed,
result.createdAddress,
receipt.status,
to.hex(result.bloom.bitvector)
to.hex(receipt.bitvector)
);
receipts.push(rcpt);

const rcptBuffer = Buffer.from(JSON.stringify(rcpt.toJSON()));
const rawReceipt = [receipt.status, receipt.gasUsed, receipt.bitvector, receipt.logs];
const rcptBuffer = utils.rlp.encode(rawReceipt);
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
const key = utils.rlp.encode(v);
promises.push(putInTrie(txTrie, key, tx.serialize()));
promises.push(putInTrie(rcptTrie, key, rcptBuffer));
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/forkedblockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ ForkedBlockchain.prototype.getFallbackBlock = function(numberOrHash, cb) {
block.header.uncleHash = utils.toBuffer(json.sha3Uncles);
block.header.coinbase = utils.toBuffer(json.miner);
block.header.stateRoot = utils.toBuffer(json.stateRoot); // Should we include the following three?
block.header.transactionTrie = utils.toBuffer(json.transactionsRoot);
block.header.transactionsTrie = utils.toBuffer(json.transactionsRoot);
block.header.receiptTrie = utils.toBuffer(json.receiptsRoot);
block.header.bloom = utils.toBuffer(json.logsBloom);
block.header.difficulty = utils.toBuffer("0x" + json.totalDifficulty.toString(16)); // BigNumber
Expand Down
16 changes: 13 additions & 3 deletions lib/utils/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ const BUFFER_ZERO = Buffer.from([0]);

function configZeroableField(tx, fieldName, fieldLength = 32) {
const index = tx._fields.indexOf(fieldName);
const descriptor = Object.getOwnPropertyDescriptor(tx, fieldName);
// eslint-disable-next-line accessor-pairs
Object.defineProperty(tx, fieldName, {
set: (v) => {
descriptor.set.call(tx, v);
v = ethUtil.toBuffer(v);
assert(fieldLength >= v.length, `The field ${fieldName} must not have more ${fieldLength} bytes`);
tx.raw[index] = v;
tx._originals[index] = v;
},
get: () => {
return tx._originals[index];
}
});
}
Expand All @@ -40,8 +45,13 @@ function fixProps(tx, data) {
// ethereumjs-tx doesn't allow for a `0` value in fields, but we want it to
// in order to differentiate between a value that isn't set and a value
// that is set to 0 in a fake transaction.
// Once https://github.com/ethereumjs/ethereumjs-tx/issues/112 is fixed we
// can probably remove this fix.
// Once https://github.com/ethereumjs/ethereumjs-tx/issues/112 is figured
// out we can probably remove this fix/hack.
// We keep track of the original value and return that value when
// referenced by its property name. This lets us properly encode a `0` as
// an empty buffer while still being able to differentiate between a `0`
// and `null`/`undefined`.
tx._originals = [];
const fieldNames = ["nonce", "gasPrice", "gasLimit", "value"];
fieldNames.forEach((fieldName) => configZeroableField(tx, fieldName, 32));

Expand Down
12 changes: 11 additions & 1 deletion test/block_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,19 @@ describe("Block Tags", function() {
assert(newCode.length > 20); // Just because we don't know the actual code we're supposed to get back
});

it("should not have the same tx and receipt root when the block contains 1 (or more) tx's", async function() {
it("should produce correct tx and receipt root when the block contains 1 (or more) tx's", async function() {
const block = await web3.eth.getBlock(initialBlockNumber + 1, false);
assert.strictEqual(block.transactions.length, 1, "should have one tx in the block.");
assert.notStrictEqual(block.transactionsRoot, block.receiptsRoot, "Trie roots should not be equal.");
assert.strictEqual(
block.transactionsRoot,
"0xce8a25092b27c67e802dff9e3ec66aacf6232da66e2796243aaccdc0deaaa1db",
"Should produce correct transactionsRoot"
);
assert.strictEqual(
block.receiptsRoot,
"0xa63df9d6e2147dbffa164b173ead7c10d14d95c6e83dbb879ddc45ad7e8dfc89",
"Should produce correct receiptsRoot"
);
});
});