|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/NethermindEth/juno/encoder" |
| 8 | +) |
| 9 | + |
| 10 | +type BlockTransactionsSerializer struct{} |
| 11 | + |
| 12 | +func (BlockTransactionsSerializer) Marshal(value *BlockTransactions) ([]byte, error) { |
| 13 | + var buf bytes.Buffer |
| 14 | + if err := encoder.NewEncoder(&buf).Encode(value.Indexes); err != nil { |
| 15 | + return nil, err |
| 16 | + } |
| 17 | + if _, err := buf.Write(value.Data); err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + return buf.Bytes(), nil |
| 21 | +} |
| 22 | + |
| 23 | +func (BlockTransactionsSerializer) Unmarshal(data []byte, value *BlockTransactions) error { |
| 24 | + partial := blockTransactionsPartialSerializer[extractAll, struct{}, BlockTransactions]{} |
| 25 | + return partial.UnmarshalPartial(struct{}{}, data, value) |
| 26 | +} |
| 27 | + |
| 28 | +type blockTransactionsExtractor[S, T any] interface { |
| 29 | + ~struct{} |
| 30 | + extract(*BlockTransactions, S) (T, error) |
| 31 | +} |
| 32 | + |
| 33 | +type extractTransaction struct{} |
| 34 | + |
| 35 | +func (extractTransaction) extract(b *BlockTransactions, subKey int) (Transaction, error) { |
| 36 | + return b.Transactions().Get(subKey) |
| 37 | +} |
| 38 | + |
| 39 | +type extractReceipt struct{} |
| 40 | + |
| 41 | +func (extractReceipt) extract(b *BlockTransactions, subKey int) (*TransactionReceipt, error) { |
| 42 | + return b.Receipts().Get(subKey) |
| 43 | +} |
| 44 | + |
| 45 | +type extractAllTransactions struct{} |
| 46 | + |
| 47 | +func (extractAllTransactions) extract(b *BlockTransactions, _ struct{}) ([]Transaction, error) { |
| 48 | + return b.Transactions().All() |
| 49 | +} |
| 50 | + |
| 51 | +type extractAllReceipts struct{} |
| 52 | + |
| 53 | +func (extractAllReceipts) extract(b *BlockTransactions, _ struct{}) ([]*TransactionReceipt, error) { |
| 54 | + return b.Receipts().All() |
| 55 | +} |
| 56 | + |
| 57 | +type extractAll struct{} |
| 58 | + |
| 59 | +func (extractAll) extract(b *BlockTransactions, _ struct{}) (BlockTransactions, error) { |
| 60 | + return BlockTransactions{ |
| 61 | + Indexes: b.Indexes, |
| 62 | + Data: slices.Clone(b.Data), |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +type blockTransactionsPartialSerializer[E blockTransactionsExtractor[S, T], S, T any] struct{} |
| 67 | + |
| 68 | +func (blockTransactionsPartialSerializer[E, S, T]) UnmarshalPartial( |
| 69 | + subKey S, |
| 70 | + data []byte, |
| 71 | + value *T, |
| 72 | +) error { |
| 73 | + var blockTransactions BlockTransactions |
| 74 | + remaining, err := encoder.UnmarshalFirst(data, &blockTransactions.Indexes) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + blockTransactions.Data = remaining |
| 79 | + *value, err = E{}.extract(&blockTransactions, subKey) |
| 80 | + return err |
| 81 | +} |
| 82 | + |
| 83 | +var ( |
| 84 | + BlockTransactionsTransactionPartialSerializer = blockTransactionsPartialSerializer[ |
| 85 | + extractTransaction, |
| 86 | + int, |
| 87 | + Transaction, |
| 88 | + ]{} |
| 89 | + BlockTransactionsReceiptPartialSerializer = blockTransactionsPartialSerializer[ |
| 90 | + extractReceipt, |
| 91 | + int, |
| 92 | + *TransactionReceipt, |
| 93 | + ]{} |
| 94 | + BlockTransactionsAllTransactionsPartialSerializer = blockTransactionsPartialSerializer[ |
| 95 | + extractAllTransactions, |
| 96 | + struct{}, |
| 97 | + []Transaction, |
| 98 | + ]{} |
| 99 | + BlockTransactionsAllReceiptsPartialSerializer = blockTransactionsPartialSerializer[ |
| 100 | + extractAllReceipts, |
| 101 | + struct{}, |
| 102 | + []*TransactionReceipt, |
| 103 | + ]{} |
| 104 | +) |
0 commit comments