From 5f2f196032b6cb1538e7c8430a893367800ab43f Mon Sep 17 00:00:00 2001 From: wanderer Date: Mon, 18 Jun 2018 19:43:56 -0400 Subject: [PATCH] should throw on invalid stateRoots --- index.js | 5 ++++- tests/index.js | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d55c951..0e207fd 100644 --- a/index.js +++ b/index.js @@ -149,7 +149,10 @@ module.exports = class Hypervisor { async setStateRoot (stateRoot) { this.tree.root = stateRoot const node = await this.tree.get(Buffer.from([0])) - this.nonce = node.value || 0 + if (!node.value) { + throw new Error('invalid state root') + } + this.nonce = node.value } /** diff --git a/tests/index.js b/tests/index.js index 2a7d622..b6babb3 100644 --- a/tests/index.js +++ b/tests/index.js @@ -266,7 +266,7 @@ tape('three communicating actors, with tick counting', async t => { }) tape('errors', async t => { - t.plan(3) + t.plan(4) const expectedState = Buffer.from('25bc7e81511bfded44a1846f4bca1acc99f24273', 'hex') const tree = new RadixTree({ db @@ -306,6 +306,12 @@ tape('errors', async t => { hypervisor.send(message) const stateRoot = await hypervisor.createStateRoot() t.deepEquals(stateRoot, expectedState, 'expected root!') + + try { + await hypervisor.setStateRoot(Buffer.from([0])) + } catch (e) { + t.pass('should catch invalid state roots') + } }) tape('out-of-gas', async t => {