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

add minimal rfq relayer query interface #2772

Merged
merged 9 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
8 changes: 7 additions & 1 deletion contrib/opbot/botmd/botmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@

bot.signozClient = signoz.NewClientFromUser(handler, cfg.SignozBaseURL, cfg.SignozEmail, cfg.SignozPassword)

server.AddCommand(bot.traceCommand())
bot.addCommands(bot.traceCommand(), bot.rfqLookupCommand())

Check warning on line 30 in contrib/opbot/botmd/botmd.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/botmd.go#L30

Added line #L30 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Add test coverage for the new commands in NewBot.

The addition of new commands in the NewBot method is crucial for the bot's functionality. However, these changes are not covered by tests, which is a significant oversight given the importance of this functionality.

Tools
GitHub Check: codecov/patch

[warning] 30-30: contrib/opbot/botmd/botmd.go#L30
Added line #L30 was not covered by tests


return bot
}

func (b *Bot) addCommands(commands ...*slacker.CommandDefinition) {
for _, command := range commands {
b.server.AddCommand(command)
}

Check warning on line 38 in contrib/opbot/botmd/botmd.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/botmd.go#L35-L38

Added lines #L35 - L38 were not covered by tests
Comment on lines +35 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure test coverage for the addCommands method.

The addCommands method improves code modularity by enabling the addition of multiple commands. However, the new lines introduced in this method are also not covered by tests, which could potentially lead to untested paths in production.

Tools
GitHub Check: codecov/patch

[warning] 35-38: contrib/opbot/botmd/botmd.go#L35-L38
Added lines #L35 - L38 were not covered by tests

}

// Start starts the bot server.
// nolint: wrapcheck
func (b *Bot) Start(ctx context.Context) error {
Expand Down
135 changes: 134 additions & 1 deletion contrib/opbot/botmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/hako/durafmt"
"github.com/slack-go/slack"
"github.com/slack-io/slacker"
"github.com/synapsecns/sanguine/contrib/opbot/signoz"
"github.com/synapsecns/sanguine/ethergo/chaindata"
"github.com/synapsecns/sanguine/services/rfq/relayer/relapi"
"log"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -73,7 +77,7 @@
return
}

slackBlocks := []slack.Block{slack.NewHeaderBlock(slack.NewTextBlockObject(slack.PlainTextType, "Traces", false, false))}
slackBlocks := []slack.Block{slack.NewHeaderBlock(slack.NewTextBlockObject(slack.PlainTextType, fmt.Sprintf("Traces for %s", tags), false, false))}

Check warning on line 80 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L80

Added line #L80 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure Test Coverage for the Modified traceCommand Function.

The modified line 80, which formats the Slack message header, was not covered by tests according to the static analysis report. Consider adding unit tests to cover this functionality.

Tools
GitHub Check: codecov/patch

[warning] 80-80: contrib/opbot/botmd/commands.go#L80
Added line #L80 was not covered by tests


for _, results := range traceList {
trace := results.Data["traceID"].(string)
Expand Down Expand Up @@ -109,3 +113,132 @@
},
}
}

func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition {
return &slacker.CommandDefinition{
Command: "rfq <tx>",
Description: "find a rfq transaction by either tx hash or txid on all configured relayers",
Examples: []string{
"rfq 0x30f96b45ba689c809f7e936c140609eb31c99b182bef54fccf49778716a7e1ca",
},
Handler: func(ctx *slacker.CommandContext) {
type Status struct {
relayer string
*relapi.GetQuoteRequestStatusResponse
}

var statuses []Status
var sliceMux sync.Mutex

if len(b.cfg.RelayerURLS) == 0 {
_, err := ctx.Response().Reply("no relayer urls configured")
if err != nil {
log.Println(err)
}
return

Check warning on line 138 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L117-L138

Added lines #L117 - L138 were not covered by tests
trajan0x marked this conversation as resolved.
Show resolved Hide resolved
}

tx := ctx.Request().Param("tx")

var wg sync.WaitGroup
// 2 routines per relayer, one for tx hashh one for tx id
wg.Add(len(b.cfg.RelayerURLS) * 2)
for _, relayer := range b.cfg.RelayerURLS {
client := relapi.NewRelayerClient(b.handler, relayer)
go func() {
defer wg.Done()
res, err := client.GetQuoteRequestStatusByTxHash(ctx.Context(), tx)
if err != nil {
log.Printf("error fetching quote request status by tx hash: %v\n", err)
return
}
sliceMux.Lock()
defer sliceMux.Unlock()
statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestStatusResponse: res})

Check warning on line 157 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L141-L157

Added lines #L141 - L157 were not covered by tests
Comment on lines +155 to +157
Copy link

Choose a reason for hiding this comment

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

Consider using a more efficient way to handle concurrent writes to the statuses slice.

}()

go func() {
defer wg.Done()
res, err := client.GetQuoteRequestStatusByTxID(ctx.Context(), tx)
if err != nil {
log.Printf("error fetching quote request status by tx id: %v\n", err)
return
}
sliceMux.Lock()
defer sliceMux.Unlock()
statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestStatusResponse: res})

Check warning on line 169 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L160-L169

Added lines #L160 - L169 were not covered by tests
}()
}
wg.Wait()

