Skip to content

Commit d126f55

Browse files
authored
refactor: remove embeds in per-era TransactionBody types (#979)
Fixes #978 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent ecc4f3c commit d126f55

File tree

12 files changed

+581
-427
lines changed

12 files changed

+581
-427
lines changed

ledger/allegra/allegra.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,50 @@ func (h *AllegraBlockHeader) Era() common.Era {
135135
}
136136

137137
type AllegraTransactionBody struct {
138-
shelley.ShelleyTransactionBody
139-
Update struct {
138+
common.TransactionBodyBase
139+
TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"`
140+
TxOutputs []shelley.ShelleyTransactionOutput `cbor:"1,keyasint,omitempty"`
141+
TxFee uint64 `cbor:"2,keyasint,omitempty"`
142+
Ttl uint64 `cbor:"3,keyasint,omitempty"`
143+
TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"`
144+
TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"`
145+
Update struct {
140146
cbor.StructAsArray
141147
ProtocolParamUpdates map[common.Blake2b224]AllegraProtocolParameterUpdate
142148
Epoch uint64
143149
} `cbor:"6,keyasint,omitempty"`
144-
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
150+
TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"`
151+
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
145152
}
146153

147154
func (b *AllegraTransactionBody) UnmarshalCBOR(cborData []byte) error {
148155
return b.UnmarshalCbor(cborData, b)
149156
}
150157

158+
func (b *AllegraTransactionBody) Inputs() []common.TransactionInput {
159+
ret := []common.TransactionInput{}
160+
for _, input := range b.TxInputs.Items() {
161+
ret = append(ret, input)
162+
}
163+
return ret
164+
}
165+
166+
func (b *AllegraTransactionBody) Outputs() []common.TransactionOutput {
167+
ret := []common.TransactionOutput{}
168+
for _, output := range b.TxOutputs {
169+
ret = append(ret, &output)
170+
}
171+
return ret
172+
}
173+
174+
func (b *AllegraTransactionBody) Fee() uint64 {
175+
return b.TxFee
176+
}
177+
178+
func (b *AllegraTransactionBody) TTL() uint64 {
179+
return b.Ttl
180+
}
181+
151182
func (b *AllegraTransactionBody) ValidityIntervalStart() uint64 {
152183
return b.TxValidityIntervalStart
153184
}
@@ -160,6 +191,26 @@ func (b *AllegraTransactionBody) ProtocolParameterUpdates() (uint64, map[common.
160191
return b.Update.Epoch, updateMap
161192
}
162193

194+
func (b *AllegraTransactionBody) Certificates() []common.Certificate {
195+
ret := make([]common.Certificate, len(b.TxCertificates))
196+
for i, cert := range b.TxCertificates {
197+
ret[i] = cert.Certificate
198+
}
199+
return ret
200+
}
201+
202+
func (b *AllegraTransactionBody) Withdrawals() map[*common.Address]uint64 {
203+
return b.TxWithdrawals
204+
}
205+
206+
func (b *AllegraTransactionBody) AuxDataHash() *common.Blake2b256 {
207+
return b.TxAuxDataHash
208+
}
209+
210+
func (b *AllegraTransactionBody) Utxorpc() *utxorpc.Tx {
211+
return common.TransactionBodyToUtxorpc(b)
212+
}
213+
163214
type AllegraTransaction struct {
164215
cbor.StructAsArray
165216
cbor.DecodeStoreCbor

ledger/allegra/rules_test.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,12 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) {
131131
func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) {
132132
testTx := &allegra.AllegraTransaction{
133133
Body: allegra.AllegraTransactionBody{
134-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
135-
TxInputs: shelley.NewShelleyTransactionInputSet(
136-
// Non-empty input set
137-
[]shelley.ShelleyTransactionInput{
138-
{},
139-
},
140-
),
141-
},
134+
TxInputs: shelley.NewShelleyTransactionInputSet(
135+
// Non-empty input set
136+
[]shelley.ShelleyTransactionInput{
137+
{},
138+
},
139+
),
142140
},
143141
}
144142
testLedgerState := test.MockLedgerState{}
@@ -200,9 +198,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) {
200198
testTxCbor, _ := hex.DecodeString("abcdef")
201199
testTx := &allegra.AllegraTransaction{
202200
Body: allegra.AllegraTransactionBody{
203-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
204-
TxFee: testExactFee,
205-
},
201+
TxFee: testExactFee,
206202
},
207203
}
208204
testTx.SetCbor(testTxCbor)
@@ -369,11 +365,9 @@ func TestUtxoValidateWrongNetwork(t *testing.T) {
369365
)
370366
testTx := &allegra.AllegraTransaction{
371367
Body: allegra.AllegraTransactionBody{
372-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
373-
TxOutputs: []shelley.ShelleyTransactionOutput{
374-
{
375-
OutputAmount: 123456,
376-
},
368+
TxOutputs: []shelley.ShelleyTransactionOutput{
369+
{
370+
OutputAmount: 123456,
377371
},
378372
},
379373
},
@@ -441,9 +435,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) {
441435
)
442436
testTx := &allegra.AllegraTransaction{
443437
Body: allegra.AllegraTransactionBody{
444-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
445-
TxWithdrawals: map[*common.Address]uint64{},
446-
},
438+
TxWithdrawals: map[*common.Address]uint64{},
447439
},
448440
}
449441
testLedgerState := test.MockLedgerState{
@@ -510,17 +502,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
510502
testOutputOverAmount := testOutputExactAmount + 999
511503
testTx := &allegra.AllegraTransaction{
512504
Body: allegra.AllegraTransactionBody{
513-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
514-
TxFee: testFee,
515-
TxInputs: shelley.NewShelleyTransactionInputSet(
516-
[]shelley.ShelleyTransactionInput{
517-
shelley.NewShelleyTransactionInput(testInputTxId, 0),
518-
},
519-
),
520-
TxOutputs: []shelley.ShelleyTransactionOutput{
521-
// Empty placeholder output
522-
{},
505+
TxFee: testFee,
506+
TxInputs: shelley.NewShelleyTransactionInputSet(
507+
[]shelley.ShelleyTransactionInput{
508+
shelley.NewShelleyTransactionInput(testInputTxId, 0),
523509
},
510+
),
511+
TxOutputs: []shelley.ShelleyTransactionOutput{
512+
// Empty placeholder output
513+
{},
524514
},
525515
},
526516
}
@@ -676,11 +666,9 @@ func TestUtxoValidateOutputTooSmallUtxo(t *testing.T) {
676666
var testOutputAmountBad uint64 = 123
677667
testTx := &allegra.AllegraTransaction{
678668
Body: allegra.AllegraTransactionBody{
679-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
680-
TxOutputs: []shelley.ShelleyTransactionOutput{
681-
// Empty placeholder output
682-
{},
683-
},
669+
TxOutputs: []shelley.ShelleyTransactionOutput{
670+
// Empty placeholder output
671+
{},
684672
},
685673
},
686674
}
@@ -763,11 +751,9 @@ func TestUtxoValidateOutputBootAddrAttrsTooBig(t *testing.T) {
763751
)
764752
testTx := &allegra.AllegraTransaction{
765753
Body: allegra.AllegraTransactionBody{
766-
ShelleyTransactionBody: shelley.ShelleyTransactionBody{
767-
TxOutputs: []shelley.ShelleyTransactionOutput{
768-
// Empty placeholder
769-
{},
770-
},
754+
TxOutputs: []shelley.ShelleyTransactionOutput{
755+
// Empty placeholder
756+
{},
771757
},
772758
},
773759
}

ledger/alonzo/alonzo.go

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,39 @@ func (h *AlonzoBlockHeader) Era() common.Era {
144144
}
145145

146146
type AlonzoTransactionBody struct {
147-
mary.MaryTransactionBody
148-
TxOutputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"`
149-
Update struct {
147+
common.TransactionBodyBase
148+
TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"`
149+
TxOutputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"`
150+
TxFee uint64 `cbor:"2,keyasint,omitempty"`
151+
Ttl uint64 `cbor:"3,keyasint,omitempty"`
152+
TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"`
153+
TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"`
154+
Update struct {
150155
cbor.StructAsArray
151156
ProtocolParamUpdates map[common.Blake2b224]AlonzoProtocolParameterUpdate
152157
Epoch uint64
153158
} `cbor:"6,keyasint,omitempty"`
154-
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
155-
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
156-
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
157-
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
159+
TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"`
160+
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
161+
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
162+
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
163+
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
164+
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
165+
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
158166
}
159167

160168
func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
161169
return b.UnmarshalCbor(cborData, b)
162170
}
163171

172+
func (b *AlonzoTransactionBody) Inputs() []common.TransactionInput {
173+
ret := []common.TransactionInput{}
174+
for _, input := range b.TxInputs.Items() {
175+
ret = append(ret, input)
176+
}
177+
return ret
178+
}
179+
164180
func (b *AlonzoTransactionBody) Outputs() []common.TransactionOutput {
165181
ret := []common.TransactionOutput{}
166182
for _, output := range b.TxOutputs {
@@ -169,6 +185,18 @@ func (b *AlonzoTransactionBody) Outputs() []common.TransactionOutput {
169185
return ret
170186
}
171187

188+
func (b *AlonzoTransactionBody) Fee() uint64 {
189+
return b.TxFee
190+
}
191+
192+
func (b *AlonzoTransactionBody) TTL() uint64 {
193+
return b.Ttl
194+
}
195+
196+
func (b *AlonzoTransactionBody) ValidityIntervalStart() uint64 {
197+
return b.TxValidityIntervalStart
198+
}
199+
172200
func (b *AlonzoTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) {
173201
updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate)
174202
for k, v := range b.Update.ProtocolParamUpdates {
@@ -177,6 +205,26 @@ func (b *AlonzoTransactionBody) ProtocolParameterUpdates() (uint64, map[common.B
177205
return b.Update.Epoch, updateMap
178206
}
179207

208+
func (b *AlonzoTransactionBody) Certificates() []common.Certificate {
209+
ret := make([]common.Certificate, len(b.TxCertificates))
210+
for i, cert := range b.TxCertificates {
211+
ret[i] = cert.Certificate
212+
}
213+
return ret
214+
}
215+
216+
func (b *AlonzoTransactionBody) Withdrawals() map[*common.Address]uint64 {
217+
return b.TxWithdrawals
218+
}
219+
220+
func (b *AlonzoTransactionBody) AuxDataHash() *common.Blake2b256 {
221+
return b.TxAuxDataHash
222+
}
223+
224+
func (b *AlonzoTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
225+
return b.TxMint
226+
}
227+
180228
func (b *AlonzoTransactionBody) Collateral() []common.TransactionInput {
181229
ret := []common.TransactionInput{}
182230
for _, collateral := range b.TxCollateral {
@@ -193,6 +241,10 @@ func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 {
193241
return b.TxScriptDataHash
194242
}
195243

244+
func (b *AlonzoTransactionBody) Utxorpc() *utxorpc.Tx {
245+
return common.TransactionBodyToUtxorpc(b)
246+
}
247+
196248
type AlonzoTransactionOutput struct {
197249
cbor.StructAsArray
198250
cbor.DecodeStoreCbor

0 commit comments

Comments
 (0)