Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
using rlp for serialize/deserialize; closes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Jul 13, 2018
1 parent 0480a2a commit 9f3169b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 18 deletions.
18 changes: 18 additions & 0 deletions common/coder/coder.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 51 additions & 1 deletion core/chain/mainchain/main_block_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (

var (
hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
p = Props{
bSig = BlockSig{
R: "0x1",
S: "0x1",
}
p = Props{
BlockHash: &hash,
BlockNumber: "0x1",
BlockTime: "0x5",
Expand All @@ -22,6 +26,8 @@ var (
PrevBlockHash: "prevBlockHash",
Nonce: "0x1",
Difficulty: "0x1",
MinerAddress: "0x123",
MinerSig: &bSig,
}
p1 = Props{
BlockHash: nil,
Expand All @@ -32,6 +38,8 @@ var (
PrevBlockHash: "prevBlockHash",
Nonce: "0x1",
Difficulty: "0x1",
MinerAddress: "",
MinerSig: nil,
}
s = `{
"blockHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions core/chain/mainchain/mainblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ...
Expand Down
4 changes: 2 additions & 2 deletions core/chain/mainchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 ...
Expand Down
11 changes: 9 additions & 2 deletions core/chain/statechain/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 ...
Expand Down
11 changes: 9 additions & 2 deletions core/chain/statechain/stateblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 ...
Expand Down
10 changes: 5 additions & 5 deletions core/chain/statechain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions core/chain/statechain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand All @@ -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"`
Expand All @@ -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"`
}
Expand Down

0 comments on commit 9f3169b

Please sign in to comment.