Skip to content

Commit

Permalink
refactor(trie): combine get/set for root into function
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Aug 24, 2022
1 parent 913f7f1 commit 00ddc9d
Show file tree
Hide file tree
Showing 33 changed files with 142 additions and 169 deletions.
4 changes: 2 additions & 2 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ export class Block {
return result
}

if (this.txTrie.root.equals(KECCAK256_RLP)) {
if (this.txTrie.root().equals(KECCAK256_RLP)) {
await this.genTxTrie()
}
result = this.txTrie.root.equals(this.header.transactionsTrie)
result = this.txTrie.root().equals(this.header.transactionsTrie)
return result
}

Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/src/genesisStates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export async function genesisStateRoot(genesisState: GenesisState) {
)
await storageTrie.put(storageKey, storageVal)
}
account.storageRoot = storageTrie.root
account.storageRoot = storageTrie.root()
}
}
await trie.put(address, account.serialize())
}
return trie.root
return trie.root()
}
2 changes: 1 addition & 1 deletion packages/client/lib/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const txsTrieRoot = async (txs: TypedTransaction[]) => {
for (const [i, tx] of txs.entries()) {
await trie.put(Buffer.from(RLP.encode(i)), tx.serialize())
}
return trie.root
return trie.root()
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
// from state trie
const account = await this.getAccount(address)
const storageTrie = this._trie.copy(false)
storageTrie.root = account.storageRoot
storageTrie.root(account.storageRoot)
storageTrie.db.checkpoints = []
return storageTrie
}
Expand Down Expand Up @@ -235,7 +235,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage

// update contract storageRoot
const contract = this._cache.get(address)
contract.storageRoot = storageTrie.root
contract.storageRoot = storageTrie.root()

await this.putAccount(address, contract)
resolve()
Expand Down Expand Up @@ -286,7 +286,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
*/
async clearContractStorage(address: Address): Promise<void> {
await this._modifyContractStorage(address, (storageTrie, done) => {
storageTrie.root = storageTrie.EMPTY_TRIE_ROOT
storageTrie.root(storageTrie.EMPTY_TRIE_ROOT)
done()
})
}
Expand Down Expand Up @@ -444,8 +444,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
*/
async getStateRoot(): Promise<Buffer> {
await this._cache.flush()
const stateRoot = this._trie.root
return stateRoot
return this._trie.root()
}

