mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 04:34:19 +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:
@@ -45,7 +45,8 @@ type covenantService struct {
|
||||
paymentRequests *paymentsMap
|
||||
forfeitTxs *forfeitTxsMap
|
||||
|
||||
eventsCh chan domain.RoundEvent
|
||||
eventsCh chan domain.RoundEvent
|
||||
transactionEventsCh chan TransactionEvent
|
||||
|
||||
currentRoundLock sync.Mutex
|
||||
currentRound *domain.Round
|
||||
@@ -59,23 +60,30 @@ func NewCovenantService(
|
||||
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)
|
||||
|
||||
svc := &covenantService{
|
||||
network, pubkey,
|
||||
roundLifetime, roundInterval, unilateralExitDelay, boardingExitDelay,
|
||||
walletSvc, repoManager, builder, scanner, sweeper,
|
||||
paymentRequests, forfeitTxs, eventsCh, sync.Mutex{}, nil, nil,
|
||||
network: network,
|
||||
pubkey: pubkey,
|
||||
roundLifetime: roundLifetime,
|
||||
roundInterval: roundInterval,
|
||||
unilateralExitDelay: unilateralExitDelay,
|
||||
boardingExitDelay: boardingExitDelay,
|
||||
wallet: walletSvc,
|
||||
repoManager: repoManager,
|
||||
builder: builder,
|
||||
scanner: scanner,
|
||||
sweeper: newSweeper(walletSvc, repoManager, builder, scheduler),
|
||||
paymentRequests: newPaymentsMap(),
|
||||
forfeitTxs: newForfeitTxsMap(builder),
|
||||
eventsCh: make(chan domain.RoundEvent),
|
||||
transactionEventsCh: make(chan TransactionEvent),
|
||||
currentRoundLock: sync.Mutex{},
|
||||
}
|
||||
|
||||
repoManager.RegisterEventsHandler(
|
||||
func(round *domain.Round) {
|
||||
go svc.propagateEvents(round)
|
||||
@@ -346,6 +354,10 @@ func (s *covenantService) GetEventsChannel(ctx context.Context) <-chan domain.Ro
|
||||
return s.eventsCh
|
||||
}
|
||||
|
||||
func (s *covenantService) GetTransactionEventsChannel(ctx context.Context) <-chan TransactionEvent {
|
||||
return s.transactionEventsCh
|
||||
}
|
||||
|
||||
func (s *covenantService) GetRoundByTxid(ctx context.Context, poolTxid string) (*domain.Round, error) {
|
||||
return s.repoManager.Rounds().GetRoundWithTxid(ctx, poolTxid)
|
||||
}
|
||||
@@ -851,6 +863,26 @@ func (s *covenantService) updateVtxoSet(round *domain.Round) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
// nolint:all
|
||||
tx, _ := psetv2.NewPsetFromBase64(round.UnsignedTx)
|
||||
boardingInputs := make([]domain.VtxoKey, 0)
|
||||
for _, in := range tx.Inputs {
|
||||
if len(in.TapLeafScript) > 0 {
|
||||
boardingInputs = append(boardingInputs, domain.VtxoKey{
|
||||
Txid: elementsutil.TxIDFromBytes(in.PreviousTxid),
|
||||
VOut: in.PreviousTxIndex,
|
||||
})
|
||||
}
|
||||
}
|
||||
s.transactionEventsCh <- RoundTransactionEvent{
|
||||
RoundTxID: round.Txid,
|
||||
SpentVtxos: getSpentVtxos(round.Payments),
|
||||
SpendableVtxos: s.getNewVtxos(round),
|
||||
ClaimedBoardingInputs: boardingInputs,
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *covenantService) propagateEvents(round *domain.Round) {
|
||||
|
||||
Reference in New Issue
Block a user