Skip to content

Commit 390527a

Browse files
authored
Merge pull request #240 from blinklabs-io/feat/byron-absolute-slot
feat: calculate Byron block absolute slot
2 parents 0a5838f + 11e5a13 commit 390527a

File tree

2 files changed

+17
-45
lines changed

2 files changed

+17
-45
lines changed

cmd/gouroboros/chainsync.go

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ import (
1313
"github.com/blinklabs-io/gouroboros/protocol/common"
1414
)
1515

16-
type chainSyncState struct {
17-
oConn *ouroboros.Ouroboros
18-
byronEpochBaseSlot uint64
19-
byronEpochSlot uint64
20-
}
21-
22-
var syncState chainSyncState
16+
var oConn *ouroboros.Ouroboros
2317

2418
type chainSyncFlags struct {
2519
flagset *flag.FlagSet
@@ -128,7 +122,7 @@ func testChainSync(f *globalFlags) {
128122
os.Exit(1)
129123
}
130124
}()
131-
o, err := ouroboros.New(
125+
oConn, err = ouroboros.New(
132126
ouroboros.WithConnection(conn),
133127
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
134128
ouroboros.WithErrorChan(errorChan),
@@ -141,16 +135,14 @@ func testChainSync(f *globalFlags) {
141135
fmt.Printf("ERROR: %s\n", err)
142136
os.Exit(1)
143137
}
144-
o.ChainSync().Client.Start()
138+
oConn.ChainSync().Client.Start()
145139
if f.ntnProto {
146-
o.BlockFetch().Client.Start()
140+
oConn.BlockFetch().Client.Start()
147141
}
148142

149-
syncState.oConn = o
150-
151143
var point common.Point
152144
if chainSyncFlags.tip {
153-
tip, err := o.ChainSync().Client.GetCurrentTip()
145+
tip, err := oConn.ChainSync().Client.GetCurrentTip()
154146
if err != nil {
155147
fmt.Printf("ERROR: failed to get current tip: %s\n", err)
156148
os.Exit(1)
@@ -166,17 +158,17 @@ func testChainSync(f *globalFlags) {
166158
point = common.NewPointOrigin()
167159
}
168160
if !f.ntnProto || !chainSyncFlags.bulk {
169-
if err := o.ChainSync().Client.Sync([]common.Point{point}); err != nil {
161+
if err := oConn.ChainSync().Client.Sync([]common.Point{point}); err != nil {
170162
fmt.Printf("ERROR: failed to start chain-sync: %s\n", err)
171163
os.Exit(1)
172164
}
173165
} else {
174-
tip, err := o.ChainSync().Client.GetCurrentTip()
166+
tip, err := oConn.ChainSync().Client.GetCurrentTip()
175167
if err != nil {
176168
fmt.Printf("ERROR: failed to get chain tip: %s\n", err)
177169
os.Exit(1)
178170
}
179-
if err := o.BlockFetch().Client.GetBlockRange(point, tip.Point); err != nil {
171+
if err := oConn.BlockFetch().Client.GetBlockRange(point, tip.Point); err != nil {
180172
fmt.Printf("ERROR: failed to request block range: %s\n", err)
181173
os.Exit(1)
182174
}
@@ -196,27 +188,10 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
196188
case ledger.Block:
197189
block = v
198190
case ledger.BlockHeader:
199-
var blockSlot uint64
200-
var blockHash []byte
201-
switch blockType {
202-
case ledger.BLOCK_TYPE_BYRON_EBB:
203-
byronEbbHeader := v.(*ledger.ByronEpochBoundaryBlockHeader)
204-
if syncState.byronEpochSlot > 0 {
205-
syncState.byronEpochBaseSlot += syncState.byronEpochSlot + 1
206-
}
207-
blockSlot = syncState.byronEpochBaseSlot
208-
blockHash, _ = hex.DecodeString(byronEbbHeader.Hash())
209-
case ledger.BLOCK_TYPE_BYRON_MAIN:
210-
byronHeader := v.(*ledger.ByronMainBlockHeader)
211-
syncState.byronEpochSlot = uint64(byronHeader.ConsensusData.SlotId.Slot)
212-
blockSlot = syncState.byronEpochBaseSlot + syncState.byronEpochSlot
213-
blockHash, _ = hex.DecodeString(byronHeader.Hash())
214-
default:
215-
blockSlot = v.SlotNumber()
216-
blockHash, _ = hex.DecodeString(v.Hash())
217-
}
191+
blockSlot := v.SlotNumber()
192+
blockHash, _ := hex.DecodeString(v.Hash())
218193
var err error
219-
block, err = syncState.oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash))
194+
block, err = oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash))
220195
if err != nil {
221196
return err
222197
}
@@ -225,7 +200,7 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
225200
switch blockType {
226201
case ledger.BLOCK_TYPE_BYRON_EBB:
227202
byronEbbBlock := block.(*ledger.ByronEpochBoundaryBlock)
228-
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", byronEbbBlock.Header.ConsensusData.Epoch, byronEbbBlock.Hash())
203+
fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", byronEbbBlock.Header.ConsensusData.Epoch, byronEbbBlock.SlotNumber(), byronEbbBlock.Hash())
229204
case ledger.BLOCK_TYPE_BYRON_MAIN:
230205
byronBlock := block.(*ledger.ByronMainBlock)
231206
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", byronBlock.Header.ConsensusData.SlotId.Epoch, byronBlock.SlotNumber(), byronBlock.Hash())
@@ -238,12 +213,8 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
238213
func blockFetchBlockHandler(blockData ledger.Block) error {
239214
switch block := blockData.(type) {
240215
case *ledger.ByronEpochBoundaryBlock:
241-
if syncState.byronEpochSlot > 0 {
242-
syncState.byronEpochBaseSlot += syncState.byronEpochSlot + 1
243-
}
244-
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.Hash())
216+
fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.SlotNumber(), block.Hash())
245217
case *ledger.ByronMainBlock:
246-
syncState.byronEpochSlot = uint64(block.Header.ConsensusData.SlotId.Slot)
247218
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.SlotId.Epoch, block.SlotNumber(), block.Hash())
248219
case ledger.Block:
249220
fmt.Printf("era = %s, slot = %d, block_no = %d, id = %s\n", block.Era().Name, block.SlotNumber(), block.BlockNumber(), block.Hash())

ledger/byron.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const (
1515
BLOCK_HEADER_TYPE_BYRON = 0
1616

1717
TX_TYPE_BYRON = 0
18+
19+
BYRON_SLOTS_PER_EPOCH = 21600
1820
)
1921

2022
type ByronMainBlockHeader struct {
@@ -77,7 +79,7 @@ func (h *ByronMainBlockHeader) BlockNumber() uint64 {
7779
}
7880

7981
func (h *ByronMainBlockHeader) SlotNumber() uint64 {
80-
return uint64(h.ConsensusData.SlotId.Slot)
82+
return uint64((h.ConsensusData.SlotId.Epoch * BYRON_SLOTS_PER_EPOCH) + uint64(h.ConsensusData.SlotId.Slot))
8183
}
8284

8385
func (h *ByronMainBlockHeader) Era() Era {
@@ -136,8 +138,7 @@ func (h *ByronEpochBoundaryBlockHeader) BlockNumber() uint64 {
136138
}
137139

138140
func (h *ByronEpochBoundaryBlockHeader) SlotNumber() uint64 {
139-
// There is no slot on boundary blocks
140-
return 0
141+
return uint64(h.ConsensusData.Epoch * BYRON_SLOTS_PER_EPOCH)
141142
}
142143

143144
func (h *ByronEpochBoundaryBlockHeader) Era() Era {

0 commit comments

Comments
 (0)