@@ -2,11 +2,12 @@ package main
22
33import (
44 "crypto/tls"
5+ "encoding/hex"
56 "flag"
67 "fmt"
78 "github.com/cloudstruct/go-ouroboros-network"
9+ "github.com/cloudstruct/go-ouroboros-network/block"
810 "github.com/cloudstruct/go-ouroboros-network/protocol/chainsync"
9- "github.com/cloudstruct/go-ouroboros-network/protocol/common"
1011 "github.com/cloudstruct/go-ouroboros-network/utils"
1112 "io"
1213 "net"
@@ -27,6 +28,12 @@ type cmdFlags struct {
2728 mainnet bool
2829}
2930
31+ type chainSyncState struct {
32+ readyForNextBlockChan chan bool
33+ }
34+
35+ var syncState chainSyncState
36+
3037func main () {
3138 f := cmdFlags {}
3239 flag .StringVar (& f .socket , "socket" , "" , "UNIX socket path to connect to" )
@@ -75,6 +82,14 @@ func main() {
7582 oOpts := & ouroboros.OuroborosOptions {
7683 Conn : conn ,
7784 NetworkMagic : uint32 (f .networkMagic ),
85+ ChainSyncCallbackConfig : & chainsync.ChainSyncCallbackConfig {
86+ AwaitReplyFunc : chainSyncAwaitReplyHandler ,
87+ RollBackwardFunc : chainSyncRollBackwardHandler ,
88+ RollForwardFunc : chainSyncRollForwardHandler ,
89+ IntersectFoundFunc : chainSyncIntersectFoundHandler ,
90+ IntersectNotFoundFunc : chainSyncIntersectNotFoundHandler ,
91+ DoneFunc : chainSyncDoneHandler ,
92+ },
7893 }
7994 o , err := ouroboros .New (oOpts )
8095 if err != nil {
@@ -89,22 +104,68 @@ func main() {
89104 }
90105 }()
91106 // Test chain-sync
107+ syncState .readyForNextBlockChan = make (chan bool , 0 )
108+ secondToLastByronSlot := 1598398
109+ //lastByronBlockHash, _ := hex.DecodeString("7e16781b40ebf8b6da18f7b5e8ade855d6738095ef2f1c58c77e88b6e45997a4")
110+ secondToLastByronBlockHash , _ := hex .DecodeString ("8542ae6166cc4affadefd44585488fef9a02aee7914e1e387ce5f7a33e6569c5" )
111+ if err := o .ChainSync .FindIntersect ([]interface {}{[]interface {}{secondToLastByronSlot , secondToLastByronBlockHash }}); err != nil {
112+ fmt .Printf ("ERROR: FindIntersect: %s\n " , err )
113+ os .Exit (1 )
114+ }
115+ // Wait until ready for next block
116+ <- syncState .readyForNextBlockChan
92117 for {
93- resp , err := o .ChainSync .RequestNext ()
118+ err := o .ChainSync .RequestNext ()
94119 if err != nil {
95- fmt .Printf ("ERROR: %s\n " , err )
120+ fmt .Printf ("ERROR: RequestNext: %s\n " , err )
96121 os .Exit (1 )
97122 }
98123 //fmt.Printf("resp = %#v, err = %#v\n", resp, err)
99- switch resp .BlockType {
100- case chainsync .BLOCK_TYPE_BYRON_EBB :
101- fmt .Printf ("found Byron EBB block\n " )
102- case chainsync .BLOCK_TYPE_BYRON_MAIN :
103- block := resp .Block .(common.ByronMainBlock )
104- fmt .Printf ("epoch = %d, slot = %d, prevBlock = %s\n " , block .Header .ConsensusData .SlotId .Epoch , block .Header .ConsensusData .SlotId .Slot , block .Header .PrevBlock )
105- default :
106- fmt .Printf ("unsupported (so far) block type %d\n " , resp .BlockType )
107- fmt .Printf ("%s\n " , utils .DumpCborStructure (resp .Block , "" ))
108- }
124+ // Wait until ready for next block
125+ <- syncState .readyForNextBlockChan
126+ }
127+ }
128+
129+ func chainSyncAwaitReplyHandler () error {
130+ return nil
131+ }
132+
133+ func chainSyncRollBackwardHandler (point interface {}, tip interface {}) error {
134+ fmt .Printf ("roll backward: point = %#v, tip = %#v\n " , point , tip )
135+ syncState .readyForNextBlockChan <- true
136+ return nil
137+ }
138+
139+ func chainSyncRollForwardHandler (blockType uint , blockData interface {}) error {
140+ switch blockType {
141+ case block .BLOCK_TYPE_BYRON_EBB :
142+ fmt .Printf ("found Byron EBB block\n " )
143+ case block .BLOCK_TYPE_BYRON_MAIN :
144+ b := blockData .(block.ByronMainBlock )
145+ fmt .Printf ("era = Byron, epoch = %d, slot = %d, prevBlock = %s\n " , b .Header .ConsensusData .SlotId .Epoch , b .Header .ConsensusData .SlotId .Slot , b .Header .PrevBlock )
146+ case block .BLOCK_TYPE_SHELLEY :
147+ b := blockData .(block.ShelleyBlock )
148+ fmt .Printf ("era = Shelley, slot = %d, block_no = %d, prevHash = %s\n " , b .Header .Body .Slot , b .Header .Body .BlockNumber , b .Header .Body .PrevHash )
149+ default :
150+ fmt .Printf ("unsupported (so far) block type %d\n " , blockType )
151+ fmt .Printf ("%s\n " , utils .DumpCborStructure (blockData , "" ))
109152 }
153+ syncState .readyForNextBlockChan <- true
154+ return nil
155+ }
156+
157+ func chainSyncIntersectFoundHandler (point interface {}, tip interface {}) error {
158+ fmt .Printf ("found intersect: point = %#v, tip = %#v\n " , point , tip )
159+ syncState .readyForNextBlockChan <- true
160+ return nil
161+ }
162+
163+ func chainSyncIntersectNotFoundHandler () error {
164+ fmt .Printf ("ERROR: failed to find intersection\n " )
165+ os .Exit (1 )
166+ return nil
167+ }
168+
169+ func chainSyncDoneHandler () error {
170+ return nil
110171}
0 commit comments