preimage.go 1.26 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"
9 10

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

var preimages = make(map[common.Hash][]byte)

func Preimage(hash common.Hash) []byte {
	val, ok := preimages[hash]
18 19
	key := fmt.Sprintf("/tmp/eth/%s", hash)
	ioutil.WriteFile(key, val, 0644)
20 21 22
	if !ok {
		fmt.Println("can't find preimage", hash)
	}
23
	comphash := crypto.Keccak256Hash(val)
24
	if ok && hash != comphash {
25 26
		panic("corruption in hash " + hash.String())
	}
27 28
	return val
}
29 30 31 32 33

// TODO: Maybe we will want to have a seperate preimages for next block's preimages?
func Preimages() map[common.Hash][]byte {
	return preimages
}
34 35 36 37 38 39 40 41 42 43

// 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
44
	preimages[hash] = common.CopyBytes(value)
45 46 47 48 49 50 51 52
	//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
}