Skip to content

Commit 202ca69

Browse files
authored
Merge pull request #306 from blinklabs-io/feat/return-full-transaction
feat: return full transaction instead of just the body
2 parents 96ccf52 + 57008f8 commit 202ca69

File tree

10 files changed

+171
-38
lines changed

10 files changed

+171
-38
lines changed

cbor/value.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ package cbor
1818
// cannot be easily represented in Go (such as maps with bytestring keys)
1919
type Value struct {
2020
Value interface{}
21+
// We store this as a string so that the type is still hashable for use as map keys
22+
cborData string
2123
}
2224

2325
func (v *Value) UnmarshalCBOR(data []byte) error {
26+
// Save the original CBOR
27+
v.cborData = string(data[:])
2428
cborType := data[0] & CBOR_TYPE_MASK
2529
switch cborType {
2630
case CBOR_TYPE_MAP:
@@ -84,3 +88,7 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
8488
}
8589
return nil
8690
}
91+
92+
func (v Value) Cbor() []byte {
93+
return []byte(v.cborData)
94+
}

cmd/block-fetch/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func main() {
8888
fmt.Printf("\nTransactions:\n")
8989
for _, tx := range block.Transactions() {
9090
fmt.Printf(" Hash: %s\n", tx.Hash())
91+
if tx.Metadata().Value != nil {
92+
fmt.Printf(" Metadata:\n %#v (%x)\n", tx.Metadata().Value, tx.Metadata().Cbor())
93+
}
9194
fmt.Printf(" Inputs:\n")
9295
for _, input := range tx.Inputs() {
9396
fmt.Printf(" Id: %s\n", input.Id())

ledger/allegra.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ func (b *AllegraBlock) Era() Era {
5959
return eras[ERA_ID_ALLEGRA]
6060
}
6161

62-
func (b *AllegraBlock) Transactions() []TransactionBody {
63-
ret := []TransactionBody{}
64-
for _, v := range b.TransactionBodies {
65-
// Create temp var since we take the address and the loop var gets reused
66-
tmpVal := v
67-
ret = append(ret, &tmpVal)
62+
func (b *AllegraBlock) Transactions() []Transaction {
63+
ret := []Transaction{}
64+
for idx := range b.TransactionBodies {
65+
tmpTransaction := AllegraTransaction{
66+
Body: b.TransactionBodies[idx],
67+
WitnessSet: b.TransactionWitnessSets[idx],
68+
TxMetadata: b.TransactionMetadataSet[uint(idx)],
69+
}
70+
ret = append(ret, &tmpTransaction)
6871
}
6972
return ret
7073
}
@@ -88,9 +91,26 @@ func (b *AllegraTransactionBody) UnmarshalCBOR(cborData []byte) error {
8891

8992
type AllegraTransaction struct {
9093
cbor.StructAsArray
94+
cbor.DecodeStoreCbor
9195
Body AllegraTransactionBody
9296
WitnessSet ShelleyTransactionWitnessSet
93-
Metadata cbor.Value
97+
TxMetadata cbor.Value
98+
}
99+
100+
func (t AllegraTransaction) Hash() string {
101+
return t.Body.Hash()
102+
}
103+
104+
func (t AllegraTransaction) Inputs() []TransactionInput {
105+
return t.Body.Inputs()
106+
}
107+
108+
func (t AllegraTransaction) Outputs() []TransactionOutput {
109+
return t.Body.Outputs()
110+
}
111+
112+
func (t AllegraTransaction) Metadata() cbor.Value {
113+
return t.TxMetadata
94114
}
95115

96116
func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) {

ledger/alonzo.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,23 @@ func (b *AlonzoBlock) Era() Era {
6161
return eras[ERA_ID_ALONZO]
6262
}
6363

64-
func (b *AlonzoBlock) Transactions() []TransactionBody {
65-
ret := []TransactionBody{}
66-
for _, v := range b.TransactionBodies {
67-
// Create temp var since we take the address and the loop var gets reused
68-
tmpVal := v
69-
ret = append(ret, &tmpVal)
64+
func (b *AlonzoBlock) Transactions() []Transaction {
65+
ret := []Transaction{}
66+
for idx := range b.TransactionBodies {
67+
tmpTransaction := AlonzoTransaction{
68+
Body: b.TransactionBodies[idx],
69+
WitnessSet: b.TransactionWitnessSets[idx],
70+
TxMetadata: b.TransactionMetadataSet[uint(idx)],
71+
}
72+
isValid := true
73+
for _, invalidTxIdx := range b.InvalidTransactions {
74+
if invalidTxIdx == uint(idx) {
75+
isValid = false
76+
break
77+
}
78+
}
79+
tmpTransaction.IsValid = isValid
80+
ret = append(ret, &tmpTransaction)
7081
}
7182
return ret
7283
}
@@ -155,10 +166,27 @@ type AlonzoTransactionWitnessSet struct {
155166

156167
type AlonzoTransaction struct {
157168
cbor.StructAsArray
169+
cbor.DecodeStoreCbor
158170
Body AlonzoTransactionBody
159171
WitnessSet AlonzoTransactionWitnessSet
160172
IsValid bool
161-
Metadata cbor.Value
173+
TxMetadata cbor.Value
174+
}
175+
176+
func (t AlonzoTransaction) Hash() string {
177+
return t.Body.Hash()
178+
}
179+
180+
func (t AlonzoTransaction) Inputs() []TransactionInput {
181+
return t.Body.Inputs()
182+
}
183+
184+
func (t AlonzoTransaction) Outputs() []TransactionOutput {
185+
return t.Body.Outputs()
186+
}
187+
188+
func (t AlonzoTransaction) Metadata() cbor.Value {
189+
return t.TxMetadata
162190
}
163191

164192
func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) {

ledger/babbage.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,23 @@ func (b *BabbageBlock) Era() Era {
6161
return eras[ERA_ID_BABBAGE]
6262
}
6363

64-
func (b *BabbageBlock) Transactions() []TransactionBody {
65-
ret := []TransactionBody{}
66-
for _, v := range b.TransactionBodies {
67-
// Create temp var since we take the address and the loop var gets reused
68-
tmpVal := v
69-
ret = append(ret, &tmpVal)
64+
func (b *BabbageBlock) Transactions() []Transaction {
65+
ret := []Transaction{}
66+
for idx := range b.TransactionBodies {
67+
tmpTransaction := BabbageTransaction{
68+
Body: b.TransactionBodies[idx],
69+
WitnessSet: b.TransactionWitnessSets[idx],
70+
TxMetadata: b.TransactionMetadataSet[uint(idx)],
71+
}
72+
isValid := true
73+
for _, invalidTxIdx := range b.InvalidTransactions {
74+
if invalidTxIdx == uint(idx) {
75+
isValid = false
76+
break
77+
}
78+
}
79+
tmpTransaction.IsValid = isValid
80+
ret = append(ret, &tmpTransaction)
7081
}
7182
return ret
7283
}
@@ -198,10 +209,27 @@ type BabbageTransactionWitnessSet struct {
198209

199210
type BabbageTransaction struct {
200211
cbor.StructAsArray
212+
cbor.DecodeStoreCbor
201213
Body BabbageTransactionBody
202214
WitnessSet BabbageTransactionWitnessSet
203215
IsValid bool
204-
Metadata cbor.Value
216+
TxMetadata cbor.Value
217+
}
218+
219+
func (t BabbageTransaction) Hash() string {
220+
return t.Body.Hash()
221+
}
222+
223+
func (t BabbageTransaction) Inputs() []TransactionInput {
224+
return t.Body.Inputs()
225+
}
226+
227+
func (t BabbageTransaction) Outputs() []TransactionOutput {
228+
return t.Body.Outputs()
229+
}
230+
231+
func (t BabbageTransaction) Metadata() cbor.Value {
232+
return t.TxMetadata
205233
}
206234

207235
func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {

ledger/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
type Block interface {
2525
BlockHeader
26-
Transactions() []TransactionBody
26+
Transactions() []Transaction
2727
}
2828

2929
type BlockHeader interface {

ledger/byron.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (b *ByronMainBlock) Era() Era {
187187
return b.Header.Era()
188188
}
189189

190-
func (b *ByronMainBlock) Transactions() []TransactionBody {
190+
func (b *ByronMainBlock) Transactions() []Transaction {
191191
// TODO
192192
return nil
193193
}
@@ -220,7 +220,7 @@ func (b *ByronEpochBoundaryBlock) Era() Era {
220220
return b.Header.Era()
221221
}
222222

223-
func (b *ByronEpochBoundaryBlock) Transactions() []TransactionBody {
223+
func (b *ByronEpochBoundaryBlock) Transactions() []Transaction {
224224
// Boundary blocks don't have transactions
225225
return nil
226226
}

ledger/mary.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ func (b *MaryBlock) Era() Era {
6060
return eras[ERA_ID_MARY]
6161
}
6262

63-
func (b *MaryBlock) Transactions() []TransactionBody {
64-
ret := []TransactionBody{}
65-
for _, v := range b.TransactionBodies {
66-
// Create temp var since we take the address and the loop var gets reused
67-
tmpVal := v
68-
ret = append(ret, &tmpVal)
63+
func (b *MaryBlock) Transactions() []Transaction {
64+
ret := []Transaction{}
65+
for idx := range b.TransactionBodies {
66+
tmpTransaction := MaryTransaction{
67+
Body: b.TransactionBodies[idx],
68+
WitnessSet: b.TransactionWitnessSets[idx],
69+
TxMetadata: b.TransactionMetadataSet[uint(idx)],
70+
}
71+
ret = append(ret, &tmpTransaction)
6972
}
7073
return ret
7174
}
@@ -98,9 +101,26 @@ func (b *MaryTransactionBody) Outputs() []TransactionOutput {
98101

99102
type MaryTransaction struct {
100103
cbor.StructAsArray
104+
cbor.DecodeStoreCbor
101105
Body MaryTransactionBody
102106
WitnessSet ShelleyTransactionWitnessSet
103-
Metadata cbor.Value
107+
TxMetadata cbor.Value
108+
}
109+
110+
func (t MaryTransaction) Hash() string {
111+
return t.Body.Hash()
112+
}
113+
114+
func (t MaryTransaction) Inputs() []TransactionInput {
115+
return t.Body.Inputs()
116+
}
117+
118+
func (t MaryTransaction) Outputs() []TransactionOutput {
119+
return t.Body.Outputs()
120+
}
121+
122+
func (t MaryTransaction) Metadata() cbor.Value {
123+
return t.TxMetadata
104124
}
105125

106126
type MaryTransactionOutput struct {

ledger/shelley.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ func (b *ShelleyBlock) Era() Era {
5959
return eras[ERA_ID_SHELLEY]
6060
}
6161

62-
func (b *ShelleyBlock) Transactions() []TransactionBody {
63-
ret := []TransactionBody{}
64-
for _, v := range b.TransactionBodies {
65-
// Create temp var since we take the address and the loop var gets reused
66-
tmpVal := v
67-
ret = append(ret, &tmpVal)
62+
func (b *ShelleyBlock) Transactions() []Transaction {
63+
ret := []Transaction{}
64+
for idx := range b.TransactionBodies {
65+
tmpTransaction := ShelleyTransaction{
66+
Body: b.TransactionBodies[idx],
67+
WitnessSet: b.TransactionWitnessSets[idx],
68+
TxMetadata: b.TransactionMetadataSet[uint(idx)],
69+
}
70+
ret = append(ret, &tmpTransaction)
6871
}
6972
return ret
7073
}
@@ -213,9 +216,26 @@ type ShelleyTransactionWitnessSet struct {
213216

214217
type ShelleyTransaction struct {
215218
cbor.StructAsArray
219+
cbor.DecodeStoreCbor
216220
Body ShelleyTransactionBody
217221
WitnessSet ShelleyTransactionWitnessSet
218-
Metadata cbor.Value
222+
TxMetadata cbor.Value
223+
}
224+
225+
func (t ShelleyTransaction) Hash() string {
226+
return t.Body.Hash()
227+
}
228+
229+
func (t ShelleyTransaction) Inputs() []TransactionInput {
230+
return t.Body.Inputs()
231+
}
232+
233+
func (t ShelleyTransaction) Outputs() []TransactionOutput {
234+
return t.Body.Outputs()
235+
}
236+
237+
func (t ShelleyTransaction) Metadata() cbor.Value {
238+
return t.TxMetadata
219239
}
220240

221241
func NewShelleyBlockFromCbor(data []byte) (*ShelleyBlock, error) {

ledger/tx.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020

21+
"github.com/blinklabs-io/gouroboros/cbor"
2122
"golang.org/x/crypto/blake2b"
2223
)
2324

25+
type Transaction interface {
26+
TransactionBody
27+
Metadata() cbor.Value
28+
}
29+
2430
type TransactionBody interface {
2531
Hash() string
2632
Cbor() []byte

0 commit comments

Comments
 (0)