Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Nov 13, 2023
2 parents e102532 + c2bcf4c commit e9d92af
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 66 deletions.
5 changes: 3 additions & 2 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,13 @@ func initEvmKeeper(appCodec codec.Codec, keys map[string]*sdk.KVStoreKey, keeper
func initNexusKeeper(appCodec codec.Codec, keys map[string]*sdk.KVStoreKey, keepers *keeperCache) *nexusKeeper.Keeper {
// setting validator will finalize all by sealing it
// no more validators can be added
addressValidator := nexusTypes.NewAddressValidator().
addressValidators := nexusTypes.NewAddressValidators().
AddAddressValidator(evmTypes.ModuleName, evmKeeper.NewAddressValidator()).
AddAddressValidator(axelarnetTypes.ModuleName, axelarnetKeeper.NewAddressValidator(getKeeper[axelarnetKeeper.Keeper](keepers)))
addressValidators.Seal()

nexusK := nexusKeeper.NewKeeper(appCodec, keys[nexusTypes.StoreKey], keepers.getSubspace(nexusTypes.ModuleName))
nexusK.SetAddressValidator(addressValidator)
nexusK.SetAddressValidators(addressValidators)

return &nexusK
}
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ require (
github.com/stretchr/testify v1.8.4
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.8-0.20220506192307-f628bb5dc95b
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.15.0
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/mod v0.13.0
golang.org/x/sync v0.4.0
golang.org/x/text v0.13.0
golang.org/x/tools v0.14.0
golang.org/x/mod v0.14.0
golang.org/x/sync v0.5.0
golang.org/x/text v0.14.0
golang.org/x/tools v0.15.0
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
google.golang.org/genproto/googleapis/api v0.0.0-20230525234020-1aefcd67740a
google.golang.org/grpc v1.55.0
Expand Down Expand Up @@ -171,9 +171,9 @@ require (
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20230526015343-6ee61e4f9d5f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234015-3fc162c6f38a // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
16 changes: 16 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions x/nexus/keeper/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func (k Keeper) GetRecipient(ctx sdk.Context, depositAddress exported.CrossChain

// ValidateAddress validates the given cross chain address
func (k Keeper) ValidateAddress(ctx sdk.Context, address exported.CrossChainAddress) error {
validator := k.getAddressValidator().GetAddressValidator(address.Chain.Module)
if validator == nil {
return fmt.Errorf("unknown module for chain %s", address.Chain.String())
validate, err := k.getAddressValidator(address.Chain.Module)
if err != nil {
return err
}

if err := validator(ctx, address); err != nil {
if err := validate(ctx, address); err != nil {
return err
}

Expand Down
8 changes: 5 additions & 3 deletions x/nexus/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ func setup() (sdk.Context, Keeper) {
},
}

router := types.NewAddressValidator()
router.AddAddressValidator(evmTypes.ModuleName, evmkeeper.NewAddressValidator()).
addressValidators := types.NewAddressValidators()
addressValidators.AddAddressValidator(evmTypes.ModuleName, evmkeeper.NewAddressValidator()).
AddAddressValidator(axelarnetTypes.ModuleName, axelarnetkeeper.NewAddressValidator(axelarnetK))
keeper.SetAddressValidator(router)

addressValidators.Seal()
keeper.SetAddressValidators(addressValidators)

return ctx, keeper
}
Expand Down
5 changes: 3 additions & 2 deletions x/nexus/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ func TestKeeper_TransfersForChain(t *testing.T) {
funcs.MustNoErr(k.RegisterAsset(ctx, evm.Ethereum, exported.NewAsset(axelarnet.NativeAsset, false), utils.MaxUint, time.Hour))
funcs.MustNoErr(k.RegisterAsset(ctx, axelarnet.Axelarnet, exported.NewAsset(axelarnet.NativeAsset, true), utils.MaxUint, time.Hour))

nexusRouter := types.NewAddressValidator().
addressValidators := types.NewAddressValidators().
AddAddressValidator("evm", func(sdk.Context, exported.CrossChainAddress) error {
return nil
}).AddAddressValidator("axelarnet", func(sdk.Context, exported.CrossChainAddress) error {
return nil
})
k.SetAddressValidator(nexusRouter)
addressValidators.Seal()
k.SetAddressValidators(addressValidators)

}).
When("there are some pending transfers", func() {
Expand Down
30 changes: 15 additions & 15 deletions x/nexus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/utils/key"
"github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/axelar-core/x/nexus/types"
)

Expand Down Expand Up @@ -42,8 +43,8 @@ type Keeper struct {
cdc codec.BinaryCodec
params params.Subspace

addressValidator types.AddressValidator
messageRouter types.MessageRouter
addressValidators *types.AddressValidators
messageRouter types.MessageRouter
}

// NewKeeper returns a new nexus keeper
Expand All @@ -68,26 +69,25 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return p
}

// SetAddressValidator sets the nexus address validator. It will panic if called more than once
func (k *Keeper) SetAddressValidator(validator types.AddressValidator) {
if k.addressValidator != nil {
panic("validator already set")
// SetAddressValidators sets the nexus address validator. It will panic if called more than once
func (k *Keeper) SetAddressValidators(validators *types.AddressValidators) {
if !validators.IsSealed() {
panic("address validator must be sealed")
}

k.addressValidator = validator
if k.addressValidators != nil {
panic("address validator already set")
}

// In order to avoid invalid or non-deterministic behavior, we seal the validator immediately
// to prevent additionals handlers from being registered after the keeper is initialized.
k.addressValidator.Seal()
k.addressValidators = validators
}

// getAddressValidator returns the nexus address validator. If not set, it returns a sealed empty validator
func (k Keeper) getAddressValidator() types.AddressValidator {
if k.addressValidator == nil {
k.SetAddressValidator(types.NewAddressValidator())
func (k Keeper) getAddressValidator(module string) (exported.AddressValidator, error) {
if k.addressValidators == nil {
return nil, fmt.Errorf("address validator not set")
}

return k.addressValidator
return k.addressValidators.GetAddressValidator(module)
}

func (k *Keeper) SetMessageRouter(router types.MessageRouter) {
Expand Down
12 changes: 7 additions & 5 deletions x/nexus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const maxAmount int64 = 100000000000

var k keeper.Keeper

func addressValidator() types.AddressValidator {
func addressValidators() *types.AddressValidators {
axelarnetK := &axelarnetmock.BaseKeeperMock{
GetCosmosChainByNameFunc: func(ctx sdk.Context, chain exported.ChainName) (axelarnetTypes.CosmosChain, bool) {
var prefix string
Expand All @@ -47,18 +47,20 @@ func addressValidator() types.AddressValidator {
},
}

router := types.NewAddressValidator()
router.AddAddressValidator(evmTypes.ModuleName, evmkeeper.NewAddressValidator()).
validators := types.NewAddressValidators()
validators.AddAddressValidator(evmTypes.ModuleName, evmkeeper.NewAddressValidator()).
AddAddressValidator(axelarnetTypes.ModuleName, axelarnetkeeper.NewAddressValidator(axelarnetK))

return router
validators.Seal()

return validators
}

func init() {
encCfg := app.MakeEncodingConfig()
subspace := params.NewSubspace(encCfg.Codec, encCfg.Amino, sdk.NewKVStoreKey("nexusKey"), sdk.NewKVStoreKey("tNexusKey"), "nexus")
k = keeper.NewKeeper(encCfg.Codec, sdk.NewKVStoreKey("nexus"), subspace)
k.SetAddressValidator(addressValidator())
k.SetAddressValidators(addressValidators())
}

func TestLinkAddress(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/nexus/keeper/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func setup(cfg params.EncodingConfig) (nexusKeeper.Keeper, sdk.Context) {
ctx := sdk.NewContext(fake.NewMultiStore(), tmproto.Header{}, false, log.TestingLogger())

k.SetParams(ctx, types.DefaultParams())
k.SetAddressValidator(addressValidator())
k.SetAddressValidators(addressValidators())

// register asset in ChainState
for _, chain := range chains {
Expand Down
2 changes: 1 addition & 1 deletion x/nexus/keeper/wasm_message_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestNewMessageRoute(t *testing.T) {
msg = randMsg(nexus.Processing, true)
}).
Then("should return error", func(t *testing.T) {
assert.ErrorContains(t, route(ctx, nexus.RoutingContext{}, msg), "asset is not supported")
assert.ErrorContains(t, route(ctx, nexus.RoutingContext{}, msg), "asset transfer is not supported")
}),

When("the message has no asset", func() {
Expand Down
51 changes: 26 additions & 25 deletions x/nexus/types/address_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ import (
"github.com/axelarnetwork/axelar-core/x/nexus/exported"
)

// AddressValidator implements a AddressValidator based on module name.
type AddressValidator interface {
AddAddressValidator(module string, validator exported.AddressValidator) AddressValidator
HasAddressValidator(module string) bool
GetAddressValidator(module string) exported.AddressValidator
Seal()
}

var _ AddressValidator = (*addressValidator)(nil)

type addressValidator struct {
// AddressValidators collects all registered address validators by module
type AddressValidators struct {
validators map[string]exported.AddressValidator
sealed bool
}

// NewAddressValidator creates a new AddressValidator interface instance
func NewAddressValidator() AddressValidator {
return &addressValidator{
// NewAddressValidators returns a new AddressValidators instance
func NewAddressValidators() *AddressValidators {
return &AddressValidators{
validators: make(map[string]exported.AddressValidator),
}
}

// Seal prevents additional validators from being added
func (r *addressValidator) Seal() {
func (r *AddressValidators) Seal() {
if r.sealed {
panic("cannot seal address validator (validator already sealed)")
}

r.sealed = true
}

// IsSealed returns true if the validator is sealed
func (r *AddressValidators) IsSealed() bool {
return r.sealed
}

// AddAddressValidator registers a validator for a given path
// panics if the validator is sealed, module is an empty string, or if the module has been registered already
func (r *addressValidator) AddAddressValidator(module string, validator exported.AddressValidator) AddressValidator {
func (r *AddressValidators) AddAddressValidator(module string, validator exported.AddressValidator) *AddressValidators {
if r.sealed {
panic("cannot add validator (validator sealed)")
}
Expand All @@ -44,24 +44,25 @@ func (r *addressValidator) AddAddressValidator(module string, validator exported
panic("module name cannot be an empty string")
}

if r.HasAddressValidator(module) {
if r.hasAddressValidator(module) {
panic(fmt.Sprintf("validator for module %s has already been registered", module))
}

r.validators[module] = validator
return r
}

// HasAddressValidator returns true if a validator is registered for the given module
func (r *addressValidator) HasAddressValidator(module string) bool {
return r.validators[module] != nil
// hasAddressValidator returns true if a validator is registered for the given module
func (r *AddressValidators) hasAddressValidator(module string) bool {
_, err := r.GetAddressValidator(module)
return err == nil
}

// GetAddressValidator returns a validator for a given module
func (r *addressValidator) GetAddressValidator(module string) exported.AddressValidator {
if !r.HasAddressValidator(module) {
panic(fmt.Sprintf("validator for module \"%s\" not registered", module))
func (r *AddressValidators) GetAddressValidator(module string) (exported.AddressValidator, error) {
validate, ok := r.validators[module]
if !ok || validate == nil {
return nil, fmt.Errorf("validator for module \"%s\" not registered", module)
}

return r.validators[module]
return validate, nil
}

0 comments on commit e9d92af

Please sign in to comment.