Skip to content

Commit

Permalink
move gob common to enc
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitprincess committed Nov 5, 2023
1 parent 14d8832 commit c676909
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 64 deletions.
6 changes: 3 additions & 3 deletions chain/chaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func (cdb *ChainDB) getReceipts(blockHash []byte, blockNo types.BlockNo,
var receipts types.Receipts

receipts.SetHardFork(hardForkConfig, blockNo)
err := common.GobDecode(data, &receipts)
err := enc.GobDecode(data, &receipts)

return &receipts, err
}
Expand Down Expand Up @@ -694,7 +694,7 @@ func (cdb *ChainDB) writeReceipts(blockHash []byte, blockNo types.BlockNo, recei
dbTx := cdb.store.NewTx()
defer dbTx.Discard()

val, _ := common.GobEncode(receipts)
val, _ := enc.GobEncode(receipts)
dbTx.Set(dbkey.Receipts(blockHash, blockNo), val)

dbTx.Commit()
Expand Down Expand Up @@ -736,7 +736,7 @@ func (cdb *ChainDB) getReorgMarker() (*ReorgMarker, error) {
}

var marker ReorgMarker
err := common.GobDecode(data, &marker)
err := enc.GobDecode(data, &marker)

return &marker, err
}
Expand Down
8 changes: 4 additions & 4 deletions chain/chaindbForRaft.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/consensus"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
"github.com/aergoio/etcd/raft/raftpb"
Expand Down Expand Up @@ -234,7 +234,7 @@ func (cdb *ChainDB) GetRaftEntry(idx uint64) (*consensus.WalEntry, error) {
}