if len(statuses) == 0 {
_, err := ctx.Response().Reply("no quote request found")
if err != nil {
log.Println(err)
}
return

Check warning on line 179 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L172-L179

Added lines #L172 - L179 were not covered by tests
}

var slackBlocks []slack.Block
for _, status := range statuses {
objects := []*slack.TextBlockObject{
{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*Relayer*: %s", status.relayer),
},
{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*Status*: %s", status.Status),
},
{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*TxID*: %s", toExplorerSlackLink(status.TxID)),
},
{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*OriginTxHash*: %s", toTXSlackLink(status.OriginTxHash, status.OriginChainID)),
},
}

if status.DestTxHash == (common.Hash{}).String() {
objects = append(objects, &slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "*DestTxHash*: not available",
})
} else {
objects = append(objects, &slack.TextBlockObject{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*DestTxHash*: %s", toTXSlackLink(status.DestTxHash, status.DestChainID)),
})
}

Check warning on line 213 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L182-L213

Added lines #L182 - L213 were not covered by tests

slackBlocks = append(slackBlocks, slack.NewSectionBlock(nil, objects, nil))

Check warning on line 215 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L215

Added line #L215 was not covered by tests
}

_, err := ctx.Response().ReplyBlocks(slackBlocks)
if err != nil {
log.Println(err)
}

Check warning on line 221 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L218-L221

Added lines #L218 - L221 were not covered by tests
}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove Unnecessary Trailing Newline.

There is an unnecessary trailing newline at the end of the file.

- 
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
}}
}}
Tools
GitHub Check: Lint (contrib/opbot)

[failure] 211-211:
unnecessary trailing newline (whitespace)

}

func toExplorerSlackLink(ogHash string) string {
Copy link

Choose a reason for hiding this comment

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

Typo in function name: 'toExplorerSlackLink' should be 'toExplorerSlackLink'.

rfqHash := strings.ToUpper(ogHash)
// cut off 0x
if len(rfqHash) > 0 {
rfqHash = strings.ToUpper(rfqHash[2:])
}

Check warning on line 230 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L225-L230

Added lines #L225 - L230 were not covered by tests
trajan0x marked this conversation as resolved.
Show resolved Hide resolved

return fmt.Sprintf("<https://anon.to/?https://explorer.synapseprotocol.com/tx/%s|%s>", rfqHash, ogHash)

Check warning on line 232 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L232

Added line #L232 was not covered by tests
}

// produce a salck link if the explorer exists.
Copy link

Choose a reason for hiding this comment

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

Typo in comment: 'salck' should be 'slack'.

func toTXSlackLink(txHash string, chainID uint32) string {
url := chaindata.ToTXLink(int64(chainID), txHash)
if url == "" {
return txHash
}

Check warning on line 240 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L236-L240

Added lines #L236 - L240 were not covered by tests

// TODO: remove when we can contorl unfurl
return fmt.Sprintf("<https://anon.to/?%s|%s>", url, txHash)

Check warning on line 243 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L243

Added line #L243 was not covered by tests
}
3 changes: 2 additions & 1 deletion contrib/opbot/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ type Config struct {
// inject only with init container!
SignozPassword string `yaml:"signoz_password"`
// SignozBaseURL is the base url of the signoz instance.
SignozBaseURL string `yaml:"signoz_base_url"`
SignozBaseURL string `yaml:"signoz_base_url"`
RelayerURLS []string `yaml:"rfq_relayer_urls"`
}
Loading
Loading