|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "github.com/cloudstruct/tx-submit-api-mirror/config" |
| 8 | + "github.com/cloudstruct/tx-submit-api-mirror/logging" |
| 9 | + "github.com/fxamacker/cbor/v2" |
| 10 | + ginzap "github.com/gin-contrib/zap" |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "golang.org/x/crypto/blake2b" |
| 13 | + "io/ioutil" |
| 14 | + "net/http" |
| 15 | +) |
| 16 | + |
| 17 | +func Start(cfg *config.Config) error { |
| 18 | + // Disable gin debug output |
| 19 | + gin.SetMode(gin.ReleaseMode) |
| 20 | + gin.DisableConsoleColor() |
| 21 | + |
| 22 | + // Configure router |
| 23 | + router := gin.New() |
| 24 | + // Catch panics and return a 500 |
| 25 | + router.Use(gin.Recovery()) |
| 26 | + // Access logging |
| 27 | + accessLogger := logging.GetAccessLogger() |
| 28 | + router.Use(ginzap.Ginzap(accessLogger, "", true)) |
| 29 | + router.Use(ginzap.RecoveryWithZap(accessLogger, true)) |
| 30 | + |
| 31 | + // Configure routes |
| 32 | + router.GET("/healthcheck", handleHealthcheck) |
| 33 | + router.POST("/api/submit/tx", handleSubmitTx) |
| 34 | + |
| 35 | + // Start listener |
| 36 | + err := router.Run(fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort)) |
| 37 | + return err |
| 38 | +} |
| 39 | + |
| 40 | +func handleHealthcheck(c *gin.Context) { |
| 41 | + // TODO: add some actual health checking here |
| 42 | + c.JSON(200, gin.H{"failed": false}) |
| 43 | +} |
| 44 | + |
| 45 | +func handleSubmitTx(c *gin.Context) { |
| 46 | + cfg := config.GetConfig() |
| 47 | + logger := logging.GetLogger() |
| 48 | + if len(cfg.Backends) == 0 { |
| 49 | + logger.Errorf("no backends configured") |
| 50 | + c.String(500, "no backends configured") |
| 51 | + return |
| 52 | + } |
| 53 | + // Read transaction from request body |
| 54 | + rawTx, err := ioutil.ReadAll(c.Request.Body) |
| 55 | + if err != nil { |
| 56 | + logger.Errorf("failed to read request body: %s", err) |
| 57 | + c.String(500, "failed to request body") |
| 58 | + return |
| 59 | + } |
| 60 | + if err := c.Request.Body.Close(); err != nil { |
| 61 | + logger.Errorf("failed to close request body: %s", err) |
| 62 | + } |
| 63 | + logger.Debugf("transaction dump: %x", rawTx) |
| 64 | + // Unwrap transaction and calculate ID |
| 65 | + var txUnwrap []cbor.RawMessage |
| 66 | + if err := cbor.Unmarshal(rawTx, &txUnwrap); err != nil { |
| 67 | + logger.Errorf("failed to unwrap transaction CBOR: %s", err) |
| 68 | + c.String(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err)) |
| 69 | + return |
| 70 | + } |
| 71 | + txId := blake2b.Sum256(txUnwrap[0]) |
| 72 | + txIdHex := hex.EncodeToString(txId[:]) |
| 73 | + logger.Debugf("calculated transaction ID %s", txIdHex) |
| 74 | + // Send request to each backend |
| 75 | + for _, backend := range cfg.Backends { |
| 76 | + go func(backend string) { |
| 77 | + body := bytes.NewBuffer(rawTx) |
| 78 | + resp, err := http.Post(backend, "application/cbor", body) |
| 79 | + if err != nil { |
| 80 | + logger.Errorf("failed to send request to backend %s: %s", backend, err) |
| 81 | + return |
| 82 | + } |
| 83 | + if resp.StatusCode == 202 { |
| 84 | + logger.Infof("successfully submitted transaction %s to backend %s", txIdHex, backend) |
| 85 | + } else { |
| 86 | + respBody, err := ioutil.ReadAll(resp.Body) |
| 87 | + if err != nil { |
| 88 | + logger.Errorf("failed to read response body: %s", err) |
| 89 | + return |
| 90 | + } |
| 91 | + logger.Errorf("failed to send request to backend %s: got response %d, %s", backend, resp.StatusCode, string(respBody)) |
| 92 | + } |
| 93 | + }(backend) |
| 94 | + } |
| 95 | + // Return transaction ID |
| 96 | + c.String(202, txIdHex) |
| 97 | +} |
0 commit comments