Skip to content

Commit 233a2d6

Browse files
committed
refactor: use ledger block on verify block
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 9eaeafe commit 233a2d6

File tree

17 files changed

+791
-320
lines changed

17 files changed

+791
-320
lines changed

connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"time"
2121

2222
ouroboros "github.com/blinklabs-io/gouroboros"
23-
"github.com/blinklabs-io/ouroboros-mock"
23+
ouroboros_mock "github.com/blinklabs-io/ouroboros-mock"
2424
"go.uber.org/goleak"
2525
)
2626

ledger/allegra/allegra.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (b *AllegraBlock) Utxorpc() (*utxorpc.Block, error) {
136136
return block, nil
137137
}
138138

139+
func (b *AllegraBlock) BlockBodyHash() common.Blake2b256 {
140+
return b.Header().BlockBodyHash()
141+
}
142+
139143
type AllegraBlockHeader struct {
140144
shelley.ShelleyBlockHeader
141145
}

ledger/alonzo/alonzo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ func (b *AlonzoBlock) Utxorpc() (*utxorpc.Block, error) {
189189
return block, nil
190190
}
191191

192+
func (b *AlonzoBlock) BlockBodyHash() common.Blake2b256 {
193+
return b.Header().BlockBodyHash()
194+
}
195+
192196
type AlonzoBlockHeader struct {
193197
shelley.ShelleyBlockHeader
194198
}

ledger/babbage/babbage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (b *BabbageBlock) Utxorpc() (*utxorpc.Block, error) {
149149
return block, nil
150150
}
151151

