Skip to content

Commit

Permalink
Remove redundant methods from app context
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Nov 22, 2024
1 parent e3f43d8 commit 578bf5e
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 90 deletions.
2 changes: 0 additions & 2 deletions zetaclient/chains/evm/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ func getAppContext(

// feed chain params
err := appContext.Update(
observertypes.Keygen{},
[]chains.Chain{evmChain, chains.ZetaChainMainnet},
nil,
chainParams,
"tssPubKey",
*sample.CrosschainFlags(),
)
require.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions zetaclient/chains/evm/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,11 @@ func makeCtx(t *testing.T) context.Context {
bscParams := mocks.MockChainParams(chains.BscMainnet.ChainId, 10)

err := app.Update(
observertypes.Keygen{},
[]chains.Chain{chains.BscMainnet, chains.ZetaChainMainnet},
nil,
map[int64]*observertypes.ChainParams{
chains.BscMainnet.ChainId: &bscParams,
},
"tssPubKey",
observertypes.CrosschainFlags{},
)
require.NoError(t, err, "unable to update app context")
Expand Down
57 changes: 6 additions & 51 deletions zetaclient/context/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,25 @@ type AppContext struct {
// config is the config of the app
config config.Config

// logger is the logger of the app
logger zerolog.Logger

// chainRegistry is a registry of supported chains
chainRegistry *ChainRegistry

// currentTssPubKey is the current tss pubKey
currentTssPubKey string

// crosschainFlags is the current crosschain flags state
crosschainFlags observertypes.CrosschainFlags

// keygen is the current tss keygen state
keygen observertypes.Keygen
// logger is the logger of the app
logger zerolog.Logger

mu sync.RWMutex
}

// New creates and returns new empty AppContext
func New(cfg config.Config, relayerKeyPasswords map[string]string, logger zerolog.Logger) *AppContext {
return &AppContext{
config: cfg,
logger: logger.With().Str("module", "appcontext").Logger(),

chainRegistry: NewChainRegistry(relayerKeyPasswords),

crosschainFlags: observertypes.CrosschainFlags{},
currentTssPubKey: "",
keygen: observertypes.Keygen{},

mu: sync.RWMutex{},
config: cfg,
chainRegistry: NewChainRegistry(relayerKeyPasswords),
crosschainFlags: observertypes.CrosschainFlags{},
logger: logger.With().Str("module", "appcontext").Logger(),
}
}

Expand Down Expand Up @@ -102,32 +90,6 @@ func (a *AppContext) IsInboundObservationEnabled() bool {
return a.GetCrossChainFlags().IsInboundEnabled
}

// GetKeygen returns the current keygen
func (a *AppContext) GetKeygen() observertypes.Keygen {
a.mu.RLock()
defer a.mu.RUnlock()

var copiedPubKeys []string
if a.keygen.GranteePubkeys != nil {
copiedPubKeys = make([]string, len(a.keygen.GranteePubkeys))
copy(copiedPubKeys, a.keygen.GranteePubkeys)
}

return observertypes.Keygen{
Status: a.keygen.Status,
GranteePubkeys: copiedPubKeys,
BlockNumber: a.keygen.BlockNumber,
}
}

// GetCurrentTssPubKey returns the current tss pubKey.
func (a *AppContext) GetCurrentTssPubKey() string {
a.mu.RLock()
defer a.mu.RUnlock()

return a.currentTssPubKey
}

// GetCrossChainFlags returns crosschain flags
func (a *AppContext) GetCrossChainFlags() observertypes.CrosschainFlags {
a.mu.RLock()
Expand All @@ -139,10 +101,8 @@ func (a *AppContext) GetCrossChainFlags() observertypes.CrosschainFlags {
// Update updates AppContext and params for all chains
// this must be the ONLY function that writes to AppContext
func (a *AppContext) Update(
keygen observertypes.Keygen,
freshChains, additionalChains []chains.Chain,
freshChainParams map[int64]*observertypes.ChainParams,
tssPubKey string,
crosschainFlags observertypes.CrosschainFlags,
) error {
// some sanity checks
Expand All @@ -151,9 +111,6 @@ func (a *AppContext) Update(
return fmt.Errorf("no chains present")
case len(freshChainParams) == 0:
return fmt.Errorf("no chain params present")
case tssPubKey == "" && a.currentTssPubKey != "":
// note that if we're doing a fresh start, we ALLOW an empty tssPubKey
return fmt.Errorf("tss pubkey is empty")
case len(additionalChains) > 0:
for _, c := range additionalChains {
if !c.IsExternal {
Expand All @@ -171,8 +128,6 @@ func (a *AppContext) Update(
defer a.mu.Unlock()

a.crosschainFlags = crosschainFlags
a.keygen = keygen
a.currentTssPubKey = tssPubKey

return nil
}
Expand Down
22 changes: 6 additions & 16 deletions zetaclient/context/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ func TestAppContext(t *testing.T) {
testCfg = config.New(false)
logger = zerolog.New(zerolog.NewTestWriter(t))

keyGen = types.Keygen{
Status: types.KeygenStatus_KeyGenSuccess,
GranteePubkeys: []string{"testPubKey1"},
BlockNumber: 123,
}
ccFlags = types.CrosschainFlags{
IsInboundEnabled: true,
IsOutboundEnabled: true,
GasPriceIncreaseFlags: nil,
}
ttsPubKey = "tssPubKeyTest"
)

testCfg.BTCChainConfigs[111] = config.BTCConfig{RPCUsername: "satoshi"}
Expand Down Expand Up @@ -64,8 +58,6 @@ func TestAppContext(t *testing.T) {
require.ErrorIs(t, err, ErrChainNotFound)

require.Equal(t, testCfg, appContext.Config())
require.Empty(t, appContext.GetKeygen())
require.Empty(t, appContext.GetCurrentTssPubKey())
require.Empty(t, appContext.GetCrossChainFlags())
require.False(t, appContext.IsInboundObservationEnabled())
require.False(t, appContext.IsOutboundObservationEnabled())
Expand All @@ -89,15 +81,13 @@ func TestAppContext(t *testing.T) {
}

// ACT
err = appContext.Update(keyGen, newChains, additionalChains, chainParams, ttsPubKey, ccFlags)
err = appContext.Update(newChains, additionalChains, chainParams, ccFlags)

// ASSERT
require.NoError(t, err)

// Check getters
assert.Equal(t, testCfg, appContext.Config())
assert.Equal(t, keyGen, appContext.GetKeygen())
assert.Equal(t, ttsPubKey, appContext.GetCurrentTssPubKey())
assert.Equal(t, ccFlags, appContext.GetCrossChainFlags())
assert.True(t, appContext.IsInboundObservationEnabled())
assert.True(t, appContext.IsOutboundObservationEnabled())
Expand Down Expand Up @@ -132,7 +122,7 @@ func TestAppContext(t *testing.T) {
{
name: "update with empty chains results in an error",
act: func(a *AppContext) error {
return appContext.Update(keyGen, newChains, nil, nil, ttsPubKey, ccFlags)
return appContext.Update(newChains, nil, nil, ccFlags)
},
assert: func(t *testing.T, a *AppContext, err error) {
assert.ErrorContains(t, err, "no chain params present")
Expand All @@ -153,7 +143,7 @@ func TestAppContext(t *testing.T) {
chainParamsWithOpt := maps.Clone(chainParams)
chainParamsWithOpt[opParams.ChainId] = opParams

return a.Update(keyGen, chainsWithOpt, additionalChains, chainParamsWithOpt, ttsPubKey, ccFlags)
return a.Update(chainsWithOpt, additionalChains, chainParamsWithOpt, ccFlags)
},
assert: func(t *testing.T, a *AppContext, err error) {
assert.ErrorIs(t, err, ErrChainNotSupported)
Expand All @@ -164,7 +154,7 @@ func TestAppContext(t *testing.T) {
name: "trying to add zeta chain without chain params is allowed",
act: func(a *AppContext) error {
chainsWithZeta := append(newChains, chains.ZetaChainMainnet)
return a.Update(keyGen, chainsWithZeta, additionalChains, chainParams, ttsPubKey, ccFlags)
return a.Update(chainsWithZeta, additionalChains, chainParams, ccFlags)
},
assert: func(t *testing.T, a *AppContext, err error) {
assert.NoError(t, err)
Expand All @@ -186,7 +176,7 @@ func TestAppContext(t *testing.T) {

chainsWithZeta := append(newChains, chains.ZetaChainMainnet)

return a.Update(keyGen, chainsWithZeta, additionalChains, chainParamsWithZeta, ttsPubKey, ccFlags)
return a.Update(chainsWithZeta, additionalChains, chainParamsWithZeta, ccFlags)
},
assert: func(t *testing.T, a *AppContext, err error) {
assert.NoError(t, err)
Expand All @@ -209,7 +199,7 @@ func TestAppContext(t *testing.T) {
updatedChainParams[maticParams.ChainId] = maticParams
delete(updatedChainParams, chains.ZetaChainMainnet.ChainId)

return a.Update(keyGen, newChains, additionalChains, updatedChainParams, ttsPubKey, ccFlags)
return a.Update(newChains, additionalChains, updatedChainParams, ccFlags)
},
assert: func(t *testing.T, a *AppContext, err error) {
assert.ErrorContains(t, err, "unable to locate fresh chain 137 based on chain params")
Expand Down
2 changes: 0 additions & 2 deletions zetaclient/orchestrator/bootstap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,9 @@ func mustUpdateAppContext(
chainParams map[int64]*observertypes.ChainParams,
) {
err := app.Update(
app.GetKeygen(),
chains,
additionalChains,
chainParams,
"tssPubKey",
app.GetCrossChainFlags(),
)

Expand Down
14 changes: 1 addition & 13 deletions zetaclient/orchestrator/contextupdater.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,11 @@ func UpdateAppContext(ctx context.Context, app *zctx.AppContext, zc Zetacore, lo
return errors.Wrap(err, "unable to fetch chain params")
}

keyGen, err := zc.GetKeyGen(ctx)
if err != nil {
return errors.Wrap(err, "unable to fetch keygen from zetacore")
}

crosschainFlags, err := zc.GetCrosschainFlags(ctx)
if err != nil {
return errors.Wrap(err, "unable to fetch crosschain flags from zetacore")
}

tss, err := zc.GetTSS(ctx)
if err != nil {
return errors.Wrap(err, "unable to fetch current TSS")
}

freshParams := make(map[int64]*observertypes.ChainParams, len(chainParams))

// check and update chain params for each chain
Expand All @@ -117,7 +107,7 @@ func UpdateAppContext(ctx context.Context, app *zctx.AppContext, zc Zetacore, lo
continue
}

if err := observertypes.ValidateChainParams(cp); err != nil {
if err = observertypes.ValidateChainParams(cp); err != nil {
logger.Warn().Err(err).Int64("chain.id", cp.ChainId).Msg("Skipping invalid chain params")
continue
}
Expand All @@ -126,11 +116,9 @@ func UpdateAppContext(ctx context.Context, app *zctx.AppContext, zc Zetacore, lo
}

return app.Update(
keyGen,
supportedChains,
additionalChains,
freshParams,
tss.GetTssPubkey(),
crosschainFlags,
)
}
Expand Down
2 changes: 0 additions & 2 deletions zetaclient/orchestrator/contextupdater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func Test_UpdateAppContext(t *testing.T) {
zetacore.On("GetSupportedChains", mock.Anything).Return(newChains, nil)
zetacore.On("GetAdditionalChains", mock.Anything).Return(nil, nil)
zetacore.On("GetChainParams", mock.Anything).Return(newParams, nil)
zetacore.On("GetKeyGen", mock.Anything).Return(observertypes.Keygen{}, nil)
zetacore.On("GetCrosschainFlags", mock.Anything).Return(ccFlags, nil)
zetacore.On("GetTSS", mock.Anything).Return(observertypes.TSS{TssPubkey: "0x123"}, nil)

// ACT
err := UpdateAppContext(ctx, app, zetacore, logger)
Expand Down
2 changes: 0 additions & 2 deletions zetaclient/orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,9 @@ func createAppContext(t *testing.T, chainsOrParams ...any) *zctx.AppContext {

// feed chain params
err := appContext.Update(
observertypes.Keygen{},
supportedChains,
nil,
params,
"tssPubKey",
*ccFlags,
)
require.NoError(t, err, "failed to update app context")
Expand Down

0 comments on commit 578bf5e

Please sign in to comment.