diff --git a/ledger/allegra/allegra.go b/ledger/allegra/allegra.go index 57fc3530..a4172e27 100644 --- a/ledger/allegra/allegra.go +++ b/ledger/allegra/allegra.go @@ -135,19 +135,50 @@ func (h *AllegraBlockHeader) Era() common.Era { } type AllegraTransactionBody struct { - shelley.ShelleyTransactionBody - Update struct { + common.TransactionBodyBase + TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"` + TxOutputs []shelley.ShelleyTransactionOutput `cbor:"1,keyasint,omitempty"` + TxFee uint64 `cbor:"2,keyasint,omitempty"` + Ttl uint64 `cbor:"3,keyasint,omitempty"` + TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"` + TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"` + Update struct { cbor.StructAsArray ProtocolParamUpdates map[common.Blake2b224]AllegraProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` + TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"` + TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` } func (b *AllegraTransactionBody) UnmarshalCBOR(cborData []byte) error { return b.UnmarshalCbor(cborData, b) } +func (b *AllegraTransactionBody) Inputs() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, input := range b.TxInputs.Items() { + ret = append(ret, input) + } + return ret +} + +func (b *AllegraTransactionBody) Outputs() []common.TransactionOutput { + ret := []common.TransactionOutput{} + for _, output := range b.TxOutputs { + ret = append(ret, &output) + } + return ret +} + +func (b *AllegraTransactionBody) Fee() uint64 { + return b.TxFee +} + +func (b *AllegraTransactionBody) TTL() uint64 { + return b.Ttl +} + func (b *AllegraTransactionBody) ValidityIntervalStart() uint64 { return b.TxValidityIntervalStart } @@ -160,6 +191,26 @@ func (b *AllegraTransactionBody) ProtocolParameterUpdates() (uint64, map[common. return b.Update.Epoch, updateMap } +func (b *AllegraTransactionBody) Certificates() []common.Certificate { + ret := make([]common.Certificate, len(b.TxCertificates)) + for i, cert := range b.TxCertificates { + ret[i] = cert.Certificate + } + return ret +} + +func (b *AllegraTransactionBody) Withdrawals() map[*common.Address]uint64 { + return b.TxWithdrawals +} + +func (b *AllegraTransactionBody) AuxDataHash() *common.Blake2b256 { + return b.TxAuxDataHash +} + +func (b *AllegraTransactionBody) Utxorpc() *utxorpc.Tx { + return common.TransactionBodyToUtxorpc(b) +} + type AllegraTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor diff --git a/ledger/allegra/rules_test.go b/ledger/allegra/rules_test.go index 0f23aa77..972ad499 100644 --- a/ledger/allegra/rules_test.go +++ b/ledger/allegra/rules_test.go @@ -131,14 +131,12 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) { testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxInputs: shelley.NewShelleyTransactionInputSet( - // Non-empty input set - []shelley.ShelleyTransactionInput{ - {}, - }, - ), - }, + TxInputs: shelley.NewShelleyTransactionInputSet( + // Non-empty input set + []shelley.ShelleyTransactionInput{ + {}, + }, + ), }, } testLedgerState := test.MockLedgerState{} @@ -200,9 +198,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) { testTxCbor, _ := hex.DecodeString("abcdef") testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testExactFee, - }, + TxFee: testExactFee, }, } testTx.SetCbor(testTxCbor) @@ -369,11 +365,9 @@ func TestUtxoValidateWrongNetwork(t *testing.T) { ) testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxOutputs: []shelley.ShelleyTransactionOutput{ - { - OutputAmount: 123456, - }, + TxOutputs: []shelley.ShelleyTransactionOutput{ + { + OutputAmount: 123456, }, }, }, @@ -441,9 +435,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) { ) testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxWithdrawals: map[*common.Address]uint64{}, - }, + TxWithdrawals: map[*common.Address]uint64{}, }, } testLedgerState := test.MockLedgerState{ @@ -510,17 +502,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) { testOutputOverAmount := testOutputExactAmount + 999 testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - TxInputs: shelley.NewShelleyTransactionInputSet( - []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput(testInputTxId, 0), - }, - ), - TxOutputs: []shelley.ShelleyTransactionOutput{ - // Empty placeholder output - {}, + TxFee: testFee, + TxInputs: shelley.NewShelleyTransactionInputSet( + []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput(testInputTxId, 0), }, + ), + TxOutputs: []shelley.ShelleyTransactionOutput{ + // Empty placeholder output + {}, }, }, } @@ -676,11 +666,9 @@ func TestUtxoValidateOutputTooSmallUtxo(t *testing.T) { var testOutputAmountBad uint64 = 123 testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxOutputs: []shelley.ShelleyTransactionOutput{ - // Empty placeholder output - {}, - }, + TxOutputs: []shelley.ShelleyTransactionOutput{ + // Empty placeholder output + {}, }, }, } @@ -763,11 +751,9 @@ func TestUtxoValidateOutputBootAddrAttrsTooBig(t *testing.T) { ) testTx := &allegra.AllegraTransaction{ Body: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxOutputs: []shelley.ShelleyTransactionOutput{ - // Empty placeholder - {}, - }, + TxOutputs: []shelley.ShelleyTransactionOutput{ + // Empty placeholder + {}, }, }, } diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index 303fceb8..ea1ca6ae 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -144,23 +144,39 @@ func (h *AlonzoBlockHeader) Era() common.Era { } type AlonzoTransactionBody struct { - mary.MaryTransactionBody - TxOutputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"` - Update struct { + common.TransactionBodyBase + TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"` + TxOutputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"` + TxFee uint64 `cbor:"2,keyasint,omitempty"` + Ttl uint64 `cbor:"3,keyasint,omitempty"` + TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"` + TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"` + Update struct { cbor.StructAsArray ProtocolParamUpdates map[common.Blake2b224]AlonzoProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"` - TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"` - TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"` - NetworkId uint8 `cbor:"15,keyasint,omitempty"` + TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"` + TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` + TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"` + TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"` + TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"` + TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"` + NetworkId uint8 `cbor:"15,keyasint,omitempty"` } func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error { return b.UnmarshalCbor(cborData, b) } +func (b *AlonzoTransactionBody) Inputs() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, input := range b.TxInputs.Items() { + ret = append(ret, input) + } + return ret +} + func (b *AlonzoTransactionBody) Outputs() []common.TransactionOutput { ret := []common.TransactionOutput{} for _, output := range b.TxOutputs { @@ -169,6 +185,18 @@ func (b *AlonzoTransactionBody) Outputs() []common.TransactionOutput { return ret } +func (b *AlonzoTransactionBody) Fee() uint64 { + return b.TxFee +} + +func (b *AlonzoTransactionBody) TTL() uint64 { + return b.Ttl +} + +func (b *AlonzoTransactionBody) ValidityIntervalStart() uint64 { + return b.TxValidityIntervalStart +} + func (b *AlonzoTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) { updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate) for k, v := range b.Update.ProtocolParamUpdates { @@ -177,6 +205,26 @@ func (b *AlonzoTransactionBody) ProtocolParameterUpdates() (uint64, map[common.B return b.Update.Epoch, updateMap } +func (b *AlonzoTransactionBody) Certificates() []common.Certificate { + ret := make([]common.Certificate, len(b.TxCertificates)) + for i, cert := range b.TxCertificates { + ret[i] = cert.Certificate + } + return ret +} + +func (b *AlonzoTransactionBody) Withdrawals() map[*common.Address]uint64 { + return b.TxWithdrawals +} + +func (b *AlonzoTransactionBody) AuxDataHash() *common.Blake2b256 { + return b.TxAuxDataHash +} + +func (b *AlonzoTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] { + return b.TxMint +} + func (b *AlonzoTransactionBody) Collateral() []common.TransactionInput { ret := []common.TransactionInput{} for _, collateral := range b.TxCollateral { @@ -193,6 +241,10 @@ func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 { return b.TxScriptDataHash } +func (b *AlonzoTransactionBody) Utxorpc() *utxorpc.Tx { + return common.TransactionBodyToUtxorpc(b) +} + type AlonzoTransactionOutput struct { cbor.StructAsArray cbor.DecodeStoreCbor diff --git a/ledger/alonzo/rules_test.go b/ledger/alonzo/rules_test.go index ca5c8201..2c40af4b 100644 --- a/ledger/alonzo/rules_test.go +++ b/ledger/alonzo/rules_test.go @@ -35,11 +35,7 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { var testZeroSlot uint64 = 0 testTx := &alonzo.AlonzoTransaction{ Body: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - TxValidityIntervalStart: testSlot, - }, - }, + TxValidityIntervalStart: testSlot, }, } testLedgerState := test.MockLedgerState{} @@ -138,18 +134,12 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) { testTx := &alonzo.AlonzoTransaction{ Body: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxInputs: shelley.NewShelleyTransactionInputSet( - // Non-empty input set - []shelley.ShelleyTransactionInput{ - {}, - }, - ), - }, + TxInputs: shelley.NewShelleyTransactionInputSet( + // Non-empty input set + []shelley.ShelleyTransactionInput{ + {}, }, - }, + ), }, } testLedgerState := test.MockLedgerState{} @@ -212,13 +202,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) { testTxCbor, _ := hex.DecodeString("abcdef01") testTx := &alonzo.AlonzoTransaction{ Body: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testExactFee, - }, - }, - }, + TxFee: testExactFee, }, } testTx.SetCbor(testTxCbor) @@ -461,13 +445,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) { ) testTx := &alonzo.AlonzoTransaction{ Body: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxWithdrawals: map[*common.Address]uint64{}, - }, - }, - }, + TxWithdrawals: map[*common.Address]uint64{}, }, } testLedgerState := test.MockLedgerState{ @@ -538,21 +516,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) { // Empty placeholder output {}, }, - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - TxInputs: shelley.NewShelleyTransactionInputSet( - []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput( - testInputTxId, - 0, - ), - }, - ), - }, + TxFee: testFee, + TxInputs: shelley.NewShelleyTransactionInputSet( + []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput( + testInputTxId, + 0, + ), }, - }, + ), }, } testLedgerState := test.MockLedgerState{ @@ -1010,13 +982,7 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) { var testCollateralAmount2 uint64 = 200000 testTx := &alonzo.AlonzoTransaction{ Body: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - }, - }, - }, + TxFee: testFee, }, WitnessSet: alonzo.AlonzoTransactionWitnessSet{ WsRedeemers: []alonzo.AlonzoRedeemer{ diff --git a/ledger/babbage/babbage.go b/ledger/babbage/babbage.go index 92922865..6035c1bf 100644 --- a/ledger/babbage/babbage.go +++ b/ledger/babbage/babbage.go @@ -210,22 +210,42 @@ func (h *BabbageBlockHeader) Era() common.Era { } type BabbageTransactionBody struct { - alonzo.AlonzoTransactionBody - TxOutputs []BabbageTransactionOutput `cbor:"1,keyasint,omitempty"` - Update struct { + common.TransactionBodyBase + TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"` + TxOutputs []BabbageTransactionOutput `cbor:"1,keyasint,omitempty"` + TxFee uint64 `cbor:"2,keyasint,omitempty"` + Ttl uint64 `cbor:"3,keyasint,omitempty"` + TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"` + TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"` + Update struct { cbor.StructAsArray ProtocolParamUpdates map[common.Blake2b224]BabbageProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxCollateralReturn *BabbageTransactionOutput `cbor:"16,keyasint,omitempty"` - TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"` - TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"` + TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"` + TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` + TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"` + TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"` + TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"` + TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"` + NetworkId uint8 `cbor:"15,keyasint,omitempty"` + TxCollateralReturn *BabbageTransactionOutput `cbor:"16,keyasint,omitempty"` + TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"` + TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"` } func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error { return b.UnmarshalCbor(cborData, b) } +func (b *BabbageTransactionBody) Inputs() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, input := range b.TxInputs.Items() { + ret = append(ret, input) + } + return ret +} + func (b *BabbageTransactionBody) Outputs() []common.TransactionOutput { ret := []common.TransactionOutput{} for _, output := range b.TxOutputs { @@ -234,6 +254,18 @@ func (b *BabbageTransactionBody) Outputs() []common.TransactionOutput { return ret } +func (b *BabbageTransactionBody) Fee() uint64 { + return b.TxFee +} + +func (b *BabbageTransactionBody) TTL() uint64 { + return b.Ttl +} + +func (b *BabbageTransactionBody) ValidityIntervalStart() uint64 { + return b.TxValidityIntervalStart +} + func (b *BabbageTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) { updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate) for k, v := range b.Update.ProtocolParamUpdates { @@ -242,6 +274,42 @@ func (b *BabbageTransactionBody) ProtocolParameterUpdates() (uint64, map[common. return b.Update.Epoch, updateMap } +func (b *BabbageTransactionBody) Certificates() []common.Certificate { + ret := make([]common.Certificate, len(b.TxCertificates)) + for i, cert := range b.TxCertificates { + ret[i] = cert.Certificate + } + return ret +} + +func (b *BabbageTransactionBody) Withdrawals() map[*common.Address]uint64 { + return b.TxWithdrawals +} + +func (b *BabbageTransactionBody) AuxDataHash() *common.Blake2b256 { + return b.TxAuxDataHash +} + +func (b *BabbageTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] { + return b.TxMint +} + +func (b *BabbageTransactionBody) Collateral() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, collateral := range b.TxCollateral { + ret = append(ret, collateral) + } + return ret +} + +func (b *BabbageTransactionBody) RequiredSigners() []common.Blake2b224 { + return b.TxRequiredSigners[:] +} + +func (b *BabbageTransactionBody) ScriptDataHash() *common.Blake2b256 { + return b.TxScriptDataHash +} + func (b *BabbageTransactionBody) ReferenceInputs() []common.TransactionInput { ret := []common.TransactionInput{} for _, input := range b.TxReferenceInputs { @@ -265,37 +333,7 @@ func (b *BabbageTransactionBody) TotalCollateral() uint64 { } func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx { - txi := []*utxorpc.TxInput{} - txri := []*utxorpc.TxInput{} - txo := []*utxorpc.TxOutput{} - for _, i := range b.Inputs() { - input := i.Utxorpc() - txi = append(txi, input) - } - for _, o := range b.Outputs() { - output := o.Utxorpc() - txo = append(txo, output) - } - for _, ri := range b.ReferenceInputs() { - input := ri.Utxorpc() - txri = append(txri, input) - } - tx := &utxorpc.Tx{ - Inputs: txi, - Outputs: txo, - // Certificates: b.Certificates(), - // Withdrawals: b.Withdrawals(), - // Mint: b.Mint(), - ReferenceInputs: txri, - // Witnesses: b.Witnesses(), - // Collateral: b.Collateral(), - Fee: b.Fee(), - // Successful: b.Successful(), - // Auxiliary: b.AuxData(), - // Validity: b.Validity(), - Hash: b.Hash().Bytes(), - } - return tx + return common.TransactionBodyToUtxorpc(b) } const ( diff --git a/ledger/babbage/rules_test.go b/ledger/babbage/rules_test.go index 70b3da61..e09dc7f1 100644 --- a/ledger/babbage/rules_test.go +++ b/ledger/babbage/rules_test.go @@ -36,13 +36,7 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { var testZeroSlot uint64 = 0 testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - TxValidityIntervalStart: testSlot, - }, - }, - }, + TxValidityIntervalStart: testSlot, }, } testLedgerState := test.MockLedgerState{} @@ -141,20 +135,12 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) { testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxInputs: shelley.NewShelleyTransactionInputSet( - // Non-empty input set - []shelley.ShelleyTransactionInput{ - {}, - }, - ), - }, - }, + TxInputs: shelley.NewShelleyTransactionInputSet( + // Non-empty input set + []shelley.ShelleyTransactionInput{ + {}, }, - }, + ), }, } testLedgerState := test.MockLedgerState{} @@ -217,15 +203,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) { testTxCbor, _ := hex.DecodeString("abcdef01") testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testExactFee, - }, - }, - }, - }, + TxFee: testExactFee, }, } testTx.SetCbor(testTxCbor) @@ -462,15 +440,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) { ) testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxWithdrawals: map[*common.Address]uint64{}, - }, - }, - }, - }, + TxWithdrawals: map[*common.Address]uint64{}, }, } testLedgerState := test.MockLedgerState{ @@ -541,23 +511,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) { // Empty placeholder output {}, }, - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - TxInputs: shelley.NewShelleyTransactionInputSet( - []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput( - testInputTxId, - 0, - ), - }, - ), - }, - }, + TxFee: testFee, + TxInputs: shelley.NewShelleyTransactionInputSet( + []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput( + testInputTxId, + 0, + ), }, - }, + ), }, } testLedgerState := test.MockLedgerState{ @@ -1003,15 +965,7 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) { var testCollateralAmount2 uint64 = 200000 testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - }, - }, - }, - }, + TxFee: testFee, }, WitnessSet: babbage.BabbageTransactionWitnessSet{ AlonzoTransactionWitnessSet: alonzo.AlonzoTransactionWitnessSet{ @@ -1426,10 +1380,8 @@ func TestUtxoValidateCollateralEqBalance(t *testing.T) { testTx := &babbage.BabbageTransaction{ Body: babbage.BabbageTransactionBody{ TxTotalCollateral: testTotalCollateral, - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - TxCollateral: []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput(testInputTxId, 0), - }, + TxCollateral: []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput(testInputTxId, 0), }, }, } diff --git a/ledger/common/tx.go b/ledger/common/tx.go index 20a19d61..93430278 100644 --- a/ledger/common/tx.go +++ b/ledger/common/tx.go @@ -93,3 +93,134 @@ type Utxo struct { Id TransactionInput Output TransactionOutput } + +// TransactionBodyBase provides a set of functions that return empty values to satisfy the +// TransactionBody interface. It also provides functionality for generating a transaction hash +// and storing/retrieving the original CiBOR +type TransactionBodyBase struct { + cbor.DecodeStoreCbor + hash *Blake2b256 +} + +func (b *TransactionBodyBase) Hash() Blake2b256 { + if b.hash == nil { + tmpHash := Blake2b256Hash(b.Cbor()) + b.hash = &tmpHash + } + return *b.hash +} + +func (b *TransactionBodyBase) Inputs() []TransactionInput { + return nil +} + +func (b *TransactionBodyBase) Outputs() []TransactionOutput { + return nil +} + +func (b *TransactionBodyBase) Fee() uint64 { + return 0 +} + +func (b *TransactionBodyBase) TTL() uint64 { + return 0 +} + +func (b *TransactionBodyBase) ValidityIntervalStart() uint64 { + return 0 +} + +func (b *TransactionBodyBase) ReferenceInputs() []TransactionInput { + return []TransactionInput{} +} + +func (b *TransactionBodyBase) Collateral() []TransactionInput { + return nil +} + +func (b *TransactionBodyBase) CollateralReturn() TransactionOutput { + return nil +} + +func (b *TransactionBodyBase) TotalCollateral() uint64 { + return 0 +} + +func (b *TransactionBodyBase) Certificates() []Certificate { + return nil +} + +func (b *TransactionBodyBase) Withdrawals() map[*Address]uint64 { + return nil +} + +func (b *TransactionBodyBase) AuxDataHash() *Blake2b256 { + return nil +} + +func (b *TransactionBodyBase) RequiredSigners() []Blake2b224 { + return nil +} + +func (b *TransactionBodyBase) AssetMint() *MultiAsset[MultiAssetTypeMint] { + return nil +} + +func (b *TransactionBodyBase) ScriptDataHash() *Blake2b256 { + return nil +} + +func (b *TransactionBodyBase) VotingProcedures() VotingProcedures { + return nil +} + +func (b *TransactionBodyBase) ProposalProcedures() []ProposalProcedure { + return nil +} + +func (b *TransactionBodyBase) CurrentTreasuryValue() int64 { + return 0 +} + +func (b *TransactionBodyBase) Donation() uint64 { + return 0 +} + +func (b *TransactionBodyBase) Utxorpc() *utxorpc.Tx { + return nil +} + +// TransactionBodyToUtxorpc is a common helper for converting TransactionBody to utxorpc.Tx +func TransactionBodyToUtxorpc(tx TransactionBody) *utxorpc.Tx { + txi := []*utxorpc.TxInput{} + txri := []*utxorpc.TxInput{} + txo := []*utxorpc.TxOutput{} + for _, i := range tx.Inputs() { + input := i.Utxorpc() + txi = append(txi, input) + } + for _, o := range tx.Outputs() { + output := o.Utxorpc() + txo = append(txo, output) + } + for _, ri := range tx.ReferenceInputs() { + input := ri.Utxorpc() + txri = append(txri, input) + } + ret := &utxorpc.Tx{ + Inputs: txi, + Outputs: txo, + // Certificates: tx.Certificates(), + // Withdrawals: tx.Withdrawals(), + // Mint: tx.Mint(), + ReferenceInputs: txri, + // Witnesses: tx.Witnesses(), + // Collateral: tx.Collateral(), + Fee: tx.Fee(), + // Successful: tx.Successful(), + // Auxiliary: tx.AuxData(), + // Validity: tx.Validity(), + Hash: tx.Hash().Bytes(), + } + return ret +} diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index 986495e1..8c942c5f 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -262,12 +262,32 @@ func (s *ConwayTransactionInputSet) SetItems( } type ConwayTransactionBody struct { - babbage.BabbageTransactionBody - TxInputs ConwayTransactionInputSet `cbor:"0,keyasint,omitempty"` - TxVotingProcedures common.VotingProcedures `cbor:"19,keyasint,omitempty"` - TxProposalProcedures []common.ProposalProcedure `cbor:"20,keyasint,omitempty"` - TxCurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"` - TxDonation uint64 `cbor:"22,keyasint,omitempty"` + common.TransactionBodyBase + TxInputs ConwayTransactionInputSet `cbor:"0,keyasint,omitempty"` + TxOutputs []babbage.BabbageTransactionOutput `cbor:"1,keyasint,omitempty"` + TxFee uint64 `cbor:"2,keyasint,omitempty"` + Ttl uint64 `cbor:"3,keyasint,omitempty"` + TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"` + TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"` + Update struct { + cbor.StructAsArray + ProtocolParamUpdates map[common.Blake2b224]babbage.BabbageProtocolParameterUpdate + Epoch uint64 + } `cbor:"6,keyasint,omitempty"` + TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"` + TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` + TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"` + TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"` + TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"` + TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"` + NetworkId uint8 `cbor:"15,keyasint,omitempty"` + TxCollateralReturn *babbage.BabbageTransactionOutput `cbor:"16,keyasint,omitempty"` + TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"` + TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"` + TxVotingProcedures common.VotingProcedures `cbor:"19,keyasint,omitempty"` + TxProposalProcedures []common.ProposalProcedure `cbor:"20,keyasint,omitempty"` + TxCurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"` + TxDonation uint64 `cbor:"22,keyasint,omitempty"` } func (b *ConwayTransactionBody) UnmarshalCBOR(cborData []byte) error { @@ -282,6 +302,26 @@ func (b *ConwayTransactionBody) Inputs() []common.TransactionInput { return ret } +func (b *ConwayTransactionBody) Outputs() []common.TransactionOutput { + ret := []common.TransactionOutput{} + for _, output := range b.TxOutputs { + ret = append(ret, &output) + } + return ret +} + +func (b *ConwayTransactionBody) Fee() uint64 { + return b.TxFee +} + +func (b *ConwayTransactionBody) TTL() uint64 { + return b.Ttl +} + +func (b *ConwayTransactionBody) ValidityIntervalStart() uint64 { + return b.TxValidityIntervalStart +} + func (b *ConwayTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) { updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate) for k, v := range b.Update.ProtocolParamUpdates { @@ -290,6 +330,64 @@ func (b *ConwayTransactionBody) ProtocolParameterUpdates() (uint64, map[common.B return b.Update.Epoch, updateMap } +func (b *ConwayTransactionBody) Certificates() []common.Certificate { + ret := make([]common.Certificate, len(b.TxCertificates)) + for i, cert := range b.TxCertificates { + ret[i] = cert.Certificate + } + return ret +} + +func (b *ConwayTransactionBody) Withdrawals() map[*common.Address]uint64 { + return b.TxWithdrawals +} + +func (b *ConwayTransactionBody) AuxDataHash() *common.Blake2b256 { + return b.TxAuxDataHash +} + +func (b *ConwayTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] { + return b.TxMint +} + +func (b *ConwayTransactionBody) Collateral() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, collateral := range b.TxCollateral { + ret = append(ret, collateral) + } + return ret +} + +func (b *ConwayTransactionBody) RequiredSigners() []common.Blake2b224 { + return b.TxRequiredSigners[:] +} + +func (b *ConwayTransactionBody) ScriptDataHash() *common.Blake2b256 { + return b.TxScriptDataHash +} + +func (b *ConwayTransactionBody) ReferenceInputs() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, input := range b.TxReferenceInputs { + ret = append(ret, &input) + } + return ret +} + +func (b *ConwayTransactionBody) CollateralReturn() common.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 +} + +func (b *ConwayTransactionBody) TotalCollateral() uint64 { + return b.TxTotalCollateral +} + func (b *ConwayTransactionBody) VotingProcedures() common.VotingProcedures { return b.TxVotingProcedures } @@ -306,6 +404,10 @@ func (b *ConwayTransactionBody) Donation() uint64 { return b.TxDonation } +func (b *ConwayTransactionBody) Utxorpc() *utxorpc.Tx { + return common.TransactionBodyToUtxorpc(b) +} + type ConwayTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor diff --git a/ledger/conway/rules_test.go b/ledger/conway/rules_test.go index a87c4d8e..aed7df32 100644 --- a/ledger/conway/rules_test.go +++ b/ledger/conway/rules_test.go @@ -37,15 +37,7 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { var testZeroSlot uint64 = 0 testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - TxValidityIntervalStart: testSlot, - }, - }, - }, - }, + TxValidityIntervalStart: testSlot, }, } testLedgerState := test.MockLedgerState{} @@ -212,17 +204,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) { testTxCbor, _ := hex.DecodeString("abcdef01") testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testExactFee, - }, - }, - }, - }, - }, + TxFee: testExactFee, }, } testTx.SetCbor(testTxCbor) @@ -387,12 +369,10 @@ func TestUtxoValidateWrongNetwork(t *testing.T) { ) testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxOutputs: []babbage.BabbageTransactionOutput{ - { - OutputAmount: mary.MaryTransactionOutputValue{ - Amount: 123456, - }, + TxOutputs: []babbage.BabbageTransactionOutput{ + { + OutputAmount: mary.MaryTransactionOutputValue{ + Amount: 123456, }, }, }, @@ -461,17 +441,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) { ) testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxWithdrawals: map[*common.Address]uint64{}, - }, - }, - }, - }, - }, + TxWithdrawals: map[*common.Address]uint64{}, }, } testLedgerState := test.MockLedgerState{ @@ -543,21 +513,11 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) { shelley.NewShelleyTransactionInput(testInputTxId, 0), }, ), - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxOutputs: []babbage.BabbageTransactionOutput{ - // Empty placeholder output - {}, - }, - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - }, - }, - }, - }, + TxOutputs: []babbage.BabbageTransactionOutput{ + // Empty placeholder output + {}, }, + TxFee: testFee, }, } testLedgerState := test.MockLedgerState{ @@ -710,11 +670,9 @@ func TestUtxoValidateOutputTooSmallUtxo(t *testing.T) { var testOutputAmountBad uint64 = 123 testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxOutputs: []babbage.BabbageTransactionOutput{ - // Empty placeholder output - {}, - }, + TxOutputs: []babbage.BabbageTransactionOutput{ + // Empty placeholder output + {}, }, }, } @@ -801,11 +759,9 @@ func TestUtxoValidateOutputTooBigUtxo(t *testing.T) { } testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxOutputs: []babbage.BabbageTransactionOutput{ - // Empty placeholder output - {}, - }, + TxOutputs: []babbage.BabbageTransactionOutput{ + // Empty placeholder output + {}, }, }, } @@ -886,11 +842,9 @@ func TestUtxoValidateOutputBootAddrAttrsTooBig(t *testing.T) { ) testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxOutputs: []babbage.BabbageTransactionOutput{ - // Empty placeholder - {}, - }, + TxOutputs: []babbage.BabbageTransactionOutput{ + // Empty placeholder + {}, }, }, } @@ -1009,17 +963,7 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) { var testCollateralAmount2 uint64 = 200000 testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - MaryTransactionBody: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - }, - }, - }, - }, - }, + TxFee: testFee, }, WitnessSet: conway.ConwayTransactionWitnessSet{ WsRedeemers: conway.ConwayRedeemers{ @@ -1109,9 +1053,7 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) { var testCollateralAmount uint64 = 100000 testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxTotalCollateral: testCollateralAmount, - }, + TxTotalCollateral: testCollateralAmount, }, WitnessSet: conway.ConwayTransactionWitnessSet{ WsRedeemers: conway.ConwayRedeemers{ @@ -1510,13 +1452,9 @@ func TestUtxoValidateCollateralEqBalance(t *testing.T) { var testCollateralReturnAmountBad uint64 = 16_000_000 testTx := &conway.ConwayTransaction{ Body: conway.ConwayTransactionBody{ - BabbageTransactionBody: babbage.BabbageTransactionBody{ - TxTotalCollateral: testTotalCollateral, - AlonzoTransactionBody: alonzo.AlonzoTransactionBody{ - TxCollateral: []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput(testInputTxId, 0), - }, - }, + TxTotalCollateral: testTotalCollateral, + TxCollateral: []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput(testInputTxId, 0), }, }, } diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index 713343bc..a3297a9f 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -19,7 +19,6 @@ import ( "fmt" "github.com/blinklabs-io/gouroboros/cbor" - "github.com/blinklabs-io/gouroboros/ledger/allegra" "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/shelley" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" @@ -137,20 +136,35 @@ func (h *MaryBlockHeader) Era() common.Era { } type MaryTransactionBody struct { - allegra.AllegraTransactionBody - Update struct { + common.TransactionBodyBase + TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"` + TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"` + TxFee uint64 `cbor:"2,keyasint,omitempty"` + Ttl uint64 `cbor:"3,keyasint,omitempty"` + TxCertificates []common.CertificateWrapper `cbor:"4,keyasint,omitempty"` + TxWithdrawals map[*common.Address]uint64 `cbor:"5,keyasint,omitempty"` + Update struct { cbor.StructAsArray ProtocolParamUpdates map[common.Blake2b224]MaryProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"` - TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"` + TxAuxDataHash *common.Blake2b256 `cbor:"7,keyasint,omitempty"` + TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` + TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"` } func (b *MaryTransactionBody) UnmarshalCBOR(cborData []byte) error { return b.UnmarshalCbor(cborData, b) } +func (b *MaryTransactionBody) Inputs() []common.TransactionInput { + ret := []common.TransactionInput{} + for _, input := range b.TxInputs.Items() { + ret = append(ret, input) + } + return ret +} + func (b *MaryTransactionBody) Outputs() []common.TransactionOutput { ret := []common.TransactionOutput{} for _, output := range b.TxOutputs { @@ -159,6 +173,18 @@ func (b *MaryTransactionBody) Outputs() []common.TransactionOutput { return ret } +func (b *MaryTransactionBody) Fee() uint64 { + return b.TxFee +} + +func (b *MaryTransactionBody) TTL() uint64 { + return b.Ttl +} + +func (b *MaryTransactionBody) ValidityIntervalStart() uint64 { + return b.TxValidityIntervalStart +} + func (b *MaryTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) { updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate) for k, v := range b.Update.ProtocolParamUpdates { @@ -167,10 +193,30 @@ func (b *MaryTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Bla return b.Update.Epoch, updateMap } +func (b *MaryTransactionBody) Certificates() []common.Certificate { + ret := make([]common.Certificate, len(b.TxCertificates)) + for i, cert := range b.TxCertificates { + ret[i] = cert.Certificate + } + return ret +} + +func (b *MaryTransactionBody) Withdrawals() map[*common.Address]uint64 { + return b.TxWithdrawals +} + +func (b *MaryTransactionBody) AuxDataHash() *common.Blake2b256 { + return b.TxAuxDataHash +} + func (b *MaryTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] { return b.TxMint } +func (b *MaryTransactionBody) Utxorpc() *utxorpc.Tx { + return common.TransactionBodyToUtxorpc(b) +} + type MaryTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor diff --git a/ledger/mary/rules_test.go b/ledger/mary/rules_test.go index aed97af1..c0271a03 100644 --- a/ledger/mary/rules_test.go +++ b/ledger/mary/rules_test.go @@ -34,9 +34,7 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { var testZeroSlot uint64 = 0 testTx := &mary.MaryTransaction{ Body: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - TxValidityIntervalStart: testSlot, - }, + TxValidityIntervalStart: testSlot, }, } testLedgerState := test.MockLedgerState{} @@ -135,16 +133,12 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) { func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) { testTx := &mary.MaryTransaction{ Body: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxInputs: shelley.NewShelleyTransactionInputSet( - // Non-empty input set - []shelley.ShelleyTransactionInput{ - {}, - }, - ), + TxInputs: shelley.NewShelleyTransactionInputSet( + // Non-empty input set + []shelley.ShelleyTransactionInput{ + {}, }, - }, + ), }, } testLedgerState := test.MockLedgerState{} @@ -206,11 +200,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) { testTxCbor, _ := hex.DecodeString("abcdef") testTx := &mary.MaryTransaction{ Body: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testExactFee, - }, - }, + TxFee: testExactFee, }, } testTx.SetCbor(testTxCbor) @@ -451,11 +441,7 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) { ) testTx := &mary.MaryTransaction{ Body: mary.MaryTransactionBody{ - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxWithdrawals: map[*common.Address]uint64{}, - }, - }, + TxWithdrawals: map[*common.Address]uint64{}, }, } testLedgerState := test.MockLedgerState{ @@ -526,19 +512,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) { // Empty placeholder output {}, }, - AllegraTransactionBody: allegra.AllegraTransactionBody{ - ShelleyTransactionBody: shelley.ShelleyTransactionBody{ - TxFee: testFee, - TxInputs: shelley.NewShelleyTransactionInputSet( - []shelley.ShelleyTransactionInput{ - shelley.NewShelleyTransactionInput( - testInputTxId, - 0, - ), - }, + TxFee: testFee, + TxInputs: shelley.NewShelleyTransactionInputSet( + []shelley.ShelleyTransactionInput{ + shelley.NewShelleyTransactionInput( + testInputTxId, + 0, ), }, - }, + ), }, } testLedgerState := test.MockLedgerState{ diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index aa1222a2..744afd85 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -191,8 +191,7 @@ func (h *ShelleyBlockHeader) Era() common.Era { } type ShelleyTransactionBody struct { - cbor.DecodeStoreCbor - hash *common.Blake2b256 + common.TransactionBodyBase TxInputs ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"` TxOutputs []ShelleyTransactionOutput `cbor:"1,keyasint,omitempty"` TxFee uint64 `cbor:"2,keyasint,omitempty"` @@ -211,14 +210,6 @@ func (b *ShelleyTransactionBody) UnmarshalCBOR(cborData []byte) error { return b.UnmarshalCbor(cborData, b) } -func (b *ShelleyTransactionBody) Hash() common.Blake2b256 { - if b.hash == nil { - tmpHash := common.Blake2b256Hash(b.Cbor()) - b.hash = &tmpHash - } - return *b.hash -} - func (b *ShelleyTransactionBody) Inputs() []common.TransactionInput { ret := []common.TransactionInput{} for _, input := range b.TxInputs.Items() { @@ -243,11 +234,6 @@ func (b *ShelleyTransactionBody) TTL() uint64 { return b.Ttl } -func (b *ShelleyTransactionBody) ValidityIntervalStart() uint64 { - // No validity interval start in Shelley - return 0 -} - func (b *ShelleyTransactionBody) ProtocolParameterUpdates() (uint64, map[common.Blake2b224]common.ProtocolParameterUpdate) { updateMap := make(map[common.Blake2b224]common.ProtocolParameterUpdate) for k, v := range b.Update.ProtocolParamUpdates { @@ -256,25 +242,6 @@ func (b *ShelleyTransactionBody) ProtocolParameterUpdates() (uint64, map[common. return b.Update.Epoch, updateMap } -func (b *ShelleyTransactionBody) ReferenceInputs() []common.TransactionInput { - return []common.TransactionInput{} -} - -func (b *ShelleyTransactionBody) Collateral() []common.TransactionInput { - // No collateral in Shelley - return nil -} - -func (b *ShelleyTransactionBody) CollateralReturn() common.TransactionOutput { - // No collateral in Shelley - return nil -} - -func (b *ShelleyTransactionBody) TotalCollateral() uint64 { - // No collateral in Shelley - return 0 -} - func (b *ShelleyTransactionBody) Certificates() []common.Certificate { ret := make([]common.Certificate, len(b.TxCertificates)) for i, cert := range b.TxCertificates { @@ -291,65 +258,8 @@ func (b *ShelleyTransactionBody) AuxDataHash() *common.Blake2b256 { return b.TxAuxDataHash } -func (b *ShelleyTransactionBody) RequiredSigners() []common.Blake2b224 { - // No required signers in Shelley - return nil -} - -func (b *ShelleyTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] { - // No asset minting in Shelley - return nil -} - -func (b *ShelleyTransactionBody) ScriptDataHash() *common.Blake2b256 { - // No script data hash in Shelley - return nil -} - -func (b *ShelleyTransactionBody) VotingProcedures() common.VotingProcedures { - // No voting procedures in Shelley - return nil -} - -func (b *ShelleyTransactionBody) ProposalProcedures() []common.ProposalProcedure { - // No proposal procedures in Shelley - return nil -} - -func (b *ShelleyTransactionBody) CurrentTreasuryValue() int64 { - // No current treasury value in Shelley - return 0 -} - -func (b *ShelleyTransactionBody) Donation() uint64 { - // No donation in Shelley - return 0 -} - func (b *ShelleyTransactionBody) Utxorpc() *utxorpc.Tx { - txi := []*utxorpc.TxInput{} - txo := []*utxorpc.TxOutput{} - for _, i := range b.Inputs() { - input := i.Utxorpc() - txi = append(txi, input) - } - for _, o := range b.Outputs() { - output := o.Utxorpc() - txo = append(txo, output) - } - tx := &utxorpc.Tx{ - Inputs: txi, - Outputs: txo, - // Certificates: b.Certificates(), - // Withdrawals: b.Withdrawals(), - // Witnesses: b.Witnesses(), - Fee: b.Fee(), - // Validity: b.Validity(), - // Successful: b.Successful(), - // Auxiliary: b.AuxData(), - Hash: b.Hash().Bytes(), - } - return tx + return common.TransactionBodyToUtxorpc(b) } type ShelleyTransactionInputSet struct {