Skip to content

Commit

Permalink
[CT-1259] account plus module code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy04 committed Sep 30, 2024
1 parent 3adc282 commit 419dc00
Show file tree
Hide file tree
Showing 23 changed files with 189 additions and 173 deletions.
2 changes: 1 addition & 1 deletion protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ func New(

// Initialize authenticators
app.AuthenticatorManager = authenticator.NewAuthenticatorManager()
app.AuthenticatorManager.InitializeAuthenticators([]authenticator.Authenticator{
app.AuthenticatorManager.InitializeAuthenticators([]accountplusmoduletypes.Authenticator{
authenticator.NewSignatureVerification(app.AccountKeeper),
})
app.AccountPlusKeeper = *accountplusmodulekeeper.NewKeeper(
Expand Down
3 changes: 1 addition & 2 deletions protocol/x/accountplus/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
txsigning "cosmossdk.io/x/tx/signing"

"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/authenticator"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
Expand Down Expand Up @@ -133,7 +132,7 @@ func (ad AuthenticatorDecorator) AnteHandle(
}

// Generate the authentication request data
authenticationRequest, err := authenticator.GenerateAuthenticationRequest(
authenticationRequest, err := lib.GenerateAuthenticationRequest(
ctx,
ad.cdc,
ad.accountKeeper,
Expand Down
17 changes: 9 additions & 8 deletions protocol/x/accountplus/authenticator/all_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
)

type AllOf struct {
SubAuthenticators []Authenticator
SubAuthenticators []types.Authenticator
am *AuthenticatorManager
signatureAssignment SignatureAssignment
}

var _ Authenticator = &AllOf{}
var _ types.Authenticator = &AllOf{}

func NewAllOf(am *AuthenticatorManager) AllOf {
return AllOf{
am: am,
SubAuthenticators: []Authenticator{},
SubAuthenticators: []types.Authenticator{},
signatureAssignment: Single,
}
}

func NewPartitionedAllOf(am *AuthenticatorManager) AllOf {
return AllOf{
am: am,
SubAuthenticators: []Authenticator{},
SubAuthenticators: []types.Authenticator{},
signatureAssignment: Partitioned,
}
}
Expand All @@ -47,7 +48,7 @@ func (aoa AllOf) StaticGas() uint64 {
return totalGas
}

func (aoa AllOf) Initialize(config []byte) (Authenticator, error) {
func (aoa AllOf) Initialize(config []byte) (types.Authenticator, error) {
var initDatas []SubAuthenticatorInitData
if err := json.Unmarshal(config, &initDatas); err != nil {
return nil, errorsmod.Wrap(err, "failed to parse sub-authenticators initialization data")
Expand All @@ -74,7 +75,7 @@ func (aoa AllOf) Initialize(config []byte) (Authenticator, error) {
return aoa, nil
}

func (aoa AllOf) Authenticate(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AllOf) Authenticate(ctx sdk.Context, request types.AuthenticationRequest) error {
if len(aoa.SubAuthenticators) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "no sub-authenticators provided")
}
Expand Down Expand Up @@ -104,11 +105,11 @@ func (aoa AllOf) Authenticate(ctx sdk.Context, request AuthenticationRequest) er
return nil
}

func (aoa AllOf) Track(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AllOf) Track(ctx sdk.Context, request types.AuthenticationRequest) error {
return subTrack(ctx, request, aoa.SubAuthenticators)
}

func (aoa AllOf) ConfirmExecution(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AllOf) ConfirmExecution(ctx sdk.Context, request types.AuthenticationRequest) error {
var signatures [][]byte
var err error
if aoa.signatureAssignment == Partitioned {
Expand Down
17 changes: 9 additions & 8 deletions protocol/x/accountplus/authenticator/any_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
)

type SignatureAssignment string
Expand All @@ -19,25 +20,25 @@ const (
)

type AnyOf struct {
SubAuthenticators []Authenticator
SubAuthenticators []types.Authenticator
am *AuthenticatorManager
signatureAssignment SignatureAssignment
}

var _ Authenticator = &AnyOf{}
var _ types.Authenticator = &AnyOf{}

func NewAnyOf(am *AuthenticatorManager) AnyOf {
return AnyOf{
am: am,
SubAuthenticators: []Authenticator{},
SubAuthenticators: []types.Authenticator{},
signatureAssignment: Single,
}
}

func NewPartitionedAnyOf(am *AuthenticatorManager) AnyOf {
return AnyOf{
am: am,
SubAuthenticators: []Authenticator{},
SubAuthenticators: []types.Authenticator{},
signatureAssignment: Partitioned,
}
}
Expand All @@ -57,7 +58,7 @@ func (aoa AnyOf) StaticGas() uint64 {
return totalGas
}

func (aoa AnyOf) Initialize(config []byte) (Authenticator, error) {
func (aoa AnyOf) Initialize(config []byte) (types.Authenticator, error) {
// Decode the initialization data for each sub-authenticator
var initDatas []SubAuthenticatorInitData
if err := json.Unmarshal(config, &initDatas); err != nil {
Expand Down Expand Up @@ -86,7 +87,7 @@ func (aoa AnyOf) Initialize(config []byte) (Authenticator, error) {
return aoa, nil
}

func (aoa AnyOf) Authenticate(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AnyOf) Authenticate(ctx sdk.Context, request types.AuthenticationRequest) error {
if len(aoa.SubAuthenticators) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "no sub-authenticators provided")
}
Expand Down Expand Up @@ -134,13 +135,13 @@ func (aoa AnyOf) Authenticate(ctx sdk.Context, request AuthenticationRequest) er
return nil
}

func (aoa AnyOf) Track(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AnyOf) Track(ctx sdk.Context, request types.AuthenticationRequest) error {
return subTrack(ctx, request, aoa.SubAuthenticators)
}

// ConfirmExecution is called on all sub-authenticators, but only the changes
// made by the authenticator that succeeds are written.
func (aoa AnyOf) ConfirmExecution(ctx sdk.Context, request AuthenticationRequest) error {
func (aoa AnyOf) ConfirmExecution(ctx sdk.Context, request types.AuthenticationRequest) error {
var signatures [][]byte
var err error

Expand Down
11 changes: 6 additions & 5 deletions protocol/x/accountplus/authenticator/clob_pair_id_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

var _ Authenticator = &ClobPairIdFilter{}
var _ types.Authenticator = &ClobPairIdFilter{}

// ClobPairIdFilter filters incoming messages based on a whitelist of clob pair ids.
// It ensures that only messages with whitelisted clob pair ids are allowed.
Expand All @@ -35,7 +36,7 @@ func (m ClobPairIdFilter) StaticGas() uint64 {

// Initialize sets up the authenticator with the given configuration,
// which should be a list of clob pair ids separated by a predefined separator.
func (m ClobPairIdFilter) Initialize(config []byte) (Authenticator, error) {
func (m ClobPairIdFilter) Initialize(config []byte) (types.Authenticator, error) {
strSlice := strings.Split(string(config), SEPARATOR)

m.whitelist = make(map[uint32]struct{})
Expand All @@ -50,12 +51,12 @@ func (m ClobPairIdFilter) Initialize(config []byte) (Authenticator, error) {
}

// Track is a no-op in this implementation but can be used to track message handling.
func (m ClobPairIdFilter) Track(ctx sdk.Context, request AuthenticationRequest) error {
func (m ClobPairIdFilter) Track(ctx sdk.Context, request types.AuthenticationRequest) error {
return nil
}

// Authenticate checks if the message's clob pair ids are in the whitelist.
func (m ClobPairIdFilter) Authenticate(ctx sdk.Context, request AuthenticationRequest) error {
func (m ClobPairIdFilter) Authenticate(ctx sdk.Context, request types.AuthenticationRequest) error {
// Collect the clob pair ids from the request.
requestOrderIds := make([]uint32, 0)
switch msg := request.Msg.(type) {
Expand Down Expand Up @@ -87,7 +88,7 @@ func (m ClobPairIdFilter) Authenticate(ctx sdk.Context, request AuthenticationRe
}

// ConfirmExecution confirms the execution of a message. Currently, it always confirms.
func (m ClobPairIdFilter) ConfirmExecution(ctx sdk.Context, request AuthenticationRequest) error {
func (m ClobPairIdFilter) ConfirmExecution(ctx sdk.Context, request types.AuthenticationRequest) error {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"

"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/authenticator"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/lib"

"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *ClobPairIdFilterTest) TestFilter() {
sigModeHandler := s.EncodingConfig.TxConfig.SignModeHandler()
tx, err := s.GenSimpleTx([]sdk.Msg{tt.msg}, []cryptotypes.PrivKey{s.TestPrivKeys[0]})
s.Require().NoError(err)
request, err := authenticator.GenerateAuthenticationRequest(
request, err := lib.GenerateAuthenticationRequest(
s.Ctx,
s.tApp.App.AppCodec(),
ak,
Expand Down
5 changes: 3 additions & 2 deletions protocol/x/accountplus/authenticator/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
)

type SubAuthenticatorInitData struct {
Expand All @@ -16,8 +17,8 @@ type SubAuthenticatorInitData struct {

func subTrack(
ctx sdk.Context,
request AuthenticationRequest,
subAuthenticators []Authenticator,
request types.AuthenticationRequest,
subAuthenticators []types.Authenticator,
) error {
baseId := request.AuthenticatorId
for id, auth := range subAuthenticators {
Expand Down
Loading

0 comments on commit 419dc00

Please sign in to comment.