diff --git a/ledger/allegra.go b/ledger/allegra/allegra.go similarity index 76% rename from ledger/allegra.go rename to ledger/allegra/allegra.go index 7a738751..1c91ce78 100644 --- a/ledger/allegra.go +++ b/ledger/allegra/allegra.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ledger +package allegra import ( "encoding/hex" @@ -20,6 +20,7 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/shelley" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -51,7 +52,7 @@ type AllegraBlock struct { cbor.DecodeStoreCbor Header *AllegraBlockHeader TransactionBodies []AllegraTransactionBody - TransactionWitnessSets []ShelleyTransactionWitnessSet + TransactionWitnessSets []shelley.ShelleyTransactionWitnessSet TransactionMetadataSet map[uint]*cbor.LazyValue } @@ -71,7 +72,7 @@ func (b *AllegraBlock) SlotNumber() uint64 { return b.Header.SlotNumber() } -func (b *AllegraBlock) IssuerVkey() IssuerVkey { +func (b *AllegraBlock) IssuerVkey() common.IssuerVkey { return b.Header.IssuerVkey() } @@ -79,12 +80,12 @@ func (b *AllegraBlock) BlockBodySize() uint64 { return b.Header.BlockBodySize() } -func (b *AllegraBlock) Era() Era { +func (b *AllegraBlock) Era() common.Era { return EraAllegra } -func (b *AllegraBlock) Transactions() []Transaction { - ret := make([]Transaction, len(b.TransactionBodies)) +func (b *AllegraBlock) Transactions() []common.Transaction { + ret := make([]common.Transaction, len(b.TransactionBodies)) for idx := range b.TransactionBodies { ret[idx] = &AllegraTransaction{ Body: b.TransactionBodies[idx], @@ -118,18 +119,18 @@ func (b *AllegraBlock) Utxorpc() *utxorpc.Block { } type AllegraBlockHeader struct { - ShelleyBlockHeader + shelley.ShelleyBlockHeader } -func (h *AllegraBlockHeader) Era() Era { +func (h *AllegraBlockHeader) Era() common.Era { return EraAllegra } type AllegraTransactionBody struct { - ShelleyTransactionBody + shelley.ShelleyTransactionBody Update struct { cbor.StructAsArray - ProtocolParamUpdates map[Blake2b224]AllegraProtocolParameterUpdate + ProtocolParamUpdates map[common.Blake2b224]AllegraProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"` @@ -143,8 +144,8 @@ func (b *AllegraTransactionBody) ValidityIntervalStart() uint64 { return b.TxValidityIntervalStart } -func (b *AllegraTransactionBody) ProtocolParametersUpdate() map[Blake2b224]any { - updateMap := make(map[Blake2b224]any) +func (b *AllegraTransactionBody) ProtocolParametersUpdate() map[common.Blake2b224]any { + updateMap := make(map[common.Blake2b224]any) for k, v := range b.Update.ProtocolParamUpdates { updateMap[k] = v } @@ -155,7 +156,7 @@ type AllegraTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor Body AllegraTransactionBody - WitnessSet ShelleyTransactionWitnessSet + WitnessSet shelley.ShelleyTransactionWitnessSet TxMetadata *cbor.LazyValue } @@ -163,11 +164,11 @@ func (t AllegraTransaction) Hash() string { return t.Body.Hash() } -func (t AllegraTransaction) Inputs() []TransactionInput { +func (t AllegraTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } -func (t AllegraTransaction) Outputs() []TransactionOutput { +func (t AllegraTransaction) Outputs() []common.TransactionOutput { return t.Body.Outputs() } @@ -183,15 +184,15 @@ func (t AllegraTransaction) ValidityIntervalStart() uint64 { return t.Body.ValidityIntervalStart() } -func (t AllegraTransaction) ReferenceInputs() []TransactionInput { +func (t AllegraTransaction) ReferenceInputs() []common.TransactionInput { return t.Body.ReferenceInputs() } -func (t AllegraTransaction) Collateral() []TransactionInput { +func (t AllegraTransaction) Collateral() []common.TransactionInput { return t.Body.Collateral() } -func (t AllegraTransaction) CollateralReturn() TransactionOutput { +func (t AllegraTransaction) CollateralReturn() common.TransactionOutput { return t.Body.CollateralReturn() } @@ -199,19 +200,19 @@ func (t AllegraTransaction) TotalCollateral() uint64 { return t.Body.TotalCollateral() } -func (t AllegraTransaction) Certificates() []Certificate { +func (t AllegraTransaction) Certificates() []common.Certificate { return t.Body.Certificates() } -func (t AllegraTransaction) Withdrawals() map[*Address]uint64 { +func (t AllegraTransaction) Withdrawals() map[*common.Address]uint64 { return t.Body.Withdrawals() } -func (t AllegraTransaction) AuxDataHash() *Blake2b256 { +func (t AllegraTransaction) AuxDataHash() *common.Blake2b256 { return t.Body.AuxDataHash() } -func (t AllegraTransaction) RequiredSigners() []Blake2b224 { +func (t AllegraTransaction) RequiredSigners() []common.Blake2b224 { return t.Body.RequiredSigners() } @@ -219,15 +220,15 @@ func (t AllegraTransaction) AssetMint() *common.MultiAsset[common.MultiAssetType return t.Body.AssetMint() } -func (t AllegraTransaction) ScriptDataHash() *Blake2b256 { +func (t AllegraTransaction) ScriptDataHash() *common.Blake2b256 { return t.Body.ScriptDataHash() } -func (t AllegraTransaction) VotingProcedures() VotingProcedures { +func (t AllegraTransaction) VotingProcedures() common.VotingProcedures { return t.Body.VotingProcedures() } -func (t AllegraTransaction) ProposalProcedures() []ProposalProcedure { +func (t AllegraTransaction) ProposalProcedures() []common.ProposalProcedure { return t.Body.ProposalProcedures() } @@ -251,17 +252,17 @@ func (t AllegraTransaction) IsValid() bool { return true } -func (t AllegraTransaction) Consumed() []TransactionInput { +func (t AllegraTransaction) Consumed() []common.TransactionInput { return t.Inputs() } -func (t AllegraTransaction) Produced() []Utxo { - var ret []Utxo +func (t AllegraTransaction) Produced() []common.Utxo { + var ret []common.Utxo for idx, output := range t.Outputs() { ret = append( ret, - Utxo{ - Id: NewShelleyTransactionInput(t.Hash(), idx), + common.Utxo{ + Id: shelley.NewShelleyTransactionInput(t.Hash(), idx), Output: output, }, ) @@ -269,7 +270,7 @@ func (t AllegraTransaction) Produced() []Utxo { return ret } -func (t AllegraTransaction) ProtocolParametersUpdate() map[Blake2b224]any { +func (t AllegraTransaction) ProtocolParametersUpdate() map[common.Blake2b224]any { return t.Body.ProtocolParametersUpdate() } @@ -300,11 +301,11 @@ func (t *AllegraTransaction) Cbor() []byte { } type AllegraProtocolParameters struct { - ShelleyProtocolParameters + shelley.ShelleyProtocolParameters } type AllegraProtocolParameterUpdate struct { - ShelleyProtocolParameterUpdate + shelley.ShelleyProtocolParameterUpdate } func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) { diff --git a/ledger/alonzo.go b/ledger/alonzo/alonzo.go similarity index 77% rename from ledger/alonzo.go rename to ledger/alonzo/alonzo.go index 71bd84f1..028a2035 100644 --- a/ledger/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ledger +package alonzo import ( "encoding/hex" @@ -21,6 +21,8 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -73,7 +75,7 @@ func (b *AlonzoBlock) SlotNumber() uint64 { return b.Header.SlotNumber() } -func (b *AlonzoBlock) IssuerVkey() IssuerVkey { +func (b *AlonzoBlock) IssuerVkey() common.IssuerVkey { return b.Header.IssuerVkey() } @@ -81,17 +83,17 @@ func (b *AlonzoBlock) BlockBodySize() uint64 { return b.Header.BlockBodySize() } -func (b *AlonzoBlock) Era() Era { +func (b *AlonzoBlock) Era() common.Era { return EraAlonzo } -func (b *AlonzoBlock) Transactions() []Transaction { +func (b *AlonzoBlock) Transactions() []common.Transaction { invalidTxMap := make(map[uint]bool, len(b.InvalidTransactions)) for _, invalidTxIdx := range b.InvalidTransactions { invalidTxMap[invalidTxIdx] = true } - ret := make([]Transaction, len(b.TransactionBodies)) + ret := make([]common.Transaction, len(b.TransactionBodies)) for idx := range b.TransactionBodies { ret[idx] = &AlonzoTransaction{ Body: b.TransactionBodies[idx], @@ -126,33 +128,33 @@ func (b *AlonzoBlock) Utxorpc() *utxorpc.Block { } type AlonzoBlockHeader struct { - ShelleyBlockHeader + shelley.ShelleyBlockHeader } -func (h *AlonzoBlockHeader) Era() Era { +func (h *AlonzoBlockHeader) Era() common.Era { return EraAlonzo } type AlonzoTransactionBody struct { - MaryTransactionBody + mary.MaryTransactionBody TxOutputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"` Update struct { cbor.StructAsArray - ProtocolParamUpdates map[Blake2b224]AlonzoProtocolParameterUpdate + ProtocolParamUpdates map[common.Blake2b224]AlonzoProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxScriptDataHash *Blake2b256 `cbor:"11,keyasint,omitempty"` - TxCollateral []ShelleyTransactionInput `cbor:"13,keyasint,omitempty"` - TxRequiredSigners []Blake2b224 `cbor:"14,keyasint,omitempty"` - NetworkId uint8 `cbor:"15,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) Outputs() []TransactionOutput { - ret := []TransactionOutput{} +func (b *AlonzoTransactionBody) Outputs() []common.TransactionOutput { + ret := []common.TransactionOutput{} for _, output := range b.TxOutputs { output := output ret = append(ret, &output) @@ -160,46 +162,46 @@ func (b *AlonzoTransactionBody) Outputs() []TransactionOutput { return ret } -func (b *AlonzoTransactionBody) ProtocolParametersUpdate() map[Blake2b224]any { - updateMap := make(map[Blake2b224]any) +func (b *AlonzoTransactionBody) ProtocolParametersUpdate() map[common.Blake2b224]any { + updateMap := make(map[common.Blake2b224]any) for k, v := range b.Update.ProtocolParamUpdates { updateMap[k] = v } return updateMap } -func (b *AlonzoTransactionBody) Collateral() []TransactionInput { - ret := []TransactionInput{} +func (b *AlonzoTransactionBody) Collateral() []common.TransactionInput { + ret := []common.TransactionInput{} for _, collateral := range b.TxCollateral { ret = append(ret, collateral) } return ret } -func (b *AlonzoTransactionBody) RequiredSigners() []Blake2b224 { +func (b *AlonzoTransactionBody) RequiredSigners() []common.Blake2b224 { return b.TxRequiredSigners[:] } -func (b *AlonzoTransactionBody) ScriptDataHash() *Blake2b256 { +func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 { return b.TxScriptDataHash } type AlonzoTransactionOutput struct { cbor.StructAsArray cbor.DecodeStoreCbor - OutputAddress Address - OutputAmount MaryTransactionOutputValue - TxOutputDatumHash *Blake2b256 + OutputAddress common.Address + OutputAmount mary.MaryTransactionOutputValue + TxOutputDatumHash *common.Blake2b256 legacyOutput bool } func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error { // Save original CBOR o.SetCbor(cborData) - // Try to parse as legacy Mary output first - var tmpOutput MaryTransactionOutput + // Try to parse as legacy mary.Mary output first + var tmpOutput mary.MaryTransactionOutput if _, err := cbor.Decode(cborData, &tmpOutput); err == nil { - // Copy from temp Mary output to Alonzo format + // Copy from temp mary.Mary output to Alonzo format o.OutputAddress = tmpOutput.OutputAddress o.OutputAmount = tmpOutput.OutputAmount o.legacyOutput = true @@ -211,7 +213,7 @@ func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error { func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) { if o.legacyOutput { - tmpOutput := MaryTransactionOutput{ + tmpOutput := mary.MaryTransactionOutput{ OutputAddress: o.OutputAddress, OutputAmount: o.OutputAmount, } @@ -222,7 +224,7 @@ func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) { func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) { tmpObj := struct { - Address Address `json:"address"` + Address common.Address `json:"address"` Amount uint64 `json:"amount"` Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"` DatumHash string `json:"datumHash,omitempty"` @@ -237,7 +239,7 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) { return json.Marshal(&tmpObj) } -func (o AlonzoTransactionOutput) Address() Address { +func (o AlonzoTransactionOutput) Address() common.Address { return o.OutputAddress } @@ -249,7 +251,7 @@ func (o AlonzoTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTy return o.OutputAmount.Assets } -func (o AlonzoTransactionOutput) DatumHash() *Blake2b256 { +func (o AlonzoTransactionOutput) DatumHash() *common.Blake2b256 { return o.TxOutputDatumHash } @@ -292,17 +294,11 @@ type AlonzoRedeemer struct { Tag uint8 Index uint32 Data cbor.RawMessage - ExUnits RedeemerExUnits -} - -type RedeemerExUnits struct { - cbor.StructAsArray - Memory uint64 - Steps uint64 + ExUnits common.RedeemerExUnits } type AlonzoTransactionWitnessSet struct { - ShelleyTransactionWitnessSet + shelley.ShelleyTransactionWitnessSet PlutusScripts []cbor.RawMessage `cbor:"3,keyasint,omitempty"` PlutusData []cbor.RawMessage `cbor:"4,keyasint,omitempty"` Redeemers []AlonzoRedeemer `cbor:"5,keyasint,omitempty"` @@ -325,11 +321,11 @@ func (t AlonzoTransaction) Hash() string { return t.Body.Hash() } -func (t AlonzoTransaction) Inputs() []TransactionInput { +func (t AlonzoTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } -func (t AlonzoTransaction) Outputs() []TransactionOutput { +func (t AlonzoTransaction) Outputs() []common.TransactionOutput { return t.Body.Outputs() } @@ -345,19 +341,19 @@ func (t AlonzoTransaction) ValidityIntervalStart() uint64 { return t.Body.ValidityIntervalStart() } -func (t AlonzoTransaction) ProtocolParametersUpdate() map[Blake2b224]any { +func (t AlonzoTransaction) ProtocolParametersUpdate() map[common.Blake2b224]any { return t.Body.ProtocolParametersUpdate() } -func (t AlonzoTransaction) ReferenceInputs() []TransactionInput { +func (t AlonzoTransaction) ReferenceInputs() []common.TransactionInput { return t.Body.ReferenceInputs() } -func (t AlonzoTransaction) Collateral() []TransactionInput { +func (t AlonzoTransaction) Collateral() []common.TransactionInput { return t.Body.Collateral() } -func (t AlonzoTransaction) CollateralReturn() TransactionOutput { +func (t AlonzoTransaction) CollateralReturn() common.TransactionOutput { return t.Body.CollateralReturn() } @@ -365,19 +361,19 @@ func (t AlonzoTransaction) TotalCollateral() uint64 { return t.Body.TotalCollateral() } -func (t AlonzoTransaction) Certificates() []Certificate { +func (t AlonzoTransaction) Certificates() []common.Certificate { return t.Body.Certificates() } -func (t AlonzoTransaction) Withdrawals() map[*Address]uint64 { +func (t AlonzoTransaction) Withdrawals() map[*common.Address]uint64 { return t.Body.Withdrawals() } -func (t AlonzoTransaction) AuxDataHash() *Blake2b256 { +func (t AlonzoTransaction) AuxDataHash() *common.Blake2b256 { return t.Body.AuxDataHash() } -func (t AlonzoTransaction) RequiredSigners() []Blake2b224 { +func (t AlonzoTransaction) RequiredSigners() []common.Blake2b224 { return t.Body.RequiredSigners() } @@ -385,15 +381,15 @@ func (t AlonzoTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeM return t.Body.AssetMint() } -func (t AlonzoTransaction) ScriptDataHash() *Blake2b256 { +func (t AlonzoTransaction) ScriptDataHash() *common.Blake2b256 { return t.Body.ScriptDataHash() } -func (t AlonzoTransaction) VotingProcedures() VotingProcedures { +func (t AlonzoTransaction) VotingProcedures() common.VotingProcedures { return t.Body.VotingProcedures() } -func (t AlonzoTransaction) ProposalProcedures() []ProposalProcedure { +func (t AlonzoTransaction) ProposalProcedures() []common.ProposalProcedure { return t.Body.ProposalProcedures() } @@ -413,7 +409,7 @@ func (t AlonzoTransaction) IsValid() bool { return t.IsTxValid } -func (t AlonzoTransaction) Consumed() []TransactionInput { +func (t AlonzoTransaction) Consumed() []common.TransactionInput { if t.IsValid() { return t.Inputs() } else { @@ -421,14 +417,14 @@ func (t AlonzoTransaction) Consumed() []TransactionInput { } } -func (t AlonzoTransaction) Produced() []Utxo { +func (t AlonzoTransaction) Produced() []common.Utxo { if t.IsValid() { - var ret []Utxo + var ret []common.Utxo for idx, output := range t.Outputs() { ret = append( ret, - Utxo{ - Id: NewShelleyTransactionInput(t.Hash(), idx), + common.Utxo{ + Id: shelley.NewShelleyTransactionInput(t.Hash(), idx), Output: output, }, ) @@ -436,7 +432,7 @@ func (t AlonzoTransaction) Produced() []Utxo { return ret } else { // No collateral return in Alonzo - return []Utxo{} + return []common.Utxo{} } } @@ -484,7 +480,7 @@ type ExUnitPrice struct { } type AlonzoProtocolParameters struct { - MaryProtocolParameters + mary.MaryProtocolParameters MinPoolCost uint AdaPerUtxoByte uint CostModels uint @@ -497,7 +493,7 @@ type AlonzoProtocolParameters struct { } type AlonzoProtocolParameterUpdate struct { - MaryProtocolParameterUpdate + mary.MaryProtocolParameterUpdate MinPoolCost uint `cbor:"16,keyasint"` AdaPerUtxoByte uint `cbor:"17,keyasint"` CostModels map[uint][]uint `cbor:"18,keyasint"` diff --git a/ledger/babbage.go b/ledger/babbage/babbage.go similarity index 83% rename from ledger/babbage.go rename to ledger/babbage/babbage.go index 5b74f516..6ce818d2 100644 --- a/ledger/babbage.go +++ b/ledger/babbage/babbage.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ledger +package babbage import ( "encoding/hex" @@ -20,7 +20,10 @@ import ( "fmt" "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/alonzo" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -73,7 +76,7 @@ func (b *BabbageBlock) SlotNumber() uint64 { return b.Header.SlotNumber() } -func (b *BabbageBlock) IssuerVkey() IssuerVkey { +func (b *BabbageBlock) IssuerVkey() common.IssuerVkey { return b.Header.IssuerVkey() } @@ -81,17 +84,17 @@ func (b *BabbageBlock) BlockBodySize() uint64 { return b.Header.BlockBodySize() } -func (b *BabbageBlock) Era() Era { +func (b *BabbageBlock) Era() common.Era { return EraBabbage } -func (b *BabbageBlock) Transactions() []Transaction { +func (b *BabbageBlock) Transactions() []common.Transaction { invalidTxMap := make(map[uint]bool, len(b.InvalidTransactions)) for _, invalidTxIdx := range b.InvalidTransactions { invalidTxMap[invalidTxIdx] = true } - ret := make([]Transaction, len(b.TransactionBodies)) + ret := make([]common.Transaction, len(b.TransactionBodies)) for idx := range b.TransactionBodies { ret[idx] = &BabbageTransaction{ Body: b.TransactionBodies[idx], @@ -133,12 +136,12 @@ type BabbageBlockHeader struct { cbor.StructAsArray BlockNumber uint64 Slot uint64 - PrevHash Blake2b256 - IssuerVkey IssuerVkey + PrevHash common.Blake2b256 + IssuerVkey common.IssuerVkey VrfKey []byte VrfResult interface{} BlockBodySize uint64 - BlockBodyHash Blake2b256 + BlockBodyHash common.Blake2b256 OpCert struct { cbor.StructAsArray HotVkey []byte @@ -161,7 +164,8 @@ func (h *BabbageBlockHeader) UnmarshalCBOR(cborData []byte) error { func (h *BabbageBlockHeader) Hash() string { if h.hash == "" { - h.hash = generateBlockHeaderHash(h.Cbor(), nil) + tmpHash := common.Blake2b256Hash(h.Cbor()) + h.hash = hex.EncodeToString(tmpHash.Bytes()) } return h.hash } @@ -174,7 +178,7 @@ func (h *BabbageBlockHeader) SlotNumber() uint64 { return h.Body.Slot } -func (h *BabbageBlockHeader) IssuerVkey() IssuerVkey { +func (h *BabbageBlockHeader) IssuerVkey() common.IssuerVkey { return h.Body.IssuerVkey } @@ -182,29 +186,29 @@ func (h *BabbageBlockHeader) BlockBodySize() uint64 { return h.Body.BlockBodySize } -func (h *BabbageBlockHeader) Era() Era { +func (h *BabbageBlockHeader) Era() common.Era { return EraBabbage } type BabbageTransactionBody struct { - AlonzoTransactionBody + alonzo.AlonzoTransactionBody TxOutputs []BabbageTransactionOutput `cbor:"1,keyasint,omitempty"` Update struct { cbor.StructAsArray - ProtocolParamUpdates map[Blake2b224]BabbageProtocolParameterUpdate + ProtocolParamUpdates map[common.Blake2b224]BabbageProtocolParameterUpdate Epoch uint64 } `cbor:"6,keyasint,omitempty"` - TxCollateralReturn *BabbageTransactionOutput `cbor:"16,keyasint,omitempty"` - TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"` - TxReferenceInputs []ShelleyTransactionInput `cbor:"18,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) Outputs() []TransactionOutput { - ret := []TransactionOutput{} +func (b *BabbageTransactionBody) Outputs() []common.TransactionOutput { + ret := []common.TransactionOutput{} for _, output := range b.TxOutputs { output := output ret = append(ret, &output) @@ -212,16 +216,16 @@ func (b *BabbageTransactionBody) Outputs() []TransactionOutput { return ret } -func (b *BabbageTransactionBody) ProtocolParametersUpdate() map[Blake2b224]any { - updateMap := make(map[Blake2b224]any) +func (b *BabbageTransactionBody) ProtocolParametersUpdate() map[common.Blake2b224]any { + updateMap := make(map[common.Blake2b224]any) for k, v := range b.Update.ProtocolParamUpdates { updateMap[k] = v } return updateMap } -func (b *BabbageTransactionBody) ReferenceInputs() []TransactionInput { - ret := []TransactionInput{} +func (b *BabbageTransactionBody) ReferenceInputs() []common.TransactionInput { + ret := []common.TransactionInput{} for _, input := range b.TxReferenceInputs { input := input ret = append(ret, &input) @@ -229,7 +233,7 @@ func (b *BabbageTransactionBody) ReferenceInputs() []TransactionInput { return ret } -func (b *BabbageTransactionBody) CollateralReturn() TransactionOutput { +func (b *BabbageTransactionBody) 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 @@ -286,7 +290,7 @@ const ( ) type BabbageTransactionOutputDatumOption struct { - hash *Blake2b256 + hash *common.Blake2b256 data *cbor.LazyValue } @@ -300,7 +304,7 @@ func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error { var tmpDatumHash struct { cbor.StructAsArray Type int - Hash Blake2b256 + Hash common.Blake2b256 } if _, err := cbor.Decode(data, &tmpDatumHash); err != nil { return err @@ -340,8 +344,8 @@ func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) { type BabbageTransactionOutput struct { cbor.DecodeStoreCbor - OutputAddress Address `cbor:"0,keyasint,omitempty"` - OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"` + OutputAddress common.Address `cbor:"0,keyasint,omitempty"` + OutputAmount mary.MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"` DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"` ScriptRef *cbor.Tag `cbor:"3,keyasint,omitempty"` legacyOutput bool @@ -351,7 +355,7 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error { // Save original CBOR o.SetCbor(cborData) // Try to parse as legacy output first - var tmpOutput AlonzoTransactionOutput + var tmpOutput alonzo.AlonzoTransactionOutput if _, err := cbor.Decode(cborData, &tmpOutput); err == nil { // Copy from temp legacy object to Babbage format o.OutputAddress = tmpOutput.OutputAddress @@ -365,7 +369,7 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error { func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) { if o.legacyOutput { - tmpOutput := AlonzoTransactionOutput{ + tmpOutput := alonzo.AlonzoTransactionOutput{ OutputAddress: o.OutputAddress, OutputAmount: o.OutputAmount, } @@ -376,7 +380,7 @@ func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) { func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) { tmpObj := struct { - Address Address `json:"address"` + Address common.Address `json:"address"` Amount uint64 `json:"amount"` Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"` Datum *cbor.LazyValue `json:"datum,omitempty"` @@ -397,7 +401,7 @@ func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) { return json.Marshal(&tmpObj) } -func (o BabbageTransactionOutput) Address() Address { +func (o BabbageTransactionOutput) Address() common.Address { return o.OutputAddress } @@ -409,11 +413,11 @@ func (o BabbageTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetT return o.OutputAmount.Assets } -func (o BabbageTransactionOutput) DatumHash() *Blake2b256 { +func (o BabbageTransactionOutput) DatumHash() *common.Blake2b256 { if o.DatumOption != nil { return o.DatumOption.hash } - return &Blake2b256{} + return &common.Blake2b256{} } func (o BabbageTransactionOutput) Datum() *cbor.LazyValue { @@ -470,7 +474,7 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput { } type BabbageTransactionWitnessSet struct { - AlonzoTransactionWitnessSet + alonzo.AlonzoTransactionWitnessSet PlutusV2Scripts []cbor.RawMessage `cbor:"6,keyasint,omitempty"` } @@ -491,11 +495,11 @@ func (t BabbageTransaction) Hash() string { return t.Body.Hash() } -func (t BabbageTransaction) Inputs() []TransactionInput { +func (t BabbageTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } -func (t BabbageTransaction) Outputs() []TransactionOutput { +func (t BabbageTransaction) Outputs() []common.TransactionOutput { return t.Body.Outputs() } @@ -511,19 +515,19 @@ func (t BabbageTransaction) ValidityIntervalStart() uint64 { return t.Body.ValidityIntervalStart() } -func (t BabbageTransaction) ProtocolParametersUpdate() map[Blake2b224]any { +func (t BabbageTransaction) ProtocolParametersUpdate() map[common.Blake2b224]any { return t.Body.ProtocolParametersUpdate() } -func (t BabbageTransaction) ReferenceInputs() []TransactionInput { +func (t BabbageTransaction) ReferenceInputs() []common.TransactionInput { return t.Body.ReferenceInputs() } -func (t BabbageTransaction) Collateral() []TransactionInput { +func (t BabbageTransaction) Collateral() []common.TransactionInput { return t.Body.Collateral() } -func (t BabbageTransaction) CollateralReturn() TransactionOutput { +func (t BabbageTransaction) CollateralReturn() common.TransactionOutput { return t.Body.CollateralReturn() } @@ -531,27 +535,27 @@ func (t BabbageTransaction) TotalCollateral() uint64 { return t.Body.TotalCollateral() } -func (t BabbageTransaction) Certificates() []Certificate { +func (t BabbageTransaction) Certificates() []common.Certificate { return t.Body.Certificates() } -func (t BabbageTransaction) Withdrawals() map[*Address]uint64 { +func (t BabbageTransaction) Withdrawals() map[*common.Address]uint64 { return t.Body.Withdrawals() } -func (t BabbageTransaction) AuxDataHash() *Blake2b256 { +func (t BabbageTransaction) AuxDataHash() *common.Blake2b256 { return t.Body.AuxDataHash() } -func (t BabbageTransaction) ScriptDataHash() *Blake2b256 { +func (t BabbageTransaction) ScriptDataHash() *common.Blake2b256 { return t.Body.ScriptDataHash() } -func (t BabbageTransaction) VotingProcedures() VotingProcedures { +func (t BabbageTransaction) VotingProcedures() common.VotingProcedures { return t.Body.VotingProcedures() } -func (t BabbageTransaction) RequiredSigners() []Blake2b224 { +func (t BabbageTransaction) RequiredSigners() []common.Blake2b224 { return t.Body.RequiredSigners() } @@ -559,7 +563,7 @@ func (t BabbageTransaction) AssetMint() *common.MultiAsset[common.MultiAssetType return t.Body.AssetMint() } -func (t BabbageTransaction) ProposalProcedures() []ProposalProcedure { +func (t BabbageTransaction) ProposalProcedures() []common.ProposalProcedure { return t.Body.ProposalProcedures() } @@ -579,7 +583,7 @@ func (t BabbageTransaction) IsValid() bool { return t.IsTxValid } -func (t BabbageTransaction) Consumed() []TransactionInput { +func (t BabbageTransaction) Consumed() []common.TransactionInput { if t.IsValid() { return t.Inputs() } else { @@ -587,14 +591,14 @@ func (t BabbageTransaction) Consumed() []TransactionInput { } } -func (t BabbageTransaction) Produced() []Utxo { +func (t BabbageTransaction) Produced() []common.Utxo { if t.IsValid() { - var ret []Utxo + var ret []common.Utxo for idx, output := range t.Outputs() { ret = append( ret, - Utxo{ - Id: NewShelleyTransactionInput(t.Hash(), idx), + common.Utxo{ + Id: shelley.NewShelleyTransactionInput(t.Hash(), idx), Output: output, }, ) @@ -602,11 +606,11 @@ func (t BabbageTransaction) Produced() []Utxo { return ret } else { if t.CollateralReturn() == nil { - return []Utxo{} + return []common.Utxo{} } - return []Utxo{ + return []common.Utxo{ { - Id: NewShelleyTransactionInput(t.Hash(), len(t.Outputs())), + Id: shelley.NewShelleyTransactionInput(t.Hash(), len(t.Outputs())), Output: t.CollateralReturn(), }, } diff --git a/ledger/babbage_test.go b/ledger/babbage/babbage_test.go similarity index 95% rename from ledger/babbage_test.go rename to ledger/babbage/babbage_test.go index aa7dfb8e..249b5487 100644 --- a/ledger/babbage_test.go +++ b/ledger/babbage/babbage_test.go @@ -1,9 +1,27 @@ -package ledger +// Copyright 2024 Blink Labs Software +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package babbage import ( "encoding/hex" "testing" + "github.com/blinklabs-io/gouroboros/ledger/alonzo" + "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/stretchr/testify/assert" ) @@ -28,8 +46,8 @@ func TestBabbageBlockTransactions(t *testing.T) { TxTotalCollateral: 1 << i, } b.TransactionWitnessSets[i] = BabbageTransactionWitnessSet{ - AlonzoTransactionWitnessSet: AlonzoTransactionWitnessSet{ - ShelleyTransactionWitnessSet: ShelleyTransactionWitnessSet{ + AlonzoTransactionWitnessSet: alonzo.AlonzoTransactionWitnessSet{ + ShelleyTransactionWitnessSet: shelley.ShelleyTransactionWitnessSet{ VkeyWitnesses: []interface{}{ append(make([]byte, 95), 1<