Skip to content

Commit f6ccab9

Browse files
committed
feat: make chainsync timeouts configurable
1 parent 64e5116 commit f6ccab9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

protocol/chainsync/chainsync.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package chainsync
22

33
import (
4+
"time"
5+
46
"github.com/cloudstruct/go-ouroboros-network/protocol"
57
"github.com/cloudstruct/go-ouroboros-network/protocol/common"
68
)
@@ -93,6 +95,8 @@ type ChainSync struct {
9395
type Config struct {
9496
RollBackwardFunc RollBackwardFunc
9597
RollForwardFunc RollForwardFunc
98+
IntersectTimeout time.Duration
99+
BlockTimeout time.Duration
96100
}
97101

98102
// Callback function types
@@ -110,7 +114,14 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *ChainSync {
110114
type ChainSyncOptionFunc func(*Config)
111115

112116
func NewConfig(options ...ChainSyncOptionFunc) Config {
113-
c := Config{}
117+
c := Config{
118+
IntersectTimeout: 5 * time.Second,
119+
// We should really use something more useful like 30-60s, but we've seen 55s between blocks
120+
// in the preview network
121+
// https://preview.cexplorer.io/block/cb08a386363a946d2606e912fcd81ffed2bf326cdbc4058297b14471af4f67e9
122+
// https://preview.cexplorer.io/block/86806dca4ba735b233cbeee6da713bdece36fd41fb5c568f9ef5a3f5cbf572a3
123+
BlockTimeout: 180 * time.Second,
124+
}
114125
// Apply provided options functions
115126
for _, option := range options {
116127
option(&c)
@@ -129,3 +140,15 @@ func WithRollForwardFunc(rollForwardFunc RollForwardFunc) ChainSyncOptionFunc {
129140
c.RollForwardFunc = rollForwardFunc
130141
}
131142
}
143+
144+
func WithIntersectTimeout(timeout time.Duration) ChainSyncOptionFunc {
145+
return func(c *Config) {
146+
c.IntersectTimeout = timeout
147+
}
148+
}
149+
150+
func WithBlockTimeout(timeout time.Duration) ChainSyncOptionFunc {
151+
return func(c *Config) {
152+
c.BlockTimeout = timeout
153+
}
154+
}

protocol/chainsync/client.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,29 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
2727
protocolId = PROTOCOL_ID_NTN
2828
msgFromCborFunc = NewMsgFromCborNtN
2929
}
30+
if cfg == nil {
31+
tmpCfg := NewConfig()
32+
cfg = &tmpCfg
33+
}
3034
c := &Client{
3135
config: cfg,
3236
intersectResultChan: make(chan error),
3337
readyForNextBlockChan: make(chan bool),
3438
currentTipChan: make(chan Tip),
3539
}
40+
// Update state map with timeouts
41+
stateMap := StateMap
42+
if entry, ok := stateMap[STATE_INTERSECT]; ok {
43+
entry.Timeout = c.config.IntersectTimeout
44+
stateMap[STATE_INTERSECT] = entry
45+
}
46+
for _, state := range []protocol.State{STATE_CAN_AWAIT, STATE_MUST_REPLY} {
47+
if entry, ok := stateMap[state]; ok {
48+
entry.Timeout = c.config.BlockTimeout
49+
stateMap[state] = entry
50+
}
51+
}
52+
// Configure underlying Protocol
3653
protoConfig := protocol.ProtocolConfig{
3754
Name: PROTOCOL_NAME,
3855
ProtocolId: protocolId,

0 commit comments

Comments
 (0)