|
| 1 | +package proposer |
| 2 | + |
| 3 | +import ( |
| 4 | + "iter" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/NethermindEth/juno/adapters/consensus2p2p" |
| 8 | + "github.com/NethermindEth/juno/builder" |
| 9 | + "github.com/NethermindEth/juno/consensus/types" |
| 10 | + "github.com/starknet-io/starknet-p2pspecs/p2p/proto/common" |
| 11 | + "github.com/starknet-io/starknet-p2pspecs/p2p/proto/consensus/consensus" |
| 12 | + "google.golang.org/protobuf/proto" |
| 13 | +) |
| 14 | + |
| 15 | +const txBatchSize = 64 // TODO: make this configurable |
| 16 | + |
| 17 | +type dispatcher[V types.Hashable[H], H types.Hash, A types.Addr] struct { |
| 18 | + adapter ProposerAdapter[V, H, A] |
| 19 | + proposal *types.Proposal[V, H, A] |
| 20 | + buildResult *builder.BuildResult |
| 21 | + streamID string |
| 22 | + seqNo uint64 |
| 23 | +} |
| 24 | + |
| 25 | +func newProposerDispatcher[V types.Hashable[H], H types.Hash, A types.Addr]( |
| 26 | + adapter ProposerAdapter[V, H, A], |
| 27 | + proposal *types.Proposal[V, H, A], |
| 28 | + buildResult *builder.BuildResult, |
| 29 | +) (dispatcher[V, H, A], error) { |
| 30 | + streamIDStruct := &consensus.ConsensusStreamId{ |
| 31 | + BlockNumber: uint64(proposal.Height), |
| 32 | + Round: uint32(proposal.Round), |
| 33 | + Nonce: 0, |
| 34 | + } |
| 35 | + |
| 36 | + streamID, err := proto.Marshal(streamIDStruct) |
| 37 | + if err != nil { |
| 38 | + return dispatcher[V, H, A]{}, err // TODO: log error |
| 39 | + } |
| 40 | + |
| 41 | + return dispatcher[V, H, A]{ |
| 42 | + adapter: adapter, |
| 43 | + proposal: proposal, |
| 44 | + buildResult: buildResult, |
| 45 | + streamID: string(streamID), |
| 46 | + seqNo: 0, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (d *dispatcher[V, H, A]) run() iter.Seq2[*consensus.StreamMessage, error] { |
| 51 | + return func(yield func(*consensus.StreamMessage, error) bool) { |
| 52 | + if !yield(d.fromProposalInit()) { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if !yield(d.fromBlockInfo()) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + txStream, err := d.adapter.ProposalTransactions(d.buildResult) |
| 61 | + if err != nil { |
| 62 | + yield(nil, err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + for txBatch := range slices.Chunk(txStream, txBatchSize) { |
| 67 | + if !yield(d.fromTransactions(txBatch)) { |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if !yield(d.fromProposalCommitment()) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if !yield(d.fromProposalFin()) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + if !yield(d.fromFin(), nil) { |
| 81 | + return |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (d *dispatcher[V, H, A]) fromProposalInit() (*consensus.StreamMessage, error) { |
| 87 | + proposalInit, err := d.adapter.ProposalInit(d.proposal) |
| 88 | + if err != nil { |
| 89 | + return nil, err // TODO: log error |
| 90 | + } |
| 91 | + |
| 92 | + p2pProposalInit := consensus2p2p.AdaptProposalInit(&proposalInit) |
| 93 | + |
| 94 | + return d.sendProposalPart(&consensus.ProposalPart{ |
| 95 | + Messages: &consensus.ProposalPart_Init{ |
| 96 | + Init: &p2pProposalInit, |
| 97 | + }, |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +func (d *dispatcher[V, H, A]) fromBlockInfo() (*consensus.StreamMessage, error) { |
| 102 | + blockInfo, err := d.adapter.ProposalBlockInfo(d.buildResult) |
| 103 | + if err != nil { |
| 104 | + return nil, err // TODO: log error |
| 105 | + } |
| 106 | + |
| 107 | + p2pBlockInfo := consensus2p2p.AdaptBlockInfo(&blockInfo) |
| 108 | + |
| 109 | + return d.sendProposalPart(&consensus.ProposalPart{ |
| 110 | + Messages: &consensus.ProposalPart_BlockInfo{ |
| 111 | + BlockInfo: &p2pBlockInfo, |
| 112 | + }, |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +func (d *dispatcher[V, H, A]) fromTransactions(txs []types.Transaction) (*consensus.StreamMessage, error) { |
| 117 | + p2pTxBatch, err := consensus2p2p.AdaptProposalTransaction(txs) |
| 118 | + if err != nil { |
| 119 | + return nil, err // TODO: log error |
| 120 | + } |
| 121 | + |
| 122 | + return d.sendProposalPart(&consensus.ProposalPart{ |
| 123 | + Messages: &consensus.ProposalPart_Transactions{ |
| 124 | + Transactions: &p2pTxBatch, |
| 125 | + }, |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func (d *dispatcher[V, H, A]) fromProposalCommitment() (*consensus.StreamMessage, error) { |
| 130 | + commitment, err := d.adapter.ProposalCommitment(d.buildResult) |
| 131 | + if err != nil { |
| 132 | + return nil, err // TODO: log error |
| 133 | + } |
| 134 | + |
| 135 | + p2pCommitment := consensus2p2p.AdaptProposalCommitment(&commitment) |
| 136 | + |
| 137 | + return d.sendProposalPart(&consensus.ProposalPart{ |
| 138 | + Messages: &consensus.ProposalPart_Commitment{ |
| 139 | + Commitment: &p2pCommitment, |
| 140 | + }, |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +func (d *dispatcher[V, H, A]) fromProposalFin() (*consensus.StreamMessage, error) { |
| 145 | + fin, err := d.adapter.ProposalFin(d.proposal) |
| 146 | + if err != nil { |
| 147 | + return nil, err // TODO: log error |
| 148 | + } |
| 149 | + |
| 150 | + p2pFin := consensus2p2p.AdaptProposalFin(&fin) |
| 151 | + |
| 152 | + return d.sendProposalPart(&consensus.ProposalPart{ |
| 153 | + Messages: &consensus.ProposalPart_Fin{ |
| 154 | + Fin: &p2pFin, |
| 155 | + }, |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +func (d *dispatcher[V, H, A]) sendProposalPart(proposal *consensus.ProposalPart) (*consensus.StreamMessage, error) { |
| 160 | + proposalBytes, err := proto.Marshal(proposal) |
| 161 | + if err != nil { |
| 162 | + return nil, err // TODO: log error |
| 163 | + } |
| 164 | + |
| 165 | + return &consensus.StreamMessage{ |
| 166 | + Message: &consensus.StreamMessage_Content{ |
| 167 | + Content: proposalBytes, |
| 168 | + }, |
| 169 | + StreamId: []byte(d.streamID), |
| 170 | + SequenceNumber: d.next(), |
| 171 | + }, nil |
| 172 | +} |
| 173 | + |
| 174 | +func (d *dispatcher[V, H, A]) fromFin() *consensus.StreamMessage { |
| 175 | + return &consensus.StreamMessage{ |
| 176 | + Message: &consensus.StreamMessage_Fin{ |
| 177 | + Fin: &common.Fin{}, |
| 178 | + }, |
| 179 | + StreamId: []byte(d.streamID), |
| 180 | + SequenceNumber: d.next(), |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func (d *dispatcher[V, H, A]) next() uint64 { |
| 185 | + seqNo := d.seqNo |
| 186 | + d.seqNo++ |
| 187 | + return seqNo |
| 188 | +} |
0 commit comments