Added Paranoia check for VM execution

This commit is contained in:
obscuren 2014-06-30 20:03:31 +02:00
parent 82272ee08a
commit ed276cd7c2
7 changed files with 57 additions and 114 deletions

View File

@ -204,84 +204,3 @@ func (m *Manifest) AddStorageChange(stateObject *StateObject, storageAddr []byte
m.storageChanges[string(stateObject.Address())][string(storageAddr)] = storage
}
/*
// Resets the trie and all siblings
func (s *State) Reset() {
s.trie.Undo()
// Reset all nested states
for _, state := range s.states {
state.Reset()
}
}
// Syncs the trie and all siblings
func (s *State) Sync() {
// Sync all nested states
for _, state := range s.states {
state.Sync()
}
s.trie.Sync()
}
func (s *State) GetStateObject(addr []byte) *StateObject {
data := s.trie.Get(string(addr))
if data == "" {
return nil
}
stateObject := NewStateObjectFromBytes(addr, []byte(data))
// Check if there's a cached state for this contract
cachedStateObject := s.states[string(addr)]
if cachedStateObject != nil {
//fmt.Printf("get cached #%d %x addr: %x\n", cachedStateObject.trie.Cache().Len(), cachedStateObject.Root(), addr[0:4])
stateObject.state = cachedStateObject
}
return stateObject
}
// Updates any given state object
func (s *State) UpdateStateObject(object *StateObject) {
addr := object.Address()
if object.state != nil && s.states[string(addr)] == nil {
s.states[string(addr)] = object.state
}
ethutil.Config.Db.Put(ethutil.Sha3Bin(object.Script()), object.Script())
s.trie.Update(string(addr), string(object.RlpEncode()))
s.manifest.AddObjectChange(object)
}
func (s *State) GetAccount(addr []byte) (account *StateObject) {
data := s.trie.Get(string(addr))
if data == "" {
account = NewAccount(addr, big.NewInt(0))
} else {
account = NewStateObjectFromBytes(addr, []byte(data))
}
// Check if there's a cached state for this contract
cachedStateObject := s.states[string(addr)]
if cachedStateObject != nil {
account.state = cachedStateObject
}
return
}
func (s *State) Copy() *State {
state := NewState(s.trie.Copy())
for k, subState := range s.states {
state.states[k] = subState.Copy()
}
return state
}
*/

View File

@ -123,7 +123,8 @@ done:
break done
default:
statelogger.Infoln(err)
//statelogger.Infoln(err)
return nil, nil, nil, err
}
}
@ -236,7 +237,10 @@ func (sm *StateManager) ApplyDiff(state *State, parent, block *Block) (receipts
coinbase.SetGasPool(block.CalcGasLimit(parent))
// Process the transactions on to current block
receipts, _, _, _ = sm.ProcessTransactions(coinbase, state, block, parent, block.Transactions())
receipts, _, _, err = sm.ProcessTransactions(coinbase, state, block, parent, block.Transactions())
if err != nil {
return nil, err
}
return receipts, nil
}

View File

