chain maker failure

This commit is contained in:
Péter Szilágyi 2016-01-11 13:38:04 +02:00
parent 34e399463b
commit a2b4d989ca
15 changed files with 207 additions and 91 deletions

View file

@ -185,11 +185,15 @@ func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int,
panic(fmt.Sprintf("state write error: %v", err))
}
h.Root = root
for _, receipt := range b.receipts {
fmt.Printf("%+v\n", receipt)
}
return types.NewBlock(h, b.txs, b.uncles, b.receipts), b.receipts
}
for i := 0; i < n; i++ {
header := makeHeader(parent, statedb)
block, receipt := genblock(i, header)
fmt.Printf("%x\n\n", block.Header().ReceiptHash)
blocks[i] = block
receipts[i] = receipt
parent = block

160
core/index_test.go Normal file
View file

@ -0,0 +1,160 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"bytes"
"fmt"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)
// blackHoleContract is a trivial Ethereum contract that can just store some data
// points in its state trie. It's goal is to have a means to test functionality
// depending on evolving state roots.
//
// contract BlackHole {
// mapping (int256 => uint) public data;
//
// function set(int256 key, uint value) {
// data[key] = value;
// }
// }
var blackHoleContract = common.Hex2Bytes("6060604052605f8060106000396000f3606060405260e060020a60003504639398e0cd81146024578063a22c554014603b575b005b605560043560006020819052908152604090205481565b600435600090815260208190526040902060243590556022565b6060908152602090f3")
var blackHoleSetter = common.Hex2Bytes("a22c5540")
// Tests that state trie indexes are properly constructed for an entire chain of
// imported blocks, cross referencing between various state roots, as well as
// making sure that state updates do not lose reference entries.
func TestChainIndex(t *testing.T) {
// Configure the test chain and ensure we have enough funds to play with
var (
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
addr2 = crypto.PubkeyToAddress(key2.PublicKey)
addr3 = crypto.PubkeyToAddress(key3.PublicKey)
db, _ = ethdb.NewMemDatabase()
)
genesis := WriteGenesisBlockForTesting(db, GenesisAccount{addr1, big.NewInt(10000000000)})
// Generate a chain will all kinds of events happening in it
var contract common.Address
chain, _ := GenerateChain(genesis, db, 8, func(i int, gen *BlockGen) {
switch i {
case 0:
// In block 1, addr1 sends addr2 some ether.
tx, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(key1)
gen.AddTx(tx)
case 1:
// In block 2, addr1 sends some more ether to addr2.
// addr2 passes it on to addr3.
tx1, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key1)
tx2, _ := types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key2)
gen.AddTx(tx1)
gen.AddTx(tx2)
case 2:
// Block 3 is empty but was mined by addr3.
gen.SetCoinbase(addr3)
gen.SetExtra([]byte("yeehaw"))
case 3:
// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
b2 := gen.PrevBlock(1).Header()
b2.Extra = []byte("foo")
gen.AddUncle(b2)
b3 := gen.PrevBlock(2).Header()
b3.Extra = []byte("foo")
gen.AddUncle(b3)
case 4:
// In block 5, we create a simple storage contract to add data entries to
tx, _ := types.NewContractCreation(gen.TxNonce(addr1), big.NewInt(31415), params.GenesisGasLimit, big.NewInt(1), blackHoleContract).SignECDSA(key1)
contract = crypto.CreateAddress(addr1, tx.Nonce())
gen.AddTx(tx)
case 5:
// In block 6, we store a single entry into the storage to check single update indexing
key := common.Hex2BytesFixed("01", 32)
val := common.Hex2BytesFixed("02", 32)
tx, _ := types.NewTransaction(gen.TxNonce(addr1), contract, nil, params.GenesisGasLimit, nil, append(append(blackHoleSetter, key...), val...)).SignECDSA(key1)
gen.AddTx(tx)
case 6:
// In block 7, we store a lot of entries into the storage to check multi update indexing
for i := int64(0); i < 50; i++ {
key := common.Hex2BytesFixed(common.Bytes2Hex(big.NewInt(i).Bytes()), 32)
val := common.Hex2BytesFixed(common.Bytes2Hex(big.NewInt(i+2).Bytes()), 32)
tx, _ := types.NewTransaction(gen.TxNonce(addr1), contract, nil, big.NewInt(45000), nil, append(append(blackHoleSetter, key...), val...)).SignECDSA(key1)
gen.AddTx(tx)
}
}
})
// Import the chain. This runs all block validation rules.
evmux := &event.TypeMux{}
vm.Debug = true
blockchain, _ := NewBlockChain(db, FakePow{}, evmux)
if i, err := blockchain.InsertChain(chain); err != nil {
t.Fatalf("block %d: insert failed: %v\n", i, err)
return
}
// Iterate over all the blocks and check the state trie indexes
indexes := make(map[string]struct{})
for block := uint64(0); block <= blockchain.CurrentBlock().NumberU64(); block++ {
// Gather all the indexes that should be present in the database
root := blockchain.GetBlockByNumber(block).Root()
stateDb, err := state.New(root, db)
if err != nil {
t.Fatalf("failed to create state trie at %x: %v", root, err)
}
fmt.Println(blockchain.GetBlockByNumber(block).Transactions())
for it := state.NewNodeIterator(stateDb); it.Next(); {
if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
fmt.Printf("%d: %x -> %x\n", block, it.Parent, it.Hash)
indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
}
// Cross check the indexes and the database itself
fmt.Println(len(indexes))
for index, _ := range indexes {
if _, err := db.Get([]byte(index)); err != nil {
t.Errorf("failed to retrieve reported index %x: %v", index, err)
}
}
for _, key := range db.Keys() {
if bytes.HasPrefix(key, trie.ParentReferenceIndexPrefix) {
if _, ok := indexes[string(key)]; !ok {
t.Errorf("index entry not reported %x", key)
}
}
}
}

