Skip to content

Commit 2a090f1

Browse files
committed
fix: use gouroboros tx hash
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ee600b5 commit 2a090f1

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

api/api.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package api
33
import (
44
"bytes"
55
"context"
6-
"encoding/hex"
76
"fmt"
87
"io"
98
"net/http"
109
"net/http/httptrace"
1110
"time"
1211

13-
"github.com/fxamacker/cbor/v2"
12+
"github.com/blinklabs-io/gouroboros/ledger"
1413
ginzap "github.com/gin-contrib/zap"
1514
"github.com/gin-gonic/gin"
16-
"golang.org/x/crypto/blake2b"
1715

1816
"github.com/blinklabs-io/tx-submit-api-mirror/config"
1917
"github.com/blinklabs-io/tx-submit-api-mirror/logging"
@@ -66,16 +64,20 @@ func handleSubmitTx(c *gin.Context) {
6664
logger.Errorf("failed to close request body: %s", err)
6765
}
6866
logger.Debugf("transaction dump: %x", rawTx)
69-
// Unwrap transaction and calculate ID
70-
var txUnwrap []cbor.RawMessage
71-
if err := cbor.Unmarshal(rawTx, &txUnwrap); err != nil {
72-
logger.Errorf("failed to unwrap transaction CBOR: %s", err)
73-
c.String(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err))
67+
// Determine transaction type (era)
68+
txType, err := ledger.DetermineTransactionType(rawTx)
69+
if err != nil {
70+
logger.Errorf("could not parse transaction to determine type: %s", err)
71+
c.JSON(400, "could not parse transaction to determine type")
72+
return
73+
}
74+
tx, err := ledger.NewTransactionFromCbor(txType, rawTx)
75+
if err != nil {
76+
logger.Errorf("failed to parse transaction CBOR: %s", err)
77+
c.String(400, fmt.Sprintf("failed to parse transaction CBOR: %s", err))
7478
return
7579
}
76-
txId := blake2b.Sum256(txUnwrap[0])
77-
txIdHex := hex.EncodeToString(txId[:])
78-
logger.Debugf("calculated transaction ID %s", txIdHex)
80+
logger.Debugf("transaction ID %s", tx.Hash())
7981
// Send request to each backend
8082
for _, backend := range cfg.Backends {
8183
go func(backend string) {
@@ -107,12 +109,12 @@ func handleSubmitTx(c *gin.Context) {
107109
}
108110
defer resp.Body.Close()
109111
if resp.StatusCode == 202 {
110-
logger.Infow(fmt.Sprintf("successfully submitted transaction %s to backend %s", txIdHex, backend), "latency", elapsedTime.Seconds(), "connReused", connReused)
112+
logger.Infow(fmt.Sprintf("successfully submitted transaction %s to backend %s", tx.Hash(), backend), "latency", elapsedTime.Seconds(), "connReused", connReused)
111113
} else {
112114
logger.Errorw(fmt.Sprintf("failed to send request to backend %s: got response %d, %s", backend, resp.StatusCode, string(respBody)), "latency", elapsedTime.Seconds(), "connReused", connReused)
113115
}
114116
}(backend)
115117
}
116118
// Return transaction ID
117-
c.String(202, txIdHex)
119+
c.String(202, tx.Hash())
118120
}

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ module github.com/blinklabs-io/tx-submit-api-mirror
33
go 1.20
44

55
require (
6-
github.com/fxamacker/cbor/v2 v2.5.0
6+
github.com/blinklabs-io/gouroboros v0.57.0
77
github.com/gin-contrib/zap v0.2.0
88
github.com/gin-gonic/gin v1.9.1
99
github.com/kelseyhightower/envconfig v1.4.0
1010
go.uber.org/zap v1.26.0
11-
golang.org/x/crypto v0.14.0
1211
gopkg.in/yaml.v2 v2.4.0
1312
)
1413

1514
require (
1615
github.com/bytedance/sonic v1.10.0 // indirect
1716
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
1817
github.com/chenzhuoyu/iasm v0.9.0 // indirect
18+
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
1919
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2020
github.com/gin-contrib/sse v0.1.0 // indirect
2121
github.com/go-playground/locales v0.14.1 // indirect
2222
github.com/go-playground/universal-translator v0.18.1 // indirect
2323
github.com/go-playground/validator/v10 v10.15.3 // indirect
2424
github.com/goccy/go-json v0.10.2 // indirect
2525
github.com/google/go-cmp v0.5.8 // indirect
26+
github.com/jinzhu/copier v0.4.0 // indirect
2627
github.com/json-iterator/go v1.1.12 // indirect
2728
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
2829
github.com/leodido/go-urn v1.2.4 // indirect
@@ -35,6 +36,7 @@ require (
3536
github.com/x448/float16 v0.8.4 // indirect
3637
go.uber.org/multierr v1.11.0 // indirect
3738
golang.org/x/arch v0.5.0 // indirect
39+
golang.org/x/crypto v0.14.0 // indirect
3840
golang.org/x/net v0.17.0 // indirect
3941
golang.org/x/sys v0.13.0 // indirect
4042
golang.org/x/text v0.13.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/blinklabs-io/gouroboros v0.57.0 h1:k5Y706vvYAGM3bCtEhh6WRHGrvS3S6n1MT9vNLFAe/E=
2+
github.com/blinklabs-io/gouroboros v0.57.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k=
13
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
24
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
35
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
@@ -35,6 +37,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3537
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
3638
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3739
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
40+
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
41+
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
3842
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
3943
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
4044
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=

0 commit comments

Comments
 (0)