Skip to content

Commit d0eef33

Browse files
authored
feat: include UTxO ID with produced TX outputs (#669)
Fixes #612
1 parent 4b9ba36 commit d0eef33

File tree

8 files changed

+128
-20
lines changed

8 files changed

+128
-20
lines changed

ledger/allegra.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,18 @@ func (t AllegraTransaction) Consumed() []TransactionInput {
242242
return t.Inputs()
243243
}
244244

245-
func (t AllegraTransaction) Produced() []TransactionOutput {
246-
return t.Outputs()
245+
func (t AllegraTransaction) Produced() []Utxo {
246+
var ret []Utxo
247+
for idx, output := range t.Outputs() {
248+
ret = append(
249+
ret,
250+
Utxo{
251+
Id: NewShelleyTransactionInput(t.Hash(), idx),
252+
Output: output,
253+
},
254+
)
255+
}
256+
return ret
247257
}
248258

249259
func (t AllegraTransaction) ProtocolParametersUpdate() map[Blake2b224]any {

ledger/alonzo.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,22 @@ func (t AlonzoTransaction) Consumed() []TransactionInput {
373373
}
374374
}
375375

376-
func (t AlonzoTransaction) Produced() []TransactionOutput {
376+
func (t AlonzoTransaction) Produced() []Utxo {
377377
if t.IsValid() {
378-
return t.Outputs()
378+
var ret []Utxo
379+
for idx, output := range t.Outputs() {
380+
ret = append(
381+
ret,
382+
Utxo{
383+
Id: NewShelleyTransactionInput(t.Hash(), idx),
384+
Output: output,
385+
},
386+
)
387+
}
388+
return ret
379389
} else {
380390
// No collateral return in Alonzo
381-
return []TransactionOutput{}
391+
return []Utxo{}
382392
}
383393
}
384394

ledger/babbage.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,29 @@ func (t BabbageTransaction) Consumed() []TransactionInput {
553553
}
554554
}
555555

556-
func (t BabbageTransaction) Produced() []TransactionOutput {
556+
func (t BabbageTransaction) Produced() []Utxo {
557557
if t.IsValid() {
558-
return t.Outputs()
558+
var ret []Utxo
559+
for idx, output := range t.Outputs() {
560+
ret = append(
561+
ret,
562+
Utxo{
563+
Id: NewShelleyTransactionInput(t.Hash(), idx),
564+
Output: output,
565+
},
566+
)
567+
}
568+
return ret
559569
} else {
560570
if t.CollateralReturn() == nil {
561-
return []TransactionOutput{}
571+
return []Utxo{}
572+
}
573+
return []Utxo{
574+
{
575+
Id: NewShelleyTransactionInput(t.Hash(), len(t.Outputs())),
576+
Output: t.CollateralReturn(),
577+
},
562578
}
563-
return []TransactionOutput{t.CollateralReturn()}
564579
}
565580
}
566581

ledger/byron.go

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

1717
import (
18+
"encoding/hex"
1819
"fmt"
1920

2021
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
@@ -257,8 +258,18 @@ func (t *ByronTransaction) Consumed() []TransactionInput {
257258
return t.Inputs()
258259
}
259260

260-
func (t *ByronTransaction) Produced() []TransactionOutput {
261-
return t.Outputs()
261+
func (t *ByronTransaction) Produced() []Utxo {
262+
var ret []Utxo
263+
for idx, output := range t.Outputs() {
264+
ret = append(
265+
ret,
266+
Utxo{
267+
Id: NewByronTransactionInput(t.Hash(), idx),
268+
Output: output,
269+
},
270+
)
271+
}
272+
return ret
262273
}
263274

264275
func (t *ByronTransaction) Utxorpc() *utxorpc.Tx {
@@ -280,6 +291,17 @@ type ByronTransactionInput struct {
280291
OutputIndex uint32
281292
}
282293

294+
func NewByronTransactionInput(hash string, idx int) ByronTransactionInput {
295+
tmpHash, err := hex.DecodeString(hash)
296+
if err != nil {
297+
panic(fmt.Sprintf("failed to decode transaction hash: %s", err))
298+
}
299+
return ByronTransactionInput{
300+
TxId: Blake2b256(tmpHash),
301+
OutputIndex: uint32(idx),
302+
}
303+
}
304+
283305
func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
284306
id, err := cbor.DecodeIdFromList(data)
285307
if err != nil {

ledger/conway.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,29 @@ func (t ConwayTransaction) Consumed() []TransactionInput {
441441
}
442442
}
443443

444-
func (t ConwayTransaction) Produced() []TransactionOutput {
444+
func (t ConwayTransaction) Produced() []Utxo {
445445
if t.IsValid() {
446-
return t.Outputs()
446+
var ret []Utxo
447+
for idx, output := range t.Outputs() {
448+
ret = append(
449+
ret,
450+
Utxo{
451+
Id: NewShelleyTransactionInput(t.Hash(), idx),
452+
Output: output,
453+
},
454+
)
455+
}
456+
return ret
447457
} else {
448458
if t.CollateralReturn() == nil {
449-
return []TransactionOutput{}
459+
return []Utxo{}
460+
}
461+
return []Utxo{
462+
{
463+
Id: NewShelleyTransactionInput(t.Hash(), len(t.Outputs())),
464+
Output: t.CollateralReturn(),
465+
},
450466
}
451-
return []TransactionOutput{t.CollateralReturn()}
452467
}
453468
}
454469

ledger/mary.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,18 @@ func (t MaryTransaction) Consumed() []TransactionInput {
259259
return t.Inputs()
260260
}
261261

262-
func (t MaryTransaction) Produced() []TransactionOutput {
263-
return t.Outputs()
262+
func (t MaryTransaction) Produced() []Utxo {
263+
var ret []Utxo
264+
for idx, output := range t.Outputs() {
265+
ret = append(
266+
ret,
267+
Utxo{
268+
Id: NewShelleyTransactionInput(t.Hash(), idx),
269+
Output: output,
270+
},
271+
)
272+
}
273+
return ret
264274
}
265275

266276
func (t *MaryTransaction) Cbor() []byte {

ledger/shelley.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ type ShelleyTransactionInput struct {
332332
OutputIndex uint32
333333
}
334334

335+
func NewShelleyTransactionInput(hash string, idx int) ShelleyTransactionInput {
336+
tmpHash, err := hex.DecodeString(hash)
337+
if err != nil {
338+
panic(fmt.Sprintf("failed to decode transaction hash: %s", err))
339+
}
340+
return ShelleyTransactionInput{
341+
TxId: Blake2b256(tmpHash),
342+
OutputIndex: uint32(idx),
343+
}
344+
}
345+
335346
func (i ShelleyTransactionInput) Id() Blake2b256 {
336347
return i.TxId
337348
}
@@ -502,8 +513,18 @@ func (t ShelleyTransaction) Consumed() []TransactionInput {
502513
return t.Inputs()
503514
}
504515

505-
func (t ShelleyTransaction) Produced() []TransactionOutput {
506-
return t.Outputs()
516+
func (t ShelleyTransaction) Produced() []Utxo {
517+
var ret []Utxo
518+
for idx, output := range t.Outputs() {
519+
ret = append(
520+
ret,
521+
Utxo{
522+
Id: NewShelleyTransactionInput(t.Hash(), idx),
523+
Output: output,
524+
},
525+
)
526+
}
527+
return ret
507528
}
508529

509530
func (t ShelleyTransaction) Utxorpc() *utxorpc.Tx {

ledger/tx.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Transaction interface {
2929
Metadata() *cbor.LazyValue
3030
IsValid() bool
3131
Consumed() []TransactionInput
32-
Produced() []TransactionOutput
32+
Produced() []Utxo
3333
}
3434

3535
type TransactionBody interface {
@@ -74,6 +74,11 @@ type TransactionOutput interface {
7474
Utxorpc() *utxorpc.TxOutput
7575
}
7676

77+
type Utxo struct {
78+
Id TransactionInput
79+
Output TransactionOutput
80+
}
81+
7782
func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {
7883
switch txType {
7984
case TxTypeByron:

0 commit comments

Comments
 (0)