Skip to content

Commit 895dc53

Browse files
authored
Merge pull request #78 from cloudstruct/chore/upgrade-go-ouroboros-network
chore: update go-ouroboros-network to v0.27.0
2 parents 981022f + 410333b commit 895dc53

File tree

4 files changed

+45
-67
lines changed

4 files changed

+45
-67
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/Bitrue-exchange/libada-go v0.0.1-rc.0.20220817020305-79d8b4c4dd9c
77
github.com/cloudstruct/go-cardano-ledger v0.4.0
8-
github.com/cloudstruct/go-ouroboros-network v0.16.1
8+
github.com/cloudstruct/go-ouroboros-network v0.27.0
99
github.com/fxamacker/cbor/v2 v2.4.0
1010
github.com/gin-contrib/zap v0.0.2
1111
github.com/gin-gonic/gin v1.8.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
6363
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
6464
github.com/cloudstruct/go-cardano-ledger v0.4.0 h1:4BZvwZSM82r/Bt+W4XHTstJEABg0jXcougcZgDmZBo0=
6565
github.com/cloudstruct/go-cardano-ledger v0.4.0/go.mod h1:EBB9JG+3nBs3IV0p751yz1468XYkzJce1/A2X+xH8L8=
66-
github.com/cloudstruct/go-ouroboros-network v0.16.1 h1:iAYviR5keS3/rbAnJp2slXsTcEAE04ut8WDcRYzK6qM=
67-
github.com/cloudstruct/go-ouroboros-network v0.16.1/go.mod h1:/Rz+GYS+ItV7e5F4rXfZuCVuHa+SkFfaH3zgqjVN3eI=
66+
github.com/cloudstruct/go-ouroboros-network v0.27.0 h1:LURvMntHdR0WuBFmzMe+dyoeC7gQQ2hxugbQevJaqKw=
67+
github.com/cloudstruct/go-ouroboros-network v0.27.0/go.mod h1:GP7VdlqIqpio2y9xlKnLL7v93Wk2jpaPySycvJvs9A4=
6868
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
6969
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
7070
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

