Skip to content

Commit

Permalink
feat: endpoints for gRPC submit module for LocalTxSubmission
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 committed Apr 30, 2024
1 parent 3431f96 commit 25e9d83
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 107 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.21.6

require (
connectrpc.com/connect v1.16.1
github.com/blinklabs-io/gouroboros v0.79.0
github.com/blinklabs-io/gouroboros v0.81.0
github.com/blinklabs-io/snek v0.17.4
github.com/blinklabs-io/tx-submit-api v0.17.0
github.com/gin-contrib/zap v1.1.1
Expand All @@ -17,7 +17,7 @@ require (
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
github.com/utxorpc/go-codegen v0.4.4
github.com/utxorpc/go-codegen v0.5.1
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.27.0
golang.org/x/net v0.24.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjLyS07ChA=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/blinklabs-io/gouroboros v0.79.0 h1:baURwZB1Br7d0jPi5Da3M/3ulNe/R/2tuf8QyTx4NPw=
github.com/blinklabs-io/gouroboros v0.79.0/go.mod h1:wqeaBdd92uMfBdtLrpbY8HtWyDu0kP+U/wBylh/b9TM=
github.com/blinklabs-io/gouroboros v0.81.0 h1:9iuXxM+Dr6+V/FtZyleUgIWs15A36mheJWHXJbzYGnU=
github.com/blinklabs-io/gouroboros v0.81.0/go.mod h1:uBExUfgZvhPqiur3N9ReoXv4LpqGmR9jSZjolr1nZ2w=
github.com/blinklabs-io/ouroboros-mock v0.3.0 h1:6VRWyhAv0k7nQEgzFpuqhS/n8OM+OAaLN/sCT5K2Hbc=
github.com/blinklabs-io/ouroboros-mock v0.3.0/go.mod h1:0dzTNEk/Kvqa7qYHDy7/Nn3OTt+EOosMknB37FRzI1k=
github.com/blinklabs-io/snek v0.17.4 h1:/BoWqfJTZXSQsRkVRS3tLFhlrvjme82Xu6jYkEBtMJ4=
Expand Down Expand Up @@ -321,8 +321,8 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/utxorpc/go-codegen v0.4.4 h1:NnRFkqKJ9ZkZ3pffpD3bb6K0IU6IaDXbkVD/DzDmuKs=
github.com/utxorpc/go-codegen v0.4.4/go.mod h1:NWAVa5/vHWgSrp/wJzXnMpbaBeST0PmWCU5vX63hZdw=
github.com/utxorpc/go-codegen v0.5.1 h1:Xhq3CdWAQEJgi46Naq7epeO4G5EyGApP38yNxMCqCbY=
github.com/utxorpc/go-codegen v0.5.1/go.mod h1:sEfglXN19j3cq0qQvb2NS4IcxUSrK1crYXbsVf11SGM=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
6 changes: 3 additions & 3 deletions internal/utxorpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"
"net/http"

"github.com/utxorpc/go-codegen/utxorpc/v1alpha/build/buildconnect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit/submitconnect"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

Expand All @@ -27,8 +27,8 @@ import (

func Start(cfg *config.Config) error {
mux := http.NewServeMux()
buildPath, buildHandler := buildconnect.NewLedgerStateServiceHandler(&ledgerStateServiceServer{})
mux.Handle(buildPath, buildHandler)
submitPath, submitHandler := submitconnect.NewSubmitServiceHandler(&submitServiceServer{})
mux.Handle(submitPath, submitHandler)
err := http.ListenAndServe(
fmt.Sprintf("%s:%d", cfg.Utxorpc.ListenAddress, cfg.Utxorpc.ListenPort),
// Use h2c so we can serve HTTP/2 without TLS
Expand Down
98 changes: 0 additions & 98 deletions internal/utxorpc/build.go

This file was deleted.

97 changes: 97 additions & 0 deletions internal/utxorpc/submit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utxorpc

import (
"context"
"encoding/hex"
"fmt"
"log"

connect "connectrpc.com/connect"
"github.com/blinklabs-io/gouroboros/ledger"
submit "github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit/submitconnect"

"github.com/blinklabs-io/cardano-node-api/internal/node"
)

// submitServiceServer implements the SubmitService API
type submitServiceServer struct {
submitconnect.UnimplementedSubmitServiceHandler
}

// SubmitTx
func (s *submitServiceServer) SubmitTx(
ctx context.Context,
req *connect.Request[submit.SubmitTxRequest],
) (*connect.Response[submit.SubmitTxResponse], error) {

// txRawList
txRawList := req.Msg.GetTx() // []*AnyChainTx
resp := &submit.SubmitTxResponse{}
log.Printf("Got a SubmitTx request with %d transactions", len(txRawList))

// Connect to node
oConn, err := node.GetConnection(nil)
if err != nil {
return nil, err
}
defer func() {
// Close Ouroboros connection
oConn.Close()
}()

// Loop through the transactions and submit each
errorList := []error{}
for i, txi := range txRawList {
txRawBytes := txi.GetRaw() // raw bytes
txType, err := ledger.DetermineTransactionType(txRawBytes)
ref := []byte{}
if err != nil {
resp.Ref = append(resp.Ref, ref)
errorList[i] = err
continue
}
tx, err := ledger.NewTransactionFromCbor(txType, txRawBytes)
if err != nil {
resp.Ref = append(resp.Ref, ref)
errorList[i] = err
continue
}
// Submit the transaction
err = oConn.LocalTxSubmission().Client.SubmitTx(uint16(txType), txRawBytes)
if err != nil {
resp.Ref = append(resp.Ref, ref)
errorList[i] = fmt.Errorf("%s", err.Error())
continue
}
txHexBytes, err := hex.DecodeString(tx.Hash())
if err != nil {
resp.Ref = append(resp.Ref, ref)
errorList[i] = err
continue
}
errorList[i] = nil
resp.Ref = append(resp.Ref, txHexBytes)
}
return connect.NewResponse(resp), nil
}

// WaitForTx

// ReadMempool

// WatchMempool

0 comments on commit 25e9d83

Please sign in to comment.