Skip to content

Commit

Permalink
Merge pull request #370 from icon-project/feat/new-api-sockets
Browse files Browse the repository at this point in the history
feat: socket apis for admin dashboard support.
  • Loading branch information
debendraoli authored Oct 17, 2024
2 parents b0b02c8 + 0143904 commit 76ed126
Show file tree
Hide file tree
Showing 27 changed files with 1,137 additions and 566 deletions.
8 changes: 4 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ builds:
- -mod=readonly
- -trimpath
ldflags:
- -s -w -X github.com/icon-project/centralized-relay/cmd.Version={{ .Tag }}
- -s -w -X github.com/icon-project/centralized-relay/relayer.Version={{ .Tag }}
- -linkmode=external
- -extldflags '-Wl,-z,muldefs -lm'
tags:
Expand All @@ -79,7 +79,7 @@ builds:
- -mod=readonly
- -trimpath
ldflags:
- -s -w -X github.com/icon-project/centralized-relay/cmd.Version={{ .Tag }}
- -s -w -X github.com/icon-project/centralized-relay/relayer.Version={{ .Tag }}
- -linkmode=external
- -extldflags "-static"
- -extldflags '-Wl,-z,muldefs -lm'
Expand All @@ -96,12 +96,12 @@ archives:
- linux-arm64
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
format: tar.gz
wrap_in_directory: false
wrap_in_directory: true

checksum:
name_template: SHA256SUMS-{{.Version}}.txt
algorithm: sha256

release:
prerelease: auto
draft: false
draft: true
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ e2e-test:
test-all:
@go test -v ./...

test-all:
@go test -v ./...

PACKAGE_NAME := github.com/icon-project/centralized-relay
GOLANG_CROSS_VERSION ?= v1.22.4
LIBWASM_VERSION ?= v2.1.0
Expand Down Expand Up @@ -78,4 +75,4 @@ release:
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
goreleaser/goreleaser-cross:${GOLANG_CROSS_VERSION} \
release --clean
release --clean
53 changes: 25 additions & 28 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import (
"github.com/icon-project/centralized-relay/relayer"
"github.com/icon-project/centralized-relay/relayer/lvldb"
"github.com/icon-project/centralized-relay/relayer/socket"
"github.com/icon-project/centralized-relay/relayer/store"
"github.com/spf13/cobra"
)

type dbState struct {
chain string
height uint64
sn uint64
page uint
limit uint
server *socket.Server
chain string
height uint64
sn uint64
txHash string
page uint
limit uint
server *socket.Server
fromHeight uint64
toHeight uint64
}

func newDBState() *dbState {
Expand Down Expand Up @@ -89,19 +91,17 @@ func (d *dbState) messagesList(app *appState) *cobra.Command {
return err
}
defer client.Close()
pg := store.NewPagination().WithPage(d.page, d.limit)
messages, err := client.GetMessageList(d.chain, pg)
messages, err := client.GetMessageList(d.chain, d.limit)
if err != nil {
return err
}

printLabels("Sn", "Src", "Dst", "Height", "Event", "Retry")
// Print messages
for _, msg := range messages.Messages {
for _, msg := range messages.Message {
fmt.Printf("%-10d %-10s %-10s %-10d %-10s %-10d \n",
msg.Sn, msg.Src, msg.Dst, msg.MessageHeight, msg.EventType, msg.Retry)
}

return nil
},
}
Expand All @@ -122,18 +122,20 @@ func (d *dbState) messagesRelay(app *appState) *cobra.Command {
if err != nil {
return err
}
result, err := client.RelayMessage(d.chain, d.height, new(big.Int).SetUint64(d.sn))
messages, err := client.RelayMessage(d.chain, d.height, d.txHash)
if err != nil {
return err
}
printLabels("Sn", "Src", "Dst", "Height", "Event", "Retry")
printValues(result.Sn, result.Src, result.Dst, result.MessageHeight, result.EventType, result.Retry)
printLabels("Sn", "Src", "Dst", "Height", "Event")
for _, msg := range messages {
printValues(msg.Sn, msg.Src, msg.Dst, msg.MessageHeight, msg.EventType)
}
return nil
},
}
d.messageMsgIDFlag(rly, true)
d.messageChainFlag(rly, true)
d.messageHeightFlag(rly)
d.messageTxHashFlag(rly)
return rly
}