var entry consensus.WalEntry
if err := common.GobDecode(data, &entry); err != nil {
if err := enc.GobDecode(data, &entry); err != nil {
return nil, err
}

Expand Down Expand Up @@ -421,7 +421,7 @@ func (cdb *ChainDB) WriteIdentity(identity *consensus.RaftIdentity) error {

logger.Info().Str("id", identity.ToString()).Msg("save raft identity")

enc, err := common.GobEncode(identity)
enc, err := enc.GobEncode(identity)
if err != nil {
return ErrEncodeRaftIdentity
}
Expand All @@ -439,7 +439,7 @@ func (cdb *ChainDB) GetIdentity() (*consensus.RaftIdentity, error) {
}

var id consensus.RaftIdentity
if err := common.GobDecode(data, &id); err != nil {
if err := enc.GobDecode(data, &id); err != nil {
return nil, ErrDecodeRaftIdentity
}

Expand Down
3 changes: 1 addition & 2 deletions chain/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"runtime"
"runtime/debug"

"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
Expand Down Expand Up @@ -218,7 +217,7 @@ func (rm *ReorgMarker) delete() {
}

func (rm *ReorgMarker) toBytes() ([]byte, error) {
return common.GobEncode(rm)
return enc.GobEncode(rm)
}

func (rm *ReorgMarker) toString() string {
Expand Down
4 changes: 2 additions & 2 deletions consensus/impl/dpos/bp/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/v2/consensus"
"github.com/aergoio/aergo/v2/contract/system"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/state"
"github.com/aergoio/aergo/v2/types"
"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -287,7 +287,7 @@ func buildKey(blockNo types.BlockNo) []byte {

// Value returns s.list.
func (s *Snapshot) Value() []byte {
b, err := common.GobEncode(s.List)
b, err := enc.GobEncode(s.List)
if err != nil {
logger.Debug().Err(err).Msg("BP list encoding failed")
return nil
Expand Down
6 changes: 3 additions & 3 deletions consensus/impl/dpos/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/consensus"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/p2p/p2pkey"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
Expand Down Expand Up @@ -235,7 +235,7 @@ func (ls *libStatus) load(endBlockNo types.BlockNo) {
}

func (ls *libStatus) save(tx consensus.TxWriter) error {
b, err := common.GobEncode(ls)
b, err := enc.GobEncode(ls)
if err != nil {
return err
}
Expand Down Expand Up @@ -397,7 +397,7 @@ func (bs *bootLoader) decodeStatus(key []byte, dst interface{}) error {
return fmt.Errorf("LIB status not found: key = %v", string(key))
}

err := common.GobDecode(value, dst)
err := enc.GobDecode(value, dst)
if err != nil {
logger.Panic().Err(err).Str("key", string(key)).
Msg("failed to decode DPoS status")
Expand Down
3 changes: 1 addition & 2 deletions consensus/raftCommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"

"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/etcd/raft"
Expand Down Expand Up @@ -59,7 +58,7 @@ type WalEntry struct {
}

func (we *WalEntry) ToBytes() ([]byte, error) {
return common.GobEncode(we)
return enc.GobEncode(we)
}

func (we *WalEntry) ToString() string {
Expand Down
19 changes: 0 additions & 19 deletions internal/common/bytes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package common

import (
"bytes"
"encoding/gob"
)

// IsZero returns true if argument is empty or zero
func IsZero(argv []byte) bool {
if len(argv) == 0 {
Expand All @@ -25,17 +20,3 @@ func Compactz(argv []byte) []byte {
}
return argv
}

// GobEncode encodes e by using gob and returns.
func GobEncode(e interface{}) ([]byte, error) {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(e)

return buf.Bytes(), err
}

// GobDecode decodes a gob-encoded value v.
func GobDecode(v []byte, e interface{}) error {
dec := gob.NewDecoder(bytes.NewBuffer(v))
return dec.Decode(e)
}
22 changes: 0 additions & 22 deletions internal/common/bytes_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGobCodec(t *testing.T) {
a := assert.New(t)

x := []int{1, 2, 3}
b, err := GobEncode(x)
a.Nil(err)

y := []int{0, 0, 0}
err = GobDecode(b, &y)
a.Nil(err)

for i, v := range x {
a.Equal(v, y[i])
}
}
9 changes: 8 additions & 1 deletion internal/enc/base58.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package enc

import "github.com/mr-tron/base58/base58"
import (
"github.com/mr-tron/base58/base58"
)

// B58Encode returns human-readable (base58) string from b. Calling with empty or nil slice returns empty string.
func B58Encode(b []byte) string {
Expand All @@ -11,3 +13,8 @@ func B58Encode(b []byte) string {
func B58Decode(s string) ([]byte, error) {
return base58.Decode(s)
}

func B58DecodeOrNil(s string) []byte {
buf, _ := B58Decode(s)
return buf
}
10 changes: 10 additions & 0 deletions internal/enc/base58check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func B58CheckEncode(version string, data string) (string, error) {
return base58check.Encode(version, data)
}

func B58CheckEncodeOrNil(version string, data string) string {
buf, _ := B58CheckEncode(version, data)
return buf
}

func B58CheckDecode(encoded string) (string, error) {
return base58check.Decode(encoded)
}

func B58CheckDecodeOrNil(encoded string) string {
buf, _ := B58CheckDecode(encoded)
return buf
}
6 changes: 6 additions & 0 deletions internal/enc/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ func B64Encode(s []byte) string {
func B64Decode(s string) ([]byte, error) {
return base64.StdEncoding.DecodeString(s)
}

// Do not use processing real data, Only use for Logging or Testing.
func B64DecodeOrNil(s string) []byte {
buf, _ := B64Decode(s)
return buf
}
22 changes: 22 additions & 0 deletions internal/enc/gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package enc

import (
"bytes"
"encoding/gob"
)

// GobEncode encodes e by using gob and returns.
func GobEncode(e interface{}) ([]byte, error) {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(e)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// GobDecode decodes a gob-encoded value v.
func GobDecode(v []byte, e interface{}) error {
dec := gob.NewDecoder(bytes.NewBuffer(v))
return dec.Decode(e)
}
23 changes: 23 additions & 0 deletions internal/enc/gob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package enc

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGobCodec(t *testing.T) {
a := assert.New(t)

x := []int{1, 2, 3}
b, err := GobEncode(x)
a.Nil(err)

y := []int{0, 0, 0}
err = GobDecode(b, &y)
a.Nil(err)

for i, v := range x {
a.Equal(v, y[i])
}
}
6 changes: 3 additions & 3 deletions state/statedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package state

import (
"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
"github.com/aergoio/aergo/v2/types"
"github.com/golang/protobuf/proto"
)
Expand All @@ -22,7 +22,7 @@ func saveData(store db.DB, key []byte, data interface{}) error {
return err
}
default:
raw, err = common.GobEncode(data)
raw, err = enc.GobEncode(data)
if err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func loadData(store db.DB, key []byte, data interface{}) error {
case proto.Message:
err = proto.Unmarshal(raw, data.(proto.Message))
default:
err = common.GobDecode(raw, data)
err = enc.GobDecode(raw, data)
}
return err
}
Expand Down
5 changes: 2 additions & 3 deletions types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc"
)

Expand Down Expand Up @@ -281,7 +280,7 @@ func (g *Genesis) ChainID() ([]byte, error) {
func (g Genesis) Bytes() []byte {
// Omit the Balance to reduce the resulting data size.
g.Balance = nil
if b, err := common.GobEncode(g); err == nil {
if b, err := enc.GobEncode(g); err == nil {
return b
}
return nil
Expand Down Expand Up @@ -373,7 +372,7 @@ func GetTestGenesis() *Genesis {
// GetGenesisFromBytes decodes & return Genesis from b.
func GetGenesisFromBytes(b []byte) *Genesis {
g := &Genesis{}
if err := common.GobDecode(b, g); err == nil {
if err := enc.GobDecode(b, g); err == nil {
return g
}
return nil
Expand Down

0 comments on commit c676909

Please sign in to comment.