moved err check

This commit is contained in:
obscuren 2014-12-17 12:57:35 +01:00
parent ef4135eabe
commit b1c58b76a9
6 changed files with 102 additions and 17 deletions

View File

@ -148,6 +148,16 @@ func (bc *ChainManager) Reset() {
bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD()) 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) { func (bc *ChainManager) insert(block *types.Block) {
encodedBlock := block.RlpEncode() encodedBlock := block.RlpEncode()
ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) 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) // XXX Could be optimised by using a different database which only holds hashes (i.e., linked list)
for i := uint64(0); i < max; i++ { for i := uint64(0); i < max; i++ {
chain = append(chain, block.Hash()) chain = append(chain, block.Hash())
if block.Number.Cmp(ethutil.Big0) <= 0 { if block.Number.Cmp(ethutil.Big0) <= 0 {

View File

@ -1 +1,19 @@
package core 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)
}

View File

@ -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 // Skipping transfer is used on testing for the initial call
if !self.SkipTransfer { if !self.SkipTransfer {
err = env.Transfer(from, to, self.value) 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() 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()) chainlogger.Debugf("post state %x\n", env.State().Root())
}() }()
if err != nil { self.object = to
caller.ReturnGas(self.Gas, self.price) // Pre-compiled contracts (address.go) 1, 2 & 3.
naddr := ethutil.BigD(contextAddr).Uint64()
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance) if p := vm.Precompiled[naddr]; p != nil {
} else { if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 {
self.object = to ret = p.Call(self.input)
// Pre-compiled contracts (address.go) 1, 2 & 3. self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
naddr := ethutil.BigD(contextAddr).Uint64() self.vm.Endl()
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)
} }
} else {
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
} }
return return

View File

@ -2,8 +2,10 @@ package ethutil
import ( import (
"bytes" "bytes"
"encoding/binary"
"fmt" "fmt"
"math/big" "math/big"
"reflect"
) )
type RlpEncode interface { type RlpEncode interface {
@ -97,6 +99,14 @@ var (
zeroRlp = big.NewInt(0x0) zeroRlp = big.NewInt(0x0)
) )
func intlen(i int64) (length int) {
for i > 0 {
i = i >> 8
length++
}
return
}
func Encode(object interface{}) []byte { func Encode(object interface{}) []byte {
var buff bytes.Buffer var buff bytes.Buffer
@ -168,6 +178,26 @@ func Encode(object interface{}) []byte {
} }
WriteSliceHeader(len(b.Bytes())) WriteSliceHeader(len(b.Bytes()))
buff.Write(b.Bytes()) buff.Write(b.Bytes())
default:
// This is how it should have been from the start
// needs refactoring (@fjl)
v := reflect.ValueOf(t)
switch v.Kind() {
case reflect.Slice:
var b bytes.Buffer
for i := 0; i < v.Len(); i++ {
b.Write(Encode(v.Index(i).Interface()))
}
blen := b.Len()
if blen < 56 {
buff.WriteByte(byte(blen) + 0xc0)
} else {
buff.WriteByte(byte(intlen(int64(blen))) + 0xf7)
binary.Write(&buff, binary.BigEndian, int64(blen))
}
buff.ReadFrom(&b)
}
} }
} else { } else {
// Empty list for nil // Empty list for nil

View File

@ -7,6 +7,16 @@ import (
"testing" "testing"
) )
func TestNonInterfaceSlice(t *testing.T) {
vala := []string{"value1", "value2", "value3"}
valb := []interface{}{"value1", "value2", "value3"}
resa := Encode(vala)
resb := Encode(valb)
if !bytes.Equal(resa, resb) {
t.Errorf("expected []string & []interface{} to be equal")
}
}
func TestRlpValueEncoding(t *testing.T) { func TestRlpValueEncoding(t *testing.T) {
val := EmptyValue() val := EmptyValue()
val.AppendList().Append(1).Append(2).Append(3) val.AppendList().Append(1).Append(2).Append(3)

View File

@ -121,6 +121,7 @@ func (self *JSRE) initStdFuncs() {
eth.Set("startMining", self.startMining) eth.Set("startMining", self.startMining)
eth.Set("execBlock", self.execBlock) eth.Set("execBlock", self.execBlock)
eth.Set("dump", self.dump) eth.Set("dump", self.dump)
eth.Set("export", self.export)
} }
/* /*
@ -236,3 +237,20 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
return otto.TrueValue() return otto.TrueValue()
} }
func (self *JSRE) export(call otto.FunctionCall) otto.Value {
fn, err := call.Argument(0).ToString()
if err != nil {
fmt.Println(err)
return otto.FalseValue()
}
data := self.ethereum.ChainManager().Export()
if err := ethutil.WriteFile(fn, data); err != nil {
fmt.Println(err)
return otto.FalseValue()
}
return otto.TrueValue()
}