@@ -11,6 +11,7 @@ type Node struct {
1111 Miner * Miner
1212 Peer * PeerCore
1313 StateMachine1 * StateMachine
14+ Mempool * Mempool
1415 log * log.Logger
1516 syncLog * log.Logger
1617 stateLog * log.Logger
@@ -22,11 +23,14 @@ func NewNode(dag *BlockDAG, miner *Miner, peer *PeerCore) *Node {
2223 panic (err )
2324 }
2425
26+ mempool := NewMempool ()
27+
2528 n := & Node {
2629 Dag : dag ,
2730 Miner : miner ,
2831 Peer : peer ,
2932 StateMachine1 : stateMachine ,
33+ Mempool : mempool ,
3034 log : NewLogger ("node" , "" ),
3135 syncLog : NewLogger ("node" , "sync" ),
3236 stateLog : NewLogger ("node" , "state" ),
@@ -181,8 +185,6 @@ func (n *Node) setup() {
181185 // Recompute the state after a new tip.
182186 n .Dag .OnNewFullTip = func (new_tip Block , prev_tip Block ) {
183187 // 1. Rebuild state.
184- // 2. Regenerate current mempool.
185-
186188 n .stateLog .Printf ("rebuild-state\n " )
187189 start := time .Now ()
188190
@@ -194,6 +196,33 @@ func (n *Node) setup() {
194196
195197 duration := time .Since (start )
196198 n .stateLog .Printf ("rebuild-state completed duration=%s n_blocks=%d\n " , duration .String (), n .Dag .FullTip .Height )
199+
200+ // 2. Regenerate current mempool. Tx set should not include any txs that are in the chain.
201+ // 3. Restart miner.
202+ // TODO.
203+ }
204+
205+ // Listen for new transactions and add them to the mempool.
206+ n .Peer .OnNewTransaction = func (tx RawTransaction ) error {
207+ n .log .Printf ("New transaction gossip from peer: tx=%x\n " , tx .Hash ())
208+
209+ // Submit transaction to mempool.
210+ err := n .Mempool .SubmitTx (tx )
211+ if err != nil {
212+ n .log .Printf ("Failed to add transaction to mempool: %s\n " , err )
213+ // If the tx was rejected (e.g. fee too low), return the error.
214+ return err
215+ }
216+
217+ return nil
218+ }
219+
220+ // Connect the miner to the mempool.
221+ n .Miner .GetBlockBody = func () []RawTransaction {
222+ // Get the mempool transactions.
223+ var MAX_BUNDLE_SIZE uint = 8192 - 1 // minus the miner's coinbase tx
224+ bundle := n .Mempool .GetBundle (MAX_BUNDLE_SIZE )
225+ return bundle
197226 }
198227
199228 // When we get a tx, add it to the mempool.
@@ -209,14 +238,8 @@ func (n *Node) setup() {
209238 // - Reinsert any transcations that were included in blocks that were orphaned, to a maximum depth of 1 day of blocks (144 blocks). O(144)
210239 // - Revalidate the tx set. O(K).
211240 // c. Begin mining on the new tip.
212-
213- // When we get new transaction, add it to mempool.
214- n .Peer .OnNewTransaction = func (tx RawTransaction ) {
215- // Add transaction to mempool.
216- // TODO.
217- }
218-
219241 // Load peers from cache.
242+
220243 networkStore , err := LoadDataStore [NetworkStore ](n .Dag .db , "network" )
221244 if err != nil {
222245 n .log .Printf ("Failed to load network store: %s\n " , err )
0 commit comments