Skip to content

Commit

Permalink
Merge pull request #240 from blinklabs-io/feat/byron-absolute-slot
Browse files Browse the repository at this point in the history
feat: calculate Byron block absolute slot
  • Loading branch information
agaffney authored Apr 29, 2023
2 parents 0a5838f + 11e5a13 commit 390527a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
55 changes: 13 additions & 42 deletions cmd/gouroboros/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import (
"github.com/blinklabs-io/gouroboros/protocol/common"
)

type chainSyncState struct {
oConn *ouroboros.Ouroboros
byronEpochBaseSlot uint64
byronEpochSlot uint64
}

var syncState chainSyncState
var oConn *ouroboros.Ouroboros

type chainSyncFlags struct {
flagset *flag.FlagSet
Expand Down Expand Up @@ -128,7 +122,7 @@ func testChainSync(f *globalFlags) {
os.Exit(1)
}
}()
o, err := ouroboros.New(
oConn, err = ouroboros.New(
ouroboros.WithConnection(conn),
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
ouroboros.WithErrorChan(errorChan),
Expand All @@ -141,16 +135,14 @@ func testChainSync(f *globalFlags) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.ChainSync().Client.Start()
oConn.ChainSync().Client.Start()
if f.ntnProto {
o.BlockFetch().Client.Start()
oConn.BlockFetch().Client.Start()
}

syncState.oConn = o

var point common.Point
if chainSyncFlags.tip {
tip, err := o.ChainSync().Client.GetCurrentTip()
tip, err := oConn.ChainSync().Client.GetCurrentTip()
if err != nil {
fmt.Printf("ERROR: failed to get current tip: %s\n", err)
os.Exit(1)
Expand All @@ -166,17 +158,17 @@ func testChainSync(f *globalFlags) {
point = common.NewPointOrigin()
}
if !f.ntnProto || !chainSyncFlags.bulk {
if err := o.ChainSync().Client.Sync([]common.Point{point}); err != nil {
if err := oConn.ChainSync().Client.Sync([]common.Point{point}); err != nil {
fmt.Printf("ERROR: failed to start chain-sync: %s\n", err)
os.Exit(1)
}
} else {
tip, err := o.ChainSync().Client.GetCurrentTip()
tip, err := oConn.ChainSync().Client.GetCurrentTip()
if err != nil {
fmt.Printf("ERROR: failed to get chain tip: %s\n", err)
os.Exit(1)
}
if err := o.BlockFetch().Client.GetBlockRange(point, tip.Point); err != nil {
if err := oConn.BlockFetch().Client.GetBlockRange(point, tip.Point); err != nil {
fmt.Printf("ERROR: failed to request block range: %s\n", err)
os.Exit(1)
}
Expand All @@ -196,27 +188,10 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
case ledger.Block:
block = v
case ledger.BlockHeader:
var blockSlot uint64
var blockHash []byte
switch blockType {
case ledger.BLOCK_TYPE_BYRON_EBB:
byronEbbHeader := v.(*ledger.ByronEpochBoundaryBlockHeader)
if syncState.byronEpochSlot > 0 {
syncState.byronEpochBaseSlot += syncState.byronEpochSlot + 1
}
blockSlot = syncState.byronEpochBaseSlot
blockHash, _ = hex.DecodeString(byronEbbHeader.Hash())
case ledger.BLOCK_TYPE_BYRON_MAIN:
byronHeader := v.(*ledger.ByronMainBlockHeader)
syncState.byronEpochSlot = uint64(byronHeader.ConsensusData.SlotId.Slot)
blockSlot = syncState.byronEpochBaseSlot + syncState.byronEpochSlot
blockHash, _ = hex.DecodeString(byronHeader.Hash())
default:
blockSlot = v.SlotNumber()
blockHash, _ = hex.DecodeString(v.Hash())
}
blockSlot := v.SlotNumber()
blockHash, _ := hex.DecodeString(v.Hash())
var err error
block, err = syncState.oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash))
block, err = oConn.BlockFetch().Client.GetBlock(common.NewPoint(blockSlot, blockHash))
if err != nil {
return err
}
Expand All @@ -225,7 +200,7 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
switch blockType {
case ledger.BLOCK_TYPE_BYRON_EBB:
byronEbbBlock := block.(*ledger.ByronEpochBoundaryBlock)
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", byronEbbBlock.Header.ConsensusData.Epoch, byronEbbBlock.Hash())
fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", byronEbbBlock.Header.ConsensusData.Epoch, byronEbbBlock.SlotNumber(), byronEbbBlock.Hash())
case ledger.BLOCK_TYPE_BYRON_MAIN:
byronBlock := block.(*ledger.ByronMainBlock)
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", byronBlock.Header.ConsensusData.SlotId.Epoch, byronBlock.SlotNumber(), byronBlock.Hash())
Expand All @@ -238,12 +213,8 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
func blockFetchBlockHandler(blockData ledger.Block) error {
switch block := blockData.(type) {
case *ledger.ByronEpochBoundaryBlock:
if syncState.byronEpochSlot > 0 {
syncState.byronEpochBaseSlot += syncState.byronEpochSlot + 1
}
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.Hash())
fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.SlotNumber(), block.Hash())
case *ledger.ByronMainBlock:
syncState.byronEpochSlot = uint64(block.Header.ConsensusData.SlotId.Slot)
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.SlotId.Epoch, block.SlotNumber(), block.Hash())
case ledger.Block:
fmt.Printf("era = %s, slot = %d, block_no = %d, id = %s\n", block.Era().Name, block.SlotNumber(), block.BlockNumber(), block.Hash())
Expand Down
7 changes: 4 additions & 3 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
BLOCK_HEADER_TYPE_BYRON = 0

TX_TYPE_BYRON = 0

BYRON_SLOTS_PER_EPOCH = 21600
)

type ByronMainBlockHeader struct {
Expand Down Expand Up @@ -77,7 +79,7 @@ func (h *ByronMainBlockHeader) BlockNumber() uint64 {
}

func (h *ByronMainBlockHeader) SlotNumber() uint64 {
return uint64(h.ConsensusData.SlotId.Slot)
return uint64((h.ConsensusData.SlotId.Epoch * BYRON_SLOTS_PER_EPOCH) + uint64(h.ConsensusData.SlotId.Slot))
}

func (h *ByronMainBlockHeader) Era() Era {
Expand Down Expand Up @@ -136,8 +138,7 @@ func (h *ByronEpochBoundaryBlockHeader) BlockNumber() uint64 {
}

func (h *ByronEpochBoundaryBlockHeader) SlotNumber() uint64 {
// There is no slot on boundary blocks
return 0
return uint64(h.ConsensusData.Epoch * BYRON_SLOTS_PER_EPOCH)
}

func (h *ByronEpochBoundaryBlockHeader) Era() Era {
Expand Down

0 comments on commit 390527a

Please sign in to comment.