Skip to content

Commit 2e915ed

Browse files
authored
feat: TX consumed/produced helper functions (#607)
Fixes #606
1 parent a0d78a9 commit 2e915ed

File tree

8 files changed

+95
-0
lines changed

8 files changed

+95
-0
lines changed

ledger/allegra.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ func (t AllegraTransaction) IsValid() bool {
178178
return true
179179
}
180180

181+
func (t AllegraTransaction) Consumed() []TransactionInput {
182+
return t.Inputs()
183+
}
184+
185+
func (t AllegraTransaction) Produced() []TransactionOutput {
186+
return t.Outputs()
187+
}
188+
181189
func (t *AllegraTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
182190
updateMap := make(map[Blake2b224]any)
183191
for k, v := range t.Body.Update.ProtocolParamUpdates {

ledger/alonzo.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ func (t AlonzoTransaction) IsValid() bool {
284284
return t.IsTxValid
285285
}
286286

287+
func (t AlonzoTransaction) Consumed() []TransactionInput {
288+
if t.IsValid() {
289+
return t.Inputs()
290+
} else {
291+
return t.Collateral()
292+
}
293+
}
294+
295+
func (t AlonzoTransaction) Produced() []TransactionOutput {
296+
if t.IsValid() {
297+
return t.Outputs()
298+
} else {
299+
// No collateral return in Alonzo
300+
return []TransactionOutput{}
301+
}
302+
}
303+
287304
func (t *AlonzoTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
288305
updateMap := make(map[Blake2b224]any)
289306
for k, v := range t.Body.Update.ProtocolParamUpdates {

ledger/babbage.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ func (b *BabbageTransactionBody) ReferenceInputs() []TransactionInput {
209209
}
210210

211211
func (b *BabbageTransactionBody) CollateralReturn() TransactionOutput {
212+
// Return an actual nil if we have no value. If we return our nil pointer,
213+
// we get a non-nil interface containing a nil value, which is harder to
214+
// compare against
215+
if b.TxCollateralReturn == nil {
216+
return nil
217+
}
212218
return b.TxCollateralReturn
213219
}
214220

@@ -450,6 +456,25 @@ func (t BabbageTransaction) IsValid() bool {
450456
return t.IsTxValid
451457
}
452458

459+
func (t BabbageTransaction) Consumed() []TransactionInput {
460+
if t.IsValid() {
461+
return t.Inputs()
462+
} else {
463+
return t.Collateral()
464+
}
465+
}
466+
467+
func (t BabbageTransaction) Produced() []TransactionOutput {
468+
if t.IsValid() {
469+
return t.Outputs()
470+
} else {
471+
if t.CollateralReturn() == nil {
472+
return []TransactionOutput{}
473+
}
474+
return []TransactionOutput{t.CollateralReturn()}
475+
}
476+
}
477+
453478
func (t *BabbageTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
454479
updateMap := make(map[Blake2b224]any)
455480
for k, v := range t.Body.Update.ProtocolParamUpdates {

ledger/byron.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ func (t *ByronTransaction) IsValid() bool {
193193
return true
194194
}
195195

196+
func (t *ByronTransaction) Consumed() []TransactionInput {
197+
return t.Inputs()
198+
}
199+
200+
func (t *ByronTransaction) Produced() []TransactionOutput {
201+
return t.Outputs()
202+
}
203+
196204
func (t *ByronTransaction) Utxorpc() *utxorpc.Tx {
197205
return &utxorpc.Tx{}
198206
}

ledger/conway.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ func (t ConwayTransaction) IsValid() bool {
180180
return t.IsTxValid
181181
}
182182

183+
func (t ConwayTransaction) Consumed() []TransactionInput {
184+
if t.IsValid() {
185+
return t.Inputs()
186+
} else {
187+
return t.Collateral()
188+
}
189+
}
190+
191+
func (t ConwayTransaction) Produced() []TransactionOutput {
192+
if t.IsValid() {
193+
return t.Outputs()
194+
} else {
195+
if t.CollateralReturn() == nil {
196+
return []TransactionOutput{}
197+
}
198+
return []TransactionOutput{t.CollateralReturn()}
199+
}
200+
}
201+
183202
func (t *ConwayTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
184203
updateMap := make(map[Blake2b224]any)
185204
for k, v := range t.Body.Update.ProtocolParamUpdates {

ledger/mary.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ func (t MaryTransaction) IsValid() bool {
191191
return true
192192
}
193193

194+
func (t MaryTransaction) Consumed() []TransactionInput {
195+
return t.Inputs()
196+
}
197+
198+
func (t MaryTransaction) Produced() []TransactionOutput {
199+
return t.Outputs()
200+
}
201+
194202
func (t *MaryTransaction) ProtocolParametersUpdate() map[Blake2b224]any {
195203
updateMap := make(map[Blake2b224]any)
196204
for k, v := range t.Body.Update.ProtocolParamUpdates {

ledger/shelley.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ func (t ShelleyTransaction) IsValid() bool {
385385
return true
386386
}
387387

388+
func (t ShelleyTransaction) Consumed() []TransactionInput {
389+
return t.Inputs()
390+
}
391+
392+
func (t ShelleyTransaction) Produced() []TransactionOutput {
393+
return t.Outputs()
394+
}
395+
388396
func (t ShelleyTransaction) Utxorpc() *utxorpc.Tx {
389397
return t.Body.Utxorpc()
390398
}

ledger/tx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Transaction interface {
2828
TransactionBody
2929
Metadata() *cbor.Value
3030
IsValid() bool
31+
Consumed() []TransactionInput
32+
Produced() []TransactionOutput
3133
ProtocolParametersUpdate() map[Blake2b224]any
3234
}
3335

0 commit comments

Comments
 (0)