Skip to content

Commit a1d6de1

Browse files
authored
Merge pull request #27 from cloudstruct/refactor-api
chore: refactor/rename variables for clarity
2 parents 5bdc234 + 7d03fdb commit a1d6de1

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

internal/api/api.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
ginzap "github.com/gin-contrib/zap"
1313
"github.com/gin-gonic/gin"
1414
"github.com/penglongli/gin-metrics/ginmetrics"
15-
swaggerFiles "github.com/swaggo/files" // swagger embed files
15+
swaggerFiles "github.com/swaggo/files" // swagger embed files
1616
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
1717
"golang.org/x/crypto/blake2b"
1818
"io/ioutil"
@@ -34,7 +34,7 @@ import (
3434
// @license.name Apache 2.0
3535
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
3636
func Start(cfg *config.Config) error {
37-
// Disable gin debug output
37+
// Disable gin debug and color output
3838
gin.SetMode(gin.ReleaseMode)
3939
gin.DisableConsoleColor()
4040

@@ -119,29 +119,38 @@ func handleHealthcheck(c *gin.Context) {
119119
// @Failure 500 {object} string "Server Error"
120120
// @Router /api/submit/tx [post]
121121
func handleSubmitTx(c *gin.Context) {
122+
// First, initialize our configuration and loggers
122123
cfg := config.GetConfig()
123124
logger := logging.GetLogger()
124-
// Read transaction from request body
125-
rawTx, err := ioutil.ReadAll(c.Request.Body)
125+
// Read raw transaction bytes from the request body and store in a byte array
126+
txRawBytes, err := ioutil.ReadAll(c.Request.Body)
126127
if err != nil {
128+
// Log the error, return an error to the user, and increment failed count
127129
logger.Errorf("failed to read request body: %s", err)
128-
c.String(500, "failed to request body")
130+
c.String(500, "failed to read request body")
129131
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
130132
return
131133
}
134+
// Close request body after read
132135
if err := c.Request.Body.Close(); err != nil {
133136
logger.Errorf("failed to close request body: %s", err)
134137
}
135-
// Unwrap transaction and calculate ID
138+
// Unwrap raw transaction bytes into a CBOR array
136139
var txUnwrap []cbor.RawMessage
137-
if err := cbor.Unmarshal(rawTx, &txUnwrap); err != nil {
140+
if err := cbor.Unmarshal(txRawBytes, &txUnwrap); err != nil {
138141
logger.Errorf("failed to unwrap transaction CBOR: %s", err)
139142
c.String(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err))
140143
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
141144
return
142145
}
143-
txId := blake2b.Sum256(txUnwrap[0])
144-
txIdHex := hex.EncodeToString(txId[:])
146+
// index 0 is the transaction body
147+
// Store index 0 (transaction body) as byte array
148+
txBody := txUnwrap[0]
149+
150+
// Convert the body into a blake2b256 hash string
151+
txIdHash := blake2b.Sum256(txBody)
152+
// Encode hash string as byte array to hex string
153+
txIdHex := hex.EncodeToString(txIdHash[:])
145154
// Connect to cardano-node and submit TX
146155
errorChan := make(chan error)
147156
doneChan := make(chan bool)
@@ -206,7 +215,7 @@ func handleSubmitTx(c *gin.Context) {
206215
}
207216
}()
208217
// TODO: figure out better way to determine era
209-
if err = oConn.LocalTxSubmission.SubmitTx(block.TX_TYPE_ALONZO, rawTx); err != nil {
218+
if err = oConn.LocalTxSubmission.SubmitTx(block.TX_TYPE_ALONZO, txRawBytes); err != nil {
210219
logger.Errorf("failure submitting transaction: %s", err)
211220
c.String(500, "failure communicating with node")
212221
return

0 commit comments

Comments
 (0)