Skip to content

Commit

Permalink
Add more test cases for AppContext
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jul 1, 2024
1 parent 718f04d commit b48aca5
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zetaclient/config/config_chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "github.com/zeta-chain/zetacore/pkg/chains"
import (
"sync"

"github.com/zeta-chain/zetacore/pkg/chains"
)

const (
MaxBlocksPerPeriod = 100
Expand Down Expand Up @@ -32,6 +36,7 @@ func GetERC20CustodyABI() string {
// It is initialize with default chain configs
func New() Config {
return Config{
cfgLock: &sync.RWMutex{},
EVMChainConfigs: evmChainsConfigs,
BitcoinConfig: bitcoinConfigRegnet,
}
Expand Down
124 changes: 124 additions & 0 deletions zetaclient/context/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/pkg/chains"
Expand Down Expand Up @@ -367,6 +368,129 @@ func TestIsInboundObservationEnabled(t *testing.T) {
})
}

func TestGetBTCChainAndConfig(t *testing.T) {
logger := zerolog.Nop()

emptyConfig := config.NewConfig()
nonEmptyConfig := config.New()

assertEmpty := func(t *testing.T, chain chains.Chain, btcConfig config.BTCConfig, enabled bool) {
assert.Empty(t, chain)
assert.Empty(t, btcConfig)
assert.False(t, enabled)
}

for _, tt := range []struct {
name string
cfg config.Config
setup func(app *context.AppContext)
assert func(t *testing.T, chain chains.Chain, btcConfig config.BTCConfig, enabled bool)
}{
{
name: "no btc config",
cfg: emptyConfig,
setup: nil,
assert: assertEmpty,
},
{
name: "btc config exists, but not chain params are set",
cfg: nonEmptyConfig,
setup: nil,
assert: assertEmpty,
},
{
name: "btc config exists but chain is invalid",
cfg: nonEmptyConfig,
setup: func(app *context.AppContext) {
app.Update(
&observertypes.Keygen{},
[]chains.Chain{},
nil,
&observertypes.ChainParams{ChainId: 123},
"",
observertypes.CrosschainFlags{},
nil,
true,
)
},
assert: assertEmpty,
},
{
name: "btc config exists and chain params are set",
cfg: nonEmptyConfig,
setup: func(app *context.AppContext) {
app.Update(
&observertypes.Keygen{},
[]chains.Chain{},
nil,
&observertypes.ChainParams{ChainId: chains.BitcoinMainnet.ChainId},
"",
observertypes.CrosschainFlags{},
nil,
true,
)
},
assert: func(t *testing.T, chain chains.Chain, btcConfig config.BTCConfig, enabled bool) {
assert.Equal(t, chains.BitcoinMainnet.ChainId, chain.ChainId)
assert.Equal(t, "smoketest", btcConfig.RPCUsername)
assert.True(t, enabled)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
// ARRANGE
// Given app context
appContext := context.New(tt.cfg, logger)

// And optional setup
if tt.setup != nil {
tt.setup(appContext)
}

// ACT
chain, btcConfig, enabled := appContext.GetBTCChainAndConfig()

// ASSERT
tt.assert(t, chain, btcConfig, enabled)
})
}
}

func TestGetBlockHeaderEnabledChains(t *testing.T) {
// ARRANGE
// Given app config
appContext := context.New(config.New(), zerolog.Nop())

// That was eventually updated
appContext.Update(
&observertypes.Keygen{},
[]chains.Chain{},
nil,
&observertypes.ChainParams{ChainId: chains.BitcoinMainnet.ChainId},
"",
observertypes.CrosschainFlags{},
[]lightclienttypes.HeaderSupportedChain{
{ChainId: 1, Enabled: true},
},
true,
)

// ACT #1 (found)
chain, found := appContext.GetBlockHeaderEnabledChains(1)

// ASSERT #1
assert.True(t, found)
assert.Equal(t, int64(1), chain.ChainId)
assert.True(t, chain.Enabled)

// ACT #2 (not found)
chain, found = appContext.GetBlockHeaderEnabledChains(2)

// ASSERT #2
assert.False(t, found)
assert.Empty(t, chain)
}

func makeAppContext(
evmChain chains.Chain,
evmChainParams *observertypes.ChainParams,
Expand Down

0 comments on commit b48aca5

Please sign in to comment.