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

feat: implementing pip-7 #731

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 23 additions & 16 deletions sync/bundle/message/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message

import (
"fmt"
"time"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/pactus-project/pactus/crypto/bls"
Expand All @@ -12,28 +13,30 @@ import (
)

type HelloMessage struct {
PeerID peer.ID `cbor:"1,keyasint"`
Agent string `cbor:"2,keyasint"`
Moniker string `cbor:"3,keyasint"`
PublicKeys []*bls.PublicKey `cbor:"4,keyasint"`
Signature *bls.Signature `cbor:"5,keyasint"`
Height uint32 `cbor:"6,keyasint"`
Services services.Services `cbor:"7,keyasint"`
GenesisHash hash.Hash `cbor:"8,keyasint"`
BlockHash hash.Hash `cbor:"9,keyasint"`
PeerID peer.ID `cbor:"1,keyasint"`
Agent string `cbor:"2,keyasint"`
Moniker string `cbor:"3,keyasint"`
PublicKeys []*bls.PublicKey `cbor:"4,keyasint"`
Signature *bls.Signature `cbor:"5,keyasint"`
Height uint32 `cbor:"6,keyasint"`
Services services.Services `cbor:"7,keyasint"`
GenesisHash hash.Hash `cbor:"8,keyasint"`
BlockHash hash.Hash `cbor:"9,keyasint"`
MyTimeUnixMilli int64 `cbor:"10,keyasint"`
}

func NewHelloMessage(pid peer.ID, moniker string,
height uint32, services services.Services, blockHash, genesisHash hash.Hash,
) *HelloMessage {
return &HelloMessage{
PeerID: pid,
Agent: version.Agent(),
Moniker: moniker,
GenesisHash: genesisHash,
BlockHash: blockHash,
Height: height,
Services: services,
PeerID: pid,
Agent: version.Agent(),
Moniker: moniker,
GenesisHash: genesisHash,
BlockHash: blockHash,
Height: height,
Services: services,
MyTimeUnixMilli: time.Now().UnixMilli(),
}
}

Expand All @@ -48,6 +51,10 @@ func (m *HelloMessage) BasicCheck() error {
return aggPublicKey.Verify(m.SignBytes(), m.Signature)
}

func (m *HelloMessage) MyTime() time.Time {
return time.UnixMilli(m.MyTimeUnixMilli)
}

func (m *HelloMessage) SignBytes() []byte {
return []byte(fmt.Sprintf("%s:%s:%s:%s", m.Type(), m.Agent, m.PeerID, m.GenesisHash.String()))
}
Expand Down
12 changes: 12 additions & 0 deletions sync/bundle/message/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message

import (
"testing"
"time"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
Expand All @@ -15,6 +16,17 @@ func TestHelloType(t *testing.T) {
assert.Equal(t, m.Type(), TypeHello)
}

func TestHelloMessageMyTimeUnix(t *testing.T) {
myTime := time.Now()
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
myTimeUnixMilli := myTime.UnixMilli()
m := &HelloMessage{
MyTimeUnixMilli: myTimeUnixMilli,
}

assert.Equal(t, myTimeUnixMilli, m.MyTimeUnixMilli)
assert.Equal(t, myTimeUnixMilli, m.MyTime().UnixMilli())
}

func TestHelloMessage(t *testing.T) {
ts := testsuite.NewTestSuite(t)

Expand Down
9 changes: 9 additions & 0 deletions sync/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sync

import (
"fmt"
"time"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/pactus-project/pactus/sync/bundle"
Expand Down Expand Up @@ -38,6 +39,14 @@ func (handler *helloHandler) ParseMessage(m message.Message, initiator peer.ID)
return handler.acknowledge(response, initiator)
}

if time.Since(msg.MyTime()) > 10*time.Second {
response := message.NewHelloAckMessage(message.ResponseCodeRejected,
fmt.Sprintf("hello message difference time is more than 10 seconds,"+
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
" my node time: %v, hello message time: %v", time.Now().UTC(), msg.MyTime().UTC()))

return handler.acknowledge(response, initiator)
}

handler.logger.Debug("updating peer info",
"pid", initiator.ShortString(),
"moniker", msg.Moniker,
Expand Down
29 changes: 23 additions & 6 deletions sync/handler_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sync

import (
"testing"
"time"

"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/sync/bundle"
Expand Down Expand Up @@ -29,8 +30,8 @@ func TestParsingHelloMessages(t *testing.T) {

assert.NoError(t, td.receivingNewMessage(td.sync, msg, initiator))
assert.Equal(t, td.sync.peerSet.GetPeer(initiator).Status, peerset.StatusCodeBanned)
bundle := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bundle.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeRejected)
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bdl.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeRejected)
})

t.Run("Receiving Hello message from a peer. Genesis hash is wrong.",
Expand All @@ -44,8 +45,24 @@ func TestParsingHelloMessages(t *testing.T) {

assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
td.checkPeerStatus(t, pid, peerset.StatusCodeBanned)
bundle := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bundle.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeRejected)
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bdl.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeRejected)
})

t.Run("Receiving Hello message from a peer. Difference of Hello Message Time with node time is a lot.",
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, services.New(services.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg.Sign([]*bls.ValidatorKey{valKey})

msg.MyTimeUnixMilli = msg.MyTime().Add(-11 * time.Second).UnixMilli()
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
td.checkPeerStatus(t, pid, peerset.StatusCodeBanned)
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bdl.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeRejected)
})

t.Run("Receiving Hello message from a peer. It should be acknowledged and updates the peer info",
Expand All @@ -59,8 +76,8 @@ func TestParsingHelloMessages(t *testing.T) {

assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

bundle := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bundle.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeOK)
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeHelloAck)
assert.Equal(t, bdl.Message.(*message.HelloAckMessage).ResponseCode, message.ResponseCodeOK)

// Check if the peer info is updated
p := td.sync.peerSet.GetPeer(pid)
Expand Down
Loading