From b1c58b76a9588a90db5a773a997bb70265c378d3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 17 Dec 2014 12:57:35 +0100 Subject: moved err check --- core/chain_manager.go | 11 ++++++++++- core/chain_manager_test.go | 18 ++++++++++++++++++ core/execution.go | 32 ++++++++++++++++---------------- 3 files changed, 44 insertions(+), 17 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index edf50e715..3e48579b9 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -148,6 +148,16 @@ func (bc *ChainManager) Reset() { bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) } +func (self *ChainManager) Export() []byte { + chainlogger.Infoln("exporting", self.CurrentBlock.Number, "blocks") + + blocks := make(types.Blocks, int(self.CurrentBlock.Number.Int64())+1) + for block := self.CurrentBlock; block != nil; block = self.GetBlock(block.PrevHash) { + blocks[block.Number.Int64()] = block + } + return ethutil.Encode(blocks) +} + func (bc *ChainManager) insert(block *types.Block) { encodedBlock := block.RlpEncode() ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) @@ -181,7 +191,6 @@ func (self *ChainManager) GetChainHashesFromHash(hash []byte, max uint64) (chain // XXX Could be optimised by using a different database which only holds hashes (i.e., linked list) for i := uint64(0); i < max; i++ { - chain = append(chain, block.Hash()) if block.Number.Cmp(ethutil.Big0) <= 0 { diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 9a8bc9592..a84e3ff3b 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -1 +1,19 @@ package core + +import ( + "fmt" + "path" + "testing" + + "github.com/ethereum/go-ethereum/ethutil" +) + +func TestChainInsertions(t *testing.T) { + c1, err := ethutil.ReadAllFile(path.Join("..", "_data", "chain1")) + if err != nil { + fmt.Println(err) + t.FailNow() + } + data1, _ := ethutil.Decode([]byte(c1), 0) + fmt.Println(data1) +} diff --git a/core/execution.go b/core/execution.go index a464abc66..58d46c509 100644 --- a/core/execution.go +++ b/core/execution.go @@ -40,6 +40,12 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret // Skipping transfer is used on testing for the initial call if !self.SkipTransfer { err = env.Transfer(from, to, self.value) + if err != nil { + caller.ReturnGas(self.Gas, self.price) + + err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance) + return + } } snapshot := env.State().Copy() @@ -50,23 +56,17 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret chainlogger.Debugf("post state %x\n", env.State().Root()) }() - if err != nil { - caller.ReturnGas(self.Gas, self.price) - - err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance) - } else { - self.object = to - // Pre-compiled contracts (address.go) 1, 2 & 3. - naddr := ethutil.BigD(contextAddr).Uint64() - if p := vm.Precompiled[naddr]; p != nil { - if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 { - ret = p.Call(self.input) - self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret) - self.vm.Endl() - } - } else { - ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) + self.object = to + // Pre-compiled contracts (address.go) 1, 2 & 3. + naddr := ethutil.BigD(contextAddr).Uint64() + if p := vm.Precompiled[naddr]; p != nil { + if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 { + ret = p.Call(self.input) + self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret) + self.vm.Endl() } + } else { + ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) } return -- cgit From 4dbdcaecb117d7e1fcaf0869f5d4602312552991 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 17 Dec 2014 23:58:52 +0100 Subject: Moved pre-compiled, moved depth check * Depth check has been moved to the execution * Pre compiled execution has been moved to the VM * PrecompiledAddress has been renamed to PrecompiledAccount --- core/execution.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/execution.go b/core/execution.go index 58d46c509..827e1ee0e 100644 --- a/core/execution.go +++ b/core/execution.go @@ -4,7 +4,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -36,6 +35,11 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret env := self.vm.Env() chainlogger.Debugf("pre state %x\n", env.State().Root()) + if self.vm.Env().Depth() == vm.MaxCallDepth { + // Consume all gas (by not returning it) and return a depth error + return nil, vm.DepthError{} + } + from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address) // Skipping transfer is used on testing for the initial call if !self.SkipTransfer { @@ -50,24 +54,14 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret snapshot := env.State().Copy() defer func() { - if vm.IsDepthErr(err) || vm.IsOOGErr(err) { + if /*vm.IsDepthErr(err) ||*/ vm.IsOOGErr(err) { env.State().Set(snapshot) } chainlogger.Debugf("post state %x\n", env.State().Root()) }() self.object = to - // Pre-compiled contracts (address.go) 1, 2 & 3. - naddr := ethutil.BigD(contextAddr).Uint64() - if p := vm.Precompiled[naddr]; p != nil { - if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 { - ret = p.Call(self.input) - self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret) - self.vm.Endl() - } - } else { - ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) - } + ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) return } -- cgit