@ -16,12 +16,12 @@ func TestSnapshot(t *testing.T) {
state.UpdateStateObject(stateObject)
stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(42))
snapshot := state.Snapshot()
snapshot := state.Copy()
stateObject = state.GetStateObject([]byte("aa"))
stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(43))
state.Revert(snapshot)
state.Set(snapshot)
stateObject = state.GetStateObject([]byte("aa"))
if !stateObject.GetStorage(ethutil.Big("0")).Cmp(ethutil.NewValue(42)) {

View File

@ -226,20 +226,14 @@ func (self *StateTransition) transferValue(sender, receiver *StateObject) error
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount)
}
//if self.value.Cmp(ethutil.Big0) > 0 {
// Subtract the amount from the senders account
sender.SubAmount(self.value)
// Add the amount to receivers account which should conclude this transaction
receiver.AddAmount(self.value)
//statelogger.Debugf("%x => %x (%v)\n", sender.Address()[:4], receiver.Address()[:4], self.value)
//}
return nil
}
var testAddr = ethutil.FromHex("ec4f34c97e43fbb2816cfd95e388353c7181dab1")
func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []byte, err error, deepErr bool) {
var (
block = self.block
@ -263,6 +257,7 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by
deepErr = vm.err != nil
/*
var testAddr = ethutil.FromHex("ec4f34c97e43fbb2816cfd95e388353c7181dab1")
if bytes.Compare(testAddr, context.Address()) == 0 {
trie := context.state.trie
trie.NewIterator().Each(func(key string, v *ethutil.Value) {
@ -273,7 +268,7 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by
}
*/
Paranoia := true
Paranoia := true // TODO Create a flag for this
if Paranoia {
var (
trie = context.state.trie
@ -287,17 +282,19 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by
a := ethutil.NewValue(trie2.Root).Bytes()
b := ethutil.NewValue(context.state.trie.Root).Bytes()
if bytes.Compare(a, b) != 0 {
fmt.Printf("original: %x\n", trie.Root)
trie.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
fmt.Printf("%x : %x\n", key, v.Str())
})
/*
statelogger.Debugf("(o): %x\n", trie.Root)
trie.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
statelogger.Debugf("%x : %x\n", key, v.Str())
})
fmt.Printf("new: %x\n", trie2.Root)
trie2.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
fmt.Printf("%x : %x\n", key, v.Str())
})
statelogger.Debugf("(c): %x\n", trie2.Root)
trie2.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
statelogger.Debugf("%x : %x\n", key, v.Str())
})
*/
return nil, fmt.Errorf("PARANOIA: Different state object roots during copy"), false
}

View File

@ -5,9 +5,7 @@ import (
"fmt"
"github.com/ethereum/eth-go/ethdb"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/mutan"
"math/big"
"strings"
"testing"
)
@ -17,7 +15,7 @@ func TestRun4(t *testing.T) {
db, _ := ethdb.NewMemDatabase()
state := NewState(ethutil.NewTrie(db, ""))
callerScript, err := mutan.Compile(strings.NewReader(`
callerScript, err := ethutil.Compile(`
this.store[this.origin()] = 10**20
hello := "world"
@ -31,7 +29,7 @@ func TestRun4(t *testing.T) {
this.store[to] = this.store[to] + value
}
}
`), false)
`)
if err != nil {
fmt.Println(err)
}
@ -55,7 +53,7 @@ func TestRun4(t *testing.T) {
vm := NewVm(state, nil, RuntimeVars{
Origin: account.Address(),
BlockNumber: 1,
BlockNumber: big.NewInt(1),
PrevHash: ethutil.FromHex("5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"),
Coinbase: ethutil.FromHex("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"),
Time: 1,

View File

@ -173,10 +173,13 @@ func (t *Trie) Update(key string, value string) {
k := CompactHexDecode(key)
root := t.UpdateState(t.Root, k, value)
if _, ok := root.([]byte); !ok {
t.Root = t.cache.PutValue(root, true)
} else {
switch root.(type) {
case string:
t.Root = root
case []byte:
t.Root = root
default:
t.Root = t.cache.PutValue(root, true)
}
}
@ -197,10 +200,13 @@ func (t *Trie) Delete(key string) {
k := CompactHexDecode(key)
root := t.DeleteState(t.Root, k)
if _, ok := root.([]byte); !ok {
t.Root = t.cache.PutValue(root, true)
} else {
switch root.(type) {
case string:
t.Root = root
case []byte:
t.Root = root
default:
t.Root = t.cache.PutValue(root, true)
}
}

View File

@ -242,7 +242,6 @@ func TestRemote(t *testing.T) {
for key, value := range test.In {
trie.Update(get(key), get(value))
}
fmt.Printf("%-15s: %x\n", test.Name, trie.Root)
a := NewValue(h(test.Root)).Bytes()
b := NewValue(trie.Root).Bytes()
@ -271,3 +270,23 @@ func TestTrieReplay(t *testing.T) {
}
})
}
func TestIt(t *testing.T) {
_, trie := New()
test := map[string]string{
"0x000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1": "0x4e616d6552656700000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000045": "0x22b224a1420a802ab51d326e29fa98e34c4f24ea",
"0x0000000000000000000000000000000000000000000000000000000000000046": "0x67706c2076330000000000000000000000000000000000000000000000000000",
"0x000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6": "0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000",
"0x0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2": "0x4655474156000000000000000000000000000000000000000000000000000000",
"0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000": "0x697c7b8c961b56f675d570498424ac8de1a918f6",
"0x4655474156000000000000000000000000000000000000000000000000000000": "0x7ef9e639e2733cb34e4dfc576d4b23f72db776b2",
"0x4e616d6552656700000000000000000000000000000000000000000000000000": "0xec4f34c97e43fbb2816cfd95e388353c7181dab1",
}
for k, v := range test {
trie.Update(k, v)
}
fmt.Printf("root : %x\n", trie.Root)
}