Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"time"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/ouroboros-mock"
ouroboros_mock "github.com/blinklabs-io/ouroboros-mock"
"go.uber.org/goleak"
)

Expand Down
4 changes: 4 additions & 0 deletions ledger/allegra/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (b *AllegraBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *AllegraBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type AllegraBlockHeader struct {
shelley.ShelleyBlockHeader
}
Expand Down
4 changes: 4 additions & 0 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func (b *AlonzoBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *AlonzoBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type AlonzoBlockHeader struct {
shelley.ShelleyBlockHeader
}
Expand Down
8 changes: 8 additions & 0 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func (b *BabbageBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *BabbageBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type BabbageBlockHeader struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -228,6 +232,10 @@ func (h *BabbageBlockHeader) Era() common.Era {
return EraBabbage
}

func (h *BabbageBlockHeader) BlockBodyHash() common.Blake2b256 {
return h.Body.BlockBodyHash
}

type BabbageTransactionPparamUpdate struct {
cbor.StructAsArray
ProtocolParamUpdates map[common.Blake2b224]BabbageProtocolParameterUpdate
Expand Down
34 changes: 34 additions & 0 deletions ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ func (h *ByronMainBlockHeader) Era() common.Era {
return EraByron
}

func (h *ByronMainBlockHeader) BlockBodyHash() common.Blake2b256 {
// BodyProof is the hash of the block body, encoded as bytes in CBOR
if bodyProofBytes, ok := h.BodyProof.([]byte); ok &&
len(bodyProofBytes) == common.Blake2b256Size {
var hash common.Blake2b256
copy(hash[:], bodyProofBytes)
return hash
}
// Return zero hash instead of panicking to prevent DoS in verification path
// This will cause validation to fail gracefully rather than crash
return common.Blake2b256{}
}

type ByronTransaction struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -731,6 +744,19 @@ func (h *ByronEpochBoundaryBlockHeader) Era() common.Era {
return EraByron
}

func (h *ByronEpochBoundaryBlockHeader) BlockBodyHash() common.Blake2b256 {
// BodyProof is the hash of the block body, encoded as bytes in CBOR
if bodyProofBytes, ok := h.BodyProof.([]byte); ok &&
len(bodyProofBytes) == common.Blake2b256Size {
var hash common.Blake2b256
copy(hash[:], bodyProofBytes)
return hash
}
// Return zero hash instead of panicking to prevent DoS in verification path
// This will cause validation to fail gracefully rather than crash
return common.Blake2b256{}
}

type ByronMainBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -798,6 +824,10 @@ func (b *ByronMainBlock) Utxorpc() (*utxorpc.Block, error) {
return &utxorpc.Block{}, nil
}

func (b *ByronMainBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type ByronEpochBoundaryBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -863,6 +893,10 @@ func (b *ByronEpochBoundaryBlock) Utxorpc() (*utxorpc.Block, error) {
return &utxorpc.Block{}, nil
}

func (b *ByronEpochBoundaryBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

func NewByronEpochBoundaryBlockFromCbor(
data []byte,
) (*ByronEpochBoundaryBlock, error) {
Expand Down
1 change: 1 addition & 0 deletions ledger/common/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ type BlockHeader interface {
BlockBodySize() uint64
Era() Era
Cbor() []byte
BlockBodyHash() Blake2b256
}
5 changes: 4 additions & 1 deletion ledger/common/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ func calculatePoolShare(
stakeRatio := float64(poolStake) / float64(snapshot.TotalActiveStake)

// Calculate saturation (capped at 1.0)
saturation := math.Min(stakeRatio/0.05, 1.0) // TODO: consider wiring a param or helper for consistency with CalculatePoolSaturation
saturation := math.Min(
stakeRatio/0.05,
1.0,
) // TODO: consider wiring a param or helper for consistency with CalculatePoolSaturation

// Calculate pool reward share using leader stake influence formula
// R_pool = (stake_ratio * performance * (1 - margin)) / (1 + a0 * saturation)
Expand Down
4 changes: 4 additions & 0 deletions ledger/conway/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func (b *ConwayBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *ConwayBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type ConwayBlockHeader struct {
babbage.BabbageBlockHeader
}
Expand Down
25 changes: 22 additions & 3 deletions ledger/leios/leios.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package leios

// NOTE: Leios is still in development and experimental.
// Block structures and validation logic may change as the protocol evolves.
// It is acceptable to skip validation on Leios blocks, but tests must be maintained.

import (
"fmt"

Expand Down Expand Up @@ -67,11 +71,14 @@ type LeiosEndorserBlockBody struct {
}

func (b *LeiosEndorserBlockBody) BlockBodyHash() common.Blake2b256 {
// NOTE: Leios is still in development and experimental.
// This implementation may change as the protocol evolves.
// Compute hash of the block body content
bodyCbor, err := cbor.Encode(b)
if err != nil {
// Return zero hash on encoding error
return common.Blake2b256{}
// CBOR encoding failure indicates a serious structural issue
// Panic loudly during development to catch problems early
panic(fmt.Sprintf("Leios block body CBOR encoding failed: %v", err))
}
return common.Blake2b256Hash(bodyCbor)
}
Expand Down Expand Up @@ -126,6 +133,10 @@ func (h *LeiosBlockHeader) Era() common.Era {
return EraLeios
}

func (h *LeiosBlockHeader) BlockBodyHash() common.Blake2b256 {
return h.Body.BlockBodyHash
}

func (LeiosEndorserBlock) Type() int {
return BlockTypeLeiosEndorser
}
Expand Down Expand Up @@ -182,7 +193,8 @@ func (b *LeiosEndorserBlock) Utxorpc() (*utxorpc.Block, error) {

func (b *LeiosEndorserBlock) BlockBodyHash() common.Blake2b256 {
if b.Body == nil {
return common.Blake2b256{}
// Panic on nil body to distinguish from empty body
panic("LeiosEndorserBlock has nil body")
}
return b.Body.BlockBodyHash()
}
Expand Down Expand Up @@ -273,6 +285,13 @@ func (b *LeiosRankingBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *LeiosRankingBlock) BlockBodyHash() common.Blake2b256 {
if b.BlockHeader == nil {
panic("LeiosRankingBlock has nil BlockHeader")
}
return b.Header().BlockBodyHash()
}

func NewLeiosEndorserBlockFromCbor(data []byte) (*LeiosEndorserBlock, error) {
var leiosEndorserBlock LeiosEndorserBlock
if _, err := cbor.Decode(data, &leiosEndorserBlock); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions ledger/mary/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (b *MaryBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *MaryBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type MaryBlockHeader struct {
shelley.ShelleyBlockHeader
}
Expand Down
8 changes: 8 additions & 0 deletions ledger/shelley/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (b *ShelleyBlock) Utxorpc() (*utxorpc.Block, error) {
return block, nil
}

func (b *ShelleyBlock) BlockBodyHash() common.Blake2b256 {
return b.Header().BlockBodyHash()
}

type ShelleyBlockHeader struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -209,6 +213,10 @@ func (h *ShelleyBlockHeader) Era() common.Era {
return EraShelley
}

func (h *ShelleyBlockHeader) BlockBodyHash() common.Blake2b256 {
return h.Body.BlockBodyHash
}

type ShelleyTransactionPparamUpdate struct {
cbor.StructAsArray
ProtocolParamUpdates map[common.Blake2b224]ShelleyProtocolParameterUpdate
Expand Down
Loading