forked from stellar/go
/
submitter.go
69 lines (60 loc) · 1.83 KB
/
submitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package txsub
import (
"context"
"net/http"
"time"
"github.com/stellar/go/clients/stellarcore"
proto "github.com/stellar/go/protocols/stellarcore"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
)
// NewDefaultSubmitter returns a new, simple Submitter implementation
// that submits directly to the stellar-core at `url` using the http client
// `h`.
func NewDefaultSubmitter(h *http.Client, url string) Submitter {
return &submitter{
StellarCore: &stellarcore.Client{
HTTP: h,
URL: url,
},
Log: log.DefaultLogger.WithField("service", "txsub.submitter"),
}
}
// submitter is the default implementation for the Submitter interface. It
// submits directly to the configured stellar-core instance using the
// configured http client.
type submitter struct {
StellarCore *stellarcore.Client
Log *log.Entry
}
// Submit sends the provided envelope to stellar-core and parses the response into
// a SubmissionResult
func (sub *submitter) Submit(ctx context.Context, env string) (result SubmissionResult) {
start := time.Now()
defer func() {
result.Duration = time.Since(start)
sub.Log.Ctx(ctx).WithFields(log.F{
"err": result.Err,
"duration": result.Duration.Seconds(),
}).Info("Submitter result")
}()
cresp, err := sub.StellarCore.SubmitTransaction(ctx, env)
if err != nil {
result.Err = errors.Wrap(err, "failed to submit")
return
}
// interpet response
if cresp.IsException() {
result.Err = errors.Errorf("stellar-core exception: %s", cresp.Exception)
return
}
switch cresp.Status {
case proto.TXStatusError:
result.Err = &FailedTransactionError{cresp.Error}
case proto.TXStatusPending, proto.TXStatusDuplicate:
//noop. A nil Err indicates success
default:
result.Err = errors.Errorf("Unrecognized stellar-core status response: %s", cresp.Status)
}
return
}