Commit daada814 authored by George Hotz's avatar George Hotz

add broken trie test

parent ce242d9f
......@@ -22,7 +22,7 @@ mipsevm -- A MIPS runtime in the EVM (see also contracts/)
## TODO
* Support fast generation of a specific state from the checkpoints
** Load into Unicorn/evm from the trie
* Load into Unicorn/evm from the trie
* Write binary search "responder"
* Deploy to cheapETH!
......
......@@ -29,6 +29,14 @@ func TrieToJson(root common.Hash, step int) []byte {
return b
}
func TrieFromJson(dat []byte) (common.Hash, int) {
var j Jtree
err := json.Unmarshal(dat, &j)
check(err)
Preimages = j.Preimages
return j.Root, j.Step
}
// TODO: this is copied from the oracle
func (kw PreimageKeyValueWriter) Put(key []byte, value []byte) error {
hash := crypto.Keccak256Hash(value)
......@@ -80,6 +88,12 @@ func ParseNode(node common.Hash, depth int, callback func(common.Hash) []byte) {
ParseNodeInternal(elems, depth, callback)
}
func RamFromTrie(root common.Hash) map[uint32](uint32) {
ram := make(map[uint32](uint32))
// TODO: write this
return ram
}
func RamToTrie(ram map[uint32](uint32)) common.Hash {
mt := trie.NewStackTrie(PreimageKeyValueWriter{})
......
......@@ -3,6 +3,7 @@ package main
import (
"fmt"
"io/ioutil"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
......@@ -20,6 +21,21 @@ func TestTrie(t *testing.T) {
dat := TrieToJson(root, -1)
fmt.Println("serialized length is", len(dat))
ioutil.WriteFile("/tmp/cannon/ramtrie.json", dat, 0644)
// load the trie
oldPreLen := len(Preimages)
Preimages = make(map[common.Hash][]byte)
dat, err := ioutil.ReadFile("/tmp/cannon/ramtrie.json")
check(err)
newroot, _ := TrieFromJson(dat)
if root != newroot {
t.Fatal("loaded root mismatch")
}
if len(Preimages) != oldPreLen {
t.Fatal("preimage length mismatch")
}
// TODO: load memory when ready
}
func printRoot(ram map[uint32](uint32)) {
......@@ -35,6 +51,19 @@ func printTrie(ram map[uint32](uint32)) {
})
}
func TestToFromTrie(t *testing.T) {
ram := make(map[uint32](uint32))
ram[0] = 1
ram[4] = 2
trie := RamToTrie(ram)
newram := RamFromTrie(trie)
if !reflect.DeepEqual(ram, newram) {
t.Fatal("ram to/from mismatch")
}
}
func TestBuggedTrie(t *testing.T) {
ram := make(map[uint32](uint32))
......
......@@ -67,13 +67,20 @@ describe("MIPSMemory contract", function () {
for (var i = 0; i < 100; i++) {
const keys = Object.keys(kv)
const choice = Math.random()
if (choice < 0.5 || keys.length == 0) {
if (choice < 0.3 || keys.length == 0) {
// write new key
const key = randint(0x100)*4
const value = randint(0x100000000)
console.log("writing", key, value)
root = await write(mm, root, key, value)
kv[key] = value
} else if (choice < 0.5) {
// write new high key
const key = randint(0x100)*4 + 0x10000000
const value = randint(0x100000000)
console.log("writing", key, value)
root = await write(mm, root, key, value)
kv[key] = value
} else if (choice > 0.7) {
// read old key
const idx = randint(keys.length)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment