Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gRPC server #8

Merged
merged 24 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e73d7a1
go gRPC + connection
Reecepbcups Aug 16, 2024
07caec6
gRPC: rust client example w/ tonic
Reecepbcups Aug 16, 2024
36e533e
Revert "gRPC: rust client example w/ tonic"
Reecepbcups Aug 19, 2024
7ddf4a2
chore: cleanup (mark's req. changes)
Reecepbcups Aug 19, 2024
93d77a4
feat: GRPC config & GetValidators endpoint
Reecepbcups Aug 19, 2024
c148faf
migrate: txbuf to `internal/txmanager`
Reecepbcups Aug 19, 2024
ba6fea0
grpc: QueryAccountBalance
Reecepbcups Aug 19, 2024
8876f05
feat: working gRPC Transaction endpoints
Reecepbcups Aug 19, 2024
7d4206f
chore: minor nits
Reecepbcups Aug 19, 2024
d7d3fd7
nit: cleanups (comments, script, proto)
Reecepbcups Aug 19, 2024
180ffd2
feat: `TxResultResponse` direct values
Reecepbcups Aug 19, 2024
39edef3
nit: last stale
Reecepbcups Aug 19, 2024
9b42df7
fix: EOL at EOF
Reecepbcups Aug 19, 2024
939d712
fix: add mis-deleted code snippet
Reecepbcups Aug 19, 2024
a01a380
revert: `internal/txmanager` from `gsi`
Reecepbcups Aug 20, 2024
b369490
chore: comments & GOBIN installer for grpcurl
Reecepbcups Aug 20, 2024
22fed16
fix(macos); remove -w flag and pipe to tr newlines
Reecepbcups Aug 20, 2024
34b6f52
refactor: log location, Start -> serve(), grpc err handle
Reecepbcups Aug 20, 2024
4ccc16b
refactor: set validators at specific indexes
Reecepbcups Aug 20, 2024
8953c7f
refactor: user facing gRPC error handling
Reecepbcups Aug 20, 2024
b934e2b
refactor: direct copy over SDK TxResult -> Gordian
Reecepbcups Aug 20, 2024
ce439ab
fix: remove BUG prefix for user facing issue
Reecepbcups Aug 20, 2024
63816ab
fix: cover rest of wrapped errors
Reecepbcups Aug 20, 2024
32a59be
fix: `g.grpcServer.Stop()`
Reecepbcups Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gcosmos/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@

# go build . will make a binary named gcosmos, which we never want to commit.
gcosmos

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include a comment noting what this is and why it's ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added context b369490

# grpcurl is a local binary downloaded as to not polute the user's system.
grpcurl

# example-tx are the default json Tx and signed json Tx from the test case found in the README.
# This is unique every generation and will be overriden frequently.
example-tx*
42 changes: 35 additions & 7 deletions gcosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,42 @@ but sometimes a local patch makes more sense.
Begin running the updated siampp commands from the `gcosmos` directory.

```bash
go run . init moniker
echo -n "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art" > $HOME/.simappv2/mnemonic.txt
rm -rf ~/.simappv2/
go build -o gcosmos .

go run . keys add val --recover --source $HOME/.simappv2/mnemonic.txt
./gcosmos init moniker

go run . genesis add-genesis-account val 1000000stake --keyring-backend=test
go run . genesis gentx val 1000000stake --keyring-backend=test --chain-id=gcosmos
go run . genesis collect-gentxs
# example-mnemonic address: cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl
echo -n "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art" > example-mnemonic.txt

go run . start
./gcosmos keys add val --recover --source example-mnemonic.txt
./gcosmos genesis add-genesis-account val 10000000stake --keyring-backend=test
./gcosmos genesis gentx val 1000000stake --keyring-backend=test --chain-id=gcosmos
./gcosmos genesis collect-gentxs

# rm -rf ~/.simappv2/data/application.db/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help the reader out -- why is this commented code here? Would they want to run it? If so, when?

Same on the next section at L60.

./gcosmos start --g-http-addr 127.0.0.1:26657 --g-grpc-addr 127.0.0.1:9092
```

# Interact
```bash
# GOBIN="$PWD" go install github.com/fullstorydev/grpcurl/cmd/grpcurl@v1

./grpcurl -plaintext localhost:9092 list
./grpcurl -plaintext localhost:9092 server.GordianGRPC/GetBlocksWatermark
./grpcurl -plaintext localhost:9092 server.GordianGRPC/GetValidators

./grpcurl -plaintext -d '{"address":"cosmos1r5v5srda7xfth3hn2s26txvrcrntldjumt8mhl","denom":"stake"}' localhost:9092 server.GordianGRPC/QueryAccountBalance
```

# Transaction Testing
```bash
./gcosmos tx bank send val cosmos10r39fueph9fq7a6lgswu4zdsg8t3gxlqvvvyvn 1stake --chain-id=TODO:TEMPORARY_CHAIN_ID --generate-only > example-tx.json

# TODO: get account number
./gcosmos tx sign ./example-tx.json --offline --from=val --sequence=1 --account-number=1 --chain-id=TODO:TEMPORARY_CHAIN_ID --keyring-backend=test > example-tx-signed.json

./grpcurl -plaintext -emit-defaults -d '{"tx":"'$(cat example-tx-signed.json | base64 | tr -d '\n')'"}' localhost:9092 server.GordianGRPC/SimulateTransaction

./grpcurl -plaintext -emit-defaults -d '{"tx":"'$(cat example-tx-signed.json | base64 | tr -d '\n')'"}' localhost:9092 server.GordianGRPC/SubmitTransaction
```
1 change: 1 addition & 0 deletions gcosmos/example-mnemonic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art
51 changes: 50 additions & 1 deletion gcosmos/gserver/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht"
libp2phost "github.com/libp2p/go-libp2p/core/host"
libp2ppeer "github.com/libp2p/go-libp2p/core/peer"
"github.com/rollchains/gordian/gcosmos/gserver/internal/ggrpc"
"github.com/rollchains/gordian/gcosmos/gserver/internal/gsbd"
"github.com/rollchains/gordian/gcosmos/gserver/internal/gsi"
"github.com/rollchains/gordian/gcrypto"
Expand Down Expand Up @@ -72,10 +73,13 @@ type Component struct {

seedAddrs string

httpLn net.Listener
httpLn net.Listener
grpcLn net.Listener

ms tmstore.MirrorStore
fs tmstore.FinalizationStore
httpServer *gsi.HTTPServer
grpcServer *ggrpc.GordianGRPC
}

// NewComponent returns a new server component
Expand Down Expand Up @@ -249,6 +253,25 @@ func (c *Component) Start(ctx context.Context) error {
Handler: e,
})

