Skip to content

Commit 0f2998a

Browse files
committed
feat: blockfetch server support
Fixes #348
1 parent b7f2cfd commit 0f2998a

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/blinklabs-io/gouroboros/protocol"
21+
"github.com/blinklabs-io/gouroboros/protocol/common"
2122

2223
"github.com/blinklabs-io/gouroboros/ledger"
2324
)
@@ -86,12 +87,14 @@ type BlockFetch struct {
8687

8788
type Config struct {
8889
BlockFunc BlockFunc
90+
RequestRangeFunc RequestRangeFunc
8991
BatchStartTimeout time.Duration
9092
BlockTimeout time.Duration
9193
}
9294

9395
// Callback function types
9496
type BlockFunc func(ledger.Block) error
97+
type RequestRangeFunc func(common.Point, common.Point) error
9598

9699
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *BlockFetch {
97100
b := &BlockFetch{
@@ -121,6 +124,12 @@ func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
121124
}
122125
}
123126

127+
func WithRequestRangeFunc(requestRangeFunc RequestRangeFunc) BlockFetchOptionFunc {
128+
return func(c *Config) {
129+
c.RequestRangeFunc = requestRangeFunc
130+
}
131+
}
132+
124133
func WithBatchStartTimeout(timeout time.Duration) BlockFetchOptionFunc {
125134
return func(c *Config) {
126135
c.BatchStartTimeout = timeout

protocol/blockfetch/messages.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ package blockfetch
1616

1717
import (
1818
"fmt"
19+
1920
"github.com/blinklabs-io/gouroboros/cbor"
2021
"github.com/blinklabs-io/gouroboros/protocol"
22+
"github.com/blinklabs-io/gouroboros/protocol/common"
2123
)
2224

2325
const (
@@ -57,11 +59,11 @@ func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) {
5759

5860
type MsgRequestRange struct {
5961
protocol.MessageBase
60-
Start interface{} //point
61-
End interface{} //point
62+
Start common.Point
63+
End common.Point
6264
}
6365

64-
func NewMsgRequestRange(start interface{}, end interface{}) *MsgRequestRange {
66+
func NewMsgRequestRange(start common.Point, end common.Point) *MsgRequestRange {
6567
m := &MsgRequestRange{
6668
MessageBase: protocol.MessageBase{
6769
MessageType: MessageTypeRequestRange,
@@ -139,17 +141,8 @@ func NewMsgBatchDone() *MsgBatchDone {
139141
return m
140142
}
141143

142-
// TODO: use this above and expose it, or just remove it
143-
/*
144-
type point struct {
145-
Slot uint64
146-
Hash []byte
147-
}
148-
*/
149-
150144
type WrappedBlock struct {
151-
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
152-
_ struct{} `cbor:",toarray"`
145+
cbor.StructAsArray
153146
Type uint
154147
RawBlock cbor.RawMessage
155148
}

protocol/blockfetch/server.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package blockfetch
1616

1717
import (
1818
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
1921
"github.com/blinklabs-io/gouroboros/protocol"
2022
)
2123

@@ -44,10 +46,41 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
4446
return s
4547
}
4648

49+
func (s *Server) NoBlocks() error {
50+
msg := NewMsgNoBlocks()
51+
return s.SendMessage(msg)
52+
}
53+
54+
func (s *Server) StartBatch() error {
55+
msg := NewMsgStartBatch()
56+
return s.SendMessage(msg)
57+
}
58+
59+
func (s *Server) Block(blockType uint, blockData []byte) error {
60+
wrappedBlock := WrappedBlock{
61+
Type: blockType,
62+
RawBlock: blockData,
63+
}
64+
wrappedBlockData, err := cbor.Encode(&wrappedBlock)
65+
if err != nil {
66+
return err
67+
}
68+
msg := NewMsgBlock(wrappedBlockData)
69+
return s.SendMessage(msg)
70+
}
71+
72+
func (s *Server) BatchDone() error {
73+
msg := NewMsgBatchDone()
74+
return s.SendMessage(msg)
75+
}
76+
4777
func (s *Server) messageHandler(msg protocol.Message) error {
4878
var err error
49-
// TODO: add cases for messages from client
5079
switch msg.Type() {
80+
case MessageTypeRequestRange:
81+
err = s.handleRequestRange(msg)
82+
case MessageTypeClientDone:
83+
err = s.handleClientDone()
5184
default:
5285
err = fmt.Errorf(
5386
"%s: received unexpected message type %d",
@@ -57,3 +90,17 @@ func (s *Server) messageHandler(msg protocol.Message) error {
5790
}
5891
return err
5992
}
93+
94+
func (s *Server) handleRequestRange(msg protocol.Message) error {
95+
if s.config == nil || s.config.RequestRangeFunc == nil {
96+
return fmt.Errorf(
97+
"received block-fetch RequestRange message but no callback function is defined",
98+
)
99+
}
100+
msgRequestRange := msg.(*MsgRequestRange)
101+
return s.config.RequestRangeFunc(msgRequestRange.Start, msgRequestRange.End)
102+
}
103+
104+
func (s *Server) handleClientDone() error {
105+
return nil
106+
}

0 commit comments

Comments
 (0)