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

Add PruningPointProof to the P2P protocol #1825

Merged
merged 15 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
22 changes: 22 additions & 0 deletions app/appmessage/domainconverters.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,25 @@ func DomainBlockWithTrustedDataToBlockWithTrustedData(block *externalapi.BlockWi
GHOSTDAGData: ghostdagData,
}
}

// MsgPruningPointProofToDomainPruningPointProof converts *MsgPruningPointProof to *externalapi.PruningPointProof
func MsgPruningPointProofToDomainPruningPointProof(pruningPointProofMessage *MsgPruningPointProof) *externalapi.PruningPointProof {
headers := make([]externalapi.BlockHeader, len(pruningPointProofMessage.Headers))
for i, header := range pruningPointProofMessage.Headers {
headers[i] = BlockHeaderToDomainBlockHeader(header)
}
return &externalapi.PruningPointProof{
Headers: headers,
}
}

// DomainPruningPointProofToMsgPruningPointProof converts *externalapi.PruningPointProof to *MsgPruningPointProof
func DomainPruningPointProofToMsgPruningPointProof(pruningPointProof *externalapi.PruningPointProof) *MsgPruningPointProof {
headers := make([]*MsgBlockHeader, len(pruningPointProof.Headers))
for i, header := range pruningPointProof.Headers {
headers[i] = DomainBlockHeaderToBlockHeader(header)
}
return &MsgPruningPointProof{
Headers: headers,
}
}
4 changes: 4 additions & 0 deletions app/appmessage/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
CmdIBDBlock
CmdRequestIBDBlocks
CmdPruningPoints
CmdRequestPruningPointProof
CmdPruningPointProof

// rpc
CmdGetCurrentNetworkRequestMessage
Expand Down Expand Up @@ -185,6 +187,8 @@ var ProtocolMessageCommandToString = map[MessageCommand]string{
CmdIBDBlock: "IBDBlock",
CmdRequestIBDBlocks: "RequestIBDBlocks",
CmdPruningPoints: "PruningPoints",
CmdRequestPruningPointProof: "RequestPruningPointProof",
CmdPruningPointProof: "PruningPointProof",
}

// RPCMessageCommandToString maps all MessageCommands to their string representation
Expand Down
20 changes: 20 additions & 0 deletions app/appmessage/p2p_msgpruningpointproof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package appmessage

// MsgPruningPointProof represents a kaspa PruningPointProof message
type MsgPruningPointProof struct {
baseMessage

Headers []*MsgBlockHeader
}

// Command returns the protocol command string for the message
func (msg *MsgPruningPointProof) Command() MessageCommand {
return CmdPruningPointProof
}

// NewMsgPruningPointProof returns a new MsgPruningPointProof.
func NewMsgPruningPointProof(headers []*MsgBlockHeader) *MsgPruningPointProof {
return &MsgPruningPointProof{
Headers: headers,
}
}
16 changes: 16 additions & 0 deletions app/appmessage/p2p_msgrequestpruningpointproof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package appmessage

// MsgRequestPruningPointProof represents a kaspa RequestPruningPointProof message
type MsgRequestPruningPointProof struct {
baseMessage
}

// Command returns the protocol command string for the message
func (msg *MsgRequestPruningPointProof) Command() MessageCommand {
return CmdRequestPruningPointProof
}

