Skip to content

Commit

Permalink
feat: include UTxO ID with produced TX outputs (#669)
Browse files Browse the repository at this point in the history
Fixes #612
  • Loading branch information
agaffney committed Jul 13, 2024
1 parent 4b9ba36 commit d0eef33
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 20 deletions.
14 changes: 12 additions & 2 deletions ledger/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,18 @@ func (t AllegraTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t AllegraTransaction) Produced() []TransactionOutput {
return t.Outputs()
func (t AllegraTransaction) Produced() []Utxo {
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
}

func (t AllegraTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
Expand Down
16 changes: 13 additions & 3 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,22 @@ func (t AlonzoTransaction) Consumed() []TransactionInput {
}
}

func (t AlonzoTransaction) Produced() []TransactionOutput {
func (t AlonzoTransaction) Produced() []Utxo {
if t.IsValid() {
return t.Outputs()
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
} else {
// No collateral return in Alonzo
return []TransactionOutput{}
return []Utxo{}
}
}

Expand Down
23 changes: 19 additions & 4 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,14 +553,29 @@ func (t BabbageTransaction) Consumed() []TransactionInput {
}
}

func (t BabbageTransaction) Produced() []TransactionOutput {
func (t BabbageTransaction) Produced() []Utxo {
if t.IsValid() {
return t.Outputs()
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
} else {
if t.CollateralReturn() == nil {
return []TransactionOutput{}
return []Utxo{}
}
return []Utxo{
{
Id: NewShelleyTransactionInput(t.Hash(), len(t.Outputs())),
Output: t.CollateralReturn(),
},
}
return []TransactionOutput{t.CollateralReturn()}
}
}

Expand Down
26 changes: 24 additions & 2 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ledger

import (
"encoding/hex"
"fmt"

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

func (t *ByronTransaction) Produced() []TransactionOutput {
return t.Outputs()
func (t *ByronTransaction) Produced() []Utxo {
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewByronTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
}

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

func NewByronTransactionInput(hash string, idx int) ByronTransactionInput {
tmpHash, err := hex.DecodeString(hash)
if err != nil {
panic(fmt.Sprintf("failed to decode transaction hash: %s", err))
}
return ByronTransactionInput{
TxId: Blake2b256(tmpHash),
OutputIndex: uint32(idx),
}
}

func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
id, err := cbor.DecodeIdFromList(data)
if err != nil {
Expand Down
23 changes: 19 additions & 4 deletions ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,29 @@ func (t ConwayTransaction) Consumed() []TransactionInput {
}
}

func (t ConwayTransaction) Produced() []TransactionOutput {
func (t ConwayTransaction) Produced() []Utxo {
if t.IsValid() {
return t.Outputs()
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
} else {
if t.CollateralReturn() == nil {
return []TransactionOutput{}
return []Utxo{}
}
return []Utxo{
{
Id: NewShelleyTransactionInput(t.Hash(), len(t.Outputs())),
Output: t.CollateralReturn(),
},
}
return []TransactionOutput{t.CollateralReturn()}
}
}

Expand Down
14 changes: 12 additions & 2 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,18 @@ func (t MaryTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t MaryTransaction) Produced() []TransactionOutput {
return t.Outputs()
func (t MaryTransaction) Produced() []Utxo {
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
}

func (t *MaryTransaction) Cbor() []byte {
Expand Down
25 changes: 23 additions & 2 deletions ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ type ShelleyTransactionInput struct {
OutputIndex uint32
}

func NewShelleyTransactionInput(hash string, idx int) ShelleyTransactionInput {
tmpHash, err := hex.DecodeString(hash)
if err != nil {
panic(fmt.Sprintf("failed to decode transaction hash: %s", err))
}
return ShelleyTransactionInput{
TxId: Blake2b256(tmpHash),
OutputIndex: uint32(idx),
}
}

func (i ShelleyTransactionInput) Id() Blake2b256 {
return i.TxId
}
Expand Down Expand Up @@ -502,8 +513,18 @@ func (t ShelleyTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t ShelleyTransaction) Produced() []TransactionOutput {
return t.Outputs()
func (t ShelleyTransaction) Produced() []Utxo {
var ret []Utxo
for idx, output := range t.Outputs() {
ret = append(
ret,
Utxo{
Id: NewShelleyTransactionInput(t.Hash(), idx),
Output: output,
},
)
}
return ret
}

func (t ShelleyTransaction) Utxorpc() *utxorpc.Tx {
Expand Down
7 changes: 6 additions & 1 deletion ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Transaction interface {
Metadata() *cbor.LazyValue
IsValid() bool
Consumed() []TransactionInput
Produced() []TransactionOutput
Produced() []Utxo
}

type TransactionBody interface {
Expand Down Expand Up @@ -74,6 +74,11 @@ type TransactionOutput interface {
Utxorpc() *utxorpc.TxOutput
}

type Utxo struct {
Id TransactionInput
Output TransactionOutput
}

func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) {
switch txType {
case TxTypeByron:
Expand Down

0 comments on commit d0eef33

Please sign in to comment.