Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: get rid of github.com/nspcc-dev/neo-go/pkg/io dependency #89

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions block/block.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package block

import (
"bytes"
"encoding/gob"

"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/dbft/merkle"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
)

Expand Down Expand Up @@ -48,7 +50,7 @@ type (

// Transactions returns block's transaction list.
Transactions() []Transaction
// SetTransaction sets block's transaction list.
// SetTransactions sets block's transaction list.
SetTransactions([]Transaction)
}

Expand Down Expand Up @@ -135,10 +137,11 @@ func (b *neoBlock) Signature() []byte {
// 1. It must have only one valid result for one block.
// 2. Two different blocks must have different hash data.
func (b *neoBlock) GetHashData() []byte {
w := io.NewBufBinWriter()
b.EncodeBinary(w.BinWriter)
buf := bytes.Buffer{}
w := gob.NewEncoder(&buf)
_ = b.EncodeBinary(w)

return w.Bytes()
return buf.Bytes()
}

// Sign implements Block interface.
Expand Down Expand Up @@ -175,24 +178,12 @@ func (b *neoBlock) Hash() (h util.Uint256) {
return hash
}

// EncodeBinary implements io.Serializable interface.
func (b base) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(b.Version)
w.WriteBytes(b.PrevHash[:])
w.WriteBytes(b.MerkleRoot[:])
w.WriteU32LE(b.Timestamp)
w.WriteU32LE(b.Index)
w.WriteU64LE(b.ConsensusData)
w.WriteBytes(b.NextConsensus[:])
}