152+
func (b *BabbageBlock) BlockBodyHash() common.Blake2b256 {
153+
return b.Header().BlockBodyHash()
154+
}
155+
152156
type BabbageBlockHeader struct {
153157
cbor.StructAsArray
154158
cbor.DecodeStoreCbor
@@ -228,6 +232,10 @@ func (h *BabbageBlockHeader) Era() common.Era {
228232
return EraBabbage
229233
}
230234

235+
func (h *BabbageBlockHeader) BlockBodyHash() common.Blake2b256 {
236+
return h.Body.BlockBodyHash
237+
}
238+
231239
type BabbageTransactionPparamUpdate struct {
232240
cbor.StructAsArray
233241
ProtocolParamUpdates map[common.Blake2b224]BabbageProtocolParameterUpdate

ledger/byron/byron.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ func (h *ByronMainBlockHeader) Era() common.Era {
136136
return EraByron
137137
}
138138

139+
func (h *ByronMainBlockHeader) BlockBodyHash() common.Blake2b256 {
140+
// BodyProof is guaranteed to be common.Blake2b256 for valid Byron blocks
141+
// as per the Cardano Byron era specification
142+
if bodyProof, ok := h.BodyProof.(common.Blake2b256); ok {
143+
return bodyProof
144+
}
145+
// Return zero hash instead of panicking to prevent DoS in verification path
146+
// This will cause validation to fail gracefully rather than crash
147+
return common.Blake2b256{}
148+
}
149+
139150
type ByronTransaction struct {
140151
cbor.StructAsArray
141152
cbor.DecodeStoreCbor
@@ -731,6 +742,17 @@ func (h *ByronEpochBoundaryBlockHeader) Era() common.Era {
731742
return EraByron
732743
}
733744

745+
func (h *ByronEpochBoundaryBlockHeader) BlockBodyHash() common.Blake2b256 {
746+
// BodyProof is guaranteed to be common.Blake2b256 for valid Byron blocks
747+
// as per the Cardano Byron era specification
748+
if bodyProof, ok := h.BodyProof.(common.Blake2b256); ok {
749+
return bodyProof
750+
}
751+
// Return zero hash instead of panicking to prevent DoS in verification path
752+
// This will cause validation to fail gracefully rather than crash
753+
return common.Blake2b256{}
754+
}
755+
734756
type ByronMainBlock struct {
735757
cbor.StructAsArray
736758
cbor.DecodeStoreCbor
@@ -798,6 +820,10 @@ func (b *ByronMainBlock) Utxorpc() (*utxorpc.Block, error) {
798820
return &utxorpc.Block{}, nil
799821
}
800822

823+
func (b *ByronMainBlock) BlockBodyHash() common.Blake2b256 {
824+
return b.Header().BlockBodyHash()
825+
}
826+
801827
type ByronEpochBoundaryBlock struct {
802828
cbor.StructAsArray
803829
cbor.DecodeStoreCbor
@@ -863,6 +889,10 @@ func (b *ByronEpochBoundaryBlock) Utxorpc() (*utxorpc.Block, error) {
863889
return &utxorpc.Block{}, nil
864890
}
865891

892+
func (b *ByronEpochBoundaryBlock) BlockBodyHash() common.Blake2b256 {
893+
return b.Header().BlockBodyHash()
894+
}
895+
866896
func NewByronEpochBoundaryBlockFromCbor(
867897
data []byte,
868898
) (*ByronEpochBoundaryBlock, error) {

ledger/common/block.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ type BlockHeader interface {
1919
BlockBodySize() uint64
2020
Era() Era
2121
Cbor() []byte
22+
BlockBodyHash() Blake2b256
2223
}

ledger/common/rewards.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ func calculatePoolShare(
394394
stakeRatio := float64(poolStake) / float64(snapshot.TotalActiveStake)
395395

396396
// Calculate saturation (capped at 1.0)
397-
saturation := math.Min(stakeRatio/0.05, 1.0) // TODO: consider wiring a param or helper for consistency with CalculatePoolSaturation
397+
saturation := math.Min(
398+
stakeRatio/0.05,
399+
1.0,
400+
) // TODO: consider wiring a param or helper for consistency with CalculatePoolSaturation
398401

399402
// Calculate pool reward share using leader stake influence formula
400403
// R_pool = (stake_ratio * performance * (1 - margin)) / (1 + a0 * saturation)

ledger/conway/conway.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (b *ConwayBlock) Utxorpc() (*utxorpc.Block, error) {
149149
return block, nil
150150
}
151151

152+
func (b *ConwayBlock) BlockBodyHash() common.Blake2b256 {
153+
return b.Header().BlockBodyHash()
154+
}
155+
152156
type ConwayBlockHeader struct {
153157
babbage.BabbageBlockHeader
154158
}

ledger/leios/leios.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package leios
1616

17+
// NOTE: Leios is still in development and experimental.
18+
// Block structures and validation logic may change as the protocol evolves.
19+
// It is acceptable to skip validation on Leios blocks, but tests must be maintained.
20+
1721
import (
1822
"fmt"
1923

@@ -67,11 +71,14 @@ type LeiosEndorserBlockBody struct {
6771
}
6872

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

136+
func (h *LeiosBlockHeader) BlockBodyHash() common.Blake2b256 {
137+
return h.Body.BlockBodyHash
138+
}
139+
129140
func (LeiosEndorserBlock) Type() int {
130141
return BlockTypeLeiosEndorser
131142
}
@@ -273,6 +284,10 @@ func (b *LeiosRankingBlock) Utxorpc() (*utxorpc.Block, error) {
273284
return block, nil
274285
}
275286

287+
func (b *LeiosRankingBlock) BlockBodyHash() common.Blake2b256 {
288+
return b.Header().BlockBodyHash()
289+
}
290+
276291
func NewLeiosEndorserBlockFromCbor(data []byte) (*LeiosEndorserBlock, error) {
277292
var leiosEndorserBlock LeiosEndorserBlock
278293
if _, err := cbor.Decode(data, &leiosEndorserBlock); err != nil {

ledger/mary/mary.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func (b *MaryBlock) Utxorpc() (*utxorpc.Block, error) {
150150
return block, nil
151151
}
152152

153+
func (b *MaryBlock) BlockBodyHash() common.Blake2b256 {
154+
return b.Header().BlockBodyHash()
155+
}
156+
153157
type MaryBlockHeader struct {
154158
shelley.ShelleyBlockHeader
155159
}

0 commit comments

Comments
 (0)