diff options
-rw-r--r-- | cmd/blocktest/main.go | 213 | ||||
-rw-r--r-- | common/rlp.go | 2 | ||||
-rw-r--r-- | common/rlp_test.go | 28 | ||||
-rw-r--r-- | common/value.go | 69 | ||||
-rw-r--r-- | common/value_test.go | 2 | ||||
-rw-r--r-- | crypto/keypair.go | 10 | ||||
-rw-r--r-- | ethdb/memory_database.go | 2 | ||||
-rw-r--r-- | rlp/decode.go | 7 | ||||
-rw-r--r-- | rlp/decode_test.go | 22 |
9 files changed, 87 insertions, 268 deletions
diff --git a/cmd/blocktest/main.go b/cmd/blocktest/main.go deleted file mode 100644 index acbadee77..000000000 --- a/cmd/blocktest/main.go +++ /dev/null @@ -1,213 +0,0 @@ -/* - This file is part of go-ethereum - - go-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - go-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. -*/ -/** - * @authors - * Gustav Simonsson <gustav.simonsson@gmail.com> - * @date 2015 - * - */ - -package main - -import ( - "bytes" - "encoding/hex" - "encoding/json" - "flag" - "fmt" - "io/ioutil" - "log" - "math/big" - "os" - "runtime" - "strings" - - "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/core" - types "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/event" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/rlp" -) - -type Account struct { - Balance string - Code string - Nonce string - Storage map[string]string -} - -type BlockHeader struct { - Bloom string - Coinbase string - Difficulty string - ExtraData string - GasLimit string - GasUsed string - MixHash string - Nonce string - Number string - ParentHash string - ReceiptTrie string - SeedHash string - StateRoot string - Timestamp string - TransactionsTrie string - UncleHash string -} - -type Tx struct { - Data string - GasLimit string - GasPrice string - Nonce string - R string - S string - To string - V string - Value string -} - -type Block struct { - BlockHeader BlockHeader - Rlp string - Transactions []Tx - UncleHeaders []string -} - -type Test struct { - Blocks []Block - GenesisBlockHeader BlockHeader - Pre map[string]Account -} - -func main() { - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "%s <testfile>\n", os.Args[0]) - flag.PrintDefaults() - } - flag.Parse() - - runtime.GOMAXPROCS(runtime.NumCPU()) - logger.AddLogSystem(logger.NewStdLogSystem(os.Stderr, log.LstdFlags, logger.DebugDetailLevel)) - defer func() { logger.Flush() }() - - if len(os.Args) < 2 { - utils.Fatalf("Please specify a test file as the first argument.") - } - blocks, err := loadBlocksFromTestFile(os.Args[1]) - if err != nil { - utils.Fatalf("Could not load blocks: %v", err) - } - - chain := memchain() - chain.ResetWithGenesisBlock(blocks[0]) - if err = chain.InsertChain(types.Blocks{blocks[1]}); err != nil { - utils.Fatalf("Error: %v", err) - } else { - fmt.Println("PASS") - } -} - -func memchain() *core.ChainManager { - blockdb, err := ethdb.NewMemDatabase() - if err != nil { - utils.Fatalf("Could not create in-memory database: %v", err) - } - statedb, err := ethdb.NewMemDatabase() - if err != nil { - utils.Fatalf("Could not create in-memory database: %v", err) - } - return core.NewChainManager(blockdb, statedb, new(event.TypeMux)) -} - -func loadBlocksFromTestFile(filePath string) (blocks types.Blocks, err error) { - fileContent, err := ioutil.ReadFile(filePath) - if err != nil { - return - } - bt := make(map[string]Test) - if err = json.Unmarshal(fileContent, &bt); err != nil { - return - } - - // TODO: support multiple blocks; loop over all blocks - gbh := new(types.Header) - - // Let's use slighlty different namings for the same things, because that's awesome. - gbh.ParentHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ParentHash) - gbh.UncleHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.UncleHash) - gbh.Coinbase, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Coinbase) - gbh.Root, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.StateRoot) - gbh.TxHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.TransactionsTrie) - gbh.ReceiptHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ReceiptTrie) - gbh.Bloom, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Bloom) - - gbh.MixDigest, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.MixHash) - //gbh.SeedHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.SeedHash) - - d, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Difficulty, 10) - gbh.Difficulty = d - - n, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Number, 10) - gbh.Number = n - - gl, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasLimit, 10) - gbh.GasLimit = gl - - gu, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasUsed, 10) - gbh.GasUsed = gu - - ts, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Timestamp, 0) - gbh.Time = ts.Uint64() - - extra, err := hex_decode(bt["SimpleTx"].GenesisBlockHeader.ExtraData) - gbh.Extra = string(extra) // TODO: change ExtraData to byte array - - nonce, _ := hex_decode(bt["SimpleTx"].GenesisBlockHeader.Nonce) - gbh.Nonce = nonce - - if err != nil { - return - } - - gb := types.NewBlockWithHeader(gbh) - //gb.uncles = *new([]*types.Header) - //gb.transactions = *new(types.Transactions) - gb.Td = new(big.Int) - gb.Reward = new(big.Int) - - testBlock := new(types.Block) - - rlpBytes, err := hex_decode(bt["SimpleTx"].Blocks[0].Rlp) - err = rlp.Decode(bytes.NewReader(rlpBytes), &testBlock) - if err != nil { - return - } - - blocks = types.Blocks{ - gb, - testBlock, - } - - return -} - -func hex_decode(s string) (res []byte, err error) { - return hex.DecodeString(strings.TrimPrefix(s, "0x")) -} diff --git a/common/rlp.go b/common/rlp.go index 602f13202..06ac410e9 100644 --- a/common/rlp.go +++ b/common/rlp.go @@ -112,7 +112,7 @@ func Encode(object interface{}) []byte { if object != nil { switch t := object.(type) { case *Value: - buff.Write(Encode(t.Raw())) + buff.Write(Encode(t.Val)) case RlpEncodable: buff.Write(Encode(t.RlpData())) // Code dup :-/ diff --git a/common/rlp_test.go b/common/rlp_test.go index 16a3553d7..2a55da928 100644 --- a/common/rlp_test.go +++ b/common/rlp_test.go @@ -5,6 +5,8 @@ import ( "math/big" "reflect" "testing" + + "github.com/ethereum/go-ethereum/rlp" ) func TestNonInterfaceSlice(t *testing.T) { @@ -19,13 +21,16 @@ func TestNonInterfaceSlice(t *testing.T) { func TestRlpValueEncoding(t *testing.T) { val := EmptyValue() - val.AppendList().Append(1).Append(2).Append(3) - val.Append("4").AppendList().Append(5) + val.AppendList().Append(byte(1)).Append(byte(2)).Append(byte(3)) + val.Append("4").AppendList().Append(byte(5)) - res := val.Encode() + res, err := rlp.EncodeToBytes(val) + if err != nil { + t.Fatalf("encode error: %v", err) + } exp := Encode([]interface{}{[]interface{}{1, 2, 3}, "4", []interface{}{5}}) if bytes.Compare(res, exp) != 0 { - t.Errorf("expected %q, got %q", res, exp) + t.Errorf("expected %x, got %x", exp, res) } } @@ -57,9 +62,7 @@ func TestValueSlice(t *testing.T) { func TestLargeData(t *testing.T) { data := make([]byte, 100000) enc := Encode(data) - value := NewValue(enc) - value.Decode() - + value := NewValueFromBytes(enc) if value.Len() != len(data) { t.Error("Expected data to be", len(data), "got", value.Len()) } @@ -133,15 +136,16 @@ func TestEncodeDecodeBigInt(t *testing.T) { } func TestEncodeDecodeBytes(t *testing.T) { - b := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, byte(6)}) - val := NewValueFromBytes(b.Encode()) - if !b.Cmp(val) { - t.Errorf("Expected %v, got %v", val, b) + bv := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, []byte{6}}) + b, _ := rlp.EncodeToBytes(bv) + val := NewValueFromBytes(b) + if !bv.Cmp(val) { + t.Errorf("Expected %#v, got %#v", bv, val) } } func TestEncodeZero(t *testing.T) { - b := NewValue(0).Encode() + b, _ := rlp.EncodeToBytes(NewValue(0)) exp := []byte{0xc0} if bytes.Compare(b, exp) == 0 { t.Error("Expected", exp, "got", b) diff --git a/common/value.go b/common/value.go index 72a123772..c3893d565 100644 --- a/common/value.go +++ b/common/value.go @@ -3,18 +3,30 @@ package common import ( "bytes" "fmt" + "io" "math/big" "reflect" "strconv" + + "github.com/ethereum/go-ethereum/rlp" ) -// Data values are returned by the rlp decoder. The data values represents -// one item within the rlp data structure. It's responsible for all the casting -// It always returns something valid -type Value struct { - Val interface{} - kind reflect.Value -} +// Value can hold values of certain basic types and provides ways to +// convert between types without bothering to check whether the +// conversion is actually meaningful. +// +// It currently supports the following types: +// +// - int{,8,16,32,64} +// - uint{,8,16,32,64} +// - *big.Int +// - []byte, string +// - []interface{} +// +// Value is useful whenever you feel that Go's types limit your +// ability to express yourself. In these situations, use Value and +// forget about this strong typing nonsense. +type Value struct{ Val interface{} } func (val *Value) String() string { return fmt.Sprintf("%x", val.Val) @@ -38,7 +50,6 @@ func (val *Value) IsNil() bool { } func (val *Value) Len() int { - //return val.kind.Len() if data, ok := val.Val.([]interface{}); ok { return len(data) } @@ -46,14 +57,6 @@ func (val *Value) Len() int { return len(val.Bytes()) } -func (val *Value) Raw() interface{} { - return val.Val -} - -func (val *Value) Interface() interface{} { - return val.Val -} - func (val *Value) Uint() uint64 { if Val, ok := val.Val.(uint8); ok { return uint64(Val) @@ -260,26 +263,34 @@ func (self *Value) DeepCmp(o *Value) bool { return bytes.Compare(self.Bytes(), o.Bytes()) == 0 } -func (val *Value) Encode() []byte { - return Encode(val.Val) +func (self *Value) DecodeRLP(s *rlp.Stream) error { + var v interface{} + if err := s.Decode(&v); err != nil { + return err + } + self.Val = v + return nil } -// Assume that the data we have is encoded -func (self *Value) Decode() { - v, _ := Decode(self.Bytes(), 0) - self.Val = v - //self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes())) +func (self *Value) EncodeRLP(w io.Writer) error { + if self == nil { + w.Write(rlp.EmptyList) + return nil + } else { + return rlp.Encode(w, self.Val) + } } +// NewValueFromBytes decodes RLP data. +// The contained value will be nil if data contains invalid RLP. func NewValueFromBytes(data []byte) *Value { + v := new(Value) if len(data) != 0 { - value := NewValue(data) - value.Decode() - - return value + if err := rlp.DecodeBytes(data, v); err != nil { + v.Val = nil + } } - - return NewValue(nil) + return v } // Value setters diff --git a/common/value_test.go b/common/value_test.go index 09d37802d..38a0e9843 100644 --- a/common/value_test.go +++ b/common/value_test.go @@ -35,7 +35,7 @@ func (s *ValueSuite) TestValueTypes(c *checker.C) { c.Assert(str.Str(), checker.Equals, strExp) c.Assert(num.Uint(), checker.Equals, numExp) - c.Assert(NewValue(inter.Interface()).Cmp(NewValue(interExp)), checker.Equals, true) + c.Assert(NewValue(inter.Val).Cmp(NewValue(interExp)), checker.Equals, true) c.Assert(byt.Bytes(), checker.DeepEquals, bytExp) c.Assert(bigInt.BigInt(), checker.DeepEquals, bigExp) } diff --git a/crypto/keypair.go b/crypto/keypair.go index 6702e6595..cc17328cb 100644 --- a/crypto/keypair.go +++ b/crypto/keypair.go @@ -3,8 +3,8 @@ package crypto import ( "strings" - "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto/secp256k1" ) type KeyPair struct { @@ -48,11 +48,3 @@ func (k *KeyPair) Mnemonic() string { func (k *KeyPair) AsStrings() (string, string, string, string) { return k.Mnemonic(), common.Bytes2Hex(k.Address()), common.Bytes2Hex(k.PrivateKey), common.Bytes2Hex(k.PublicKey) } - -func (k *KeyPair) RlpEncode() []byte { - return k.RlpValue().Encode() -} - -func (k *KeyPair) RlpValue() *common.Value { - return common.NewValue(k.PrivateKey) -} diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index d914f47f8..d4988d0d8 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -49,7 +49,7 @@ func (db *MemDatabase) Print() { for key, val := range db.db { fmt.Printf("%x(%d): ", key, len(key)) node := common.NewValueFromBytes(val) - fmt.Printf("%q\n", node.Interface()) + fmt.Printf("%q\n", node.Val) } } diff --git a/rlp/decode.go b/rlp/decode.go index 6d7e670c2..0e99d9caa 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -367,7 +367,12 @@ func makePtrDecoder(typ reflect.Type) (decoder, error) { dec := func(s *Stream, val reflect.Value) (err error) { _, size, err := s.Kind() if err != nil || size == 0 && s.byteval == 0 { - val.Set(reflect.Zero(typ)) // set to nil + // rearm s.Kind. This is important because the input + // position must advance to the next value even though + // we don't read anything. + s.kind = -1 + // set the pointer to nil. + val.Set(reflect.Zero(typ)) return err } newval := val diff --git a/rlp/decode_test.go b/rlp/decode_test.go index 9f66840b1..0f034d5d8 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -39,7 +39,7 @@ func TestStreamKind(t *testing.T) { s := NewStream(bytes.NewReader(unhex(test.input))) kind, len, err := s.Kind() if err != nil { - t.Errorf("test %d: Type returned error: %v", i, err) + t.Errorf("test %d: Kind returned error: %v", i, err) continue } if kind != test.wantKind { @@ -93,6 +93,23 @@ func TestStreamErrors(t *testing.T) { {"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, EOL}, {"00", calls{"ListEnd"}, errNotInList}, {"C40102", calls{"List", "Uint", "ListEnd"}, errNotAtEOL}, + + // This test verifies that the input position is advanced + // correctly when calling Bytes for empty strings. Kind can be called + // any number of times in between and doesn't advance. + {"C3808080", calls{ + "List", // enter the list + "Bytes", // past first element + + "Kind", "Kind", "Kind", // this shouldn't advance + + "Bytes", // past second element + + "Kind", "Kind", // can't hurt to try + + "Bytes", // past final element + "Bytes", // this one should fail + }, EOL}, } testfor: @@ -314,6 +331,9 @@ var decodeTests = []decodeTest{ {input: "C109", ptr: new(*[]uint), value: &[]uint{9}}, {input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}}, + // check that input position is advanced also empty values. + {input: "C3808005", ptr: new([]*uint), value: []*uint{nil, nil, uintp(5)}}, + // pointer should be reset to nil {input: "05", ptr: sharedPtr, value: uintp(5)}, {input: "80", ptr: sharedPtr, value: (*uint)(nil)}, |