diff --git a/cmd/appstate.go b/cmd/appstate.go index ff415e5..158d02a 100644 --- a/cmd/appstate.go +++ b/cmd/appstate.go @@ -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{} } @@ -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)) } } diff --git a/cmd/config.go b/cmd/config.go index 27df7b0..3f878ee 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 diff --git a/cmd/flags.go b/cmd/flags.go index 10b88a8..866e45e 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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 } diff --git a/cmd/process.go b/cmd/process.go index 8a92a68..045177a 100644 --- a/cmd/process.go +++ b/cmd/process.go @@ -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) { @@ -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 @@ -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 diff --git a/cmd/process_test.go b/cmd/process_test.go index 0edaa3b..36f54c3 100644 --- a/cmd/process_test.go +++ b/cmd/process_test.go @@ -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) @@ -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) @@ -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) diff --git a/cmd/root.go b/cmd/root.go index 359e690..4fde89c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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, diff --git a/integration/eth_burn_to_noble_mint_test.go b/integration/eth_burn_to_noble_mint_test.go index 0ec1b18..025e59f 100644 --- a/integration/eth_burn_to_noble_mint_test.go +++ b/integration/eth_burn_to_noble_mint_test.go @@ -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() diff --git a/integration/noble_burn_to_eth_mint_test.go b/integration/noble_burn_to_eth_mint_test.go index d5d927b..d52d05a 100644 --- a/integration/noble_burn_to_eth_mint_test.go +++ b/integration/noble_burn_to_eth_mint_test.go @@ -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() diff --git a/test_util/setup.go b/test_util/setup.go index d84de44..0e5d5c8 100644 --- a/test_util/setup.go +++ b/test_util/setup.go @@ -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 diff --git a/types/chain.go b/types/chain.go index e2ac45e..388ed58 100644 --- a/types/chain.go +++ b/types/chain.go @@ -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. diff --git a/types/state.go b/types/state.go index ccf0413..d0f82e6 100644 --- a/types/state.go +++ b/types/state.go @@ -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 @@ -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) }