From 9f3169b4ca8fc4abc7cbfc1b33b0bb6393fe3b1f Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 13 Jul 2018 12:33:45 -0700 Subject: [PATCH] using rlp for serialize/deserialize; closes #50 --- common/coder/coder.go | 18 +++++++ core/chain/mainchain/main_block_unit_test.go | 52 +++++++++++++++++++- core/chain/mainchain/mainblock.go | 11 ++++- core/chain/mainchain/types.go | 4 +- core/chain/statechain/diff.go | 11 ++++- core/chain/statechain/stateblock.go | 11 ++++- core/chain/statechain/transaction.go | 10 ++-- core/chain/statechain/types.go | 8 +-- 8 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 common/coder/coder.go diff --git a/common/coder/coder.go b/common/coder/coder.go new file mode 100644 index 0000000..ab3b9f7 --- /dev/null +++ b/common/coder/coder.go @@ -0,0 +1,18 @@ +package coder + +import "github.com/ethereum/go-ethereum/rlp" + +// Serialize ... +func Serialize(v interface{}) ([]byte, error) { + return rlp.EncodeToBytes(v) +} + +// Deserialize ... +// note: v must be a pointer +func Deserialize(data []byte, v interface{}) error { + if err := rlp.DecodeBytes(data, v); err != nil { + return err + } + + return nil +} diff --git a/core/chain/mainchain/main_block_unit_test.go b/core/chain/mainchain/main_block_unit_test.go index ca8fa89..8bf9b25 100755 --- a/core/chain/mainchain/main_block_unit_test.go +++ b/core/chain/mainchain/main_block_unit_test.go @@ -13,7 +13,11 @@ import ( var ( hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - p = Props{ + bSig = BlockSig{ + R: "0x1", + S: "0x1", + } + p = Props{ BlockHash: &hash, BlockNumber: "0x1", BlockTime: "0x5", @@ -22,6 +26,8 @@ var ( PrevBlockHash: "prevBlockHash", Nonce: "0x1", Difficulty: "0x1", + MinerAddress: "0x123", + MinerSig: &bSig, } p1 = Props{ BlockHash: nil, @@ -32,6 +38,8 @@ var ( PrevBlockHash: "prevBlockHash", Nonce: "0x1", Difficulty: "0x1", + MinerAddress: "", + MinerSig: nil, } s = `{ "blockHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", @@ -227,6 +235,48 @@ func TestGenesisBlockHash(t *testing.T) { } } +func TestSerializeDeserialize(t *testing.T) { + b := &Block{ + props: p, + } + b1 := &Block{ + props: p1, + } + + inputs := []*Block{b, b1} + for idx, in := range inputs { + bytes, err := in.Serialize() + if err != nil { + t.Fatalf("test %d failed\nerr serializing: %v", idx+1, err) + } + + tmpBlock := new(Block) + if err := tmpBlock.Deserialize(bytes); err != nil { + t.Fatalf("test %d failed\nerr deserializing: %v", idx+1, err) + } + + if !reflect.DeepEqual(in, tmpBlock) { + if tmpBlock.props.BlockHash != nil { + t.Logf("block hash %s", *tmpBlock.props.BlockHash) + } + t.Errorf("test #%d faild\nexpected: %v\nreceived: %v\n", idx+1, *in, *tmpBlock) + } + } +} + +func TestDeepEqual(t *testing.T) { + b := &Block{ + props: p, + } + b1 := &Block{ + props: p, + } + + if !reflect.DeepEqual(b, b1) { + t.Error("not equal") + } +} + func removeWhiteSpace(str string) string { return strings.Map(func(r rune) rune { if unicode.IsSpace(r) { diff --git a/core/chain/mainchain/mainblock.go b/core/chain/mainchain/mainblock.go index 5404f4a..e8a19d4 100755 --- a/core/chain/mainchain/mainblock.go +++ b/core/chain/mainchain/mainblock.go @@ -3,6 +3,7 @@ package mainchain import ( "encoding/json" + "github.com/c3systems/c3/common/coder" "github.com/c3systems/c3/common/hashing" "github.com/c3systems/c3/common/hexutil" "github.com/c3systems/merkletree" @@ -31,12 +32,18 @@ func (b *Block) Props() Props { // Serialize ... func (b *Block) Serialize() ([]byte, error) { - return b.MarshalJSON() + return coder.Serialize(b.props) } // Deserialize ... func (b *Block) Deserialize(data []byte) error { - return b.UnmarshalJSON(data) + var tmpProps Props + if err := coder.Deserialize(data, &tmpProps); err != nil { + return err + } + + b.props = tmpProps + return nil } // SerializeString ... diff --git a/core/chain/mainchain/types.go b/core/chain/mainchain/types.go index 249a71b..c8faa1a 100644 --- a/core/chain/mainchain/types.go +++ b/core/chain/mainchain/types.go @@ -40,7 +40,7 @@ type BlockSig struct { // Props ... type Props struct { - BlockHash *string `json:"blockHash,omitempty"` + BlockHash *string `json:"blockHash,omitempty" rlp:"nil"` BlockNumber string `json:"blockNumber"` BlockTime string `json:"blockTime"` // unix timestamp ImageHash string `json:"imageHash"` @@ -49,7 +49,7 @@ type Props struct { Nonce string `json:"nonce"` Difficulty string `json:"difficulty"` MinerAddress string `json:"minerAddress"` - MinerSig *BlockSig `json:"blockSig,omitempty"` + MinerSig *BlockSig `json:"blockSig,omitempty" rlp:"nil"` } // Block ... diff --git a/core/chain/statechain/diff.go b/core/chain/statechain/diff.go index de39b3e..207af08 100644 --- a/core/chain/statechain/diff.go +++ b/core/chain/statechain/diff.go @@ -3,6 +3,7 @@ package statechain import ( "encoding/json" + "github.com/c3systems/c3/common/coder" "github.com/c3systems/c3/common/hashing" "github.com/c3systems/c3/common/hexutil" "github.com/c3systems/merkletree" @@ -26,12 +27,18 @@ func (d Diff) Props() DiffProps { // Serialize ... func (d *Diff) Serialize() ([]byte, error) { - return d.MarshalJSON() + return coder.Serialize(d.props) } // Deserialize ... func (d *Diff) Deserialize(data []byte) error { - return d.UnmarshalJSON(data) + var tmpProps DiffProps + if err := coder.Deserialize(data, &tmpProps); err != nil { + return err + } + + d.props = tmpProps + return nil } // SerializeString ... diff --git a/core/chain/statechain/stateblock.go b/core/chain/statechain/stateblock.go index 602c289..f527647 100755 --- a/core/chain/statechain/stateblock.go +++ b/core/chain/statechain/stateblock.go @@ -3,6 +3,7 @@ package statechain import ( "encoding/json" + "github.com/c3systems/c3/common/coder" "github.com/c3systems/c3/common/hashing" "github.com/c3systems/c3/common/hexutil" @@ -27,12 +28,18 @@ func (b Block) Props() BlockProps { // Serialize ... func (b *Block) Serialize() ([]byte, error) { - return b.MarshalJSON() + return coder.Serialize(b.props) } // Deserialize ... func (b *Block) Deserialize(data []byte) error { - return b.UnmarshalJSON(data) + var tmpProps BlockProps + if err := coder.Deserialize(data, &tmpProps); err != nil { + return err + } + + b.props = tmpProps + return nil } // SerializeString ... diff --git a/core/chain/statechain/transaction.go b/core/chain/statechain/transaction.go index 1848eb5..7f7ed49 100755 --- a/core/chain/statechain/transaction.go +++ b/core/chain/statechain/transaction.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "encoding/json" + "github.com/c3systems/c3/common/coder" "github.com/c3systems/c3/common/hashing" "github.com/c3systems/c3/common/hexutil" "github.com/c3systems/c3/core/c3crypto" @@ -28,18 +29,17 @@ func (tx *Transaction) Props() TransactionProps { // Serialize ... func (tx *Transaction) Serialize() ([]byte, error) { - return tx.MarshalJSON() + return coder.Serialize(tx.props) } // Deserialize ... func (tx *Transaction) Deserialize(data []byte) error { - var props TransactionProps - if err := json.Unmarshal(data, &props); err != nil { + var tmpProps TransactionProps + if err := coder.Deserialize(data, &tmpProps); err != nil { return err } - tx.props = props - + tx.props = tmpProps return nil } diff --git a/core/chain/statechain/types.go b/core/chain/statechain/types.go index f855673..e9a9381 100755 --- a/core/chain/statechain/types.go +++ b/core/chain/statechain/types.go @@ -28,12 +28,12 @@ type TransactionsMap map[string][]*Transaction // TransactionProps ... type TransactionProps struct { - TxHash *string `json:"txHash,omitempty"` + TxHash *string `json:"txHash,omitempty" rlp:"nil"` ImageHash string `json:"imageHash"` Method string `json:"method"` Payload interface{} `json:"payload"` From string `json:"from"` - Sig *TxSig `json:"txSig,omitempty"` + Sig *TxSig `json:"txSig,omitempty" rlp:"nil"` } // Transaction ... @@ -43,7 +43,7 @@ type Transaction struct { // BlockProps ... type BlockProps struct { - BlockHash *string `json:"blockHash,omitempty"` + BlockHash *string `json:"blockHash,omitempty" rlp:"nil"` BlockNumber string `json:"blockNumber"` BlockTime string `json:"blockTime"` // unix timestamp ImageHash string `json:"imageHash"` @@ -61,7 +61,7 @@ type Block struct { // DiffProps ... // note @miguelmota: any better system than simply storing as a string? type DiffProps struct { - DiffHash *string `json:"diffHash,omitempty"` + DiffHash *string `json:"diffHash,omitempty" rlp:"nil"` // what's the best way to store a diff? Data string `json:"data"` }