internal/api/api.go

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package api
33
import (
44
"encoding/hex"
55
"fmt"
6+
"io"
7+
"time"
8+
69
"github.com/cloudstruct/go-cardano-ledger"
710
"github.com/cloudstruct/go-cardano-submit-api/internal/config"
811
"github.com/cloudstruct/go-cardano-submit-api/internal/logging"
912
ouroboros "github.com/cloudstruct/go-ouroboros-network"
1013
"github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission"
14+
1115
"github.com/fxamacker/cbor/v2"
1216
ginzap "github.com/gin-contrib/zap"
1317
"github.com/gin-gonic/gin"
1418
"github.com/penglongli/gin-metrics/ginmetrics"
1519
swaggerFiles "github.com/swaggo/files" // swagger embed files
1620
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
1721
"golang.org/x/crypto/blake2b"
18-
"io"
19-
"time"
2022

2123
_ "github.com/cloudstruct/go-cardano-submit-api/docs" // docs is generated by Swag CLI
2224
)
@@ -172,19 +174,16 @@ func handleSubmitTx(c *gin.Context) {
172174
txIdHex := hex.EncodeToString(txIdHash[:])
173175
// Connect to cardano-node and submit TX
174176
errorChan := make(chan error)
175-
doneChan := make(chan bool)
176-
oOpts := &ouroboros.OuroborosOptions{
177-
NetworkMagic: uint32(cfg.Node.NetworkMagic),
178-
ErrorChan: errorChan,
179-
UseNodeToNodeProtocol: false,
180-
}
181-
oConn, err := ouroboros.New(oOpts)
182-
defer func() {
183-
// We have to close the channel to break out of the async error handler goroutine
184-
close(errorChan)
185-
// Close Ouroboros connection
186-
oConn.Close()
187-
}()
177+
oConn, err := ouroboros.New(
178+
ouroboros.WithNetworkMagic(uint32(cfg.Node.NetworkMagic)),
179+
ouroboros.WithErrorChan(errorChan),
180+
ouroboros.WithNodeToNode(false),
181+
ouroboros.WithLocalTxSubmissionConfig(
182+
localtxsubmission.NewConfig(
183+
localtxsubmission.WithTimeout(5*time.Second),
184+
),
185+
),
186+
)
188187
if err != nil {
189188
logger.Errorf("failure creating Ouroboros connection: %s", err)
190189
c.JSON(500, "failure communicating with node")
@@ -213,50 +212,36 @@ func handleSubmitTx(c *gin.Context) {
213212
logger.Errorf("failure communicating with node: %s", err)
214213
c.JSON(500, "failure communicating with node")
215214
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
216-
doneChan <- true
217215
}
218216
}()
217+
defer func() {
218+
// Close Ouroboros connection
219+
oConn.Close()
220+
}()
219221
// Start local-tx-submission protocol
220-
localTxSubmissionCallbackConfig := &localtxsubmission.CallbackConfig{
221-
AcceptTxFunc: func() error {
222-
// Return transaction ID
223-
c.JSON(202, txIdHex)
224-
doneChan <- true
225-
// Increment custom metric
226-
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_count").Inc(nil)
227-
return nil
228-
},
229-
RejectTxFunc: func(reasonCbor []byte) error {
230-
if c.GetHeader("Accept") == "application/cbor" {
231-
c.Data(400, "application/cbor", reasonCbor)
232-
} else {
233-
if reason, err := ledger.NewTxSubmitErrorFromCbor(reasonCbor); err == nil {
234-
c.JSON(400, reason)
235-
} else {
236-
c.JSON(400, fmt.Sprintf("transaction rejected by node, but the 'reason' data could not be parsed (raw CBOR: %x)", reasonCbor))
237-
}
238-
}
239-
doneChan <- true
240-
// Increment custom metric
241-
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
242-
return nil
243-
},
244-
}
245-
oConn.LocalTxSubmission.Start(localTxSubmissionCallbackConfig)
222+
oConn.LocalTxSubmission().Client.Start()
246223
// Determine transaction type (era)
247224
txType, err := determineTransactionType(txRawBytes)
248225
if err != nil {
249226
c.JSON(400, "could not parse transaction to determine type")
250227
return
251228
}
252229
// Submit the transaction
253-
if err := oConn.LocalTxSubmission.SubmitTx(txType, txRawBytes); err != nil {
254-
logger.Errorf("failure submitting transaction: %s", err)
255-
c.JSON(500, "failure communicating with node")
230+
if err := oConn.LocalTxSubmission().Client.SubmitTx(txType, txRawBytes); err != nil {
231+
if c.GetHeader("Accept") == "application/cbor" {
232+
txRejectErr := err.(localtxsubmission.TransactionRejectedError)
233+
c.Data(400, "application/cbor", txRejectErr.ReasonCbor)
234+
} else {
235+
c.JSON(400, err.Error())
236+
}
237+
// Increment custom metric
238+
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
256239
return
257240
}
258-
// Wait for async process to finish
259-
<-doneChan
241+
// Return transaction ID
242+
c.JSON(202, txIdHex)
243+
// Increment custom metric
244+
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_count").Inc(nil)
260245
}
261246

262247
func determineTransactionType(data []byte) (uint16, error) {

internal/config/config.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type LoggingConfig struct {
2323
}
2424

2525
type ApiConfig struct {
26-
ListenAddress string `yaml:"address" envconfig:"API_LISTEN_ADDRESS"`
27-
ListenPort uint `yaml:"port" envconfig:"API_LISTEN_PORT"`
26+
ListenAddress string `yaml:"address" envconfig:"API_LISTEN_ADDRESS"`
27+
ListenPort uint `yaml:"port" envconfig:"API_LISTEN_PORT"`
2828
}
2929

3030
type DebugConfig struct {
@@ -52,8 +52,8 @@ var globalConfig = &Config{
5252
Healthchecks: false,
5353
},
5454
Api: ApiConfig{
55-
ListenAddress: "",
56-
ListenPort: 8090,
55+
ListenAddress: "",
56+
ListenPort: 8090,
5757
},
5858
Debug: DebugConfig{
5959
ListenAddress: "localhost",
@@ -122,19 +122,10 @@ func (c *Config) populateNetworkMagic() error {
122122

123123
func (c *Config) checkNode() error {
124124
// Connect to cardano-node
125-
errorChan := make(chan error)
126-
oOpts := &ouroboros.OuroborosOptions{
127-
NetworkMagic: uint32(c.Node.NetworkMagic),
128-
ErrorChan: errorChan,
129-
UseNodeToNodeProtocol: false,
130-
}
131-
oConn, err := ouroboros.New(oOpts)
132-
defer func() {
133-
// We have to close the channel to break out of the async error handler goroutine
134-
close(errorChan)
135-
// Close Ouroboros connection
136-
oConn.Close()
137-
}()
125+
oConn, err := ouroboros.New(
126+
ouroboros.WithNetworkMagic(uint32(c.Node.NetworkMagic)),
127+
ouroboros.WithNodeToNode(false),
128+
)
138129
if err != nil {
139130
return fmt.Errorf("failure creating Ouroboros connection: %s", err)
140131
}
@@ -159,5 +150,7 @@ func (c *Config) checkNode() error {
159150
} else {
160151
return fmt.Errorf("you must specify either the UNIX socket path or the address/port for your cardano-node")
161152
}
153+
// Close Ouroboros connection
154+
oConn.Close()
162155
return nil
163156
}

0 commit comments

Comments
 (0)