// NewMsgRequestPruningPointProof returns a new MsgRequestPruningPointProof.
func NewMsgRequestPruningPointProof() *MsgRequestPruningPointProof {
return &MsgRequestPruningPointProof{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package blockrelay

import (
"github.com/kaspanet/kaspad/app/appmessage"
peerpkg "github.com/kaspanet/kaspad/app/protocol/peer"
"github.com/kaspanet/kaspad/domain"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
)

// PruningPointProofRequestsContext is the interface for the context needed for the HandlePruningPointProofRequests flow.
type PruningPointProofRequestsContext interface {
Domain() domain.Domain
}

// HandlePruningPointProofRequests listens to appmessage.MsgRequestPruningPointProof messages and sends
// the pruning point proof to the requesting peer.
func HandlePruningPointProofRequests(context PruningPointProofRequestsContext, incomingRoute *router.Route,
outgoingRoute *router.Route, peer *peerpkg.Peer) error {

for {
_, err := incomingRoute.Dequeue()
if err != nil {
return err
}

log.Debugf("Got request for pruning point proof from %s", peer)

pruningPointProof, err := context.Domain().Consensus().BuildPruningPointProof()
if err != nil {
return err
}
pruningPointProofMessage := appmessage.DomainPruningPointProofToMsgPruningPointProof(pruningPointProof)
err = outgoingRoute.Enqueue(pruningPointProofMessage)
if err != nil {
return err
}

log.Debugf("Sent pruning point proof to %s", peer)
}
}
23 changes: 18 additions & 5 deletions app/protocol/flows/blockrelay/ibd_with_headers_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,27 @@ func (flow *handleRelayInvsFlow) checkIfHighHashHasMoreBlueWorkThanSelectedTip(h
return msgBlockBlueWork.BlueWork.Cmp(headersSelectedTipInfo.BlueWork) > 0, nil
}

func (flow *handleRelayInvsFlow) downloadHeadersProof() error {
// TODO: Implement headers proof mechanism
return nil
func (flow *handleRelayInvsFlow) syncAndValidatePruningPointProof() error {
log.Infof("Downloading the pruning point proof from %s", flow.peer)
err := flow.outgoingRoute.Enqueue(appmessage.NewMsgRequestPruningPointProof())
if err != nil {
return err
}
message, err := flow.dequeueIncomingMessageAndSkipInvs(common.DefaultTimeout)
if err != nil {
return err
}
pruningPointProofMessage, ok := message.(*appmessage.MsgPruningPointProof)
if !ok {
return protocolerrors.Errorf(true, "received unexpected message type. "+
"expected: %s, got: %s", appmessage.CmdPruningPointProof, message.Command())
}
pruningPointProof := appmessage.MsgPruningPointProofToDomainPruningPointProof(pruningPointProofMessage)
return flow.Domain().Consensus().ValidatePruningPointProof(pruningPointProof)
}

func (flow *handleRelayInvsFlow) downloadHeadersAndPruningUTXOSet(highHash *externalapi.DomainHash) error {
err := flow.downloadHeadersProof()
err := flow.syncAndValidatePruningPointProof()
if err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +145,6 @@ func (flow *handleRelayInvsFlow) downloadHeadersAndPruningUTXOSet(highHash *exte
}

func (flow *handleRelayInvsFlow) syncPruningPointsAndPruningPointAnticone() (*externalapi.DomainHash, error) {

log.Infof("Downloading the past pruning points and the pruning point anticone from %s", flow.peer)
err := flow.outgoingRoute.Enqueue(appmessage.NewMsgRequestPruningPointAndItsAnticone())
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions app/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (m *Manager) registerBlockRelayFlows(router *routerpkg.Router, isStopping *
appmessage.CmdBlockHeaders, appmessage.CmdIBDBlockLocatorHighestHash, appmessage.CmdBlockWithTrustedData,
appmessage.CmdDoneBlocksWithTrustedData, appmessage.CmdIBDBlockLocatorHighestHashNotFound,
appmessage.CmdDonePruningPointUTXOSetChunks, appmessage.CmdIBDBlock, appmessage.CmdPruningPoints,
appmessage.CmdPruningPointProof,
},
isStopping, errChan, func(incomingRoute *routerpkg.Route, peer *peerpkg.Peer) error {
return blockrelay.HandleRelayInvs(m.context, incomingRoute,
Expand Down Expand Up @@ -235,6 +236,13 @@ func (m *Manager) registerBlockRelayFlows(router *routerpkg.Router, isStopping *
return blockrelay.HandleIBDBlockLocator(m.context, incomingRoute, outgoingRoute, peer)
},
),

m.registerFlow("HandlePruningPointProofRequests", router,
[]appmessage.MessageCommand{appmessage.CmdRequestPruningPointProof}, isStopping, errChan,
func(incomingRoute *routerpkg.Route, peer *peerpkg.Peer) error {
return blockrelay.HandlePruningPointProofRequests(m.context, incomingRoute, outgoingRoute, peer)
},
),
}
}

Expand Down
16 changes: 16 additions & 0 deletions domain/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,19 @@ func (s *consensus) ResolveVirtual() error {
}
}
}

func (s *consensus) BuildPruningPointProof() (*externalapi.PruningPointProof, error) {
s.lock.Lock()
defer s.lock.Unlock()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a TODO?

return &externalapi.PruningPointProof{
Headers: []externalapi.BlockHeader{},
}, nil
}

func (s *consensus) ValidatePruningPointProof(pruningPointProof *externalapi.PruningPointProof) error {
s.lock.Lock()
defer s.lock.Unlock()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a TODO?

return nil
}
2 changes: 2 additions & 0 deletions domain/consensus/model/externalapi/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Consensus interface {
ValidateAndInsertBlockWithTrustedData(block *BlockWithTrustedData, validateUTXO bool) (*BlockInsertionResult, error)
ValidateTransactionAndPopulateWithConsensusData(transaction *DomainTransaction) error
ImportPruningPoints(pruningPoints []BlockHeader) error
BuildPruningPointProof() (*PruningPointProof, error)
ValidatePruningPointProof(pruningPointProof *PruningPointProof) error

GetBlock(blockHash *DomainHash) (*DomainBlock, error)
GetBlockEvenIfHeaderOnly(blockHash *DomainHash) (*DomainBlock, error)
Expand Down
6 changes: 6 additions & 0 deletions domain/consensus/model/externalapi/pruning_point_proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package externalapi

// PruningPointProof is the data structure holding the pruning point proof
type PruningPointProof struct {
Headers []BlockHeader
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be two dimensional

}
Loading