View file

@ -118,7 +118,7 @@ func (c *StateObject) setAddr(addr []byte, value common.Hash) {
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
panic(err)
}
c.trie.Update(addr, v)
c.trie.UpdateIndexed(addr, v, nil)
}
func (self *StateObject) Storage() Storage {

View file

@ -35,7 +35,7 @@ func DeriveSha(list DerivableList) common.Hash {
for i := 0; i < list.Len(); i++ {
keybuf.Reset()
rlp.Encode(keybuf, uint(i))
trie.Update(keybuf.Bytes(), list.GetRlp(i))
trie.UpdateIndexed(keybuf.Bytes(), list.GetRlp(i), nil)
}
return trie.Hash()
}

View file

@ -27,6 +27,7 @@ type ContractRef interface {
ReturnGas(*big.Int, *big.Int)
Address() common.Address
SetCode([]byte)
EachStorage(cb func(key, value []byte))
}
// Contract represents an ethereum contract in the state database. It contains
@ -124,3 +125,9 @@ func (self *Contract) SetCallCode(addr *common.Address, code []byte) {
self.Code = code
self.CodeAddr = addr
}
// EachStorage iterates the contract's storage and calls a method for every key
// value pair.
func (self *Contract) EachStorage(cb func(key, value []byte)) {
self.caller.EachStorage(cb)
}

View file

@ -121,4 +121,5 @@ type Account interface {
Address() common.Address
ReturnGas(*big.Int, *big.Int)
SetCode([]byte)
EachStorage(cb func(key, value []byte))
}

View file

@ -133,6 +133,7 @@ func (account) Balance() *big.Int { return nil }
func (account) Address() common.Address { return common.Address{} }
func (account) ReturnGas(*big.Int, *big.Int) {}
func (account) SetCode([]byte) {}
func (account) EachStorage(cb func(key, value []byte)) {}
func runVmBench(test vmBench, b *testing.B) {
var sender account

View file

@ -376,12 +376,9 @@ func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, st
stck[i] = new(big.Int).Set(item)
}
storage := make(map[common.Hash][]byte)
/*
object := contract.self.(*state.StateObject)
object.EachStorage(func(k, v []byte) {
contract.self.EachStorage(func(k, v []byte) {
storage[common.BytesToHash(k)] = v
})
*/
self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, err})
}
}

View file

@ -37,7 +37,7 @@ func TestIterator(t *testing.T) {
v := make(map[string]bool)
for _, val := range vals {
v[val.k] = false
trie.Update([]byte(val.k), []byte(val.v))
trie.UpdateIndexed([]byte(val.k), []byte(val.v), nil)
}
trie.Commit()

View file

@ -119,14 +119,14 @@ func randomTrie(n int) (*Trie, map[string]*kv) {
for i := byte(0); i < 100; i++ {
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false}
trie.Update(value.k, value.v)
trie.Update(value2.k, value2.v)
trie.UpdateIndexed(value.k, value.v, nil)
trie.UpdateIndexed(value2.k, value2.v, nil)
vals[string(value.k)] = value
vals[string(value2.k)] = value2
}
for i := 0; i < n; i++ {
value := &kv{randBytes(32), randBytes(20), false}
trie.Update(value.k, value.v)
trie.UpdateIndexed(value.k, value.v, nil)
vals[string(value.k)] = value
}
return trie, vals

View file

@ -79,36 +79,6 @@ func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
return t.Trie.TryGet(t.hashKey(key))
}
// Update associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
func (t *SecureTrie) Update(key, value []byte) {
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
// TryUpdate associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryUpdate(key, value []byte) error {
hk := t.hashKey(key)
err := t.Trie.TryUpdate(hk, value)
if err != nil {
return err
}
t.Trie.db.Put(t.secKey(hk), key)
return nil
}
// UpdateIndexed is an extended version of Update, where state trie index entries
// are also generated for all entities referencing the current node.
//

View file

