Skip to content

Commit 0669777

Browse files
committed
Merge branch 'conversion' of github.com-obscure:ethereum/go-ethereum into conversion
2 parents abce680 + 069c87b commit 0669777

16 files changed

Lines changed: 201 additions & 355 deletions

File tree

‎cmd/blocktest/main.go‎

Lines changed: 0 additions & 213 deletions
This file was deleted.

‎common/rlp.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func Encode(object interface{}) []byte {
112112
if object != nil {
113113
switch t := object.(type) {
114114
case *Value:
115-
buff.Write(Encode(t.Raw()))
115+
buff.Write(Encode(t.Val))
116116
case RlpEncodable:
117117
buff.Write(Encode(t.RlpData()))
118118
// Code dup :-/

‎common/rlp_test.go‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"math/big"
66
"reflect"
77
"testing"
8+
9+
"github.com/ethereum/go-ethereum/rlp"
810
)
911

1012
func TestNonInterfaceSlice(t *testing.T) {
@@ -19,13 +21,16 @@ func TestNonInterfaceSlice(t *testing.T) {
1921

2022
func TestRlpValueEncoding(t *testing.T) {
2123
val := EmptyValue()
22-
val.AppendList().Append(1).Append(2).Append(3)
23-
val.Append("4").AppendList().Append(5)
24+
val.AppendList().Append(byte(1)).Append(byte(2)).Append(byte(3))
25+
val.Append("4").AppendList().Append(byte(5))
2426

25-
res := val.Encode()
27+
res, err := rlp.EncodeToBytes(val)
28+
if err != nil {
29+
t.Fatalf("encode error: %v", err)
30+
}
2631
exp := Encode([]interface{}{[]interface{}{1, 2, 3}, "4", []interface{}{5}})
2732
if bytes.Compare(res, exp) != 0 {
28-
t.Errorf("expected %q, got %q", res, exp)
33+
t.Errorf("expected %x, got %x", exp, res)
2934
}
3035
}
3136

@@ -57,9 +62,7 @@ func TestValueSlice(t *testing.T) {
5762
func TestLargeData(t *testing.T) {
5863
data := make([]byte, 100000)
5964
enc := Encode(data)
60-
value := NewValue(enc)
61-
value.Decode()
62-
65+
value := NewValueFromBytes(enc)
6366
if value.Len() != len(data) {
6467
t.Error("Expected data to be", len(data), "got", value.Len())
6568
}
@@ -133,15 +136,16 @@ func TestEncodeDecodeBigInt(t *testing.T) {
133136
}
134137

135138
func TestEncodeDecodeBytes(t *testing.T) {
136-
b := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, byte(6)})
137-
val := NewValueFromBytes(b.Encode())
138-
if !b.Cmp(val) {
139-
t.Errorf("Expected %v, got %v", val, b)
139+
bv := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, []byte{6}})
140+
b, _ := rlp.EncodeToBytes(bv)
141+
val := NewValueFromBytes(b)
142+
if !bv.Cmp(val) {
143+
t.Errorf("Expected %#v, got %#v", bv, val)
140144
}
141145
}
142146

143147
func TestEncodeZero(t *testing.T) {
144-
b := NewValue(0).Encode()
148+
b, _ := rlp.EncodeToBytes(NewValue(0))
145149
exp := []byte{0xc0}
146150
if bytes.Compare(b, exp) == 0 {
147151
t.Error("Expected", exp, "got", b)

‎common/value.go‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@ package common
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"math/big"
78
"reflect"
89
"strconv"
10+
11+
"github.com/ethereum/go-ethereum/rlp"
912
)
1013

11-
// Data values are returned by the rlp decoder. The data values represents
12-
// one item within the rlp data structure. It's responsible for all the casting
13-
// It always returns something valid
14-
type Value struct {
15-
Val interface{}
16-
kind reflect.Value
17-
}
14+
// Value can hold values of certain basic types and provides ways to
15+
// convert between types without bothering to check whether the
16+
// conversion is actually meaningful.
17+
//
18+
// It currently supports the following types:
19+
//
20+
// - int{,8,16,32,64}
21+
// - uint{,8,16,32,64}
22+
// - *big.Int
23+
// - []byte, string
24+
// - []interface{}
25+
//
26+
// Value is useful whenever you feel that Go's types limit your
27+
// ability to express yourself. In these situations, use Value and
28+
// forget about this strong typing nonsense.
29+
type Value struct{ Val interface{} }
1830

1931
func (val *Value) String() string {
2032
return fmt.Sprintf("%x", val.Val)
@@ -38,22 +50,13 @@ func (val *Value) IsNil() bool {
3850
}
3951

4052
func (val *Value) Len() int {
41-
//return val.kind.Len()
4253
if data, ok := val.Val.([]interface{}); ok {
4354
return len(data)
4455
}
4556

4657
return len(val.Bytes())
4758
}
4859

49-
func (val *Value) Raw() interface{} {
50-
return val.Val
51-
}
52-
53-
func (val *Value) Interface() interface{} {
54-
return val.Val
55-
}
56-
5760
func (val *Value) Uint() uint64 {
5861
if Val, ok := val.Val.(uint8); ok {
5962
return uint64(Val)
@@ -260,26 +263,34 @@ func (self *Value) DeepCmp(o *Value) bool {
260263
return bytes.Compare(self.Bytes(), o.Bytes()) == 0
261264
}
262265

263-
func (val *Value) Encode() []byte {
264-
return Encode(val.Val)
266+
func (self *Value) DecodeRLP(s *rlp.Stream) error {
267+
var v interface{}
268+
if err := s.Decode(&v); err != nil {
269+
return err
270+
}
271+
self.Val = v
272+
return nil
265273
}
266274

267-
// Assume that the data we have is encoded
268-
func (self *Value) Decode() {
269-
v, _ := Decode(self.Bytes(), 0)
270-
self.Val = v
271-
//self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes()))
275+
func (self *Value) EncodeRLP(w io.Writer) error {
276+
if self == nil {
277+
w.Write(rlp.EmptyList)
278+
return nil
279+
} else {
280+
return rlp.Encode(w, self.Val)
281+
}
272282
}
273283

284+
// NewValueFromBytes decodes RLP data.
285+
// The contained value will be nil if data contains invalid RLP.
274286
func NewValueFromBytes(data []byte) *Value {
287+
v := new(Value)
275288
if len(data) != 0 {
276-
value := NewValue(data)
277-
value.Decode()
278-
279-
return value
289+
if err := rlp.DecodeBytes(data, v); err != nil {
290+
v.Val = nil
291+
}
280292
}
281-
282-
return NewValue(nil)
293+
return v
283294
}
284295

285296
// Value setters

0 commit comments

Comments
 (0)