Skip to content

Commit

Permalink
retru submitting when get a error response from darknode
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Mar 5, 2020
1 parent ae620e2 commit 778bfe0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
29 changes: 23 additions & 6 deletions confirmer/confirmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (confirmer *Confirmer) Run(ctx context.Context) {
// confirmations.
func (confirmer *Confirmer) checkPendingTxs(parent context.Context) {
ctx, cancel := context.WithTimeout(parent, confirmer.options.PollInterval)
defer cancel()
go func() {
defer cancel()
<-ctx.Done()
}()

txs, err := confirmer.database.PendingTxs()
if err != nil {
Expand All @@ -100,13 +103,13 @@ func (confirmer *Confirmer) checkPendingTxs(parent context.Context) {

if confirmed {
confirmer.logger.Infof("tx=%v has reached sufficient confirmations", tx.Hash.String())
confirmer.confirm(tx)
confirmer.confirm(ctx, tx)
}
})
}

// confirm sends the transaction to the dispatcher and marks it as confirmed.
func (confirmer *Confirmer) confirm(tx abi.Tx) {
func (confirmer *Confirmer) confirm(ctx context.Context, tx abi.Tx) {
request, err := submitTxRequest(tx)
if err != nil {
confirmer.logger.Errorf("[confirmer] cannot construct json request for transaction: %v", err)
Expand All @@ -118,9 +121,23 @@ func (confirmer *Confirmer) confirm(tx abi.Tx) {
return
}

if err := confirmer.database.ConfirmTx(tx.Hash); err != nil {
confirmer.logger.Errorf("[confirmer] cannot confirm tx in the database: %v", err)
}
go func() {
select {
case <-ctx.Done():
return
case response := <-req.Responder:
if response.Error != nil {
confirmer.logger.Errorf("[confirmer] getting error back when submitting tx = %v: [%v] %v", tx.Hash.String(), response.Error.Code, response.Error.Message)
return
}
confirmer.logger.Infof("✅ successfully submit tx = %v to darknodes", tx.Hash.String())

if err := confirmer.database.ConfirmTx(tx.Hash); err != nil {
confirmer.logger.Errorf("[confirmer] cannot confirm tx in the database: %v", err)
}
}
}()

}

// shiftInTxConfirmed checks if a given shift in transaction has received
Expand Down
11 changes: 1 addition & 10 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dispatcher
import (
"context"
"fmt"
"log"
"time"

"github.com/renproject/darknode/addr"
Expand Down Expand Up @@ -63,22 +62,14 @@ func (dispatcher *Dispatcher) Handle(_ phi.Task, message phi.Message) {
responses := make(chan jsonrpc.Response, len(addrs))
resIter := dispatcher.newResponseIter(msg.Request.Method)

var retryOptions *http.RetryOptions
if msg.Request.Method == jsonrpc.MethodSubmitTx {
retryOptions = &http.DefaultRetryOptions
}

go func() {
phi.ParForAll(addrs, func(i int) {
address := fmt.Sprintf("http://%s:%v", addrs[i].IP4(), addrs[i].Port()+1)
response, err := dispatcher.client.SendRequest(ctx, address, msg.Request, retryOptions)
response, err := dispatcher.client.SendRequest(ctx, address, msg.Request, nil)
if err != nil {
return
}
responses <- response
if msg.Request.Method == jsonrpc.MethodSubmitTx && response.Error == nil {
log.Printf("✅ successfully send request to darknode = %v", address)
}
})
close(responses)
}()
Expand Down

0 comments on commit 778bfe0

Please sign in to comment.