1- // NOTE: file is based on the generic write target capability, but we're slightly modifying it until the two implementations can be merged
2- // in particular, we need to invert the calling flow for Aptos so receiver is the entrypoint
3- package write_target
1+ //nolint:gosec,revive // disable G115,revive
2+ package writetarget
43
54import (
65 "context"
@@ -227,12 +226,19 @@ func (c *writeTarget) Execute(ctx context.Context, request capabilities.Capabili
227226 // Source the report ID from the input
228227 info .reportInfo .reportID = binary .BigEndian .Uint16 (inputs .ID )
229228
230- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteInitiated (info ))
229+ // TODO: Not sure if I should be returning the error here or just logging it as I am now.
230+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteInitiated (info ))
231+ if err != nil {
232+ c .lggr .Errorw ("failed to emit write initiated" , "err" , err )
233+ }
231234
232235 // Check whether the report is valid (e.g., not empty)
233236 if len (inputs .Report ) == 0 {
234237 // We received any empty report -- this means we should skip transmission.
235- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteSkipped (info , "empty report" ))
238+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteSkipped (info , "empty report" ))
239+ if err != nil {
240+ c .lggr .Errorw ("failed to emit write skipped" , "err" , err )
241+ }
236242 return success (), nil
237243 }
238244
@@ -290,7 +296,10 @@ func (c *writeTarget) Execute(ctx context.Context, request capabilities.Capabili
290296 // Source the transmitter address from the on-chain state
291297 info .reportTransmissionState = state
292298
293- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteConfirmed (info , head ))
299+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteConfirmed (info , head ))
300+ if err != nil {
301+ c .lggr .Errorw ("failed to emit write confirmed" , "err" , err )
302+ }
294303 return success (), nil
295304 }
296305
@@ -304,11 +313,15 @@ func (c *writeTarget) Execute(ctx context.Context, request capabilities.Capabili
304313
305314 txID , err := c .targetStrategy .TransmitReport (ctx , info .receiver , inputs .Report , inputs .Context , inputs .Signatures , request .Metadata .WorkflowExecutionID )
306315 c .lggr .Debugw ("Transaction submitted" , "request" , request , "transaction-id" , txID )
307- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteSent (info , head , txID ))
308316 if err != nil {
309317 msg := builder .buildWriteError (info , 0 , "failed to transmit the report" , err .Error ())
310318 return capabilities.CapabilityResponse {}, c .asEmittedError (ctx , msg )
311319 }
320+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteSent (info , head , txID ))
321+ if err != nil {
322+ c .lggr .Errorw ("failed to emit write sent" , "err" , err )
323+ }
324+
312325 // TODO: implement a background WriteTxConfirmer to periodically source new events/transactions,
313326 // relevant to this forwarder), and emit write-tx-accepted/confirmed events.
314327
@@ -382,7 +395,10 @@ func (c *writeTarget) acceptAndConfirmWrite(ctx context.Context, info requestInf
382395 select {
383396 case <- ctx .Done ():
384397 // We (eventually) failed to confirm the report was transmitted
385- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteError (& info , 0 , "write confirmation - failed" , "timed out" ))
398+ err := c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteError (& info , 0 , "write confirmation - failed" , "timed out" ))
399+ if err != nil {
400+ lggr .Errorw ("failed to emit write error" , "err" , err )
401+ }
386402 return
387403 case <- ticker .C :
388404 // Fetch the latest head from the chain (timestamp)
@@ -394,9 +410,9 @@ func (c *writeTarget) acceptAndConfirmWrite(ctx context.Context, info requestInf
394410
395411 if ! accepted {
396412 // Check acceptance status
397- status , accepted , err := checkAcceptedStatus (ctx )
398- if err != nil {
399- lggr .Errorw ("failed to check accepted status" , "txID" , txID , "err" , err )
413+ status , accepted , statusErr := checkAcceptedStatus (ctx )
414+ if statusErr != nil {
415+ lggr .Errorw ("failed to check accepted status" , "txID" , txID , "err" , statusErr )
400416 continue
401417 }
402418
@@ -413,7 +429,10 @@ func (c *writeTarget) acceptAndConfirmWrite(ctx context.Context, info requestInf
413429 // Notice: this functionality is not available in the current CW/TXM API
414430 acceptedWithErr := false
415431 if acceptedWithErr {
416- // TODO: [Beholder] Emit 'platform.write-target.WriteError' if accepted with an error (surface specific on-chain error)
432+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteError (& info , 0 , "write error" , "accepted with error" ))
433+ if err != nil {
434+ lggr .Errorw ("failed to emit write error" , "err" , err )
435+ }
417436 // Notice: no return, we continue to check for confirmation (tx could be accepted by another node)
418437 }
419438 }
@@ -437,7 +456,10 @@ func (c *writeTarget) acceptAndConfirmWrite(ctx context.Context, info requestInf
437456 // Source the transmitter address from the on-chain state
438457 info .reportTransmissionState = state
439458
440- c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteConfirmed (& info , head ))
459+ err = c .beholder .ProtoEmitter .EmitWithLog (ctx , builder .buildWriteConfirmed (& info , head ))
460+ if err != nil {
461+ lggr .Errorw ("failed to emit write confirmed" , "err" , err )
462+ }
441463 return
442464 }
443465 }
0 commit comments