Skip to content

Commit e00099b

Browse files
committed
feat(ledger): Added unit test cases for each block header type in NewBlockHeaderFromCbor
Signed-off-by: Akhil Repala <[email protected]>
1 parent 3095a56 commit e00099b

File tree

4 files changed

+188
-44
lines changed

4 files changed

+188
-44
lines changed

ledger/alonzo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const (
4040
// Alonzo functions
4141
var (
4242
NewAlonzoBlockFromCbor = alonzo.NewAlonzoBlockFromCbor
43-
NewAlonzoBlockHeaderFromCbor = alonzo.NewAlonzoBlockFromCbor
43+
NewAlonzoBlockHeaderFromCbor = alonzo.NewAlonzoBlockHeaderFromCbor
4444
NewAlonzoTransactionFromCbor = alonzo.NewAlonzoTransactionFromCbor
4545
NewAlonzoTransactionBodyFromCbor = alonzo.NewAlonzoTransactionBodyFromCbor
4646
NewAlonzoTransactionOutputFromCbor = alonzo.NewAlonzoTransactionOutputFromCbor

ledger/babbage/babbage.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,33 +145,39 @@ func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
145145
type BabbageBlockHeader struct {
146146
cbor.StructAsArray
147147
cbor.DecodeStoreCbor
148-
hash string
149-
Body struct {
150-
cbor.StructAsArray
151-
BlockNumber uint64
152-
Slot uint64
153-
PrevHash common.Blake2b256
154-
IssuerVkey common.IssuerVkey
155-
VrfKey []byte
156-
VrfResult common.VrfResult
157-
BlockBodySize uint64
158-
BlockBodyHash common.Blake2b256
159-
OpCert struct {
160-
cbor.StructAsArray
161-
HotVkey []byte
162-
SequenceNumber uint32
163-
KesPeriod uint32
164-
Signature []byte
165-
}
166-
ProtoVersion struct {
167-
cbor.StructAsArray
168-
Major uint64
169-
Minor uint64
170-
}
171-
}
148+
hash string
149+
Body BabbageBlockHeaderBody
172150
Signature []byte
173151
}
174152

153+
type BabbageBlockHeaderBody struct {
154+
cbor.StructAsArray
155+
BlockNumber uint64
156+
Slot uint64
157+
PrevHash common.Blake2b256
158+
IssuerVkey common.IssuerVkey
159+
VrfKey []byte
160+
VrfResult common.VrfResult
161+
BlockBodySize uint64
162+
BlockBodyHash common.Blake2b256
163+
OpCert BabbageOpCert
164+
ProtoVersion BabbageProtoVersion
165+
}
166+
167+
type BabbageOpCert struct {
168+
cbor.StructAsArray
169+
HotVkey []byte
170+
SequenceNumber uint32
171+
KesPeriod uint32
172+
Signature []byte
173+
}
174+
175+
type BabbageProtoVersion struct {
176+
cbor.StructAsArray
177+
Major uint64
178+
Minor uint64
179+
}
180+
175181
func (h *BabbageBlockHeader) UnmarshalCBOR(cborData []byte) error {
176182
return h.UnmarshalCbor(cborData, h)
177183
}

ledger/shelley/shelley.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,28 @@ func (b *ShelleyBlock) Utxorpc() *utxorpc.Block {
135135
type ShelleyBlockHeader struct {
136136
cbor.StructAsArray
137137
cbor.DecodeStoreCbor
138-
hash string
139-
Body struct {
140-
cbor.StructAsArray
141-
BlockNumber uint64
142-
Slot uint64
143-
PrevHash common.Blake2b256
144-
IssuerVkey common.IssuerVkey
145-
VrfKey []byte
146-
NonceVrf common.VrfResult
147-
LeaderVrf common.VrfResult
148-
BlockBodySize uint64
149-
BlockBodyHash common.Blake2b256
150-
OpCertHotVkey []byte
151-
OpCertSequenceNumber uint32
152-
OpCertKesPeriod uint32
153-
OpCertSignature []byte
154-
ProtoMajorVersion uint64
155-
ProtoMinorVersion uint64
156-
}
138+
hash string
139+
Body ShelleyBlockHeaderBody
157140
Signature []byte
158141
}
142+
type ShelleyBlockHeaderBody struct {
143+
cbor.StructAsArray
144+
BlockNumber uint64
145+
Slot uint64
146+
PrevHash common.Blake2b256
147+
IssuerVkey common.IssuerVkey
148+
VrfKey []byte
149+
NonceVrf common.VrfResult
150+
LeaderVrf common.VrfResult
151+
BlockBodySize uint64
152+
BlockBodyHash common.Blake2b256
153+
OpCertHotVkey []byte
154+
OpCertSequenceNumber uint32
155+
OpCertKesPeriod uint32
156+
OpCertSignature []byte
157+
ProtoMajorVersion uint64
158+
ProtoMinorVersion uint64
159+
}
159160

160161
func (h *ShelleyBlockHeader) UnmarshalCBOR(cborData []byte) error {
161162
return h.UnmarshalCbor(cborData, h)

ledger/verify_block_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package ledger
22

33
import (
4+
"fmt"
45
"testing"
6+
7+
"github.com/blinklabs-io/gouroboros/ledger/allegra"
8+
"github.com/blinklabs-io/gouroboros/ledger/babbage"
9+
"github.com/blinklabs-io/gouroboros/ledger/common"
10+
"github.com/blinklabs-io/gouroboros/ledger/mary"
11+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
12+
"github.com/fxamacker/cbor/v2"
513
)
614

715
func TestVerifyBlockBody(t *testing.T) {
@@ -57,3 +65,132 @@ func TestVerifyBlockBody(t *testing.T) {
5765
})
5866
}
5967
}
68+
69+
func mockShelleyCBOR() []byte {
70+
shelleyHeader := shelley.ShelleyBlockHeader{
71+
Body: shelley.ShelleyBlockHeaderBody{
72+
BlockNumber: 12345,
73+
Slot: 67890,
74+
PrevHash: common.Blake2b256{},
75+
IssuerVkey: common.IssuerVkey{},
76+
VrfKey: []byte{0x01, 0x02},
77+
NonceVrf: common.VrfResult{},
78+
LeaderVrf: common.VrfResult{},
79+
BlockBodySize: 512,
80+
BlockBodyHash: common.Blake2b256{},
81+
OpCertHotVkey: []byte{0x03, 0x04},
82+
OpCertSequenceNumber: 10,
83+
OpCertKesPeriod: 20,
84+
OpCertSignature: []byte{0x05, 0x06},
85+
ProtoMajorVersion: 1,
86+
ProtoMinorVersion: 0,
87+
},
88+
Signature: []byte{0x07, 0x08},
89+
}
90+
91+
// Convert to CBOR
92+
data, err := cbor.Marshal(shelleyHeader)
93+
if err != nil {
94+
fmt.Printf("CBOR Encoding Error: %v\n", err)
95+
}
96+
return data
97+
}
98+
99+
func mockAllegraCBOR() []byte {
100+
allegraHeader := allegra.AllegraBlockHeader{ShelleyBlockHeader: ShelleyBlockHeader{}}
101+
data, _ := cbor.Marshal(allegraHeader)
102+
return data
103+
}
104+
105+
func mockMaryCBOR() []byte {
106+
maryHeader := mary.MaryBlockHeader{ShelleyBlockHeader: ShelleyBlockHeader{}}
107+
data, _ := cbor.Marshal(maryHeader)
108+
return data
109+
}
110+
111+
func mockAlonzoCBOR() []byte {
112+
alonzoHeader := AlonzoBlockHeader{ShelleyBlockHeader: ShelleyBlockHeader{}}
113+
data, _ := cbor.Marshal(alonzoHeader)
114+
return data
115+
}
116+
117+
func mockBabbageCBOR() []byte {
118+
babbageHeader := babbage.BabbageBlockHeader{
119+
Body: babbage.BabbageBlockHeaderBody{
120+
BlockNumber: 54321,
121+
Slot: 98765,
122+
PrevHash: common.Blake2b256{},
123+
IssuerVkey: common.IssuerVkey{},
124+
VrfKey: []byte{0x09, 0x10},
125+
VrfResult: common.VrfResult{},
126+
BlockBodySize: 1024,
127+
BlockBodyHash: common.Blake2b256{},
128+
OpCert: babbage.BabbageOpCert{
129+
HotVkey: []byte{0x11, 0x12},
130+
SequenceNumber: 30,
131+
KesPeriod: 40,
132+
Signature: []byte{0x13, 0x14},
133+
},
134+
ProtoVersion: babbage.BabbageProtoVersion{
135+
Major: 2,
136+
Minor: 0,
137+
},
138+
},
139+
Signature: []byte{0x15, 0x16},
140+
}
141+
142+
// Convert to CBOR
143+
data, err := cbor.Marshal(babbageHeader)
144+
if err != nil {
145+
fmt.Printf("CBOR Encoding Error for Babbage: %v\n", err)
146+
}
147+
return data
148+
}
149+
150+
func mockConwayCBOR() []byte {
151+
conwayHeader := ConwayBlockHeader{BabbageBlockHeader: BabbageBlockHeader{}}
152+
data, _ := cbor.Marshal(conwayHeader)
153+
return data
154+
}
155+
156+
func TestNewBlockHeaderFromCbor(t *testing.T) {
157+
tests := []struct {
158+
name string
159+
blockType uint
160+
data []byte
161+
expectErr bool
162+
expectedFn string
163+
}{
164+
{"Shelley Block", BlockTypeShelley, mockShelleyCBOR(), false, "NewShelleyBlockHeaderFromCbor"},
165+
{"Allegra Block", BlockTypeAllegra, mockAllegraCBOR(), false, "NewAllegraBlockHeaderFromCbor"},
166+
{"Mary Block", BlockTypeMary, mockMaryCBOR(), false, "NewMaryBlockHeaderFromCbor"},
167+
{"Alonzo Block", BlockTypeAlonzo, mockAlonzoCBOR(), false, "NewAlonzoBlockHeaderFromCbor"},
168+
{"Babbage Block", BlockTypeBabbage, mockBabbageCBOR(), false, "NewBabbageBlockHeaderFromCbor"},
169+
{"Conway Block", BlockTypeConway, mockConwayCBOR(), false, "NewConwayBlockHeaderFromCbor"},
170+
{"Invalid Block Type", 9999, []byte{0xFF, 0x00, 0x00}, true, "UnknownFunction"},
171+
}
172+
173+
for _, test := range tests {
174+
t.Run(test.name, func(t *testing.T) {
175+
fmt.Printf("\n Running Test: %s\n", test.name)
176+
177+
header, err := NewBlockHeaderFromCbor(test.blockType, test.data)
178+
179+
if test.expectErr {
180+
if err == nil {
181+
t.Errorf("Expected error for %s, but got none!", test.name)
182+
} else {
183+
fmt.Printf("Expected failure for %s: %v\n", test.name, err)
184+
}
185+
} else {
186+
if err != nil {
187+
t.Errorf("Unexpected error for %s: %v", test.name, err)
188+
} else if header == nil {
189+
t.Errorf("Expected non-nil block header for %s, but got nil", test.name)
190+
} else {
191+
fmt.Printf("Test Passed: %s → %s executed successfully!\n", test.name, test.expectedFn)
192+
}
193+
}
194+
})
195+
}
196+
}

0 commit comments

Comments
 (0)