From 8ddd4c4c52eef9f382a321fa880adba4a1e35ee2 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 30 Jun 2014 13:09:04 +0200 Subject: wip --- ethchain/state_manager.go | 3 ++- ethchain/state_object.go | 5 ++++- ethchain/state_transition.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) (limited to 'ethchain') diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index 312ba3084..48c6401e3 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -3,6 +3,7 @@ package ethchain import ( "bytes" "container/list" + "fmt" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" @@ -200,7 +201,7 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { } if !block.State().Cmp(state) { - statelogger.Errorf("Invalid merkle root.\nrec: %x\nis: %x", block.State().trie.Root, state.trie.Root) + err = fmt.Errorf("Invalid merkle root.\nrec: %x\nis: %x", block.State().trie.Root, state.trie.Root) return } diff --git a/ethchain/state_object.go b/ethchain/state_object.go index 480b4055d..edac4f6dc 100644 --- a/ethchain/state_object.go +++ b/ethchain/state_object.go @@ -48,7 +48,10 @@ func MakeContract(tx *Transaction, state *State) *StateObject { } func NewStateObject(addr []byte) *StateObject { - return &StateObject{address: addr, Amount: new(big.Int), gasPool: new(big.Int)} + object := &StateObject{address: addr, Amount: new(big.Int), gasPool: new(big.Int)} + object.state = NewState(ethutil.NewTrie(ethutil.Config.Db, "")) + + return object } func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index f84c3486b..4b4cbeb51 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -1,7 +1,9 @@ package ethchain import ( + "bytes" "fmt" + "github.com/ethereum/eth-go/ethutil" "math/big" ) @@ -236,6 +238,8 @@ func (self *StateTransition) transferValue(sender, receiver *StateObject) error 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 @@ -258,5 +262,46 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by ret, _, err = closure.Call(vm, self.data, nil) deepErr = vm.err != nil + /* + if bytes.Compare(testAddr, context.Address()) == 0 { + trie := context.state.trie + trie.NewIterator().Each(func(key string, v *ethutil.Value) { + v.Decode() + fmt.Printf("%x : %x\n", key, v.Str()) + }) + fmt.Println("\n\n") + } + */ + + Paranoia := true + if Paranoia { + var ( + trie = context.state.trie + trie2 = ethutil.NewTrie(ethutil.Config.Db, "") + ) + + trie.NewIterator().Each(func(key string, v *ethutil.Value) { + trie2.Update(key, v.Str()) + }) + + 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()) + }) + + 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()) + }) + + return nil, fmt.Errorf("PARANOIA: Different state object roots during copy"), false + } + } + return } -- cgit From ed276cd7c241749a9cf8add4e2fae3d3608a7ea4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 30 Jun 2014 20:03:31 +0200 Subject: Added Paranoia check for VM execution --- ethchain/state.go | 81 -------------------------------------------- ethchain/state_manager.go | 8 +++-- ethchain/state_test.go | 4 +-- ethchain/state_transition.go | 33 ++++++++---------- ethchain/vm_test.go | 8 ++--- 5 files changed, 26 insertions(+), 108 deletions(-) (limited to 'ethchain') diff --git a/ethchain/state.go b/ethchain/state.go index e28b91909..51d86fe2a 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -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 -} -*/ diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index 48c6401e3..363aa3da7 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -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 } diff --git a/ethchain/state_test.go b/ethchain/state_test.go index 503bdddb4..95be0f373 100644 --- a/ethchain/state_test.go +++ b/ethchain/state_test.go @@ -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)) { diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index 4b4cbeb51..b18091691 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -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()) - }) - - 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("(o): %x\n", trie.Root) + trie.NewIterator().Each(func(key string, v *ethutil.Value) { + v.Decode() + statelogger.Debugf("%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 } diff --git a/ethchain/vm_test.go b/ethchain/vm_test.go index c569d89ae..c8023cd79 100644 --- a/ethchain/vm_test.go +++ b/ethchain/vm_test.go @@ -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, -- cgit From 39263b674c1a8a13a1c29349a48b3cdc4c5948db Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 1 Jul 2014 09:56:10 +0200 Subject: Paranoia --- ethchain/state_transition.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethchain') diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index b18091691..6837f92f7 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -268,7 +268,7 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by } */ - Paranoia := true // TODO Create a flag for this + Paranoia := ethutil.Config.Paranoia if Paranoia { var ( trie = context.state.trie -- cgit From 92693e44599c44e606813d2c3259cc9f6f81a644 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 1 Jul 2014 11:26:45 +0200 Subject: The dragon has been slain. Consensus reached! --- ethchain/state.go | 2 +- ethchain/state_manager.go | 5 +++-- ethchain/state_transition.go | 30 ++++++++++++++---------------- ethchain/vm.go | 29 +++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 21 deletions(-) (limited to 'ethchain') diff --git a/ethchain/state.go b/ethchain/state.go index 51d86fe2a..06a185f59 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -125,7 +125,7 @@ func (self *State) GetOrNewStateObject(addr []byte) *StateObject { } func (self *State) NewStateObject(addr []byte) *StateObject { - statelogger.Infof("(+) %x\n", addr) + //statelogger.Infof("(+) %x\n", addr) stateObject := NewStateObject(addr) self.stateObjects[string(addr)] = stateObject diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index 363aa3da7..b0550607f 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -123,8 +123,9 @@ done: break done default: - //statelogger.Infoln(err) - return nil, nil, nil, err + statelogger.Infoln(err) + err = nil + //return nil, nil, nil, err } } diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index 6837f92f7..94c3de3d9 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -253,26 +253,22 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by Value: self.value, }) vm.Verbose = true - ret, _, err = closure.Call(vm, self.data, nil) - 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) { - v.Decode() - fmt.Printf("%x : %x\n", key, v.Str()) - }) - fmt.Println("\n\n") - } - */ + ret, err, deepErr = Call(vm, closure, self.data) + + return +} + +func Call(vm *Vm, closure *Closure, data []byte) (ret []byte, err error, deepErr bool) { + ret, _, err = closure.Call(vm, data, nil) + deepErr = vm.err != nil Paranoia := ethutil.Config.Paranoia if Paranoia { var ( - trie = context.state.trie - trie2 = ethutil.NewTrie(ethutil.Config.Db, "") + context = closure.object + trie = context.state.trie + trie2 = ethutil.NewTrie(ethutil.Config.Db, "") ) trie.NewIterator().Each(func(key string, v *ethutil.Value) { @@ -282,6 +278,8 @@ 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 { + // TODO FIXME ASAP + context.state.trie = trie2 /* statelogger.Debugf("(o): %x\n", trie.Root) trie.NewIterator().Each(func(key string, v *ethutil.Value) { @@ -296,7 +294,7 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by }) */ - return nil, fmt.Errorf("PARANOIA: Different state object roots during copy"), false + //return nil, fmt.Errorf("PARANOIA: Different state object roots during copy"), false } } diff --git a/ethchain/vm.go b/ethchain/vm.go index 82591e274..3851d0d70 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -346,6 +346,29 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro } else { stack.Push(ethutil.BigFalse) } + + case SLT: + require(2) + x, y := stack.Popn() + vm.Printf(" %v < %v", y, x) + // x < y + if y.Cmp(x) < 0 { + stack.Push(ethutil.BigTrue) + } else { + stack.Push(ethutil.BigFalse) + } + case SGT: + require(2) + x, y := stack.Popn() + vm.Printf(" %v > %v", y, x) + + // x > y + if y.Cmp(x) > 0 { + stack.Push(ethutil.BigTrue) + } else { + stack.Push(ethutil.BigFalse) + } + case EQ: require(2) x, y := stack.Popn() @@ -660,7 +683,8 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro // Create a new callable closure closure := NewClosure(closure, stateObject, stateObject.script, vm.state, gas, closure.Price) // Executer the closure and get the return value (if any) - ret, _, err := closure.Call(vm, args, hook) + //ret, _, err := closure.Call(vm, args, hook) + ret, err, _ := Call(vm, closure, args) if err != nil { stack.Push(ethutil.BigFalse) @@ -699,7 +723,8 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro return closure.Return(nil), nil default: - vmlogger.Debugf("Invalid opcode %x\n", op) + vmlogger.Debugf("(pc) %-3v Invalid opcode %x\n", pc, op) + fmt.Println(Code(closure.Script)) return closure.Return(nil), fmt.Errorf("Invalid opcode %x", op) } -- cgit From e798294a4b2049dc59df6ad4a3bc67c00916cb9f Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 1 Jul 2014 15:25:57 +0200 Subject: Update min GasLimit --- ethchain/block.go | 2 +- ethchain/dagger.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'ethchain') diff --git a/ethchain/block.go b/ethchain/block.go index fee4a2d59..1be8282fa 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -166,7 +166,7 @@ func (block *Block) CalcGasLimit(parent *Block) *big.Int { result := new(big.Int).Add(previous, curInt) result.Div(result, big.NewInt(1024)) - min := ethutil.BigPow(10, 4) + min := big.NewInt(125000) return ethutil.BigMax(min, result) /* diff --git a/ethchain/dagger.go b/ethchain/dagger.go index 08c4826db..b586657b9 100644 --- a/ethchain/dagger.go +++ b/ethchain/dagger.go @@ -31,6 +31,7 @@ func (pow *EasyPow) Search(block *Block, reactChan chan ethutil.React) []byte { for { select { case <-reactChan: + powlogger.Infoln("Breaking from mining") return nil default: i++ -- cgit From d0959063d5f67dccc214f43344d88dce25ca167f Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 1 Jul 2014 15:28:12 +0200 Subject: Up --- ethchain/block_chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethchain') diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index 6e4c72b27..ef56d674c 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -271,7 +271,7 @@ func AddTestNetFunds(block *Block) { for _, addr := range []string{ "51ba59315b3a95761d0863b05ccc7a7f54703d99", "e4157b34ea9615cfbde6b4fda419828124b70c78", - "1e12515ce3e0f817a4ddef9ca55788a1d66bd2df", + "b9c015918bdaba24b4ff057a92a3873d6eb201be", "6c386a4b26f73c802f34673f7248bb118f97424a", "cd2a3d9f938e13cd947ec05abc7fe734df8dd826", "2ef47100e0787b915105fd5e3f4ff6752079d5cb", -- cgit