Skip to content

Commit

Permalink
feat: use recent chain points as intersect for chainsync client (#86)
Browse files Browse the repository at this point in the history
Fixes #84
  • Loading branch information
agaffney authored Aug 13, 2024
1 parent 84af3f5 commit e571c55
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
20 changes: 17 additions & 3 deletions chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
)

const (
chainsyncIntersectPointCount = 100
)

func (n *Node) chainsyncServerConnOpts() []ochainsync.ChainSyncOptionFunc {
return []ochainsync.ChainSyncOptionFunc{
ochainsync.WithFindIntersectFunc(n.chainsyncServerFindIntersect),
Expand All @@ -45,12 +49,22 @@ func (n *Node) chainsyncClientStart(connId ouroboros.ConnectionId) error {
return fmt.Errorf("failed to lookup connection ID: %s", connId.String())
}
oConn := conn.Conn
// TODO: use our recent blocks to build intersect points
tip, err := oConn.ChainSync().Client.GetCurrentTip()
intersectPoints, err := n.ledgerState.RecentChainPoints(chainsyncIntersectPointCount)
if err != nil {
return err
}
intersectPoints := []ocommon.Point{tip.Point}
// Empty intersect point means initial sync
if len(intersectPoints) == 0 {
// TODO: make this behavior configurable (genesis, tip, or specific point)
tip, err := oConn.ChainSync().Client.GetCurrentTip()
if err != nil {
return err
}
intersectPoints = append(
intersectPoints,
tip.Point,
)
}
if err := oConn.ChainSync().Client.Sync(intersectPoints); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func Run(logger *slog.Logger) error {
n, err := node.New(
node.NewConfig(
node.WithLogger(logger),
// TODO: uncomment and make this configurable
//node.WithDataDir(".data"),
// TODO: make this configurable
node.WithNetwork("preview"),
node.WithListeners(
Expand Down
27 changes: 23 additions & 4 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"gorm.io/gorm"

ochainsync "github.com/blinklabs-io/gouroboros/protocol/chainsync"
"github.com/blinklabs-io/gouroboros/protocol/common"
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
badger "github.com/dgraph-io/badger/v4"
)
Expand Down Expand Up @@ -155,8 +154,28 @@ func (ls *LedgerState) GetBlock(point ocommon.Point) (*models.Block, error) {
return &ret, nil
}

func (ls *LedgerState) GetIntersectPoint(points []common.Point) (*common.Point, error) {
var ret common.Point
// RecentChainPoints returns the requested count of recent chain points in descending order. This is used mostly
// for building a set of intersect points when acting as a chainsync client
func (ls *LedgerState) RecentChainPoints(count int) ([]ocommon.Point, error) {
ls.RLock()
defer ls.RUnlock()
var tmpBlocks []models.Block
result := ls.db.Metadata().Order("number DESC").Limit(count).Find(&tmpBlocks)
if result.Error != nil {
return nil, result.Error
}
var ret []ocommon.Point
for _, tmpBlock := range tmpBlocks {
ret = append(
ret,
ocommon.NewPoint(tmpBlock.Slot, tmpBlock.Hash),
)
}
return ret, nil
}

func (ls *LedgerState) GetIntersectPoint(points []ocommon.Point) (*ocommon.Point, error) {
var ret ocommon.Point
for _, point := range points {
// Ignore points with a slot earlier than an existing match
if point.Slot < ret.Slot {
Expand All @@ -180,7 +199,7 @@ func (ls *LedgerState) GetIntersectPoint(points []common.Point) (*common.Point,
return nil, nil
}

func (ls *LedgerState) GetChainFromPoint(point common.Point) (*ChainIterator, error) {
func (ls *LedgerState) GetChainFromPoint(point ocommon.Point) (*ChainIterator, error) {
return newChainIterator(ls, point)
}

Expand Down

0 comments on commit e571c55

Please sign in to comment.