preimage.go 1.42 KB
Newer Older
1
//go:build !mips
George Hotz's avatar
George Hotz committed
2
// +build !mips
3

4 5 6 7
package oracle

import (
	"fmt"
8
	"io/ioutil"
George Hotz's avatar
George Hotz committed
9 10
	"log"
	"os"
11 12

	"github.com/ethereum/go-ethereum/common"
13
	"github.com/ethereum/go-ethereum/crypto"
14 15 16
)

var preimages = make(map[common.Hash][]byte)
17
var root = "/tmp/cannon"
George Hotz's avatar
George Hotz committed
18 19 20 21 22 23 24 25

func SetRoot(newRoot string) {
	root = newRoot
	err := os.MkdirAll(root, os.ModePerm)
	if err != nil {
		log.Fatal(err)
	}
}
26 27 28

func Preimage(hash common.Hash) []byte {
	val, ok := preimages[hash]
George Hotz's avatar
George Hotz committed
29
	key := fmt.Sprintf("%s/%s", root, hash)
30
	ioutil.WriteFile(key, val, 0644)
31 32 33
	if !ok {
		fmt.Println("can't find preimage", hash)
	}
34
	comphash := crypto.Keccak256Hash(val)
35
	if ok && hash != comphash {
36 37
		panic("corruption in hash " + hash.String())
	}
38 39
	return val
}
40 41 42 43 44

// TODO: Maybe we will want to have a seperate preimages for next block's preimages?
func Preimages() map[common.Hash][]byte {
	return preimages
}
45 46 47 48 49 50 51 52 53 54

// KeyValueWriter wraps the Put method of a backing data store.
type PreimageKeyValueWriter struct{}

// Put inserts the given value into the key-value data store.
func (kw PreimageKeyValueWriter) Put(key []byte, value []byte) error {
	hash := crypto.Keccak256Hash(value)
	if hash != common.BytesToHash(key) {
		panic("bad preimage value write")
	}
George Hotz's avatar
George Hotz committed
55
	preimages[hash] = common.CopyBytes(value)
56 57 58 59 60 61 62 63
	//fmt.Println("tx preimage", hash, common.Bytes2Hex(value))
	return nil
}

// Delete removes the key from the key-value data store.
func (kw PreimageKeyValueWriter) Delete(key []byte) error {
	return nil
}