Expand Down Expand Up @@ -178,6 +180,10 @@ func (d *dbState) messageHeightFlag(cmd *cobra.Command) {
cmd.Flags().Uint64Var(&d.height, "height", 0, "block height")
}

func (d *dbState) messageTxHashFlag(cmd *cobra.Command) {
cmd.Flags().StringVarP(&d.txHash, "tx_hash", "t", "", "tx hash")
}

func (d *dbState) messageChainFlag(cmd *cobra.Command, markRequired bool) {
cmd.Flags().StringVarP(&d.chain, "chain", "c", "", "message chain to select")
if markRequired {
Expand Down Expand Up @@ -221,7 +227,7 @@ func (d *dbState) blockInfo(app *appState) *cobra.Command {
}
printLabels("NID", "Height")
for _, block := range blocks {
printValues(block.Chain, block.Height)
printValues(block.Chain, block.CheckPointHeight)
}
return nil
},
Expand Down Expand Up @@ -284,19 +290,10 @@ func printLabels(labels ...any) {
}

func printValues(values ...any) {
padStr := `%-10s`
padInt := `%-10d`
padStr := `%-10v`
var valueCell string
for _, val := range values {
if _, ok := val.(string); ok {
valueCell += padStr + " "
} else if _, ok := val.(int); ok {
valueCell += padInt + " "
} else if _, ok := val.(uint); ok {
valueCell += padInt + " "
} else if _, ok := val.(uint64); ok {
valueCell += padInt + " "
}
for range values {
valueCell += padStr + " "
}
valueCell += "\n"
fmt.Printf(valueCell, values...)
Expand Down
138 changes: 138 additions & 0 deletions cmd/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package cmd

import (
"encoding/hex"
"fmt"
"strings"

"github.com/spf13/cobra"
)

type DebugState struct {
*dbState
app *appState
chain string
fromHeight uint64
toHeight uint64
}

func newDebugState(a *appState) *DebugState {
db := newDBState()
return &DebugState{
app: a,
dbState: db,
}
}

func debugCmd(a *appState) *cobra.Command {
state := newDebugState(a)
debug := &cobra.Command{
Use: "debug",
Short: "Commands for troubleshooting the relayer",
Aliases: []string{"dbg"},
Example: strings.TrimSpace(fmt.Sprintf(`$ %s dbg [command]`, appName)),
}

heightCmd := &cobra.Command{
Use: "height",
Short: "Get latest height of the chain",
}
heightCmd.AddCommand(state.getLatestHeight(a))

blockCmd := &cobra.Command{
Use: "block",
Short: "Get latest processed block of the chain",
}
blockCmd.AddCommand(state.getLatestProcessedBlock(a))

queryCmd := &cobra.Command{
Use: "query",
Short: "Query block range",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := state.getSocket(a)
if err != nil {
return err
}
defer client.Close()
if state.server != nil {
defer state.server.Close()
}
res, err := client.QueryBlockRange(state.chain, state.fromHeight, state.toHeight)
if err != nil {
return err
}
printLabels("Chain", "Sn", "Event Type", "height", "data")
for _, msg := range res.Msgs {
printValues(state.chain, msg.Sn.Text(10), msg.EventType, msg.MessageHeight, hex.EncodeToString(msg.Data))
}
return nil
},
}
queryCmd.Flags().StringVar(&state.chain, "chain", "", "Chain ID")
queryCmd.Flags().Uint64Var(&state.fromHeight, "from_height", 0, "From Height")
queryCmd.Flags().Uint64Var(&state.toHeight, "to_height", 0, "To height")
queryCmd.MarkFlagsRequiredTogether("chain", "from_height", "to_height")
debug.AddCommand(heightCmd, blockCmd, queryCmd)

return debug
}

func (c *DebugState) getLatestHeight(app *appState) *cobra.Command {
getLatestHeight := &cobra.Command{
Use: "get",
Short: "Get the latest chain height",
Aliases: []string{"g"},
Example: strings.TrimSpace(fmt.Sprintf(`$ %s dbg height get --chain [chain-id]`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := c.getSocket(app)
if err != nil {
return err
}
defer client.Close()
if c.server != nil {
defer c.server.Close()
}
res, err := client.GetLatestHeight(c.chain)
if err != nil {
return err
}
printLabels("Chain", "Latest Chain Height")
printValues(c.chain, res.Height)
return nil
},
}
getLatestHeight.Flags().StringVar(&c.chain, "chain", "", "Chain ID")
getLatestHeight.MarkFlagRequired("chain")
return getLatestHeight
}

func (c *DebugState) getLatestProcessedBlock(app *appState) *cobra.Command {
getLatestHeight := &cobra.Command{
Use: "get",
Short: "Get the last processed block height",
Aliases: []string{"g"},
Example: strings.TrimSpace(fmt.Sprintf(`$ %s dbg block get --chain [chain-id]`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := c.getSocket(app)
if err != nil {
return err
}
defer client.Close()
if c.server != nil {
defer c.server.Close()
}
res, err := client.GetBlock(c.chain)
if err != nil {
fmt.Println(err)
return err
}
printLabels("Chain", "Last Processed Block")
for _, block := range res {
printValues(block.Chain, block.CheckPointHeight)
}
return nil
},
}
getLatestHeight.Flags().StringVar(&c.chain, "chain", "", "Chain ID")
return getLatestHeight
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime/debug"
"time"

"github.com/icon-project/centralized-relay/relayer"
zaplogfmt "github.com/jsternberg/zap-logfmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -35,7 +36,6 @@ var (
}()
defaultDBName = "data"
defaultConfig = "config.yaml"
Version = "dev"
)

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -99,7 +99,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
Use: appName,
Short: "This application makes data relay between chains!",
Long: `Use this to relay xcall packet between chains using bridge contract.`,
Version: Version,
Version: relayer.Version,
Aliases: []string{"crly"},
}

Expand Down Expand Up @@ -159,6 +159,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
dbCmd(a),
keystoreCmd(a),
contractCMD(a),
debugCmd(a),
)
return rootCmd
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func startCmd(a *appState) *cobra.Command {
$ %s start # start all the registered chains
`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
a.log.Info("Starting relayer", zap.String("version", Version))
a.log.Info("Starting relayer", zap.String("version", relayer.Version))
chains := a.config.Chains.GetAll()

flushInterval, err := cmd.Flags().GetDuration(flagFlushInterval)
Expand Down
10 changes: 3 additions & 7 deletions relayer/chains/evm/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ethereum/go-ethereum"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/icon-project/centralized-relay/relayer/chains/evm/types"
relayertypes "github.com/icon-project/centralized-relay/relayer/types"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -169,22 +168,19 @@ func (p *Provider) isConnectionError(err error) bool {
strings.Contains(err.Error(), "websocket")
}

func (p *Provider) FindMessages(ctx context.Context, lbn *types.BlockNotification) ([]*relayertypes.Message, error) {
if lbn == nil && lbn.Logs == nil {
return nil, nil
}
func (p *Provider) FindMessages(ctx context.Context, logs []ethTypes.Log) ([]*relayertypes.Message, error) {
var messages []*relayertypes.Message
for _, log := range lbn.Logs {
for _, log := range logs {
message, err := p.getRelayMessageFromLog(log)
if err != nil {
return nil, err
}
p.log.Info("Detected eventlog",
zap.String("dst", message.Dst),
zap.Uint64("sn", message.Sn.Uint64()),
zap.Any("req_id", message.ReqID),
zap.String("event_type", message.EventType),
zap.String("tx_hash", log.TxHash.String()),
zap.String("target_network", message.Dst),
zap.Uint64("height", log.BlockNumber),
)
messages = append(messages, message)
Expand Down
Loading

0 comments on commit 76ed126

Please sign in to comment.