mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 20:24:21 +01:00
Add GetTransactionsStream RPC (#345)
* RPC GetPaymentsStream This introduces a new feature to the ArkService API that allows clients to subscribe to payment events. Here's a breakdown of the changes: 1. **OpenAPI Specification (`service.swagger.json`):** - A new endpoint `/v1/payments` is added to the API, supporting a `GET` operation for streaming payment events. - New definitions `v1GetPaymentsStreamResponse`, `v1RoundPayment`, and `v1AsyncPayment` are added to describe the structure of the streaming responses. 2. **Protobuf Definition (`service.proto`):** - Added a new RPC method `GetPaymentsStream` that streams `GetPaymentsStreamResponse` messages. - Defined new message types: `GetPaymentsStreamRequest`, `GetPaymentsStreamResponse`, `RoundPayment`, and `AsyncPayment`. 3. **Generated Protobuf Code (`service.pb.go`, `service.pb.gw.go`, `service_grpc.pb.go`):** - The generated code is updated to include the new RPC method and message types. - The gateway code includes functions to handle HTTP requests and responses for the new streaming endpoint. 4. **Application Logic (`covenant.go`, `covenantless.go`):** - New payment events channels are introduced (`paymentEventsCh`). - Payment events are propagated to these channels when a round is finalized or an async payment is completed. - New event types `RoundPaymentEvent` and `AsyncPaymentEvent` are defined, implementing a `PaymentEvent` interface. 5. **gRPC Handlers (`arkservice.go`):** - Added logic to handle `GetPaymentsStream` requests and manage payment listeners. - A new goroutine is started to listen to payment events and forward them to active listeners. Overall, this patch extends the ArkService to support real-time streaming of payment events, allowing clients to receive updates on both round payments and async payments as they occur. * Move emit events in updateVtxoSet & Use generics and parsers (#1) * Move sending event to updateVtxoSet * Use generics and parsers * pr review refactor * pr review refactor * fix --------- Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
This commit is contained in:
@@ -42,7 +42,8 @@ type covenantlessService struct {
|
||||
paymentRequests *paymentsMap
|
||||
forfeitTxs *forfeitTxsMap
|
||||
|
||||
eventsCh chan domain.RoundEvent
|
||||
eventsCh chan domain.RoundEvent
|
||||
transactionEventsCh chan TransactionEvent
|
||||
|
||||
// cached data for the current round
|
||||
lastEvent domain.RoundEvent
|
||||
@@ -62,16 +63,11 @@ func NewCovenantlessService(
|
||||
builder ports.TxBuilder, scanner ports.BlockchainScanner,
|
||||
scheduler ports.SchedulerService,
|
||||
) (Service, error) {
|
||||
eventsCh := make(chan domain.RoundEvent)
|
||||
paymentRequests := newPaymentsMap()
|
||||
|
||||
forfeitTxs := newForfeitTxsMap(builder)
|
||||
pubkey, err := walletSvc.GetPubkey(context.Background())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch pubkey: %s", err)
|
||||
}
|
||||
|
||||
sweeper := newSweeper(walletSvc, repoManager, builder, scheduler)
|
||||
asyncPaymentsCache := make(map[string]struct {
|
||||
receivers []domain.Receiver
|
||||
expireAt int64
|
||||
@@ -87,10 +83,11 @@ func NewCovenantlessService(
|
||||
repoManager: repoManager,
|
||||
builder: builder,
|
||||
scanner: scanner,
|
||||
sweeper: sweeper,
|
||||
paymentRequests: paymentRequests,
|
||||
forfeitTxs: forfeitTxs,
|
||||
eventsCh: eventsCh,
|
||||
sweeper: newSweeper(walletSvc, repoManager, builder, scheduler),
|
||||
paymentRequests: newPaymentsMap(),
|
||||
forfeitTxs: newForfeitTxsMap(builder),
|
||||
eventsCh: make(chan domain.RoundEvent),
|
||||
transactionEventsCh: make(chan TransactionEvent),
|
||||
currentRoundLock: sync.Mutex{},
|
||||
asyncPaymentsCache: asyncPaymentsCache,
|
||||
treeSigningSessions: make(map[string]*musigSigningSession),
|
||||
@@ -303,6 +300,14 @@ func (s *covenantlessService) CompleteAsyncPayment(
|
||||
|
||||
delete(s.asyncPaymentsCache, redeemTxid)
|
||||
|
||||
go func() {
|
||||
s.transactionEventsCh <- RedeemTransactionEvent{
|
||||
AsyncTxID: redeemTxid,
|
||||
SpentVtxos: spentVtxos,
|
||||
SpendableVtxos: vtxos,
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -579,6 +584,10 @@ func (s *covenantlessService) GetEventsChannel(ctx context.Context) <-chan domai
|
||||
return s.eventsCh
|
||||
}
|
||||
|
||||
func (s *covenantlessService) GetTransactionEventsChannel(ctx context.Context) <-chan TransactionEvent {
|
||||
return s.transactionEventsCh
|
||||
}
|
||||
|
||||
func (s *covenantlessService) GetRoundByTxid(ctx context.Context, roundTxid string) (*domain.Round, error) {
|
||||
return s.repoManager.Rounds().GetRoundWithTxid(ctx, roundTxid)
|
||||
}
|
||||
@@ -1262,7 +1271,27 @@ func (s *covenantlessService) updateVtxoSet(round *domain.Round) {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
go func() {
|
||||
// nolint:all
|
||||
tx, _ := psbt.NewFromRawBytes(strings.NewReader(round.UnsignedTx), true)
|
||||
boardingInputs := make([]domain.VtxoKey, 0)
|
||||
for i, in := range tx.Inputs {
|
||||
if len(in.TaprootLeafScript) > 0 {
|
||||
boardingInputs = append(boardingInputs, domain.VtxoKey{
|
||||
Txid: tx.UnsignedTx.TxIn[i].PreviousOutPoint.Hash.String(),
|
||||
VOut: tx.UnsignedTx.TxIn[i].PreviousOutPoint.Index,
|
||||
})
|
||||
}
|
||||
}
|
||||
s.transactionEventsCh <- RoundTransactionEvent{
|
||||
RoundTxID: round.Txid,
|
||||
SpentVtxos: getSpentVtxos(round.Payments),
|
||||
SpendableVtxos: s.getNewVtxos(round),
|
||||
ClaimedBoardingInputs: boardingInputs,
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *covenantlessService) propagateEvents(round *domain.Round) {
|
||||
|
||||
Reference in New Issue
Block a user