/**
Expand All @@ -465,7 +464,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage
}
}

this._trie.root = stateRoot
this._trie.root(stateRoot)
this._cache.clear()
this._storageTries = {}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/statemanager/tests/proofStateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tape('ProofStateManager', (t) => {
}
await trie.db.put(key, bufferData)
}
trie.root = stateRoot!
trie.root(stateRoot!)
const proof = await stateManager.getProof(address)
st.deepEqual(ropsten_validAccount, proof)
st.ok(await stateManager.verifyProof(ropsten_validAccount))
Expand All @@ -80,7 +80,7 @@ tape('ProofStateManager', (t) => {
}
await trie.db.put(key, bufferData)
}
trie.root = stateRoot!
trie.root(stateRoot!)
const proof = await stateManager.getProof(address)
st.deepEqual(ropsten_nonexistentAccount, proof)
st.ok(await stateManager.verifyProof(ropsten_nonexistentAccount))
Expand Down Expand Up @@ -118,10 +118,10 @@ tape('ProofStateManager', (t) => {
await storageTrie.db.put(key, toBuffer(storageProofData))
}
}
storageTrie.root = toBuffer(storageRoot)
storageTrie.root(toBuffer(storageRoot))
const addressHex = address.buf.toString('hex')
stateManager._storageTries[addressHex] = storageTrie
trie.root = stateRoot!
trie.root(stateRoot!)

const proof = await stateManager.getProof(address, storageKeys)
st.deepEqual(ropsten_contractWithStorage, proof)
Expand Down Expand Up @@ -158,10 +158,10 @@ tape('ProofStateManager', (t) => {
await storageTrie.db.put(key, toBuffer(storageProofData))
}
}
storageTrie.root = toBuffer(storageRoot)
storageTrie.root(toBuffer(storageRoot))
const addressHex = address.buf.toString('hex')
stateManager._storageTries[addressHex] = storageTrie
trie.root = stateRoot!
trie.root(stateRoot!)

// tamper with account data
const testdata = ropsten_contractWithStorage as any
Expand Down Expand Up @@ -217,10 +217,10 @@ tape('ProofStateManager', (t) => {
}
const storageRoot = ropsten_nonexistentAccount.storageHash
const storageTrie = new Trie({ useHashedKeys: true })
storageTrie.root = toBuffer(storageRoot)
storageTrie.root(toBuffer(storageRoot))
const addressHex = address.buf.toString('hex')
stateManager._storageTries[addressHex] = storageTrie
trie.root = stateRoot!
trie.root(stateRoot!)

// tamper with account data
const testdata = ropsten_nonexistentAccount as any
Expand Down
6 changes: 3 additions & 3 deletions packages/statemanager/tests/stateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ tape('StateManager', (t) => {
t.test('should instantiate', async (st) => {
const stateManager = new DefaultStateManager()

st.deepEqual(stateManager._trie.root, KECCAK256_RLP, 'it has default root')
st.deepEqual(stateManager._trie.root(), KECCAK256_RLP, 'it has default root')
const res = await stateManager.getStateRoot()
st.deepEqual(res, KECCAK256_RLP, 'it has default root')
st.end()
})

t.test('should set the state root to empty', async (st) => {
const stateManager = new DefaultStateManager()
st.ok(stateManager._trie.root.equals(KECCAK256_RLP), 'it has default root')
st.ok(stateManager._trie.root().equals(KECCAK256_RLP), 'it has default root')

// commit some data to the trie
const address = new Address(Buffer.from('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', 'hex'))
Expand All @@ -39,7 +39,7 @@ tape('StateManager', (t) => {
await stateManager.putAccount(address, account)
await stateManager.commit()
await stateManager.flush()
st.ok(!stateManager._trie.root.equals(KECCAK256_RLP), 'it has a new root')
st.ok(!stateManager._trie.root().equals(KECCAK256_RLP), 'it has a new root')

// set state root to empty trie root
const emptyTrieRoot = Buffer.from(KECCAK256_RLP_S, 'hex')
Expand Down
2 changes: 1 addition & 1 deletion packages/trie/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ This allows to swap out the applied `keccak256` hash functionality from the [@no
So the usage of the following methods change and need to be updated (for all types of tries):

- `Trie.createProof(trie, myKey)` -> `trie.createProof(myKey)`
- `Trie.verifyProof(trie.root, myKey, proof)` -> `trie.verifyProof(trie.root, myKey, proof)`
- `Trie.verifyProof(trie.root(), myKey, proof)` -> `trie.verifyProof(trie.root(), myKey, proof)`
- `Trie.verifyRangeProof(...)` -> `trie.verifyRangeProof(...)`

## Other Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/trie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const trie = new Trie()
async function test() {
await trie.put(Buffer.from('test'), Buffer.from('one'))
const proof = await trie.createProof(Buffer.from('test'))
const value = await trie.verifyProof(trie.root, Buffer.from('test'), proof)
const value = await trie.verifyProof(trie.root(), Buffer.from('test'), proof)
console.log(value.toString()) // 'one'
}

Expand All @@ -128,7 +128,7 @@ async function test() {
await trie.put(Buffer.from('test'), Buffer.from('one'))
await trie.put(Buffer.from('test2'), Buffer.from('two'))
const proof = await trie.createProof(Buffer.from('test3'))
const value = await trie.verifyProof(trie.root, Buffer.from('test3'), proof)
const value = await trie.verifyProof(trie.root(), Buffer.from('test3'), proof)
console.log(value.toString()) // null
}

Expand All @@ -148,7 +148,7 @@ async function test() {
const proof = await trie.createProof(Buffer.from('test2'))
proof[1].reverse()
try {
const value = await trie.verifyProof(trie.root, Buffer.from('test2'), proof)
const value = await trie.verifyProof(trie.root(), Buffer.from('test2'), proof)
console.log(value.toString()) // results in error
} catch (err) {
console.log(err) // Missing node in DB
Expand Down Expand Up @@ -214,7 +214,7 @@ async function test() {
console.log(`codeHash: ${bufferToHex(acc.codeHash)}`)

const storageTrie = trie.copy()
storageTrie.root = acc.stateRoot
storageTrie.root(acc.stateRoot)

console.log('------Storage------')
const stream = storageTrie.createReadStream()
Expand Down
2 changes: 1 addition & 1 deletion packages/trie/benchmarks/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createSuite(db: DB) {
}

if (i % (eraSize as number) === 0) {
key = trie.root
key = trie.root()
}
}
})
Expand Down
16 changes: 8 additions & 8 deletions packages/trie/examples/merkle_patricia_trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Let's begin right away with a simple example. Don't worry if things aren't too c
const { Trie } = require('@ethereumjs/trie') // We import the library required to create a basic Merkle Patricia Tree

const trie = new Trie() // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)
```

and then store and retrieve a single key-value pair within it. Note that we needed to convert the strings (`testKey` and `testValue`) to buffers, as that is what the Trie methods expect:
Expand All @@ -55,7 +55,7 @@ async function test() {
const retrievedValue = await trie.get(key) // We retrieve (using "get") the value at key "testKey"
console.log('Value (Bytes): ', retrievedValue)
console.log('Value (String): ', retrievedValue.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand Down Expand Up @@ -86,14 +86,14 @@ const { Trie } = require('@ethereumjs/trie')
const { keccak256 } = require('@ethereumjs/util')

const trie = new Trie() // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)

async function test() {
await trie.put(keccak256(Buffer.from('testKey')), Buffer.from('testValue')) // We update (using "put") the trie with the key-value pair hash("testKey"): "testValue"
const value = await trie.get(keccak256(Buffer.from('testKey'))) // We retrieve (using "get") the value at hash("testKey"_
console.log('Value (Bytes): ', value)
console.log('Value (String): ', value.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand All @@ -115,14 +115,14 @@ Fortunately, we also have an option called "useHashedKeys" that automatically ta
const { Trie } = require('@ethereumjs/trie')// We import the class required to create a secure Merkle Patricia Tree

const trie = new Trie({ useHashedKeys: true }) // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)

async function test() {
await trie.put(Buffer.from('testKey'), Buffer.from('testValue')) // We update (using "put") the trie with the key-value pair "testKey": "testValue"
const value = await trie.get(Buffer.from('testKey')) // We retrieve (using "get") the value at key "testKey"
console.log('Value (Bytes): ', value)
console.log('Value (String): ', value.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand All @@ -147,12 +147,12 @@ async function test() {
await trie.put(key, value) // We update (using "put") the trie with the key-value pair "testKey": "testValue"
const valuePre = await trie.get(key) // We retrieve (using "get") the value at key "testKey"
console.log('Value (String): ', valuePre.toString()) // We retrieve our value
console.log('Updated trie root:', trie.root) // The new trie root
console.log('Updated trie root:', trie.root()) // The new trie root

await trie.del(key)
const valuePost = await trie.get(key) // We try to retrieve the value at (deleted) key "testKey"
console.log('Value at key "testKey": ', valuePost) // Key not found. Value is therefore null.
console.log('Trie root after deletion:', trie.root) // Our trie root is back to its initial value
console.log('Trie root after deletion:', trie.root()) // Our trie root is back to its initial value
}

test()
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/examples/merkle_patricia_trees/example1a.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Trie } = require('../../dist') // We import the library required to create a basic Merkle Patricia Tree

const trie = new Trie() // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)

async function test() {
const key = Buffer.from('testKey')
Expand All @@ -12,7 +12,7 @@ async function test() {
const retrievedValue = await trie.get(key) // We retrieve (using "get") the value at key "testKey"
console.log('Value (Bytes): ', retrievedValue)
console.log('Value (String): ', retrievedValue.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/examples/merkle_patricia_trees/example1b.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const { Trie } = require('../../dist') // We import the library required to crea
const { keccak256 } = require('ethereum-cryptography/keccak')

const trie = new Trie() // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)

async function test() {
await trie.put(keccak256(Buffer.from('testKey')), Buffer.from('testValue')) // We update (using "put") the trie with the key-value pair "testKey": "testValue"
const value = await trie.get(keccak256(Buffer.from('testKey'))) // We retrieve (using "get") the value at key "testKey"
console.log('Value (Bytes): ', value)
console.log('Value (String): ', value.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/examples/merkle_patricia_trees/example1c.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
const { Trie } = require('../../dist') // We import the library required to create a basic Merkle Patricia Tree

const trie = new Trie({ useHashedKeys: true }) // We create an empty Merkle Patricia Tree
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes)
console.log('Empty trie root (Bytes): ', trie.root()) // The trie root (32 bytes)

async function test() {
await trie.put(Buffer.from('testKey'), Buffer.from('testValue')) // We update (using "put") the trie with the key-value pair "testKey": "testValue"
const value = await trie.get(Buffer.from('testKey')) // We retrieve (using "get") the value at key "testKey"
console.log('Value (Bytes): ', value)
console.log('Value (String): ', value.toString())
console.log('Updated trie root:', trie.root) // The new trie root (32 bytes)
console.log('Updated trie root:', trie.root()) // The new trie root (32 bytes)
}

test()
Expand Down
6 changes: 3 additions & 3 deletions packages/trie/examples/merkle_patricia_trees/example1d.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
const { Trie } = require('../../dist') // We import the library required to create a basic Merkle Patricia Tree

const trie = new Trie() // We create an empty Merkle Patricia Tree
console.log('Empty trie root: ', trie.root) // The trie root
console.log('Empty trie root: ', trie.root()) // The trie root

async function test() {
const key = Buffer.from('testKey')
const value = Buffer.from('testValue')
await trie.put(key, value) // We update (using "put") the trie with the key-value pair "testKey": "testValue"
const valuePre = await trie.get(key) // We retrieve (using "get") the value at key "testKey"
console.log('Value (String): ', valuePre.toString()) // We retrieve our value
console.log('Updated trie root:', trie.root) // The new trie root
console.log('Updated trie root:', trie.root()) // The new trie root

await trie.del(key)
const valuePost = await trie.get(key) // We try to retrieve the value at (deleted) key "testKey"
console.log('Value at key "testKey": ', valuePost) // Key not found. Value is therefore null.
console.log('Trie root after deletion:', trie.root) // Our trie root is back to its initial value
console.log('Trie root after deletion:', trie.root()) // Our trie root is back to its initial value
}

test()
Expand Down
Loading

0 comments on commit 00ddc9d

Please sign in to comment.