Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ReadMempool endpoint for gRPC submit module for LocalTxMonitor #164

Merged
merged 1 commit into from
May 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion internal/utxorpc/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (s *submitServiceServer) SubmitTx(

// txRawList
txRawList := req.Msg.GetTx() // []*AnyChainTx
resp := &submit.SubmitTxResponse{}
log.Printf("Got a SubmitTx request with %d transactions", len(txRawList))
resp := &submit.SubmitTxResponse{}

// Connect to node
oConn, err := node.GetConnection(nil)
Expand Down Expand Up @@ -100,5 +100,66 @@ func (s *submitServiceServer) SubmitTx(
// WaitForTx

// ReadMempool
func (s *submitServiceServer) ReadMempool(
ctx context.Context,
req *connect.Request[submit.ReadMempoolRequest],
) (*connect.Response[submit.ReadMempoolResponse], error) {

// This is GetTxs until https://github.com/utxorpc/spec/pull/95
txim := req.Msg.GetTxs() // []*TxInMempool
log.Printf("Got a ReadMempool request with %d transactions", len(txim))
resp := &submit.ReadMempoolResponse{}

// Connect to node
oConn, err := node.GetConnection(nil)
if err != nil {
return nil, err
}
defer func() {
// Close Ouroboros connection
oConn.Close()
}()

// Start LocalTxMonitor client
oConn.LocalTxMonitor().Client.Start()

// Collect TX hashes from the mempool
mempool := []*submit.TxInMempool{}
for {
txRawBytes, err := oConn.LocalTxMonitor().Client.NextTx()
if err != nil {
log.Printf("ERROR: %s", err)
return nil, err
}
// No transactions in mempool
if txRawBytes == nil {
break
}
var act submit.AnyChainTx
var actr submit.AnyChainTx_Raw
actr.Raw = txRawBytes
act.Type = &actr
record := &submit.TxInMempool{
Tx: &act,
Stage: submit.Stage_STAGE_MEMPOOL,
}
mempool = append(mempool, record)
}

// Check each requested Tx against our mempool
for _, txi := range txim {
txi.Stage = submit.Stage_STAGE_UNSPECIFIED
for _, tx := range mempool {
if txi.Stage == submit.Stage_STAGE_MEMPOOL {
break
}
if txi.Tx.String() == tx.Tx.String() {
txi.Stage = submit.Stage_STAGE_MEMPOOL
}
}
resp.Stage = append(resp.Stage, txi.Stage)
}
return connect.NewResponse(resp), nil
}

// WatchMempool