Skip to content

Commit ffbbe9a

Browse files
committed
mempool: connect to miner (wip)
node: submit new txs to mempool (wip)
1 parent c12fcbd commit ffbbe9a

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

core/nakamoto/mempool.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,20 @@ func (m *Mempool) GetFeeStatistics() FeeStatistics {
113113

114114
return stats
115115
}
116+
117+
// Creates a bundle from the mempool.
118+
func (m *Mempool) GetBundle(max uint) []RawTransaction {
119+
if max == 0 {
120+
return []RawTransaction{}
121+
}
122+
123+
bundle := []RawTransaction{}
124+
for _, tx := range m.txs {
125+
bundle = append(bundle, *tx)
126+
if uint(len(bundle)) == max {
127+
break
128+
}
129+
}
130+
131+
return bundle
132+
}

core/nakamoto/netpeer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PeerCore struct {
4545
GossipPeersIntervalSeconds int
4646

4747
OnNewBlock func(block RawBlock)
48-
OnNewTransaction func(tx RawTransaction)
48+
OnNewTransaction func(tx RawTransaction) error
4949
OnGetBlocks func(msg GetBlocksMessage) ([][]byte, error)
5050
OnGetTip func(msg GetTipMessage) (BlockHeader, error)
5151
OnSyncGetTipAtDepth func(msg SyncGetTipAtDepthMessage) (SyncGetTipAtDepthReply, error)
@@ -130,7 +130,10 @@ func NewPeerCore(config PeerConfig) *PeerCore {
130130

131131
// Call the OnNewTransaction callback.
132132
if p.OnNewTransaction != nil {
133-
p.OnNewTransaction(msg.RawTransaction)
133+
err = p.OnNewTransaction(msg.RawTransaction)
134+
if err != nil {
135+
return nil, err
136+
}
134137
}
135138
return nil, nil
136139
})

core/nakamoto/node.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)