Skip to content

Commit

Permalink
feat: TX consumed/produced helper functions (#607)
Browse files Browse the repository at this point in the history
Fixes #606
  • Loading branch information
agaffney committed May 3, 2024
1 parent a0d78a9 commit 2e915ed
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 0 deletions.
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

0 comments on commit 2e915ed

Please sign in to comment.