Skip to content

Commit

Permalink
feat: TX consumed/produced helper functions
Browse files Browse the repository at this point in the history
Fixes #606
  • Loading branch information
agaffney committed May 2, 2024
1 parent a0d78a9 commit 48e5223
Show file tree
Hide file tree
Showing 8 changed files with 89 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
19 changes: 19 additions & 0 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,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 {

Check failure on line 465 in ledger/babbage.go

View workflow job for this annotation

GitHub Actions / lint

SA4023: this comparison is never true (staticcheck)
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 {

Check failure on line 195 in ledger/conway.go

View workflow job for this annotation

GitHub Actions / lint

SA4023: this comparison is never true (staticcheck)
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 48e5223

Please sign in to comment.