if c.grpcLn != nil {
// TODO; share this with the http server as a wrapper.
// https://github.com/rollchains/gordian/pull/14
c.grpcServer = ggrpc.NewGordianGRPCServer(ctx, c.log.With("sys", "grpc"), ggrpc.GRPCServerConfig{
Listener: c.grpcLn,

FinalizationStore: c.fs,
MirrorStore: c.ms,

CryptoRegistry: reg,

// debug:
TxCodec: c.txc,
AppManager: am,
TxBuf: txBuf,
Codec: c.codec,
})
}

if c.httpLn != nil {
c.httpServer = gsi.NewHTTPServer(ctx, c.log.With("sys", "http"), gsi.HTTPServerConfig{
Listener: c.httpLn,
Expand Down Expand Up @@ -305,6 +328,20 @@ func (c *Component) Stop(_ context.Context) error {
c.httpServer.Wait()
}
}
if c.grpcLn != nil {
if err := c.grpcLn.Close(); err != nil {
// If the GRPC server is closed directly,
// it will close the underlying listener,
// which will probably happen before our call to close the listener here.
// Don't log if the error already indicated the network connection was closed.
if !errors.Is(err, net.ErrClosed) {
c.log.Warn("Error closing gRPC listener", "err", err)
}
}
if c.grpcServer != nil {
c.grpcServer.Wait()
}
}
return nil
}

Expand Down Expand Up @@ -335,6 +372,16 @@ func (c *Component) Init(app serverv2.AppI[transaction.Tx], v *viper.Viper, log
c.httpLn = ln
}

// Maybe set up the GRPC server.
if grpcAddrFlag := v.GetString(grpcAddrFlag); grpcAddrFlag != "" {
ln, err := net.Listen("tcp", grpcAddrFlag)
if err != nil {
return fmt.Errorf("failed to listen for gRPC on %q: %w", grpcAddrFlag, err)
}

c.grpcLn = ln
}

c.seedAddrs = v.GetString(seedAddrsFlag)
if c.seedAddrs == "" {
c.log.Warn("No seed addresses provided; relying on incoming connections to discover peers")
Expand Down Expand Up @@ -406,6 +453,7 @@ func (c *Component) Init(app serverv2.AppI[transaction.Tx], v *viper.Viper, log

const (
httpAddrFlag = "g-http-addr"
grpcAddrFlag = "g-grpc-addr"
httpAddrFileFlag = "g-http-addr-file"

seedAddrsFlag = "g-seed-addrs"
Expand All @@ -415,6 +463,7 @@ func (c *Component) StartCmdFlags() *pflag.FlagSet {
flags := pflag.NewFlagSet("gserver", pflag.ExitOnError)

flags.String(httpAddrFlag, "", "TCP address of Gordian's introspective HTTP server; if blank, server will not be started")
flags.String(grpcAddrFlag, "", "TCP address of Gordian's introspective GRPC server; if blank, server will not be started")
flags.String(httpAddrFileFlag, "", "Write the actual Gordian HTTP listen address to the given file (useful for tests when configured to listen on :0)")

flags.String(seedAddrsFlag, "", "Newline-separated multiaddrs to connect to; if omitted, relies on incoming connections to discover peers")
Expand Down
Loading
Loading