Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
boojamya committed Feb 29, 2024
1 parent 520a483 commit 1761033
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 33 deletions.
21 changes: 18 additions & 3 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ type AppState struct {

ConfigPath string

// Depreciated in favor of LogLevel
Debug bool

LogLevel string

Logger log.Logger
}

func NewappState() *AppState {
func NewAppState() *AppState {
return &AppState{}
}

Expand All @@ -34,10 +37,22 @@ func (a *AppState) InitAppState() {
}

func (a *AppState) InitLogger() {
// info level is default
level := zerolog.InfoLevel
switch a.LogLevel {
case "debug":
level = zerolog.DebugLevel
case "warn":
level = zerolog.WarnLevel
case "error":
level = zerolog.ErrorLevel
}

// a.Debug is Depreciatred!
if a.Debug {
a.Logger = log.NewLogger(os.Stdout)
a.Logger = log.NewLogger(os.Stdout, log.LevelOption(zerolog.DebugLevel))
} else {
a.Logger = log.NewLogger(os.Stdout, log.LevelOption(zerolog.InfoLevel))
a.Logger = log.NewLogger(os.Stdout, log.LevelOption(level))
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func configShowCmd(a *AppState) *cobra.Command {
Use: "show-config",
Aliases: []string{"sc"},
Short: "Prints current configuration. By default it prints in yaml",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
a.InitAppState()
return nil
},
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s show-config --config %s
Expand Down
8 changes: 6 additions & 2 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (

const (
flagConfigPath = "config"
flagVerbose = "verbose"
flagJSON = "json"
// depreciated
flagVerbose = "verbose"
flagLogLevel = "log-level"
flagJSON = "json"
)

func addAppPersistantFlags(cmd *cobra.Command, a *AppState) *cobra.Command {
cmd.PersistentFlags().StringVar(&a.ConfigPath, flagConfigPath, defaultConfigPath, "file path of config file")
cmd.PersistentFlags().BoolVarP(&a.Debug, flagVerbose, "v", false, "use this flag to set log level to `debug`")
cmd.PersistentFlags().MarkDeprecated(flagVerbose, "depericated")
cmd.PersistentFlags().StringVar(&a.LogLevel, flagLogLevel, "info", "log level (debug, info, warn, error)")
return cmd

}
Expand Down
16 changes: 4 additions & 12 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ func Start(a *AppState) *cobra.Command {
Use: "start",
Short: "Start relaying CCTP transactions",

PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
a.InitAppState()
return nil
},
Run: func(cmd *cobra.Command, args []string) {

Expand Down Expand Up @@ -97,16 +96,14 @@ func StartProcessor(
dequeuedTx := <-processingQueue

// if this is the first time seeing this message, add it to the State
State.Mu.Lock()
tx, ok := State.Load(LookupKey(dequeuedTx.TxHash))
tx, ok := State.Load(dequeuedTx.TxHash)
if !ok {
State.Store(LookupKey(dequeuedTx.TxHash), dequeuedTx)
tx, _ = State.Load(LookupKey(dequeuedTx.TxHash))
State.Store(dequeuedTx.TxHash, dequeuedTx)
tx, _ = State.Load(dequeuedTx.TxHash)
for _, msg := range tx.Msgs {
msg.Status = types.Created
}
}
State.Mu.Unlock()

var broadcastMsgs = make(map[types.Domain][]*types.MessageState)
var requeue bool
Expand Down Expand Up @@ -269,11 +266,6 @@ func filterLowTransfers(cfg *types.Config, logger log.Logger, msg *types.Message
return false
}

func LookupKey(sourceTxHash string) string {
// return fmt.Sprintf("%s-%s", sourceTxHash, messageType)
return sourceTxHash
}

func startApi(a *AppState) {
logger := a.Logger
cfg := a.Config
Expand Down
6 changes: 0 additions & 6 deletions cmd/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func TestProcessNewLog(t *testing.T) {

time.Sleep(5 * time.Second)

cmd.State.Mu.Lock()
defer cmd.State.Mu.Unlock()
actualState, ok := cmd.State.Load(expectedState.TxHash)
require.True(t, ok)
require.Equal(t, types.Created, actualState.Msgs[0].Status)
Expand Down Expand Up @@ -79,8 +77,6 @@ func TestProcessDisabledCctpRoute(t *testing.T) {

time.Sleep(2 * time.Second)

// cmd.State.Mu.Lock()
// defer cmd.State.Mu.Unlock()
actualState, ok := cmd.State.Load(expectedState.TxHash)
require.True(t, ok)
require.Equal(t, types.Filtered, actualState.Msgs[0].Status)
Expand Down Expand Up @@ -116,8 +112,6 @@ func TestProcessInvalidDestinationCaller(t *testing.T) {

time.Sleep(2 * time.Second)

cmd.State.Mu.Lock()
defer cmd.State.Mu.Unlock()
actualState, ok := cmd.State.Load(expectedState.TxHash)
require.True(t, ok)
require.Equal(t, types.Filtered, actualState.Msgs[0].Status)
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var defaultConfigPath = "./config"
func NewRootCmd() *cobra.Command {
// Use a local app state instance scoped to the new root command,
// so that tests don't concurrently access the state.
a := NewappState()
a := NewAppState()

var rootCmd = &cobra.Command{
Use: appName,
Expand Down
4 changes: 2 additions & 2 deletions integration/eth_burn_to_noble_mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
// This test uses the Noble wallet in the config as the destination caller to ensure that
// to ensure that this instance of the relayer picks up the transaction
func TestEthBurnToNobleMint(t *testing.T) {
a := cmd.NewappState()
a.Debug = true
a := cmd.NewAppState()
a.LogLevel = "debug"
a.InitLogger()

ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions integration/noble_burn_to_eth_mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import (
// This test uses the Sepolia wallet in the config as the destination caller to ensure that
// to ensure that this instance of the relayer picks up the transaction
func TestNobleBurnToEthMint(t *testing.T) {
a := cmd.NewappState()
a.Debug = true
a := cmd.NewAppState()
a.LogLevel = "debug"
a.InitLogger()

ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions test_util/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func ConfigSetup(t *testing.T) (a *cmd.AppState, registeredDomains map[types.Dom
},
}

a = cmd.NewappState()
a.Debug = true
a = cmd.NewAppState()
a.LogLevel = "debug"
a.InitLogger()
a.Config = &testConfig

Expand Down
2 changes: 1 addition & 1 deletion types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Chain interface {
Domain() Domain

// IsDestinationCaller returns true if the specified destination caller is the minter for the specified domain OR
// if destination caller is left empty
// if destination caller is a zero byte array(left empty in deposit for burn message)
IsDestinationCaller(destinationCaller []byte) bool

// InitializeBroadcaster initializes the minter account info for the chain.
Expand Down
9 changes: 9 additions & 0 deletions types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func NewStateMap() *StateMap {

// load loads the message states tied to a specific transaction hash
func (sm *StateMap) Load(key string) (value *TxState, ok bool) {
sm.Mu.Lock()
defer sm.Mu.Unlock()

internalResult, ok := sm.internal.Load(key)
if !ok {
return nil, ok
Expand All @@ -28,10 +31,16 @@ func (sm *StateMap) Load(key string) (value *TxState, ok bool) {
}

func (sm *StateMap) Delete(key string) {
sm.Mu.Lock()
defer sm.Mu.Unlock()

sm.internal.Delete(key)
}

// store stores the message states tied to a specific transaction hash
func (sm *StateMap) Store(key string, value *TxState) {
sm.Mu.Lock()
defer sm.Mu.Unlock()

sm.internal.Store(key, value)
}

0 comments on commit 1761033

Please sign in to comment.