|
| 1 | +const { promisify } = require('util') |
| 2 | +const tape = require('tape') |
| 3 | +const Trie = require('merkle-patricia-tree/secure.js') |
| 4 | +const Account = require('ethereumjs-account') |
| 5 | +const Cache = require('../../lib/cache') |
| 6 | +const utils = require('./utils') |
| 7 | + |
| 8 | +tape('cache initialization', (t) => { |
| 9 | + t.test('should initialize', async (st) => { |
| 10 | + const trie = new Trie() |
| 11 | + const c = new Cache(trie) |
| 12 | + st.ok(trie.root.equals(c._trie.root), 'initializes given trie') |
| 13 | + st.end() |
| 14 | + }) |
| 15 | +}) |
| 16 | + |
| 17 | +tape('cache put and get account', (t) => { |
| 18 | + const trie = new Trie() |
| 19 | + const c = new Cache(trie) |
| 20 | + const flushP = promisify(c.flush.bind(c)) |
| 21 | + const trieGetP = promisify(trie.get.bind(trie)) |
| 22 | + |
| 23 | + const addr = Buffer.from('cd2a3d9f938e13cd947ec05abc7fe734df8dd826', 'hex') |
| 24 | + const acc = utils.createAccount('0x00', '0xff11') |
| 25 | + |
| 26 | + t.test('should fail to get non-existent account', async (st) => { |
| 27 | + const res = c.get(addr) |
| 28 | + st.notOk(res.balance.equals(acc.balance)) |
| 29 | + st.end() |
| 30 | + }) |
| 31 | + |
| 32 | + t.test('should put account', async (st) => { |
| 33 | + c.put(addr, acc) |
| 34 | + const res = c.get(addr) |
| 35 | + st.ok(res.balance.equals(acc.balance)) |
| 36 | + st.end() |
| 37 | + }) |
| 38 | + |
| 39 | + t.test('should not have flushed to trie', async (st) => { |
| 40 | + const res = await trieGetP(addr) |
| 41 | + st.notOk(res) |
| 42 | + st.end() |
| 43 | + }) |
| 44 | + |
| 45 | + t.test('should flush to trie', async (st) => { |
| 46 | + await flushP() |
| 47 | + st.end() |
| 48 | + }) |
| 49 | + |
| 50 | + t.test('trie should contain flushed account', async (st) => { |
| 51 | + const raw = await trieGetP(addr) |
| 52 | + const res = new Account(raw) |
| 53 | + st.ok(res.balance.equals(acc.balance)) |
| 54 | + st.end() |
| 55 | + }) |
| 56 | + |
| 57 | + t.test('should delete account from cache', async (st) => { |
| 58 | + c.del(addr) |
| 59 | + |
| 60 | + const res = c.get(addr) |
| 61 | + st.notOk(res.balance.equals(acc.balance)) |
| 62 | + st.end() |
| 63 | + }) |
| 64 | + |
| 65 | + t.test('should warm cache and load account from trie', async (st) => { |
| 66 | + await promisify(c.warm.bind(c))([addr]) |
| 67 | + |
| 68 | + const res = c.get(addr) |
| 69 | + st.ok(res.balance.equals(acc.balance)) |
| 70 | + st.end() |
| 71 | + }) |
| 72 | + |
| 73 | + t.test('should update loaded account and flush it', async (st) => { |
| 74 | + const updatedAcc = utils.createAccount('0x00', '0xff00') |
| 75 | + c.put(addr, updatedAcc) |
| 76 | + await flushP() |
| 77 | + |
| 78 | + const raw = await trieGetP(addr) |
| 79 | + const res = new Account(raw) |
| 80 | + st.ok(res.balance.equals(updatedAcc.balance)) |
| 81 | + st.end() |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +tape('cache checkpointing', (t) => { |
| 86 | + const trie = new Trie() |
| 87 | + const c = new Cache(trie) |
| 88 | + |
| 89 | + const addr = Buffer.from('cd2a3d9f938e13cd947ec05abc7fe734df8dd826', 'hex') |
| 90 | + const acc = utils.createAccount('0x00', '0xff11') |
| 91 | + const updatedAcc = utils.createAccount('0x00', '0xff00') |
| 92 | + |
| 93 | + t.test('should revert to correct state', async (st) => { |
| 94 | + c.put(addr, acc) |
| 95 | + c.checkpoint() |
| 96 | + c.put(addr, updatedAcc) |
| 97 | + |
| 98 | + let res = c.get(addr) |
| 99 | + st.ok(res.balance.equals(updatedAcc.balance)) |
| 100 | + |
| 101 | + c.revert() |
| 102 | + |
| 103 | + res = c.get(addr) |
| 104 | + st.ok(res.balance.equals(acc.balance)) |
| 105 | + |
| 106 | + st.end() |
| 107 | + }) |
| 108 | +}) |
0 commit comments