Skip to content

Commit 2a8568e

Browse files
authored
refactor: return Blake2b256 for block and TX hash (#974)
This switches from returning the string representation of a block/TX hash to the underlying Blake2b256 type containing the hash bytes. Those bytes are used all over the place, and it's cumbersome to need to use hex.DecodeString() everywhere to convert back to bytes Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 7b126b9 commit 2a8568e

File tree

16 files changed

+88
-120
lines changed

16 files changed

+88
-120
lines changed

cmd/gouroboros/chainsync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func chainSyncRollForwardHandler(
264264
block = v
265265
case ledger.BlockHeader:
266266
blockSlot := v.SlotNumber()
267-
blockHash, _ := hex.DecodeString(v.Hash())
267+
blockHash := v.Hash().Bytes()
268268
var err error
269269
if oConn == nil {
270270
return errors.New("empty ouroboros connection, aborting!")

ledger/allegra/allegra.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package allegra
1616

1717
import (
18-
"encoding/hex"
1918
"fmt"
2019

2120
"github.com/blinklabs-io/gouroboros/cbor"
@@ -61,15 +60,15 @@ func (AllegraBlock) Type() int {
6160
return BlockTypeAllegra
6261
}
6362

64-
func (b *AllegraBlock) Hash() string {
63+
func (b *AllegraBlock) Hash() common.Blake2b256 {
6564
return b.BlockHeader.Hash()
6665
}
6766

6867
func (b *AllegraBlock) Header() common.BlockHeader {
6968
return b.BlockHeader
7069
}
7170

72-
func (b *AllegraBlock) PrevHash() string {
71+
func (b *AllegraBlock) PrevHash() common.Blake2b256 {
7372
return b.BlockHeader.PrevHash()
7473
}
7574

@@ -108,7 +107,6 @@ func (b *AllegraBlock) Transactions() []common.Transaction {
108107

109108
func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
110109
txs := []*utxorpc.Tx{}
111-
tmpHash, _ := hex.DecodeString(b.Hash())
112110
for _, t := range b.Transactions() {
113111
tx := t.Utxorpc()
114112
txs = append(txs, tx)
@@ -117,7 +115,7 @@ func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
117115
Tx: txs,
118116
}
119117
header := &utxorpc.BlockHeader{
120-
Hash: tmpHash,
118+
Hash: b.Hash().Bytes(),
121119
Height: b.BlockNumber(),
122120
Slot: b.SlotNumber(),
123121
}
@@ -178,7 +176,7 @@ func (AllegraTransaction) Type() int {
178176
return TxTypeAllegra
179177
}
180178

181-
func (t AllegraTransaction) Hash() string {
179+
func (t AllegraTransaction) Hash() common.Blake2b256 {
182180
return t.Body.Hash()
183181
}
184182

@@ -280,7 +278,7 @@ func (t AllegraTransaction) Produced() []common.Utxo {
280278
ret = append(
281279
ret,
282280
common.Utxo{
283-
Id: shelley.NewShelleyTransactionInput(t.Hash(), idx),
281+
Id: shelley.NewShelleyTransactionInput(t.Hash().String(), idx),
284282
Output: output,
285283
},
286284
)

ledger/alonzo/alonzo.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package alonzo
1616

1717
import (
18-
"encoding/hex"
1918
"encoding/json"
2019
"fmt"
2120

@@ -64,15 +63,15 @@ func (AlonzoBlock) Type() int {
6463
return BlockTypeAlonzo
6564
}
6665

67-
func (b *AlonzoBlock) Hash() string {
66+
func (b *AlonzoBlock) Hash() common.Blake2b256 {
6867
return b.BlockHeader.Hash()
6968
}
7069

7170
func (b *AlonzoBlock) Header() common.BlockHeader {
7271
return b.BlockHeader
7372
}
7473

75-
func (b *AlonzoBlock) PrevHash() string {
74+
func (b *AlonzoBlock) PrevHash() common.Blake2b256 {
7675
return b.BlockHeader.PrevHash()
7776
}
7877

@@ -117,7 +116,6 @@ func (b *AlonzoBlock) Transactions() []common.Transaction {
117116

118117
func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
119118
txs := []*utxorpc.Tx{}
120-
tmpHash, _ := hex.DecodeString(b.Hash())
121119
for _, t := range b.Transactions() {
122120
tx := t.Utxorpc()
123121
txs = append(txs, tx)
@@ -126,7 +124,7 @@ func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
126124
Tx: txs,
127125
}
128126
header := &utxorpc.BlockHeader{
129-
Hash: tmpHash,
127+
Hash: b.Hash().Bytes(),
130128
Height: b.BlockNumber(),
131129
Slot: b.SlotNumber(),
132130
}
@@ -370,7 +368,7 @@ func (AlonzoTransaction) Type() int {
370368
return TxTypeAlonzo
371369
}
372370

373-
func (t AlonzoTransaction) Hash() string {
371+
func (t AlonzoTransaction) Hash() common.Blake2b256 {
374372
return t.Body.Hash()
375373
}
376374

@@ -477,7 +475,7 @@ func (t AlonzoTransaction) Produced() []common.Utxo {
477475
ret = append(
478476
ret,
479477
common.Utxo{
480-
Id: shelley.NewShelleyTransactionInput(t.Hash(), idx),
478+
Id: shelley.NewShelleyTransactionInput(t.Hash().String(), idx),
481479
Output: output,
482480
},
483481
)

ledger/babbage/babbage.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package babbage
1616

1717
import (
18-
"encoding/hex"
1918
"encoding/json"
2019
"errors"
2120
"fmt"
@@ -66,15 +65,15 @@ func (BabbageBlock) Type() int {
6665
return BlockTypeBabbage
6766
}
6867

69-
func (b *BabbageBlock) Hash() string {
68+
func (b *BabbageBlock) Hash() common.Blake2b256 {
7069
return b.BlockHeader.Hash()
7170
}
7271

7372
func (b *BabbageBlock) Header() common.BlockHeader {
7473
return b.BlockHeader
7574
}
7675

77-
func (b *BabbageBlock) PrevHash() string {
76+
func (b *BabbageBlock) PrevHash() common.Blake2b256 {
7877
return b.BlockHeader.PrevHash()
7978
}
8079

@@ -119,7 +118,6 @@ func (b *BabbageBlock) Transactions() []common.Transaction {
119118

120119
func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
121120
txs := []*utxorpc.Tx{}
122-
tmpHash, _ := hex.DecodeString(b.Hash())
123121
for _, t := range b.Transactions() {
124122
tx := t.Utxorpc()
125123
txs = append(txs, tx)
@@ -128,7 +126,7 @@ func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
128126
Tx: txs,
129127
}
130128
header := &utxorpc.BlockHeader{
131-
Hash: tmpHash,
129+
Hash: b.Hash().Bytes(),
132130
Height: b.BlockNumber(),
133131
Slot: b.SlotNumber(),
134132
}
@@ -142,7 +140,7 @@ func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
142140
type BabbageBlockHeader struct {
143141
cbor.StructAsArray
144142
cbor.DecodeStoreCbor
145-
hash string
143+
hash *common.Blake2b256
146144
Body BabbageBlockHeaderBody
147145
Signature []byte
148146
}
@@ -179,16 +177,16 @@ func (h *BabbageBlockHeader) UnmarshalCBOR(cborData []byte) error {
179177
return h.UnmarshalCbor(cborData, h)
180178
}
181179

182-
func (h *BabbageBlockHeader) Hash() string {
183-
if h.hash == "" {
180+
func (h *BabbageBlockHeader) Hash() common.Blake2b256 {
181+
if h.hash == nil {
184182
tmpHash := common.Blake2b256Hash(h.Cbor())
185-
h.hash = hex.EncodeToString(tmpHash.Bytes())
183+
h.hash = &tmpHash
186184
}
187-
return h.hash
185+
return *h.hash
188186
}
189187

190-
func (h *BabbageBlockHeader) PrevHash() string {
191-
return h.Body.PrevHash.String()
188+
func (h *BabbageBlockHeader) PrevHash() common.Blake2b256 {
189+
return h.Body.PrevHash
192190
}
193191

194192
func (h *BabbageBlockHeader) BlockNumber() uint64 {
@@ -282,10 +280,6 @@ func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx {
282280
input := ri.Utxorpc()
283281
txri = append(txri, input)
284282
}
285-
tmpHash, err := hex.DecodeString(b.Hash())
286-
if err != nil {
287-
return &utxorpc.Tx{}
288-
}
289283
tx := &utxorpc.Tx{
290284
Inputs: txi,
291285
Outputs: txo,
@@ -299,7 +293,7 @@ func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx {
299293
// Successful: b.Successful(),
300294
// Auxiliary: b.AuxData(),
301295
// Validity: b.Validity(),
302-
Hash: tmpHash,
296+
Hash: b.Hash().Bytes(),
303297
}
304298
return tx
305299
}
@@ -523,7 +517,7 @@ func (BabbageTransaction) Type() int {
523517
return TxTypeBabbage
524518
}
525519

526-
func (t BabbageTransaction) Hash() string {
520+
func (t BabbageTransaction) Hash() common.Blake2b256 {
527521
return t.Body.Hash()
528522
}
529523

@@ -630,7 +624,7 @@ func (t BabbageTransaction) Produced() []common.Utxo {
630624
ret = append(
631625
ret,
632626
common.Utxo{
633-
Id: shelley.NewShelleyTransactionInput(t.Hash(), idx),
627+
Id: shelley.NewShelleyTransactionInput(t.Hash().String(), idx),
634628
Output: output,
635629
},
636630
)
@@ -642,7 +636,7 @@ func (t BabbageTransaction) Produced() []common.Utxo {
642636
}
643637
return []common.Utxo{
644638
{
645-
Id: shelley.NewShelleyTransactionInput(t.Hash(), len(t.Outputs())),
639+
Id: shelley.NewShelleyTransactionInput(t.Hash().String(), len(t.Outputs())),
646640
Output: t.CollateralReturn(),
647641
},
648642
}

ledger/babbage/babbage_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package babbage
1616

1717
import (
18-
"encoding/hex"
1918
"testing"
2019

2120
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
@@ -2747,8 +2746,7 @@ func TestBabbageBlock_Utxorpc(t *testing.T) {
27472746
assert.Equal(t, len(babbageBlock.TransactionBodies), len(utxoBlock.Body.Tx))
27482747

27492748
// Validate the header details
2750-
tmpHash, _ := hex.DecodeString(babbageBlock.Hash())
2751-
assert.Equal(t, tmpHash, utxoBlock.Header.Hash)
2749+
assert.Equal(t, babbageBlock.Hash().Bytes(), utxoBlock.Header.Hash)
27522750
assert.Equal(t, babbageBlock.BlockNumber(), utxoBlock.Header.Height)
27532751
assert.Equal(t, babbageBlock.SlotNumber(), utxoBlock.Header.Slot)
27542752

0 commit comments

Comments
 (0)