Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Babbage block parsing #208

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/go-ouroboros-network/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var eraIntersect = map[string]map[string][]interface{}{
"genesis": []interface{}{},
"alonzo": []interface{}{},
// Last block of epoch 3 (Alonzo era)
"babbage": []interface{}{345599, "6e4de9c9b2dcc2436488aa8a6ce584250a45b42583c5d3d0749597bcf59dc0b5"},
"babbage": []interface{}{345594, "e47ac07272e95d6c3dc8279def7b88ded00e310f99ac3dfbae48ed9ff55e6001"},
},
}

Expand Down
28 changes: 25 additions & 3 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (h *AlonzoBlockHeader) Era() Era {

type AlonzoTransactionBody struct {
MaryTransactionBody
Outputs []AlonzoTransactionOutput `cbor:"1,keyasint,omitempty"`
ScriptDataHash Blake2b256 `cbor:"11,keyasint,omitempty"`
Collateral []ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
RequiredSigners []Blake2b224 `cbor:"14,keyasint,omitempty"`
Expand All @@ -74,11 +75,32 @@ func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
return b.UnmarshalCborGeneric(cborData, b)
}

type AlonzoTransactionOutput struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Address Blake2b256
Amount cbor.Value
DatumHash Blake2b256
}

func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Try to parse as Mary output first
var tmpOutput MaryTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
// Copy from temp Shelley output to Alonzo format
o.Address = tmpOutput.Address
o.Amount = tmpOutput.Amount
} else {
return o.UnmarshalCborGeneric(cborData, o)
}
return nil
}

type AlonzoTransactionWitnessSet struct {
ShelleyTransactionWitnessSet
PlutusScripts interface{} `cbor:"3,keyasint,omitempty"`
PlutusData []cbor.Value `cbor:"4,keyasint,omitempty"`
Redeemers []cbor.Value `cbor:"5,keyasint,omitempty"`
PlutusScripts []cbor.RawMessage `cbor:"3,keyasint,omitempty"`
PlutusData []cbor.RawMessage `cbor:"4,keyasint,omitempty"`
Redeemers []cbor.Value `cbor:"5,keyasint,omitempty"`
}

type AlonzoTransaction struct {
Expand Down
39 changes: 34 additions & 5 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BabbageBlock struct {
cbor.DecodeStoreCbor
Header *BabbageBlockHeader
TransactionBodies []BabbageTransactionBody
TransactionWitnessSets []AlonzoTransactionWitnessSet
TransactionWitnessSets []BabbageTransactionWitnessSet
TransactionMetadataSet map[uint]cbor.Value
InvalidTransactions []uint
}
Expand Down Expand Up @@ -109,19 +109,48 @@ func (h *BabbageBlockHeader) Era() Era {

type BabbageTransactionBody struct {
AlonzoTransactionBody
CollateralReturn ShelleyTransactionOutput `cbor:"16,keyasint,omitempty"`
TotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
ReferenceInputs []ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
Outputs []BabbageTransactionOutput `cbor:"1,keyasint,omitempty"`
CollateralReturn BabbageTransactionOutput `cbor:"16,keyasint,omitempty"`
TotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
ReferenceInputs []ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
}

func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
return b.UnmarshalCborGeneric(cborData, b)
}

type BabbageTransactionOutput struct {
cbor.DecodeStoreCbor
Address Blake2b256 `cbor:"0,keyasint,omitempty"`
Amount cbor.Value `cbor:"1,keyasint,omitempty"`
DatumOption []cbor.RawMessage `cbor:"2,keyasint,omitempty"`
ScriptRef []cbor.RawMessage `cbor:"3,keyasint,omitempty"`
legacyOutput bool
}

func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Try to parse as legacy output first
var tmpOutput AlonzoTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
// Copy from temp legacy object to Babbage format
o.Address = tmpOutput.Address
o.Amount = tmpOutput.Amount
o.legacyOutput = true
} else {
return o.UnmarshalCborGeneric(cborData, o)
}
return nil
}

type BabbageTransactionWitnessSet struct {
AlonzoTransactionWitnessSet
PlutusV2Scripts []cbor.RawMessage `cbor:"6,keyasint,omitempty"`
}

type BabbageTransaction struct {
cbor.StructAsArray
Body BabbageTransactionBody
WitnessSet AlonzoTransactionWitnessSet
WitnessSet BabbageTransactionWitnessSet
IsValid bool
Metadata cbor.Value
}
Expand Down
10 changes: 6 additions & 4 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func (h *MaryBlockHeader) Era() Era {

type MaryTransactionBody struct {
AllegraTransactionBody
//Outputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
Outputs []cbor.Value `cbor:"1,keyasint,omitempty"`
Outputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
// TODO: further parsing of this field
Mint cbor.Value `cbor:"9,keyasint,omitempty"`
}
Expand All @@ -85,9 +84,12 @@ type MaryTransaction struct {
transaction_output = [address, amount : value]
value = coin / [coin,multiasset<uint>]
*/
//type MaryTransactionOutput interface{}

type MaryTransactionOutput cbor.Value
type MaryTransactionOutput struct {
cbor.StructAsArray
Address Blake2b256
Amount cbor.Value
}

func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) {
var maryBlock MaryBlock
Expand Down