Skip to content

Commit 7010c05

Browse files
authored
refactor: use ledger block on verify block (#1264)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 9eaeafe commit 7010c05

File tree

17 files changed

+693
-325
lines changed

17 files changed

+693
-325
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ func (h *ByronMainBlockHeader) Era() common.Era {
136136
return EraByron
137137
}
138138

139+
func (h *ByronMainBlockHeader) BlockBodyHash() common.Blake2b256 {
140+
// BodyProof is the hash of the block body, encoded as bytes in CBOR
141+
if bodyProofBytes, ok := h.BodyProof.([]byte); ok &&
142+
len(bodyProofBytes) == common.Blake2b256Size {
143+
var hash common.Blake2b256
144+
copy(hash[:], bodyProofBytes)
145+
return hash
146+
}
147+
// Return zero hash instead of panicking to prevent DoS in verification path
148+
// This will cause validation to fail gracefully rather than crash
149+
return common.Blake2b256{}
150+
}
151+
139152
type ByronTransaction struct {
140153
cbor.StructAsArray
141154
cbor.DecodeStoreCbor
@@ -731,6 +744,19 @@ func (h *ByronEpochBoundaryBlockHeader) Era() common.Era {
731744
return EraByron
732745
}
733746

747+
func (h *ByronEpochBoundaryBlockHeader) BlockBodyHash() common.Blake2b256 {
748+
// BodyProof is the hash of the block body, encoded as bytes in CBOR
749+
if bodyProofBytes, ok := h.BodyProof.([]byte); ok &&
750+
len(bodyProofBytes) == common.Blake2b256Size {
751+
var hash common.Blake2b256
752+
copy(hash[:], bodyProofBytes)
753+
return hash
754+
}
755+
// Return zero hash instead of panicking to prevent DoS in verification path
756+
// This will cause validation to fail gracefully rather than crash
757+
return common.Blake2b256{}
758+
}
759+
734760
type ByronMainBlock struct {
735761
cbor.StructAsArray
736762
cbor.DecodeStoreCbor
@@ -798,6 +824,10 @@ func (b *ByronMainBlock) Utxorpc() (*utxorpc.Block, error) {
798824
return &utxorpc.Block{}, nil
799825
}
800826

827+
func (b *ByronMainBlock) BlockBodyHash() common.Blake2b256 {
828+
return b.Header().BlockBodyHash()
829+
}
830+
801831
type ByronEpochBoundaryBlock struct {
802832
cbor.StructAsArray
803833
cbor.DecodeStoreCbor
@@ -863,6 +893,10 @@ func (b *ByronEpochBoundaryBlock) Utxorpc() (*utxorpc.Block, error) {
863893
return &utxorpc.Block{}, nil
864894
}
865895

896+
func (b *ByronEpochBoundaryBlock) BlockBodyHash() common.Blake2b256 {
897+
return b.Header().BlockBodyHash()
898+
}
899+
866900
func NewByronEpochBoundaryBlockFromCbor(
867901
data []byte,
868902
) (*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: 22 additions & 3 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
}
@@ -182,7 +193,8 @@ func (b *LeiosEndorserBlock) Utxorpc() (*utxorpc.Block, error) {
182193

183194
func (b *LeiosEndorserBlock) BlockBodyHash() common.Blake2b256 {
184195
if b.Body == nil {
185-
return common.Blake2b256{}
196+
// Panic on nil body to distinguish from empty body
197+
panic("LeiosEndorserBlock has nil body")
186198
}
187199
return b.Body.BlockBodyHash()
188200
}
@@ -273,6 +285,13 @@ func (b *LeiosRankingBlock) Utxorpc() (*utxorpc.Block, error) {
273285
return block, nil
274286
}
275287

288+
func (b *LeiosRankingBlock) BlockBodyHash() common.Blake2b256 {
289+
if b.BlockHeader == nil {
290+
panic("LeiosRankingBlock has nil BlockHeader")
291+
}
292+
return b.Header().BlockBodyHash()
293+
}
294+
276295
func NewLeiosEndorserBlockFromCbor(data []byte) (*LeiosEndorserBlock, error) {
277296
var leiosEndorserBlock LeiosEndorserBlock
278297
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)