Skip to content

Commit a782810

Browse files
authored
feat(ledger): add leios specific hash function (#1205)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 8f4c804 commit a782810

File tree

10 files changed

+86
-1
lines changed

10 files changed

+86
-1
lines changed

ledger/allegra/allegra.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ func (b *AllegraTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
236236
type AllegraTransaction struct {
237237
cbor.StructAsArray
238238
cbor.DecodeStoreCbor
239+
hash *common.Blake2b256
239240
Body AllegraTransactionBody
240241
WitnessSet shelley.ShelleyTransactionWitnessSet
241242
TxMetadata *cbor.LazyValue
@@ -264,6 +265,14 @@ func (t AllegraTransaction) Id() common.Blake2b256 {
264265
return t.Body.Id()
265266
}
266267

268+
func (t AllegraTransaction) LeiosHash() common.Blake2b256 {
269+
if t.hash == nil {
270+
tmpHash := common.Blake2b256Hash(t.Cbor())
271+
t.hash = &tmpHash
272+
}
273+
return *t.hash
274+
}
275+
267276
func (t AllegraTransaction) Inputs() []common.TransactionInput {
268277
return t.Body.Inputs()
269278
}

ledger/alonzo/alonzo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ func (w AlonzoTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeem
587587
type AlonzoTransaction struct {
588588
cbor.StructAsArray
589589
cbor.DecodeStoreCbor
590+
hash *common.Blake2b256
590591
Body AlonzoTransactionBody
591592
WitnessSet AlonzoTransactionWitnessSet
592593
TxIsValid bool
@@ -616,6 +617,14 @@ func (t AlonzoTransaction) Id() common.Blake2b256 {
616617
return t.Body.Id()
617618
}
618619

620+
func (t AlonzoTransaction) LeiosHash() common.Blake2b256 {
621+
if t.hash == nil {
622+
tmpHash := common.Blake2b256Hash(t.Cbor())
623+
t.hash = &tmpHash
624+
}
625+
return *t.hash
626+
}
627+
619628
func (t AlonzoTransaction) Inputs() []common.TransactionInput {
620629
return t.Body.Inputs()
621630
}

ledger/babbage/babbage.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ func (w BabbageTransactionWitnessSet) Redeemers() common.TransactionWitnessRedee
729729
type BabbageTransaction struct {
730730
cbor.StructAsArray
731731
cbor.DecodeStoreCbor
732+
hash *common.Blake2b256
732733
Body BabbageTransactionBody
733734
WitnessSet BabbageTransactionWitnessSet
734735
TxIsValid bool
@@ -758,6 +759,14 @@ func (t BabbageTransaction) Id() common.Blake2b256 {
758759
return t.Body.Id()
759760
}
760761

762+
func (t BabbageTransaction) LeiosHash() common.Blake2b256 {
763+
if t.hash == nil {
764+
tmpHash := common.Blake2b256Hash(t.Cbor())
765+
t.hash = &tmpHash
766+
}
767+
return *t.hash
768+
}
769+
761770
func (t BabbageTransaction) Inputs() []common.TransactionInput {
762771
return t.Body.Inputs()
763772
}

ledger/byron/byron.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ func (t *ByronTransaction) Metadata() *cbor.LazyValue {
279279
return t.Attributes
280280
}
281281

282+
func (t *ByronTransaction) LeiosHash() common.Blake2b256 {
283+
if t.hash == nil {
284+
tmpHash := common.Blake2b256Hash(t.Cbor())
285+
t.hash = &tmpHash
286+
}
287+
return *t.hash
288+
}
289+
282290
func (t *ByronTransaction) IsValid() bool {
283291
return true
284292
}

ledger/common/tx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Transaction interface {
2727
Type() int
2828
Cbor() []byte
2929
Hash() Blake2b256
30+
LeiosHash() Blake2b256
3031
Metadata() *cbor.LazyValue
3132
IsValid() bool
3233
Consumed() []TransactionInput

ledger/conway/conway.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ func (b *ConwayTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
514514
type ConwayTransaction struct {
515515
cbor.StructAsArray
516516
cbor.DecodeStoreCbor
517+
hash *common.Blake2b256
517518
Body ConwayTransactionBody
518519
WitnessSet ConwayTransactionWitnessSet
519520
TxIsValid bool
@@ -543,6 +544,14 @@ func (t ConwayTransaction) Id() common.Blake2b256 {
543544
return t.Body.Id()
544545
}
545546

547+
func (t ConwayTransaction) LeiosHash() common.Blake2b256 {
548+
if t.hash == nil {
549+
tmpHash := common.Blake2b256Hash(t.Cbor())
550+
t.hash = &tmpHash
551+
}
552+
return *t.hash
553+
}
554+
546555
func (t ConwayTransaction) Inputs() []common.TransactionInput {
547556
return t.Body.Inputs()
548557
}

ledger/leios/tx.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,26 @@
1515
package leios
1616

1717
import (
18+
"github.com/blinklabs-io/gouroboros/ledger/common"
1819
"github.com/blinklabs-io/gouroboros/ledger/conway"
1920
)
2021

2122
type (
22-
LeiosTransaction = conway.ConwayTransaction
2323
LeiosTransactionBody = conway.ConwayTransactionBody
2424
LeiosTransactionWitnessSet = conway.ConwayTransactionWitnessSet
2525
)
26+
27+
type LeiosTransaction struct {
28+
conway.ConwayTransaction
29+
hash *common.Blake2b256
30+
Body LeiosTransactionBody
31+
WitnessSet LeiosTransactionWitnessSet
32+
}
33+
34+
func (t *LeiosTransaction) LeiosHash() common.Blake2b256 {
35+
if t.hash == nil {
36+
tmpHash := common.Blake2b256Hash(t.Cbor())
37+
t.hash = &tmpHash
38+
}
39+
return *t.hash
40+
}

ledger/mary/mary.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ func (b *MaryTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
244244
type MaryTransaction struct {
245245
cbor.StructAsArray
246246
cbor.DecodeStoreCbor
247+
hash *common.Blake2b256
247248
Body MaryTransactionBody
248249
WitnessSet shelley.ShelleyTransactionWitnessSet
249250
TxMetadata *cbor.LazyValue
@@ -272,6 +273,14 @@ func (t MaryTransaction) Id() common.Blake2b256 {
272273
return t.Body.Id()
273274
}
274275

276+
func (t MaryTransaction) LeiosHash() common.Blake2b256 {
277+
if t.hash == nil {
278+
tmpHash := common.Blake2b256Hash(t.Cbor())
279+
t.hash = &tmpHash
280+
}
281+
return *t.hash
282+
}
283+
275284
func (t MaryTransaction) Inputs() []common.TransactionInput {
276285
return t.Body.Inputs()
277286
}

ledger/shelley/shelley.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func (w ShelleyTransactionWitnessSet) Redeemers() common.TransactionWitnessRedee
537537
type ShelleyTransaction struct {
538538
cbor.StructAsArray
539539
cbor.DecodeStoreCbor
540+
hash *common.Blake2b256
540541
Body ShelleyTransactionBody
541542
WitnessSet ShelleyTransactionWitnessSet
542543
TxMetadata *cbor.LazyValue
@@ -565,6 +566,14 @@ func (t ShelleyTransaction) Id() common.Blake2b256 {
565566
return t.Body.Id()
566567
}
567568

569+
func (t ShelleyTransaction) LeiosHash() common.Blake2b256 {
570+
if t.hash == nil {
571+
tmpHash := common.Blake2b256Hash(t.Cbor())
572+
t.hash = &tmpHash
573+
}
574+
return *t.hash
575+
}
576+
568577
func (t ShelleyTransaction) Inputs() []common.TransactionInput {
569578
return t.Body.Inputs()
570579
}

ledger/tx.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {
4646
return NewBabbageTransactionFromCbor(data)
4747
case TxTypeConway:
4848
return NewConwayTransactionFromCbor(data)
49+
case TxTypeLeios:
50+
return NewLeiosTransactionFromCbor(data)
4951
}
5052
return nil, fmt.Errorf("unknown transaction type: %d", txType)
5153
}
@@ -69,6 +71,8 @@ func NewTransactionBodyFromCbor(
6971
return NewBabbageTransactionBodyFromCbor(data)
7072
case TxTypeConway:
7173
return NewConwayTransactionBodyFromCbor(data)
74+
case TxTypeLeios:
75+
return NewLeiosTransactionBodyFromCbor(data)
7276
}
7377
return nil, fmt.Errorf("unknown transaction type: %d", txType)
7478
}
@@ -116,5 +120,8 @@ func DetermineTransactionType(data []byte) (uint, error) {
116120
if _, err := NewConwayTransactionFromCbor(data); err == nil {
117121
return TxTypeConway, nil
118122
}
123+
if _, err := NewLeiosTransactionFromCbor(data); err == nil {
124+
return TxTypeLeios, nil
125+
}
119126
return 0, errors.New("unknown transaction type")
120127
}

0 commit comments

Comments
 (0)