Skip to content

feat: TX consumed/produced helper functions #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ledger/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func (t AllegraTransaction) IsValid() bool {
return true
}

func (t AllegraTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t AllegraTransaction) Produced() []TransactionOutput {
return t.Outputs()
}

func (t *AllegraTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
updateMap := make(map[Blake2b224]any)
for k, v := range t.Body.Update.ProtocolParamUpdates {
Expand Down
17 changes: 17 additions & 0 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ func (t AlonzoTransaction) IsValid() bool {
return t.IsTxValid
}

func (t AlonzoTransaction) Consumed() []TransactionInput {
if t.IsValid() {
return t.Inputs()
} else {
return t.Collateral()
}
}

func (t AlonzoTransaction) Produced() []TransactionOutput {
if t.IsValid() {
return t.Outputs()
} else {
// No collateral return in Alonzo
return []TransactionOutput{}
}
}

func (t *AlonzoTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
updateMap := make(map[Blake2b224]any)
for k, v := range t.Body.Update.ProtocolParamUpdates {
Expand Down
25 changes: 25 additions & 0 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ func (b *BabbageTransactionBody) ReferenceInputs() []TransactionInput {
}

func (b *BabbageTransactionBody) CollateralReturn() TransactionOutput {
// Return an actual nil if we have no value. If we return our nil pointer,
// we get a non-nil interface containing a nil value, which is harder to
// compare against
if b.TxCollateralReturn == nil {
return nil
}
return b.TxCollateralReturn
}

Expand Down Expand Up @@ -450,6 +456,25 @@ func (t BabbageTransaction) IsValid() bool {
return t.IsTxValid
}

func (t BabbageTransaction) Consumed() []TransactionInput {
if t.IsValid() {
return t.Inputs()
} else {
return t.Collateral()
}
}

func (t BabbageTransaction) Produced() []TransactionOutput {
if t.IsValid() {
return t.Outputs()
} else {
if t.CollateralReturn() == nil {
return []TransactionOutput{}
}
return []TransactionOutput{t.CollateralReturn()}
}
}

func (t *BabbageTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
updateMap := make(map[Blake2b224]any)
for k, v := range t.Body.Update.ProtocolParamUpdates {
Expand Down
8 changes: 8 additions & 0 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ func (t *ByronTransaction) IsValid() bool {
return true
}

func (t *ByronTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t *ByronTransaction) Produced() []TransactionOutput {
return t.Outputs()
}

func (t *ByronTransaction) Utxorpc() *utxorpc.Tx {
return &utxorpc.Tx{}
}
Expand Down
19 changes: 19 additions & 0 deletions ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ func (t ConwayTransaction) IsValid() bool {
return t.IsTxValid
}

func (t ConwayTransaction) Consumed() []TransactionInput {
if t.IsValid() {
return t.Inputs()
} else {
return t.Collateral()
}
}

func (t ConwayTransaction) Produced() []TransactionOutput {
if t.IsValid() {
return t.Outputs()
} else {
if t.CollateralReturn() == nil {
return []TransactionOutput{}
}
return []TransactionOutput{t.CollateralReturn()}
}
}

func (t *ConwayTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
updateMap := make(map[Blake2b224]any)
for k, v := range t.Body.Update.ProtocolParamUpdates {
Expand Down
8 changes: 8 additions & 0 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func (t MaryTransaction) IsValid() bool {
return true
}

func (t MaryTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t MaryTransaction) Produced() []TransactionOutput {
return t.Outputs()
}

func (t *MaryTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
updateMap := make(map[Blake2b224]any)
for k, v := range t.Body.Update.ProtocolParamUpdates {
Expand Down
8 changes: 8 additions & 0 deletions ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ func (t ShelleyTransaction) IsValid() bool {
return true
}

func (t ShelleyTransaction) Consumed() []TransactionInput {
return t.Inputs()
}

func (t ShelleyTransaction) Produced() []TransactionOutput {
return t.Outputs()
}

func (t ShelleyTransaction) Utxorpc() *utxorpc.Tx {
return t.Body.Utxorpc()
}
Expand Down
2 changes: 2 additions & 0 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Transaction interface {
TransactionBody
Metadata() *cbor.Value
IsValid() bool
Consumed() []TransactionInput
Produced() []TransactionOutput
ProtocolParametersUpdate() map[Blake2b224]any
}

Expand Down