From 5ede1224e48fd82961bd4a0b2ec1a3eda0b6d99b Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 1 Aug 2014 10:21:43 +0200 Subject: [PATCH] minor rlp things --- ethutil/rlp.go | 21 ++++++++------------- ethutil/value.go | 7 +++++-- peer.go | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ethutil/rlp.go b/ethutil/rlp.go index 333a84927..cf7f97ffd 100644 --- a/ethutil/rlp.go +++ b/ethutil/rlp.go @@ -32,12 +32,14 @@ const ( RlpEmptyStr = 0x40 ) +const rlpEof = -1 + func Char(c []byte) int { if len(c) > 0 { return int(c[0]) } - return 0 + return rlpEof } func DecodeWithReader(reader *bytes.Buffer) interface{} { @@ -46,8 +48,6 @@ func DecodeWithReader(reader *bytes.Buffer) interface{} { // Read the next byte char := Char(reader.Next(1)) switch { - case char == 0: - return nil case char <= 0x7f: return char @@ -63,11 +63,7 @@ func DecodeWithReader(reader *bytes.Buffer) interface{} { length := int(char - 0xc0) for i := 0; i < length; i++ { obj := DecodeWithReader(reader) - if obj != nil { - slice = append(slice, obj) - } else { - break - } + slice = append(slice, obj) } return slice @@ -75,13 +71,12 @@ func DecodeWithReader(reader *bytes.Buffer) interface{} { length := ReadVarInt(reader.Next(int(char - 0xf7))) for i := uint64(0); i < length; i++ { obj := DecodeWithReader(reader) - if obj != nil { - slice = append(slice, obj) - } else { - break - } + slice = append(slice, obj) } + + return slice default: + panic(fmt.Sprintf("byte not supported: %q", char)) } return slice diff --git a/ethutil/value.go b/ethutil/value.go index 85dc44ed6..3128cd724 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -221,12 +221,15 @@ func (val *Value) Encode() []byte { func (self *Value) Decode() { v, _ := Decode(self.Bytes(), 0) self.Val = v + //self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes())) } func NewValueFromBytes(data []byte) *Value { if len(data) != 0 { - data, _ := Decode(data, 0) - return NewValue(data) + value := NewValue(data) + value.Decode() + + return value } return NewValue(nil) diff --git a/peer.go b/peer.go index 4b0523e70..fa683e488 100644 --- a/peer.go +++ b/peer.go @@ -403,7 +403,7 @@ func (p *Peer) HandleInbound() { } } } - if !p.ethereum.StateManager().BlockChain().HasBlock(lastBlock.Hash()) { + if !blockChain.HasBlock(lastBlock.Hash()) { // If we can't find a common ancenstor we need to request more blocks. // FIXME: At one point this won't scale anymore since we are not asking for an offset // we just keep increasing the amount of blocks.