@ -45,7 +45,7 @@ func TestSecureDelete(t *testing.T) {
}
for _, val := range vals {
if val.v != "" {
trie.Update([]byte(val.k), []byte(val.v))
trie.UpdateIndexed([]byte(val.k), []byte(val.v), nil)
} else {
trie.Delete([]byte(val.k))
}
@ -59,7 +59,7 @@ func TestSecureDelete(t *testing.T) {
func TestSecureGetKey(t *testing.T) {
trie := newEmptySecure()
trie.Update([]byte("foo"), []byte("bar"))
trie.UpdateIndexed([]byte("foo"), []byte("bar"), nil)
key := []byte("foo")
value := []byte("bar")

View file

@ -37,17 +37,17 @@ func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
// Map the same data under multiple keys
key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
content[string(key)] = val
trie.Update(key, val)
trie.UpdateIndexed(key, val, nil)
key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
content[string(key)] = val
trie.Update(key, val)
trie.UpdateIndexed(key, val, nil)
// Add some other data to inflate th trie
for j := byte(3); j < 13; j++ {
key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
content[string(key)] = val
trie.Update(key, val)
trie.UpdateIndexed(key, val, nil)
}
}
trie.Commit()

View file

@ -146,30 +146,6 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) {
return tn.(valueNode).Value, nil
}
// Update associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
func (t *Trie) Update(key, value []byte) {
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
// TryUpdate associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
func (t *Trie) TryUpdate(key, value []byte) error {
return t.tryUpdateIndexed(key, value, nil)
}
// UpdateIndexed is an extended version of Update, where state trie index entries
// are also generated for all entities referencing the current node.
//

View file

@ -54,7 +54,7 @@ func TestNull(t *testing.T) {
var trie Trie
key := make([]byte, 32)
value := common.FromHex("0x823140710bf13990e4500136726d8b55")
trie.Update(key, value)
trie.UpdateIndexed(key, value, nil)
value = trie.Get(key)
}
@ -97,7 +97,7 @@ func TestMissingNode(t *testing.T) {
}
trie, _ = New(root, db)
err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
err = trie.TryUpdateIndexed([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"), nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
@ -130,7 +130,7 @@ func TestMissingNode(t *testing.T) {
}
trie, _ = New(root, db)
err = trie.TryUpdate([]byte("120099"), []byte("zxcv"))
err = trie.TryUpdateIndexed([]byte("120099"), []byte("zxcv"), nil)
if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err)
}
@ -304,7 +304,7 @@ func paranoiaCheck(t1 *Trie) (bool, *Trie) {
t2 := new(Trie)
it := NewIterator(t1)
for it.Next() {
t2.Update(it.Key, it.Value)
t2.UpdateIndexed(it.Key, it.Value, nil)
}
return t2.Hash() == t1.Hash(), t2
}
@ -356,8 +356,8 @@ func TestOutput(t *testing.T) {
func TestLargeValue(t *testing.T) {
trie := newEmpty()
trie.Update([]byte("key1"), []byte{99, 99, 99, 99})
trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32))
trie.UpdateIndexed([]byte("key1"), []byte{99, 99, 99, 99}, nil)
trie.UpdateIndexed([]byte("key2"), bytes.Repeat([]byte{1}, 32), nil)
trie.Hash()
}
@ -374,8 +374,8 @@ func TestLargeData(t *testing.T) {
for i := byte(0); i < 255; i++ {
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
trie.Update(value.k, value.v)
trie.Update(value2.k, value2.v)
trie.UpdateIndexed(value.k, value.v, nil)
trie.UpdateIndexed(value2.k, value2.v, nil)
vals[string(value.k)] = value
vals[string(value2.k)] = value2
}
@ -419,7 +419,7 @@ func benchGet(b *testing.B, commit bool) {
k := make([]byte, 32)
for i := 0; i < benchElemCount; i++ {
binary.LittleEndian.PutUint64(k, uint64(i))
trie.Update(k, k)
trie.UpdateIndexed(k, k, nil)
}
binary.LittleEndian.PutUint64(k, benchElemCount/2)
if commit {
@ -437,7 +437,7 @@ func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
k := make([]byte, 32)
for i := 0; i < b.N; i++ {
e.PutUint64(k, uint64(i))
trie.Update(k, k)
trie.UpdateIndexed(k, k, nil)
}
return trie
}
@ -447,7 +447,7 @@ func benchHash(b *testing.B, e binary.ByteOrder) {
k := make([]byte, 32)
for i := 0; i < benchElemCount; i++ {
e.PutUint64(k, uint64(i))
trie.Update(k, k)
trie.UpdateIndexed(k, k, nil)
}
b.ResetTimer()
@ -473,7 +473,7 @@ func getString(trie *Trie, k string) []byte {
}
func updateString(trie *Trie, k, v string) {
trie.Update([]byte(k), []byte(v))
trie.UpdateIndexed([]byte(k), []byte(v), nil)
}
func deleteString(trie *Trie, k string) {