|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ledger |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + EraIdConway = 6 |
| 25 | + |
| 26 | + BlockTypeConway = 7 |
| 27 | + |
| 28 | + BlockHeaderTypeConway = 6 |
| 29 | + |
| 30 | + TxTypeConway = 6 |
| 31 | +) |
| 32 | + |
| 33 | +type ConwayBlock struct { |
| 34 | + cbor.StructAsArray |
| 35 | + cbor.DecodeStoreCbor |
| 36 | + Header *ConwayBlockHeader |
| 37 | + TransactionBodies []ConwayTransactionBody |
| 38 | + TransactionWitnessSets []BabbageTransactionWitnessSet |
| 39 | + TransactionMetadataSet map[uint]*cbor.Value |
| 40 | + InvalidTransactions []uint |
| 41 | +} |
| 42 | + |
| 43 | +func (b *ConwayBlock) UnmarshalCBOR(cborData []byte) error { |
| 44 | + return b.UnmarshalCbor(cborData, b) |
| 45 | +} |
| 46 | + |
| 47 | +func (b *ConwayBlock) Hash() string { |
| 48 | + return b.Header.Hash() |
| 49 | +} |
| 50 | + |
| 51 | +func (b *ConwayBlock) BlockNumber() uint64 { |
| 52 | + return b.Header.BlockNumber() |
| 53 | +} |
| 54 | + |
| 55 | +func (b *ConwayBlock) SlotNumber() uint64 { |
| 56 | + return b.Header.SlotNumber() |
| 57 | +} |
| 58 | + |
| 59 | +func (b *ConwayBlock) IssuerVkey() IssuerVkey { |
| 60 | + return b.Header.IssuerVkey() |
| 61 | +} |
| 62 | + |
| 63 | +func (b *ConwayBlock) BlockBodySize() uint64 { |
| 64 | + return b.Header.BlockBodySize() |
| 65 | +} |
| 66 | + |
| 67 | +func (b *ConwayBlock) Era() Era { |
| 68 | + return eras[EraIdConway] |
| 69 | +} |
| 70 | + |
| 71 | +func (b *ConwayBlock) Transactions() []Transaction { |
| 72 | + ret := []Transaction{} |
| 73 | + for idx := range b.TransactionBodies { |
| 74 | + tmpTransaction := ConwayTransaction{ |
| 75 | + Body: b.TransactionBodies[idx], |
| 76 | + WitnessSet: b.TransactionWitnessSets[idx], |
| 77 | + TxMetadata: b.TransactionMetadataSet[uint(idx)], |
| 78 | + } |
| 79 | + isValid := true |
| 80 | + for _, invalidTxIdx := range b.InvalidTransactions { |
| 81 | + if invalidTxIdx == uint(idx) { |
| 82 | + isValid = false |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + tmpTransaction.IsValid = isValid |
| 87 | + ret = append(ret, &tmpTransaction) |
| 88 | + } |
| 89 | + return ret |
| 90 | +} |
| 91 | + |
| 92 | +type ConwayBlockHeader struct { |
| 93 | + BabbageBlockHeader |
| 94 | +} |
| 95 | + |
| 96 | +func (h *ConwayBlockHeader) Era() Era { |
| 97 | + return eras[EraIdConway] |
| 98 | +} |
| 99 | + |
| 100 | +type ConwayTransactionBody struct { |
| 101 | + BabbageTransactionBody |
| 102 | + VotingProcedures *cbor.Value `cbor:"19,keyasint,omitempty"` |
| 103 | + ProposalProcedures *cbor.Value `cbor:"20,keyasint,omitempty"` |
| 104 | + CurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"` |
| 105 | + Donation uint64 `cbor:"22,keyasint,omitempty"` |
| 106 | +} |
| 107 | + |
| 108 | +func (b *ConwayTransactionBody) UnmarshalCBOR(cborData []byte) error { |
| 109 | + return b.UnmarshalCbor(cborData, b) |
| 110 | +} |
| 111 | + |
| 112 | +type ConwayTransaction struct { |
| 113 | + cbor.StructAsArray |
| 114 | + cbor.DecodeStoreCbor |
| 115 | + Body ConwayTransactionBody |
| 116 | + WitnessSet BabbageTransactionWitnessSet |
| 117 | + IsValid bool |
| 118 | + TxMetadata *cbor.Value |
| 119 | +} |
| 120 | + |
| 121 | +func (t ConwayTransaction) Hash() string { |
| 122 | + return t.Body.Hash() |
| 123 | +} |
| 124 | + |
| 125 | +func (t ConwayTransaction) Inputs() []TransactionInput { |
| 126 | + return t.Body.Inputs() |
| 127 | +} |
| 128 | + |
| 129 | +func (t ConwayTransaction) Outputs() []TransactionOutput { |
| 130 | + return t.Body.Outputs() |
| 131 | +} |
| 132 | + |
| 133 | +func (t ConwayTransaction) Fee() uint64 { |
| 134 | + return t.Body.Fee() |
| 135 | +} |
| 136 | + |
| 137 | +func (t ConwayTransaction) TTL() uint64 { |
| 138 | + return t.Body.TTL() |
| 139 | +} |
| 140 | + |
| 141 | +func (t ConwayTransaction) Metadata() *cbor.Value { |
| 142 | + return t.TxMetadata |
| 143 | +} |
| 144 | + |
| 145 | +func (t *ConwayTransaction) Cbor() []byte { |
| 146 | + // Return stored CBOR if we have any |
| 147 | + cborData := t.DecodeStoreCbor.Cbor() |
| 148 | + if cborData != nil { |
| 149 | + return cborData[:] |
| 150 | + } |
| 151 | + // Return immediately if the body CBOR is also empty, which implies an empty TX object |
| 152 | + if t.Body.Cbor() == nil { |
| 153 | + return nil |
| 154 | + } |
| 155 | + // Generate our own CBOR |
| 156 | + // This is necessary when a transaction is put together from pieces stored separately in a block |
| 157 | + tmpObj := []any{ |
| 158 | + cbor.RawMessage(t.Body.Cbor()), |
| 159 | + cbor.RawMessage(t.WitnessSet.Cbor()), |
| 160 | + t.IsValid, |
| 161 | + } |
| 162 | + if t.TxMetadata != nil { |
| 163 | + tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor())) |
| 164 | + } else { |
| 165 | + tmpObj = append(tmpObj, nil) |
| 166 | + } |
| 167 | + // This should never fail, since we're only encoding a list and a bool value |
| 168 | + cborData, _ = cbor.Encode(&tmpObj) |
| 169 | + return cborData |
| 170 | +} |
| 171 | + |
| 172 | +func NewConwayBlockFromCbor(data []byte) (*ConwayBlock, error) { |
| 173 | + var conwayBlock ConwayBlock |
| 174 | + if _, err := cbor.Decode(data, &conwayBlock); err != nil { |
| 175 | + return nil, fmt.Errorf("Conway block decode error: %s", err) |
| 176 | + } |
| 177 | + return &conwayBlock, nil |
| 178 | +} |
| 179 | + |
| 180 | +func NewConwayBlockHeaderFromCbor(data []byte) (*ConwayBlockHeader, error) { |
| 181 | + var conwayBlockHeader ConwayBlockHeader |
| 182 | + if _, err := cbor.Decode(data, &conwayBlockHeader); err != nil { |
| 183 | + return nil, fmt.Errorf("Conway block header decode error: %s", err) |
| 184 | + } |
| 185 | + return &conwayBlockHeader, nil |
| 186 | +} |
| 187 | + |
| 188 | +func NewConwayTransactionBodyFromCbor( |
| 189 | + data []byte, |
| 190 | +) (*ConwayTransactionBody, error) { |
| 191 | + var conwayTx ConwayTransactionBody |
| 192 | + if _, err := cbor.Decode(data, &conwayTx); err != nil { |
| 193 | + return nil, fmt.Errorf("Conway transaction body decode error: %s", err) |
| 194 | + } |
| 195 | + return &conwayTx, nil |
| 196 | +} |
| 197 | + |
| 198 | +func NewConwayTransactionFromCbor(data []byte) (*ConwayTransaction, error) { |
| 199 | + var conwayTx ConwayTransaction |
| 200 | + if _, err := cbor.Decode(data, &conwayTx); err != nil { |
| 201 | + return nil, fmt.Errorf("Conway transaction decode error: %s", err) |
| 202 | + } |
| 203 | + return &conwayTx, nil |
| 204 | +} |
0 commit comments