// DecodeBinary implements io.Serializable interface.
func (b *base) DecodeBinary(r *io.BinReader) {
b.Version = r.ReadU32LE()
r.ReadBytes(b.PrevHash[:])
r.ReadBytes(b.MerkleRoot[:])
b.Timestamp = r.ReadU32LE()
b.Index = r.ReadU32LE()
b.ConsensusData = r.ReadU64LE()
r.ReadBytes(b.NextConsensus[:])
// EncodeBinary implements Serializable interface.
func (b base) EncodeBinary(w *gob.Encoder) error {
return w.Encode(b)
}

// DecodeBinary implements Serializable interface.
func (b *base) DecodeBinary(r *gob.Decoder) error {
return r.Decode(b)
}
16 changes: 9 additions & 7 deletions block/block_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package block

import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/gob"
"errors"
"testing"

"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -45,14 +46,15 @@ func TestNeoBlock_Setters(t *testing.T) {
assert.EqualValues(t, 100, b.Index())

t.Run("marshal block", func(t *testing.T) {
w := io.NewBufBinWriter()
b.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
buf := bytes.Buffer{}
w := gob.NewEncoder(&buf)
err := b.EncodeBinary(w)
require.NoError(t, err)

r := io.NewBinReaderFromBuf(w.Bytes())
r := gob.NewDecoder(bytes.NewReader(buf.Bytes()))
newb := new(neoBlock)
newb.DecodeBinary(r)
require.NoError(t, r.Err)
err = newb.DecodeBinary(r)
require.NoError(t, err)
require.Equal(t, b.base, newb.base)
})

Expand Down
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@
TimestampIncrement: defaultTimestampIncrement,
GetKeyPair: nil,
NewBlockFromContext: NewBlockFromContext,
RequestTx: func(h ...util.Uint256) {},
RequestTx: func(...util.Uint256) {},
StopTxFlow: func() {},
GetTx: func(h util.Uint256) block.Transaction { return nil },
GetTx: func(util.Uint256) block.Transaction { return nil },

Check warning on line 104 in config.go

View check run for this annotation

Codecov / codecov/patch

config.go#L104

Added line #L104 was not covered by tests
GetVerified: func() []block.Transaction { return make([]block.Transaction, 0) },
VerifyBlock: func(b block.Block) bool { return true },
Broadcast: func(m payload.ConsensusPayload) {},
ProcessBlock: func(b block.Block) {},
GetBlock: func(h util.Uint256) block.Block { return nil },
VerifyBlock: func(block.Block) bool { return true },
Broadcast: func(payload.ConsensusPayload) {},
ProcessBlock: func(block.Block) {},
GetBlock: func(util.Uint256) block.Block { return nil },

Check warning on line 109 in config.go

View check run for this annotation

Codecov / codecov/patch

config.go#L106-L109

Added lines #L106 - L109 were not covered by tests
WatchOnly: func() bool { return false },
CurrentHeight: nil,
CurrentBlockHash: nil,
Expand Down
4 changes: 2 additions & 2 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func (s *testState) getOptions() []Option {
WithProcessBlock(func(b block.Block) { s.blocks = append(s.blocks, b) }),
WithGetConsensusAddress(s.nextConsensus),
WithWatchOnly(func() bool { return false }),
WithGetBlock(func(h util.Uint256) block.Block { return nil }),
WithGetBlock(func(util.Uint256) block.Block { return nil }),
WithTimer(timer.New()),
WithLogger(zap.NewNop()),
WithNewBlockFromContext(NewBlockFromContext),
Expand All @@ -809,7 +809,7 @@ func (s *testState) getOptions() []Option {

verify := s.verify
if verify == nil {
verify = func(b block.Block) bool { return true }
verify = func(block.Block) bool { return true }
}

opts = append(opts, WithVerifyBlock(verify))
Expand Down
1 change: 0 additions & 1 deletion merkle/merkle_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/nspcc-dev/neo-go/pkg/util"

roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/require"
)

Expand Down
37 changes: 26 additions & 11 deletions payload/change_view.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package payload

import "github.com/nspcc-dev/neo-go/pkg/io"
import (
"encoding/gob"
)

// ChangeView represents dBFT ChangeView message.
type ChangeView interface {
Expand All @@ -23,21 +25,34 @@
SetReason(reason ChangeViewReason)
}

type changeView struct {
newViewNumber byte
timestamp uint32
}
type (
changeView struct {
newViewNumber byte
timestamp uint32
}
// changeViewAux is an auxiliary structure for changeView encoding.
changeViewAux struct {
Timestamp uint32
}
)

var _ ChangeView = (*changeView)(nil)

// EncodeBinary implements io.Serializable interface.
func (c changeView) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(c.timestamp)
// EncodeBinary implements Serializable interface.
func (c changeView) EncodeBinary(w *gob.Encoder) error {
return w.Encode(&changeViewAux{
Timestamp: c.timestamp,
})
}

// DecodeBinary implements io.Serializable interface.
func (c *changeView) DecodeBinary(r *io.BinReader) {
c.timestamp = r.ReadU32LE()
// DecodeBinary implements Serializable interface.
func (c *changeView) DecodeBinary(r *gob.Decoder) error {
aux := new(changeViewAux)
if err := r.Decode(aux); err != nil {
return err

Check warning on line 52 in payload/change_view.go

View check run for this annotation

Codecov / codecov/patch

payload/change_view.go#L52

Added line #L52 was not covered by tests
}
c.timestamp = aux.Timestamp
return nil
}

// NewViewNumber implements ChangeView interface.
Expand Down
35 changes: 25 additions & 10 deletions payload/commit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package payload

import "github.com/nspcc-dev/neo-go/pkg/io"
import (
"encoding/gob"
)

// Commit is an interface for dBFT Commit message.
type Commit interface {
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -12,22 +14,35 @@
SetSignature(signature []byte)
}

type commit struct {
signature [signatureSize]byte
}
type (
commit struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use them in tests, what if we're to move them into _test.go files? It'd avoid importing encoding/gob for non-test builds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case is that we use them not only in tests, we also use them as default payloads for dBFT. So it won't be possible to easily integrate dBFT with Geth node without these default payloads.

Copy link
Member Author

@AnnaShaleva AnnaShaleva Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single-node Geth implementation used them before custom payloads implementation was added. Who knows, maybe one day we'll integrate dBFT with some other blockchain.

signature [signatureSize]byte
}
// commitAux is an auxiliary structure for commit encoding.
commitAux struct {
Signature [signatureSize]byte
}
)

const signatureSize = 64

var _ Commit = (*commit)(nil)

// EncodeBinary implements io.Serializable interface.
func (c commit) EncodeBinary(w *io.BinWriter) {
w.WriteBytes(c.signature[:])
// EncodeBinary implements Serializable interface.
func (c commit) EncodeBinary(w *gob.Encoder) error {
return w.Encode(commitAux{
Signature: c.signature,
})
}

// DecodeBinary implements io.Serializable interface.
func (c *commit) DecodeBinary(r *io.BinReader) {
r.ReadBytes(c.signature[:])
// DecodeBinary implements Serializable interface.
func (c *commit) DecodeBinary(r *gob.Decoder) error {
aux := new(commitAux)
if err := r.Decode(aux); err != nil {
return err

Check warning on line 42 in payload/commit.go

View check run for this annotation

Codecov / codecov/patch

payload/commit.go#L42

Added line #L42 was not covered by tests
}
c.signature = aux.Signature
return nil
}

// Signature implements Commit interface.
Expand Down
62 changes: 28 additions & 34 deletions payload/compact.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
package payload

import "github.com/nspcc-dev/neo-go/pkg/io"
import (
"encoding/gob"
)

type (
changeViewCompact struct {
validatorIndex uint16
originalViewNumber byte
timestamp uint32
ValidatorIndex uint16
OriginalViewNumber byte
Timestamp uint32
}

commitCompact struct {
viewNumber byte
validatorIndex uint16
signature [signatureSize]byte
ViewNumber byte
ValidatorIndex uint16
Signature [signatureSize]byte
}

preparationCompact struct {
validatorIndex uint16
ValidatorIndex uint16
}
)

// EncodeBinary implements io.Serializable interface.
func (p changeViewCompact) EncodeBinary(w *io.BinWriter) {
w.WriteU16LE(p.validatorIndex)
w.WriteB(p.originalViewNumber)
w.WriteU32LE(p.timestamp)
// EncodeBinary implements Serializable interface.
func (p changeViewCompact) EncodeBinary(w *gob.Encoder) error {
return w.Encode(p)
}

// DecodeBinary implements io.Serializable interface.
func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
p.validatorIndex = r.ReadU16LE()
p.originalViewNumber = r.ReadB()
p.timestamp = r.ReadU32LE()
// DecodeBinary implements Serializable interface.
func (p *changeViewCompact) DecodeBinary(r *gob.Decoder) error {
return r.Decode(p)
}

// EncodeBinary implements io.Serializable interface.
func (p commitCompact) EncodeBinary(w *io.BinWriter) {
w.WriteB(p.viewNumber)
w.WriteU16LE(p.validatorIndex)
w.WriteBytes(p.signature[:])
// EncodeBinary implements Serializable interface.
func (p commitCompact) EncodeBinary(w *gob.Encoder) error {
return w.Encode(p)
}

// DecodeBinary implements io.Serializable interface.
func (p *commitCompact) DecodeBinary(r *io.BinReader) {
p.viewNumber = r.ReadB()
p.validatorIndex = r.ReadU16LE()
r.ReadBytes(p.signature[:])
// DecodeBinary implements Serializable interface.
func (p *commitCompact) DecodeBinary(r *gob.Decoder) error {
return r.Decode(p)
}

// EncodeBinary implements io.Serializable interface.
func (p preparationCompact) EncodeBinary(w *io.BinWriter) {
w.WriteU16LE(p.validatorIndex)
// EncodeBinary implements Serializable interface.
func (p preparationCompact) EncodeBinary(w *gob.Encoder) error {
return w.Encode(p)
}

// DecodeBinary implements io.Serializable interface.
func (p *preparationCompact) DecodeBinary(r *io.BinReader) {
p.validatorIndex = r.ReadU16LE()
// DecodeBinary implements Serializable interface.
func (p *preparationCompact) DecodeBinary(r *gob.Decoder) error {
return r.Decode(p)
}
Loading
Loading