1919package ledger
2020
2121import (
22- "bytes"
2322 "encoding/hex"
2423 "errors"
2524 "fmt"
@@ -34,7 +33,6 @@ import (
3433 "github.com/blinklabs-io/gouroboros/ledger/conway"
3534 "github.com/blinklabs-io/gouroboros/ledger/mary"
3635 "github.com/blinklabs-io/gouroboros/ledger/shelley"
37- "golang.org/x/crypto/blake2b"
3836)
3937
4038const (
@@ -300,47 +298,54 @@ func VerifyBlock(
300298 return false , "" , 0 , 0 , errors .New (
301299 "VerifyBlock: block CBOR is required for body hash verification" ,
302300 )
303- } else {
304- var raw []cbor.RawMessage
305- if _ , err := cbor .Decode (rawCbor , & raw ); err != nil {
301+ }
302+ era := block .Era ()
303+ eraName := era .Name
304+ minLength := 4
305+ if era .Id >= alonzo .EraAlonzo .Id {
306+ minLength = 5
307+ }
308+ if err := common .ValidateBlockBodyHash (rawCbor , expectedBodyHash , eraName , minLength ); err != nil {
309+ return false , "" , 0 , 0 , fmt .Errorf (
310+ "VerifyBlock: %w" ,
311+ err ,
312+ )
313+ }
314+ }
315+
316+ // Verify transactions (can be skipped via config)
317+ // Requires LedgerState and ProtocolParameters in config if enabled.
318+ if block .Era () != byron .EraByron && ! config .SkipTransactionValidation {
319+ var validationRules []common.UtxoValidationRuleFunc
320+ switch block .Era ().Id {
321+ case shelley .EraShelley .Id :
322+ validationRules = shelley .UtxoValidationRules
323+ case allegra .EraAllegra .Id :
324+ validationRules = allegra .UtxoValidationRules
325+ case mary .EraMary .Id :
326+ validationRules = mary .UtxoValidationRules
327+ case alonzo .EraAlonzo .Id :
328+ validationRules = alonzo .UtxoValidationRules
329+ case babbage .EraBabbage .Id :
330+ validationRules = babbage .UtxoValidationRules
331+ case conway .EraConway .Id :
332+ validationRules = conway .UtxoValidationRules
333+ default :
334+ return false , "" , 0 , 0 , fmt .Errorf (
335+ "VerifyBlock: unsupported era for transaction validation %s" ,
336+ block .Era ().Name ,
337+ )
338+ }
339+ if config .LedgerState == nil || config .ProtocolParameters == nil {
340+ return false , "" , 0 , 0 , errors .New ("VerifyBlock: missing required config field: LedgerState and ProtocolParameters must be set" )
341+ }
342+ for _ , tx := range block .Transactions () {
343+ if err := common .VerifyTransaction (tx , slot , config .LedgerState , config .ProtocolParameters , validationRules ); err != nil {
306344 return false , "" , 0 , 0 , fmt .Errorf (
307- "VerifyBlock: failed to decode block CBOR for body hash, %w" ,
345+ "VerifyBlock: transaction validation failed: %w" ,
308346 err ,
309347 )
310348 }
311- if len (raw ) < 4 {
312- return false , "" , 0 , 0 , errors .New (
313- "VerifyBlock: invalid block CBOR structure for body hash" ,
314- )
315- }
316- // Compute body hash as per Cardano spec: blake2b_256(hash_tx || hash_wit || hash_aux || hash_invalid)
317- emptyInvalidCbor , _ := cbor .Encode (cbor .IndefLengthList ([]any {}))
318- hashInvalidDefault := blake2b .Sum256 (emptyInvalidCbor )
319- var bodyHashes []byte
320- hashTx := blake2b .Sum256 (raw [1 ])
321- bodyHashes = append (bodyHashes , hashTx [:]... )
322- hashWit := blake2b .Sum256 (raw [2 ])
323- bodyHashes = append (bodyHashes , hashWit [:]... )
324- hashAux := blake2b .Sum256 (raw [3 ])
325- bodyHashes = append (bodyHashes , hashAux [:]... )
326- switch block .Header ().(type ) {
327- case * shelley.ShelleyBlockHeader , * allegra.AllegraBlockHeader , * mary.MaryBlockHeader :
328- bodyHashes = append (bodyHashes , hashInvalidDefault [:]... )
329- case * alonzo.AlonzoBlockHeader , * babbage.BabbageBlockHeader , * conway.ConwayBlockHeader :
330- hashInvalid := blake2b .Sum256 (raw [4 ])
331- bodyHashes = append (bodyHashes , hashInvalid [:]... )
332- default :
333- return false , "" , 0 , 0 , fmt .Errorf (
334- "VerifyBlock: unsupported block type for body hash %T" ,
335- block .Header (),
336- )
337- }
338- actualBodyHash := blake2b .Sum256 (bodyHashes )
339- if ! bytes .Equal (actualBodyHash [:], expectedBodyHash .Bytes ()) {
340- return false , "" , 0 , 0 , errors .New (
341- "VerifyBlock: block body hash mismatch" ,
342- )
343- }
344349 }
345350 }
346351
0 commit comments