Skip to content

Commit b1c58b7

Browse files
committed
moved err check
1 parent ef4135e commit b1c58b7

File tree

6 files changed

+102
-17
lines changed

6 files changed

+102
-17
lines changed

core/chain_manager.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ func (bc *ChainManager) Reset() {
148148
bc.TD = ethutil.BigD(ethutil.Config.Db.LastKnownTD())
149149
}
150150

151+
func (self *ChainManager) Export() []byte {
152+
chainlogger.Infoln("exporting", self.CurrentBlock.Number, "blocks")
153+
154+
blocks := make(types.Blocks, int(self.CurrentBlock.Number.Int64())+1)
155+
for block := self.CurrentBlock; block != nil; block = self.GetBlock(block.PrevHash) {
156+
blocks[block.Number.Int64()] = block
157+
}
158+
return ethutil.Encode(blocks)
159+
}
160+
151161
func (bc *ChainManager) insert(block *types.Block) {
152162
encodedBlock := block.RlpEncode()
153163
ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock)
@@ -181,7 +191,6 @@ func (self *ChainManager) GetChainHashesFromHash(hash []byte, max uint64) (chain
181191

182192
// XXX Could be optimised by using a different database which only holds hashes (i.e., linked list)
183193
for i := uint64(0); i < max; i++ {
184-
185194
chain = append(chain, block.Hash())
186195

187196
if block.Number.Cmp(ethutil.Big0) <= 0 {

core/chain_manager_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
package core
2+
3+
import (
4+
"fmt"
5+
"path"
6+
"testing"
7+
8+
"github.com/ethereum/go-ethereum/ethutil"
9+
)
10+
11+
func TestChainInsertions(t *testing.T) {
12+
c1, err := ethutil.ReadAllFile(path.Join("..", "_data", "chain1"))
13+
if err != nil {
14+
fmt.Println(err)
15+
t.FailNow()
16+
}
17+
data1, _ := ethutil.Decode([]byte(c1), 0)
18+
fmt.Println(data1)
19+
}

core/execution.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
4040
// Skipping transfer is used on testing for the initial call
4141
if !self.SkipTransfer {
4242
err = env.Transfer(from, to, self.value)
43+
if err != nil {
44+
caller.ReturnGas(self.Gas, self.price)
45+
46+
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
47+
return
48+
}
4349
}
4450

4551
snapshot := env.State().Copy()
@@ -50,23 +56,17 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
5056
chainlogger.Debugf("post state %x\n", env.State().Root())
5157
}()
5258

53-
if err != nil {
54-
caller.ReturnGas(self.Gas, self.price)
55-
56-
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
57-
} else {
58-
self.object = to
59-
// Pre-compiled contracts (address.go) 1, 2 & 3.
60-
naddr := ethutil.BigD(contextAddr).Uint64()
61-
if p := vm.Precompiled[naddr]; p != nil {
62-
if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 {
63-
ret = p.Call(self.input)
64-
self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
65-
self.vm.Endl()
66-
}
67-
} else {
68-
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
59+
self.object = to
60+
// Pre-compiled contracts (address.go) 1, 2 & 3.
61+
naddr := ethutil.BigD(contextAddr).Uint64()
62+
if p := vm.Precompiled[naddr]; p != nil {
63+
if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 {
64+
ret = p.Call(self.input)
65+
self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
66+
self.vm.Endl()
6967
}
68+
} else {
69+
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
7070
}
7171

7272
return

ethutil/rlp.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package ethutil
22

33
import (
44
"bytes"
5+
"encoding/binary"
56
"fmt"
67
"math/big"
8+
"reflect"
79
)
810

911
type RlpEncode interface {
@@ -97,6 +99,14 @@ var (
9799
zeroRlp = big.NewInt(0x0)
98100
)
99101

102+
func intlen(i int64) (length int) {
103+
for i > 0 {
104+
i = i >> 8
105+
length++
106+
}
107+
return
108+
}
109+
100110
func Encode(object interface{}) []byte {
101111
var buff bytes.Buffer
102112

@@ -168,6 +178,26 @@ func Encode(object interface{}) []byte {
168178
}
169179
WriteSliceHeader(len(b.Bytes()))
170180
buff.Write(b.Bytes())
181+
default:
182+
// This is how it should have been from the start
183+
// needs refactoring (@fjl)
184+
v := reflect.ValueOf(t)
185+
switch v.Kind() {
186+
case reflect.Slice:
187+
var b bytes.Buffer
188+
for i := 0; i < v.Len(); i++ {
189+
b.Write(Encode(v.Index(i).Interface()))
190+
}
191+
192+
blen := b.Len()
193+
if blen < 56 {
194+
buff.WriteByte(byte(blen) + 0xc0)
195+
} else {
196+
buff.WriteByte(byte(intlen(int64(blen))) + 0xf7)
197+
binary.Write(&buff, binary.BigEndian, int64(blen))
198+
}
199+
buff.ReadFrom(&b)
200+
}
171201
}
172202
} else {
173203
// Empty list for nil

ethutil/rlp_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77
"testing"
88
)
99

10+
func TestNonInterfaceSlice(t *testing.T) {
11+
vala := []string{"value1", "value2", "value3"}
12+
valb := []interface{}{"value1", "value2", "value3"}
13+
resa := Encode(vala)
14+
resb := Encode(valb)
15+
if !bytes.Equal(resa, resb) {
16+
t.Errorf("expected []string & []interface{} to be equal")
17+
}
18+
}
19+
1020
func TestRlpValueEncoding(t *testing.T) {
1121
val := EmptyValue()
1222
val.AppendList().Append(1).Append(2).Append(3)

javascript/javascript_runtime.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func (self *JSRE) initStdFuncs() {
121121
eth.Set("startMining", self.startMining)
122122
eth.Set("execBlock", self.execBlock)
123123
eth.Set("dump", self.dump)
124+
eth.Set("export", self.export)
124125
}
125126

126127
/*
@@ -236,3 +237,20 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
236237

237238
return otto.TrueValue()
238239
}
240+
241+
func (self *JSRE) export(call otto.FunctionCall) otto.Value {
242+
fn, err := call.Argument(0).ToString()
243+
if err != nil {
244+
fmt.Println(err)
245+
return otto.FalseValue()
246+
}
247+
248+
data := self.ethereum.ChainManager().Export()
249+
250+
if err := ethutil.WriteFile(fn, data); err != nil {
251+
fmt.Println(err)
252+
return otto.FalseValue()
253+
}
254+
255+
return otto.TrueValue()
256+
}

0 commit comments

Comments
 (0)