Skip to content

Commit

Permalink
Remove Serialize method from Operation interface
Browse files Browse the repository at this point in the history
Serialize is not useful, as it is solely used to serialize to JSON.
It creates problem when one wants to serialize in different formats,
e.g. JSON for network communication, and binary for on-disk storage.
Removing this interface allows us to move the choice of the serialization
format to the caller of the method, where it actually makes more sense.
  • Loading branch information
Geod24 committed Dec 3, 2018
1 parent 4c04193 commit 0cc0ebd
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 58 deletions.
3 changes: 2 additions & 1 deletion lib/block/operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package block

import (
"encoding/json"
"fmt"

"boscoin.io/sebak/lib/common"
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewBlockOperationKey(opHash, txHash string) string {
}

func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Transaction, blockHeight uint64, opIndex int) (BlockOperation, error) {
body, err := op.B.Serialize()
body, err := json.Marshal(op.B)
if err != nil {
return BlockOperation{}, err
}
Expand Down
6 changes: 2 additions & 4 deletions lib/block/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestNewBlockOperationFromOperation(t *testing.T) {
require.Equal(t, bo.Type, op.H.Type)
require.Equal(t, bo.TxHash, tx.H.Hash)
require.Equal(t, bo.Source, tx.B.Source)
encoded, err := op.B.Serialize()
require.NoError(t, err)
encoded := common.MustMarshalJSON(op.B)
require.Equal(t, bo.Body, encoded)
}

Expand Down Expand Up @@ -125,8 +124,7 @@ func TestBlockOperationSaveByTransaction(t *testing.T) {
require.Equal(t, bo.Type, op.H.Type)
require.Equal(t, bo.TxHash, tx.H.Hash)
require.Equal(t, bo.Source, tx.B.Source)
encoded, err := op.B.Serialize()
require.NoError(t, err)
encoded := common.MustMarshalJSON(op.B)
require.Equal(t, bo.Body, encoded)
}
}
Expand Down
6 changes: 0 additions & 6 deletions lib/transaction/operation/collect_tx_fee.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operation

import (
"encoding/json"

"boscoin.io/sebak/lib/common"
"boscoin.io/sebak/lib/common/keypair"
"boscoin.io/sebak/lib/errors"
Expand Down Expand Up @@ -72,10 +70,6 @@ func (o CollectTxFee) GetAmount() common.Amount {
return o.Amount
}

func (o CollectTxFee) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

func (o CollectTxFee) HasFee() bool {
return false
}
6 changes: 0 additions & 6 deletions lib/transaction/operation/congress_voting.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operation

import (
"encoding/json"

"boscoin.io/sebak/lib/common"
"boscoin.io/sebak/lib/errors"
)
Expand Down Expand Up @@ -33,10 +31,6 @@ func NewCongressVoting(contract string, start, end uint64, amount common.Amount,
}
}

func (o CongressVoting) Serialize() (encoded []byte, err error) {
encoded, err = json.Marshal(o)
return
}
func (o CongressVoting) IsWellFormed(common.Config) (err error) {
if len(o.Contract) == 0 {
return errors.OperationBodyInsufficient
Expand Down
4 changes: 0 additions & 4 deletions lib/transaction/operation/congress_voting_result.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package operation

import (
"encoding/json"
"strconv"
"strings"

Expand Down Expand Up @@ -61,9 +60,6 @@ func NewCongressVotingResult(
}
}

func (o CongressVotingResult) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}
func (o CongressVotingResult) IsWellFormed(common.Config) (err error) {
if len(o.BallotStamps.Hash) == 0 {
return errors.OperationBodyInsufficient
Expand Down
6 changes: 0 additions & 6 deletions lib/transaction/operation/create_account.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operation

import (
"encoding/json"

"boscoin.io/sebak/lib/common"
"boscoin.io/sebak/lib/common/keypair"
"boscoin.io/sebak/lib/errors"
Expand All @@ -22,10 +20,6 @@ func NewCreateAccount(target string, amount common.Amount, linked string) Create
}
}

func (o CreateAccount) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

// Implement transaction/operation : IsWellFormed
func (o CreateAccount) IsWellFormed(common.Config) (err error) {
if _, err = keypair.Parse(o.Target); err != nil {
Expand Down
5 changes: 0 additions & 5 deletions lib/transaction/operation/inflation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package operation

import (
"encoding/json"
"strings"

"boscoin.io/sebak/lib/common"
Expand Down Expand Up @@ -79,10 +78,6 @@ func (o Inflation) GetAmount() common.Amount {
return o.Amount
}

func (o Inflation) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

func (o Inflation) HasFee() bool {
return false
}
5 changes: 0 additions & 5 deletions lib/transaction/operation/inflation_pf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package operation

import (
"encoding/json"
"strconv"
"strings"

Expand Down Expand Up @@ -52,10 +51,6 @@ func (o InflationPF) GetAmount() common.Amount {
return o.Amount
}

func (o InflationPF) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

func (o InflationPF) HasFee() bool {
return false
}
5 changes: 0 additions & 5 deletions lib/transaction/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ type Body interface {
// An `error` if that transaction is invalid, `nil` otherwise
//
IsWellFormed(common.Config) error
Serialize() ([]byte, error)
HasFee() bool
}

Expand All @@ -125,10 +124,6 @@ func (o Operation) IsWellFormed(conf common.Config) (err error) {
return o.B.IsWellFormed(conf)
}

func (o Operation) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

func (o Operation) String() string {
encoded, _ := json.MarshalIndent(o, "", " ")

Expand Down
5 changes: 2 additions & 3 deletions lib/transaction/operation/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ func TestIsWellFormedOperationLowerAmount(t *testing.T) {

func TestSerializeOperation(t *testing.T) {
op := MakeTestPayment(-1)
b, err := op.Serialize()
require.NoError(t, err)
b := common.MustMarshalJSON(op)
require.Equal(t, len(b) > 0, true)

var o Operation
err = json.Unmarshal(b, &o)
err := json.Unmarshal(b, &o)
require.NoError(t, err)
}

Expand Down
6 changes: 0 additions & 6 deletions lib/transaction/operation/payment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operation

import (
"encoding/json"

"boscoin.io/sebak/lib/common"
"boscoin.io/sebak/lib/common/keypair"
"boscoin.io/sebak/lib/errors"
Expand All @@ -20,10 +18,6 @@ func NewPayment(target string, amount common.Amount) Payment {
}
}

func (o Payment) Serialize() (encoded []byte, err error) {
return json.Marshal(o)
}

// Implement transaction/operation : IsWellFormed
func (o Payment) IsWellFormed(common.Config) (err error) {
if _, err = keypair.Parse(o.Target); err != nil {
Expand Down
7 changes: 0 additions & 7 deletions lib/transaction/operation/unfreezing_request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operation

import (
"encoding/json"

"boscoin.io/sebak/lib/common"
)

Expand All @@ -12,11 +10,6 @@ func NewUnfreezeRequest() UnfreezeRequest {
return UnfreezeRequest{}
}

func (o UnfreezeRequest) Serialize() (encoded []byte, err error) {
encoded, err = json.Marshal(o)
return
}

func (o UnfreezeRequest) IsWellFormed(common.Config) (err error) {
return
}
Expand Down

0 comments on commit 0cc0ebd

Please sign in to comment.