From 6092fdda835ef6d64a97f6e01d7a2a5d931aaa7e Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Fri, 22 Nov 2024 12:45:43 -0500 Subject: [PATCH] feat: block function to get header (#800) Signed-off-by: Chris Gianelloni --- cmd/gouroboros/chainsync.go | 8 +++--- ledger/allegra/allegra.go | 18 +++++++----- ledger/alonzo/alonzo.go | 18 +++++++----- ledger/babbage/babbage.go | 18 +++++++----- ledger/byron/byron.go | 44 ++++++++++++++++++------------ ledger/common/block.go | 1 + ledger/conway/conway.go | 18 +++++++----- ledger/mary/mary.go | 18 +++++++----- ledger/shelley/shelley.go | 18 +++++++----- ledger/verify_block_test.go | 4 +-- protocol/blockfetch/client_test.go | 6 ++-- protocol/chainsync/client_test.go | 6 ++-- 12 files changed, 105 insertions(+), 72 deletions(-) diff --git a/cmd/gouroboros/chainsync.go b/cmd/gouroboros/chainsync.go index ccad2db1..c7368f07 100644 --- a/cmd/gouroboros/chainsync.go +++ b/cmd/gouroboros/chainsync.go @@ -279,7 +279,7 @@ func chainSyncRollForwardHandler( byronEbbBlock := block.(*ledger.ByronEpochBoundaryBlock) fmt.Printf( "era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", - byronEbbBlock.Header.ConsensusData.Epoch, + byronEbbBlock.BlockHeader.ConsensusData.Epoch, byronEbbBlock.SlotNumber(), byronEbbBlock.Hash(), ) @@ -287,7 +287,7 @@ func chainSyncRollForwardHandler( byronBlock := block.(*ledger.ByronMainBlock) fmt.Printf( "era = Byron, epoch = %d, slot = %d, id = %s\n", - byronBlock.Header.ConsensusData.SlotId.Epoch, + byronBlock.BlockHeader.ConsensusData.SlotId.Epoch, byronBlock.SlotNumber(), byronBlock.Hash(), ) @@ -313,9 +313,9 @@ func blockFetchBlockHandler( ) error { switch block := blockData.(type) { case *ledger.ByronEpochBoundaryBlock: - fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.SlotNumber(), block.Hash()) + fmt.Printf("era = Byron (EBB), epoch = %d, slot = %d, id = %s\n", block.BlockHeader.ConsensusData.Epoch, block.SlotNumber(), block.Hash()) case *ledger.ByronMainBlock: - fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.SlotId.Epoch, block.SlotNumber(), block.Hash()) + fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", block.BlockHeader.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()) } diff --git a/ledger/allegra/allegra.go b/ledger/allegra/allegra.go index ae45b1f3..11224085 100644 --- a/ledger/allegra/allegra.go +++ b/ledger/allegra/allegra.go @@ -50,7 +50,7 @@ func init() { type AllegraBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *AllegraBlockHeader + BlockHeader *AllegraBlockHeader TransactionBodies []AllegraTransactionBody TransactionWitnessSets []shelley.ShelleyTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -65,27 +65,31 @@ func (AllegraBlock) Type() int { } func (b *AllegraBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *AllegraBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *AllegraBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *AllegraBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *AllegraBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *AllegraBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *AllegraBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *AllegraBlock) Era() common.Era { diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index 345a1128..f9b22646 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -52,7 +52,7 @@ func init() { type AlonzoBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *AlonzoBlockHeader + BlockHeader *AlonzoBlockHeader TransactionBodies []AlonzoTransactionBody TransactionWitnessSets []AlonzoTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -68,27 +68,31 @@ func (AlonzoBlock) Type() int { } func (b *AlonzoBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *AlonzoBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *AlonzoBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *AlonzoBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *AlonzoBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *AlonzoBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *AlonzoBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *AlonzoBlock) Era() common.Era { diff --git a/ledger/babbage/babbage.go b/ledger/babbage/babbage.go index d52852bc..d85480e2 100644 --- a/ledger/babbage/babbage.go +++ b/ledger/babbage/babbage.go @@ -53,7 +53,7 @@ func init() { type BabbageBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *BabbageBlockHeader + BlockHeader *BabbageBlockHeader TransactionBodies []BabbageTransactionBody TransactionWitnessSets []BabbageTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -69,27 +69,31 @@ func (BabbageBlock) Type() int { } func (b *BabbageBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *BabbageBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *BabbageBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *BabbageBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *BabbageBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *BabbageBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *BabbageBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *BabbageBlock) Era() common.Era { diff --git a/ledger/byron/byron.go b/ledger/byron/byron.go index 9a07d5a5..8ccb0311 100644 --- a/ledger/byron/byron.go +++ b/ledger/byron/byron.go @@ -559,9 +559,9 @@ func (h *ByronEpochBoundaryBlockHeader) Era() common.Era { type ByronMainBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *ByronMainBlockHeader - Body ByronMainBlockBody - Extra []interface{} + BlockHeader *ByronMainBlockHeader + Body ByronMainBlockBody + Extra []interface{} } func (b *ByronMainBlock) UnmarshalCBOR(cborData []byte) error { @@ -574,23 +574,27 @@ func (ByronMainBlock) Type() int { } func (b *ByronMainBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *ByronMainBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *ByronMainBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *ByronMainBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *ByronMainBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *ByronMainBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *ByronMainBlock) BlockBodySize() uint64 { @@ -598,7 +602,7 @@ func (b *ByronMainBlock) BlockBodySize() uint64 { } func (b *ByronMainBlock) Era() common.Era { - return b.Header.Era() + return b.BlockHeader.Era() } func (b *ByronMainBlock) Transactions() []common.Transaction { @@ -617,9 +621,9 @@ func (b *ByronMainBlock) Utxorpc() *utxorpc.Block { type ByronEpochBoundaryBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *ByronEpochBoundaryBlockHeader - Body []common.Blake2b224 - Extra []interface{} + BlockHeader *ByronEpochBoundaryBlockHeader + Body []common.Blake2b224 + Extra []interface{} } func (b *ByronEpochBoundaryBlock) UnmarshalCBOR(cborData []byte) error { @@ -632,23 +636,27 @@ func (ByronEpochBoundaryBlock) Type() int { } func (b *ByronEpochBoundaryBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *ByronEpochBoundaryBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *ByronEpochBoundaryBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *ByronEpochBoundaryBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *ByronEpochBoundaryBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *ByronEpochBoundaryBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *ByronEpochBoundaryBlock) BlockBodySize() uint64 { @@ -657,7 +665,7 @@ func (b *ByronEpochBoundaryBlock) BlockBodySize() uint64 { } func (b *ByronEpochBoundaryBlock) Era() common.Era { - return b.Header.Era() + return b.BlockHeader.Era() } func (b *ByronEpochBoundaryBlock) Transactions() []common.Transaction { diff --git a/ledger/common/block.go b/ledger/common/block.go index 04105d30..b723a4b9 100644 --- a/ledger/common/block.go +++ b/ledger/common/block.go @@ -4,6 +4,7 @@ import utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" type Block interface { BlockHeader + Header() BlockHeader Type() int Transactions() []Transaction Utxorpc() *utxorpc.Block diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index de39e9cf..e65e076d 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -52,7 +52,7 @@ func init() { type ConwayBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *ConwayBlockHeader + BlockHeader *ConwayBlockHeader TransactionBodies []ConwayTransactionBody TransactionWitnessSets []ConwayTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -68,27 +68,31 @@ func (ConwayBlock) Type() int { } func (b *ConwayBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *ConwayBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *ConwayBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *ConwayBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *ConwayBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *ConwayBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *ConwayBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *ConwayBlock) Era() common.Era { diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index 967f4cd4..00c99c1f 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -52,7 +52,7 @@ func init() { type MaryBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *MaryBlockHeader + BlockHeader *MaryBlockHeader TransactionBodies []MaryTransactionBody TransactionWitnessSets []MaryTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -67,27 +67,31 @@ func (MaryBlock) Type() int { } func (b *MaryBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *MaryBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *MaryBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *MaryBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *MaryBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *MaryBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *MaryBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *MaryBlock) Era() common.Era { diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index 3de1e6b2..ca978f2c 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -49,7 +49,7 @@ func init() { type ShelleyBlock struct { cbor.StructAsArray cbor.DecodeStoreCbor - Header *ShelleyBlockHeader + BlockHeader *ShelleyBlockHeader TransactionBodies []ShelleyTransactionBody TransactionWitnessSets []ShelleyTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue @@ -64,27 +64,31 @@ func (ShelleyBlock) Type() int { } func (b *ShelleyBlock) Hash() string { - return b.Header.Hash() + return b.BlockHeader.Hash() +} + +func (b *ShelleyBlock) Header() common.BlockHeader { + return b.BlockHeader } func (b *ShelleyBlock) PrevHash() string { - return b.Header.PrevHash() + return b.BlockHeader.PrevHash() } func (b *ShelleyBlock) BlockNumber() uint64 { - return b.Header.BlockNumber() + return b.BlockHeader.BlockNumber() } func (b *ShelleyBlock) SlotNumber() uint64 { - return b.Header.SlotNumber() + return b.BlockHeader.SlotNumber() } func (b *ShelleyBlock) IssuerVkey() common.IssuerVkey { - return b.Header.IssuerVkey() + return b.BlockHeader.IssuerVkey() } func (b *ShelleyBlock) BlockBodySize() uint64 { - return b.Header.BlockBodySize() + return b.BlockHeader.BlockBodySize() } func (b *ShelleyBlock) Era() common.Era { diff --git a/ledger/verify_block_test.go b/ledger/verify_block_test.go index 0ad398ef..38373a04 100644 --- a/ledger/verify_block_test.go +++ b/ledger/verify_block_test.go @@ -24,7 +24,7 @@ func TestVerifyBlockBody(t *testing.T) { expectedValid: true, }, { - //https://cardanoscan.io/block/10873394 + // https://cardanoscan.io/block/10873394 name: "Large set of aux data, complicated aux", blockHexCbor: BlockHexCbor{ Flag: 0, @@ -36,7 +36,7 @@ func TestVerifyBlockBody(t *testing.T) { expectedValid: true, }, { - name: "TestVerifyBlockBody should failed", + name: "TestVerifyBlockBody should fail", blockHexCbor: BlockHexCbor{ Flag: 0, HeaderCbor: "828a1a00a11c251a07b248af582012e0d55c96d9c197fa3cc265ba81df9b9bd435d32e839b436fbec9623799145558206c6aeb4c58918e71403d8a9bee01e39ac8c8c4ffc52bf58b46beb7a7d121a05b5820e44615156d5140f59b218cbcc64ed195c23f0ddc3d80e4f63541f96082e80871825840f29a8c0373c08f8e19adbd02d8179560a32d3e7de1985499c9e250379c6f5b46db84e875d703f7239c786cad5807ca6ad16c8d08f92360a28dfd9e354fe1b13b58509f99f42454b9cdde9f5559560f9baa7a5a6f79f97659ec56e6d81ddd4e5c3e64e27ca2e0804b2456c0b59050b6d994fb60b1f8c0518c031d1f5c36b5c5e08db18afb055fe6d41252b7b0b42510d8b50c1935f658202e293f1d9aed556b51dd1eee374e3899124c8a3f9b88bd3d9c5a1e3d3b5960778458206e31b612cbbbdc6e539ebb175835424b4fa608c4937ea42c761ef7e84f25c5a1121903cf584091bafbbc5b280c96d1bc2b71cfaee13be1146c2b980ed2851bdf3f2dbb880a3efd7fd3ba0c982c0a9e27b8552a7540108e4c8f239807c44b956e1d978b93130b8208005901c034ad8775bc3af6c8fbcec1e704dc53439de0c0f86cccd79d84fd7788bf009d976960b8b291bca6e250ef1c8cd3f8c8c064c94afe039eb006e5247b8288a7340911674523ab058fded91b667a21d8b1cb1a4c2305b9f80ff03ed7ea036b87ec3185356bcd8e425811d88265ef5ec42d4aeaa526772a0e57bddf202008df47374cc144b1f08b688b27884385ca2a7d44e52e7ff96b9589ed3238eec73de6718023127e84ede20c9092c592dfaa735d64cde007416c58db0afafb311857cdb7103ebce8d311c7c2a5f52151b1a2c07fc8319f51bf3b9f2953030520b064c369518ade6696519d2ca7865483e29aa07b8cf8fa838c965b1b9d5ee55e5f25927d2f29a294a8f578045a34d71e8cbede10ca7805e9c21dd5889d05a4306a026691922803a869d6ac214698fd0778c37a383766e2000a67341ae11e0e21506f65fd1b8782472b2923318b0d466240de0d0292a08e6770c859679049e89527dfe468a5878bebee1e1dc1b4bd763f90f76cf3b1847f9a172f42b9cf81959f23bc5fe530ff573778ca1ead972c51d2f7bf1fbd01e2471533ef85f5404d64648fcfb86a118b91faee8d922eb30436a1d02d1ff106f64ef3205990d919e9cfed53f610321f87", diff --git a/protocol/blockfetch/client_test.go b/protocol/blockfetch/client_test.go index cd6e26e2..a2b54e9a 100644 --- a/protocol/blockfetch/client_test.go +++ b/protocol/blockfetch/client_test.go @@ -107,10 +107,10 @@ func TestGetBlock(t *testing.T) { // The slot value is one higher than our intersect point and the block height is less than // our expected tip testBlock := ledger.BabbageBlock{ - Header: &ledger.BabbageBlockHeader{}, + BlockHeader: &ledger.BabbageBlockHeader{}, } - testBlock.Header.Body.BlockNumber = testBlockNumber - testBlock.Header.Body.Slot = testBlockSlot + testBlock.BlockHeader.Body.BlockNumber = testBlockNumber + testBlock.BlockHeader.Body.Slot = testBlockSlot blockCbor, err := cbor.Encode(testBlock) if err != nil { t.Fatalf("received unexpected error: %s", err) diff --git a/protocol/chainsync/client_test.go b/protocol/chainsync/client_test.go index 7cf9da53..d158f561 100644 --- a/protocol/chainsync/client_test.go +++ b/protocol/chainsync/client_test.go @@ -191,10 +191,10 @@ func TestGetAvailableBlockRange(t *testing.T) { // The slot value is one higher than our intersect point and the block height is less than // our expected tip testBlock := ledger.BabbageBlock{ - Header: &ledger.BabbageBlockHeader{}, + BlockHeader: &ledger.BabbageBlockHeader{}, } - testBlock.Header.Body.BlockNumber = 12001 - testBlock.Header.Body.Slot = 20002 + testBlock.BlockHeader.Body.BlockNumber = 12001 + testBlock.BlockHeader.Body.Slot = 20002 blockCbor, err := cbor.Encode(testBlock) if err != nil { t.Fatalf("received unexpected error: %s", err)