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

refactor(sync): define errors for sync package #1504

Merged
merged 1 commit into from
Sep 25, 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
11 changes: 5 additions & 6 deletions sync/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/fxamacker/cbor/v2"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/errors"
)

const (
Expand Down Expand Up @@ -84,19 +83,19 @@ func (b *Bundle) Decode(r io.Reader) (int, error) {
err := d.Decode(&bdl)
bytesRead := d.NumBytesRead()
if err != nil {
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
return bytesRead, err
}

data := bdl.MessageData
msg := message.MakeMessage(bdl.MessageType)
if msg == nil {
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, "invalid data")
msg, err := message.MakeMessage(bdl.MessageType)
if err != nil {
return bytesRead, err
}

if util.IsFlagSet(bdl.Flags, BundleFlagCompressed) {
c, err := util.DecompressBuffer(bdl.MessageData)
if err != nil {
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
return bytesRead, err
}
data = c
}
Expand Down
5 changes: 2 additions & 3 deletions sync/bundle/message/blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/util/errors"
)

type BlocksRequestMessage struct {
Expand All @@ -27,10 +26,10 @@ func (m *BlocksRequestMessage) To() uint32 {

func (m *BlocksRequestMessage) BasicCheck() error {
if m.From == 0 {
return errors.Errorf(errors.ErrInvalidHeight, "height is zero")
return BasicCheckError{Reason: "invalid height"}
}
if m.Count == 0 {
return errors.Errorf(errors.ErrInvalidMessage, "count is zero")
return BasicCheckError{Reason: "count is zero"}
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions sync/bundle/message/blocks_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"testing"

"github.com/pactus-project/pactus/util/errors"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,12 +15,14 @@ func TestBlocksRequestMessage(t *testing.T) {
t.Run("Invalid height", func(t *testing.T) {
m := NewBlocksRequestMessage(1, 0, 0)

assert.Equal(t, errors.ErrInvalidHeight, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{Reason: "invalid height"})
})
t.Run("Invalid count", func(t *testing.T) {
m := NewBlocksRequestMessage(1, 200, 0)

assert.Equal(t, errors.ErrInvalidMessage, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{Reason: "count is zero"})
})

t.Run("OK", func(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions sync/bundle/message/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package message

import "fmt"

// BasicCheckError is returned when the basic check on the message fails.
type BasicCheckError struct {
Reason string
}

func (e BasicCheckError) Error() string {
return e.Reason
}

// InvalidMessageTypeError is returned when the message type is not valid.
type InvalidMessageTypeError struct {
Type int
}

func (e InvalidMessageTypeError) Error() string {
return fmt.Sprintf("invalid message type: %d", e.Type)
}
5 changes: 2 additions & 3 deletions sync/bundle/message/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/sync/peerset/peer/service"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/version"
)

Expand Down Expand Up @@ -43,10 +42,10 @@ func NewHelloMessage(pid peer.ID, moniker string,

func (m *HelloMessage) BasicCheck() error {
if m.Signature == nil {
return errors.Error(errors.ErrInvalidSignature)
return BasicCheckError{"no signature"}
}
if len(m.PublicKeys) == 0 {
return errors.Error(errors.ErrInvalidPublicKey)
return BasicCheckError{"no public key"}
}
aggPublicKey := bls.PublicKeyAggregate(m.PublicKeys...)

Expand Down
10 changes: 6 additions & 4 deletions sync/bundle/message/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/sync/peerset/peer/service"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)
Expand All @@ -27,7 +26,8 @@ func TestHelloMessage(t *testing.T) {
m.Sign([]*bls.ValidatorKey{valKey})
m.Signature = ts.RandBLSSignature()

assert.ErrorIs(t, crypto.ErrInvalidSignature, m.BasicCheck())
err := m.BasicCheck()
assert.ErrorIs(t, err, crypto.ErrInvalidSignature)
})

t.Run("Signature is nil", func(t *testing.T) {
Expand All @@ -37,7 +37,8 @@ func TestHelloMessage(t *testing.T) {
m.Sign([]*bls.ValidatorKey{valKey})
m.Signature = nil

assert.Equal(t, errors.ErrInvalidSignature, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{"no signature"})
})

t.Run("PublicKeys are empty", func(t *testing.T) {
Expand All @@ -47,7 +48,8 @@ func TestHelloMessage(t *testing.T) {
m.Sign([]*bls.ValidatorKey{valKey})
m.PublicKeys = make([]*bls.PublicKey, 0)

assert.Equal(t, errors.ErrInvalidPublicKey, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{"no public key"})
})

t.Run("MyTimeUnixMilli of time1 is less or equal than hello message time", func(t *testing.T) {
Expand Down
28 changes: 16 additions & 12 deletions sync/bundle/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,41 +89,45 @@ func (t Type) String() string {
}
}

func MakeMessage(t Type) Message {
func MakeMessage(t Type) (Message, error) {
var msg Message
switch t {
case TypeHello:
return &HelloMessage{}
msg = &HelloMessage{}

case TypeHelloAck:
return &HelloAckMessage{}
msg = &HelloAckMessage{}

case TypeTransaction:
return &TransactionsMessage{}
msg = &TransactionsMessage{}

case TypeQueryProposal:
return &QueryProposalMessage{}
msg = &QueryProposalMessage{}

case TypeProposal:
return &ProposalMessage{}
msg = &ProposalMessage{}

case TypeQueryVote:
return &QueryVoteMessage{}
msg = &QueryVoteMessage{}

case TypeVote:
return &VoteMessage{}
msg = &VoteMessage{}

case TypeBlockAnnounce:
return &BlockAnnounceMessage{}
msg = &BlockAnnounceMessage{}

case TypeBlocksRequest:
return &BlocksRequestMessage{}
msg = &BlocksRequestMessage{}

case TypeBlocksResponse:
return &BlocksResponseMessage{}
msg = &BlocksResponseMessage{}

default:
return nil, InvalidMessageTypeError{Type: int(t)}
}

//
return nil
return msg, nil
}

type Message interface {
Expand Down
10 changes: 9 additions & 1 deletion sync/bundle/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/pactus-project/pactus/network"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMessage(t *testing.T) {
Expand All @@ -27,9 +28,16 @@ func TestMessage(t *testing.T) {
}

for _, tc := range testCases {
msg := MakeMessage(tc.msgType)
msg, err := MakeMessage(tc.msgType)
require.NoError(t, err)

assert.Equal(t, tc.typeName, msg.Type().String())
assert.Equal(t, tc.topicID, msg.TopicID())
assert.Equal(t, tc.shouldBroadcast, msg.ShouldBroadcast())
}
}

func TestInvalidMessageType(t *testing.T) {
_, err := MakeMessage(66)
assert.ErrorIs(t, err, InvalidMessageTypeError{Type: 66})
}
3 changes: 1 addition & 2 deletions sync/bundle/message/query_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/util/errors"
)

type QueryProposalMessage struct {
Expand All @@ -24,7 +23,7 @@ func NewQueryProposalMessage(height uint32, round int16, querier crypto.Address)

func (m *QueryProposalMessage) BasicCheck() error {
if m.Round < 0 {
return errors.Error(errors.ErrInvalidRound)
return BasicCheckError{Reason: "invalid round"}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions sync/bundle/message/query_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"testing"

"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)
Expand All @@ -19,7 +18,8 @@ func TestQueryProposalMessage(t *testing.T) {
t.Run("Invalid round", func(t *testing.T) {
m := NewQueryProposalMessage(0, -1, ts.RandValAddress())

assert.Equal(t, errors.ErrInvalidRound, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{"invalid round"})
})

t.Run("OK", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions sync/bundle/message/query_votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/util/errors"
)

type QueryVoteMessage struct {
Expand All @@ -24,7 +23,7 @@ func NewQueryVoteMessage(height uint32, round int16, querier crypto.Address) *Qu

func (m *QueryVoteMessage) BasicCheck() error {
if m.Round < 0 {
return errors.Error(errors.ErrInvalidRound)
return BasicCheckError{Reason: "invalid round"}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions sync/bundle/message/query_votes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"testing"

"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)
Expand All @@ -19,7 +18,8 @@ func TestQueryVoteMessage(t *testing.T) {
t.Run("Invalid round", func(t *testing.T) {
m := NewQueryVoteMessage(0, -1, ts.RandValAddress())

assert.Equal(t, errors.ErrInvalidRound, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{Reason: "invalid round"})
})

t.Run("OK", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions sync/bundle/message/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/util/errors"
)

type TransactionsMessage struct {
Expand All @@ -21,7 +20,7 @@ func NewTransactionsMessage(trxs []*tx.Tx) *TransactionsMessage {

func (m *TransactionsMessage) BasicCheck() error {
if len(m.Transactions) == 0 {
return errors.Errorf(errors.ErrInvalidMessage, "no transaction")
return BasicCheckError{Reason: "no transaction"}
}
for _, trx := range m.Transactions {
if err := trx.BasicCheck(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions sync/bundle/message/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)
Expand All @@ -20,7 +19,8 @@ func TestTransactionsMessage(t *testing.T) {
t.Run("No transactions", func(t *testing.T) {
m := NewTransactionsMessage(nil)

assert.Equal(t, errors.ErrInvalidMessage, errors.Code(m.BasicCheck()))
err := m.BasicCheck()
assert.ErrorIs(t, err, BasicCheckError{Reason: "no transaction"})
})

t.Run("OK", func(t *testing.T) {
Expand Down
15 changes: 9 additions & 6 deletions sync/firewall/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
lp2pcore "github.com/libp2p/go-libp2p/core"
)

// ErrGossipMessage is returned when a stream message sends as gossip message.
var ErrGossipMessage = errors.New("receive stream message as gossip message")

// ErrStreamMessage is returned when a gossip message sends as stream message.
var ErrStreamMessage = errors.New("receive gossip message as stream message")

// ErrNetworkMismatch is returned when the bundle doesn't belong to this network.
var ErrNetworkMismatch = errors.New("bundle is not for this network")

// PeerBannedError is returned when a message received from a banned peer-id or banned address.
type PeerBannedError struct {
PeerID lp2pcore.PeerID
Expand All @@ -16,9 +25,3 @@ type PeerBannedError struct {
func (e PeerBannedError) Error() string {
return fmt.Sprintf("peer is banned, peer-id: %s, remote-address: %s", e.PeerID, e.Address)
}

// ErrGossipMessage is returned when a stream message sends as gossip message.
var ErrGossipMessage = errors.New("receive stream message as gossip message")

// ErrStreamMessage is returned when a gossip message sends as stream message.
var ErrStreamMessage = errors.New("receive gossip message as stream message")
Loading
Loading