-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3235709
commit 8d2250f
Showing
20 changed files
with
230 additions
and
1,048 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package bcnet | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/libsv/go-p2p/chaincfg/chainhash" | ||
"github.com/libsv/go-p2p/wire" | ||
) | ||
|
||
// BlockMessage only stores the transaction IDs of the block, not the full transactions | ||
type BlockMessage struct { | ||
Hash *chainhash.Hash | ||
Header *wire.BlockHeader | ||
Height uint64 | ||
TransactionHashes []*chainhash.Hash | ||
Size uint64 | ||
} | ||
|
||
func (bm *BlockMessage) Bsvdecode(io.Reader, uint32, wire.MessageEncoding) error { | ||
return nil | ||
} | ||
func (bm *BlockMessage) BsvEncode(io.Writer, uint32, wire.MessageEncoding) error { | ||
return nil | ||
} | ||
func (bm *BlockMessage) Command() string { | ||
return "block" | ||
} | ||
func (bm *BlockMessage) MaxPayloadLength(uint32) uint64 { | ||
return wire.MaxExtMsgPayload | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package blocktx_p2p | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
|
||
"github.com/bitcoin-sv/arc/internal/blocktx/bcnet" | ||
"github.com/bitcoin-sv/arc/internal/p2p" | ||
"github.com/libsv/go-p2p/chaincfg/chainhash" | ||
"github.com/libsv/go-p2p/wire" | ||
) | ||
|
||
var ErrUnableToCastWireMessage = errors.New("unable to cast wire.Message to blockchain.BlockMessage") | ||
|
||
type BlockRequest struct { | ||
Hash *chainhash.Hash | ||
Peer p2p.PeerI | ||
} | ||
|
||
var _ p2p.MessageHandlerI = (*MsgHandler)(nil) | ||
|
||
type MsgHandler struct { | ||
logger *slog.Logger | ||
blockRequestingCh chan<- BlockRequest | ||
blockProcessingCh chan<- *bcnet.BlockMessage | ||
} | ||
|
||
func NewMsgHandler(logger *slog.Logger, blockRequestCh chan<- BlockRequest, blockProcessCh chan<- *bcnet.BlockMessage) *MsgHandler { | ||
return &MsgHandler{ | ||
logger: logger.With(slog.String("module", "peer-msg-handler")), | ||
blockRequestingCh: blockRequestCh, | ||
blockProcessingCh: blockProcessCh, | ||
} | ||
} | ||
|
||
// OnReceive handles incoming messages depending on command type | ||
func (h *MsgHandler) OnReceive(msg wire.Message, peer p2p.PeerI) { | ||
cmd := msg.Command() | ||
|
||
switch cmd { | ||
case wire.CmdInv: | ||
invMsg, ok := msg.(*wire.MsgInv) | ||
if !ok { | ||
return | ||
} | ||
|
||
go func() { | ||
for _, iv := range invMsg.InvList { | ||
if iv.Type == wire.InvTypeBlock { | ||
req := BlockRequest{ | ||
Hash: &iv.Hash, | ||
Peer: peer, | ||
} | ||
|
||
h.blockRequestingCh <- req | ||
} | ||
// ignore INV with transaction or error | ||
} | ||
}() | ||
|
||
case wire.CmdBlock: | ||
blockMsg, ok := msg.(*bcnet.BlockMessage) | ||
if !ok { | ||
h.logger.Error("Block msg receive", slog.Any("err", ErrUnableToCastWireMessage)) | ||
return | ||
} | ||
|
||
h.blockProcessingCh <- blockMsg | ||
|
||
default: | ||
// ignore other messages | ||
} | ||
} | ||
|
||
// OnSend handles outgoing messages depending on command type | ||
func (h *MsgHandler) OnSend(_ wire.Message, _ p2p.PeerI) { | ||
// ignore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.