From 34fef8f45b877d40a22ba2a0435778674916b23c Mon Sep 17 00:00:00 2001 From: David Terpay <35130517+davidterpay@users.noreply.github.com> Date: Thu, 16 Mar 2023 09:38:59 -0400 Subject: [PATCH] [ENG-597]: Front-running Protection (#27) --- proto/pob/auction/v1/genesis.proto | 3 + x/auction/keeper/auction.go | 11 ++- x/auction/keeper/auction_test.go | 26 +++++--- x/auction/keeper/keeper.go | 10 +++ x/auction/types/genesis.pb.go | 104 ++++++++++++++++++++--------- x/auction/types/params.go | 25 ++++--- 6 files changed, 128 insertions(+), 51 deletions(-) diff --git a/proto/pob/auction/v1/genesis.proto b/proto/pob/auction/v1/genesis.proto index f82b7b0..8098a88 100644 --- a/proto/pob/auction/v1/genesis.proto +++ b/proto/pob/auction/v1/genesis.proto @@ -39,4 +39,7 @@ message Params { (amino.dont_omitempty) = true, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins" ]; + // front_running_protection specifies whether front running and sandwich attack protection + // is enabled. + bool front_running_protection = 6; } diff --git a/x/auction/keeper/auction.go b/x/auction/keeper/auction.go index 1ac7a41..c093d1d 100644 --- a/x/auction/keeper/auction.go +++ b/x/auction/keeper/auction.go @@ -25,11 +25,18 @@ func (k Keeper) ValidateAuctionMsg(ctx sdk.Context, bidder sdk.AccAddress, bid s return err } - // Validate the bundle of transactions. - if err := k.ValidateAuctionBundle(ctx, bidder, transactions); err != nil { + // Validate the bundle of transactions if front-running protection is enabled. + protectionEnabled, err := k.FrontRunningProtectionEnabled(ctx) + if err != nil { return err } + if protectionEnabled { + if err := k.ValidateAuctionBundle(ctx, bidder, transactions); err != nil { + return err + } + } + return nil } diff --git a/x/auction/keeper/auction_test.go b/x/auction/keeper/auction_test.go index 5b0421f..639487d 100644 --- a/x/auction/keeper/auction_test.go +++ b/x/auction/keeper/auction_test.go @@ -21,10 +21,11 @@ func (suite *IntegrationTestSuite) TestValidateAuctionMsg() { bid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000))) // Auction params - maxBundleSize uint32 = 10 - reserveFee = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000))) - minBuyInFee = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000))) - escrowAddress = sdk.AccAddress([]byte("escrow")) + maxBundleSize uint32 = 10 + reserveFee = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000))) + minBuyInFee = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000))) + escrowAddress = sdk.AccAddress([]byte("escrow")) + frontRunningProtection = true ) rnd := rand.New(rand.NewSource(time.Now().Unix())) @@ -114,6 +115,14 @@ func (suite *IntegrationTestSuite) TestValidateAuctionMsg() { }, false, }, + { + "disabled front-running protection", + func() { + accounts = RandomAccounts(rnd, 10) + frontRunningProtection = false + }, + true, + }, } for _, tc := range cases { @@ -134,10 +143,11 @@ func (suite *IntegrationTestSuite) TestValidateAuctionMsg() { suite.authorityAccount.String(), ) params := auctiontypes.Params{ - MaxBundleSize: maxBundleSize, - ReserveFee: reserveFee, - MinBuyInFee: minBuyInFee, - EscrowAccountAddress: escrowAddress.String(), + MaxBundleSize: maxBundleSize, + ReserveFee: reserveFee, + MinBuyInFee: minBuyInFee, + EscrowAccountAddress: escrowAddress.String(), + FrontRunningProtection: frontRunningProtection, } suite.auctionKeeper.SetParams(suite.ctx, params) diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 269e94e..09ae15d 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -139,3 +139,13 @@ func (k Keeper) GetMinBidIncrement(ctx sdk.Context) (sdk.Coins, error) { return params.MinBidIncrement, nil } + +// FrontRunningProtectionEnabled returns true if front-running protection is enabled. +func (k Keeper) FrontRunningProtectionEnabled(ctx sdk.Context) (bool, error) { + params, err := k.GetParams(ctx) + if err != nil { + return false, err + } + + return params.FrontRunningProtection, nil +} diff --git a/x/auction/types/genesis.pb.go b/x/auction/types/genesis.pb.go index 58a69bf..881ec9f 100644 --- a/x/auction/types/genesis.pb.go +++ b/x/auction/types/genesis.pb.go @@ -86,6 +86,9 @@ type Params struct { // min_bid_increment specifies the minimum amount that the next bid must be // greater than the previous bid. MinBidIncrement github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,5,rep,name=min_bid_increment,json=minBidIncrement,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"min_bid_increment"` + // front_running_protection specifies whether front running and sandwich attack protection + // is enabled. + FrontRunningProtection bool `protobuf:"varint,6,opt,name=front_running_protection,json=frontRunningProtection,proto3" json:"front_running_protection,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -156,6 +159,13 @@ func (m *Params) GetMinBidIncrement() github_com_cosmos_cosmos_sdk_types.Coins { return nil } +func (m *Params) GetFrontRunningProtection() bool { + if m != nil { + return m.FrontRunningProtection + } + return false +} + func init() { proto.RegisterType((*GenesisState)(nil), "skipmev.pob.auction.v1.GenesisState") proto.RegisterType((*Params)(nil), "skipmev.pob.auction.v1.Params") @@ -164,36 +174,37 @@ func init() { func init() { proto.RegisterFile("pob/auction/v1/genesis.proto", fileDescriptor_9ed8651e43f855a1) } var fileDescriptor_9ed8651e43f855a1 = []byte{ - // 450 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x92, 0x31, 0x6f, 0x13, 0x31, - 0x14, 0xc7, 0x73, 0xa4, 0x44, 0xc2, 0xa1, 0x54, 0x3d, 0x55, 0x55, 0x28, 0xc8, 0x8d, 0x3a, 0x40, - 0x54, 0xa9, 0xb6, 0x52, 0x60, 0x41, 0x2c, 0x09, 0x12, 0xa8, 0x12, 0x03, 0x4a, 0x37, 0x16, 0xcb, - 0xf6, 0x3d, 0x82, 0x55, 0x6c, 0x1f, 0x67, 0xdf, 0x91, 0x54, 0x7c, 0x02, 0x26, 0x3e, 0x06, 0x62, - 0xea, 0xc7, 0xe8, 0xd8, 0x91, 0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a2, - 0x13, 0xa2, 0xf8, 0xe8, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x17, 0x29, 0x90, 0x62, 0x0b, 0x03, - 0x00, 0x00, + // 480 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xb1, 0x6e, 0xd4, 0x30, + 0x18, 0xc7, 0x2f, 0xf4, 0x38, 0x81, 0x8f, 0x52, 0x35, 0xaa, 0x4e, 0xa1, 0xa0, 0x34, 0xea, 0x00, + 0x51, 0xa5, 0xda, 0xba, 0x02, 0x12, 0x42, 0x2c, 0x77, 0x48, 0xa0, 0x4a, 0x0c, 0x55, 0xba, 0xb1, + 0x58, 0x4e, 0xf2, 0x35, 0x58, 0xc5, 0x76, 0x88, 0x9d, 0x70, 0x57, 0xf1, 0x04, 0x4c, 0x3c, 0x06, + 0xea, 0xd4, 0xc7, 0xe8, 0xd8, 0x91, 0x09, 0xd0, 0xdd, 0xd0, 0xd7, 0x40, 0xb1, 0x43, 0xe9, 0xc0, + 0xda, 0x25, 0x89, 0xfc, 0xfb, 0xec, 0xdf, 0xf7, 0x45, 0x7f, 0xa3, 0x47, 0xa5, 0x4a, 0x09, 0xab, + 0x33, 0xc3, 0x95, 0x24, 0xcd, 0x98, 0x14, 0x20, 0x41, 0x73, 0x8d, 0xcb, 0x4a, 0x19, 0xe5, 0x8f, + 0xf4, 0x31, 0x2f, 0x05, 0x34, 0xb8, 0x54, 0x29, 0xee, 0xaa, 0x70, 0x33, 0xde, 0xdc, 0x28, 0x54, + 0xa1, 0x6c, 0x09, 0x69, 0xbf, 0x5c, 0xf5, 0x66, 0x98, 0x29, 0x2d, 0x94, 0x26, 0x29, 0xd3, 0x40, + 0x9a, 0x71, 0x0a, 0x86, 0x8d, 0x49, 0xa6, 0xb8, 0xec, 0xf8, 0x3a, 0x13, 0x5c, 0x2a, 0x62, 0x9f, + 0x6e, 0x69, 0xf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b, 0xb2, 0x0e, 0x05, 0x97, 0xd3, 0x7a, 0xbe, + 0x2f, 0x5b, 0xed, 0x17, 0xb4, 0x6e, 0xb5, 0x3c, 0xa7, 0x5c, 0x66, 0x15, 0x08, 0x90, 0x26, 0xb8, + 0x7d, 0x43, 0xe6, 0xb5, 0xd6, 0xcc, 0xf3, 0xfd, 0xbf, 0x22, 0xff, 0x05, 0x0a, 0x8e, 0x2a, 0x25, + 0x0d, 0xad, 0x6a, 0x29, 0xb9, 0x2c, 0x68, 0x9b, 0x1a, 0xb0, 0x21, 0x08, 0x06, 0x91, 0x17, 0xdf, + 0x49, 0x46, 0x96, 0x27, 0x0e, 0x1f, 0x5c, 0xd1, 0x97, 0xd1, 0xd7, 0xcb, 0xb3, 0x9d, 0x87, 0xd7, + 0x64, 0xb3, 0xab, 0x94, 0x77, 0xd1, 0x99, 0x9c, 0x2f, 0x42, 0xef, 0x62, 0x11, 0x7a, 0xbf, 0x17, + 0xa1, 0xf7, 0x6d, 0x19, 0xf6, 0x2e, 0x96, 0x61, 0xef, 0xc7, 0x32, 0xec, 0xbd, 0x7f, 0x72, 0xad, + 0xeb, 0x36, 0x7e, 0xbb, 0x02, 0x1a, 0xd2, 0xde, 0x93, 0x7f, 0x67, 0xd8, 0xd6, 0xd3, 0x81, 0x0d, + 0xf1, 0xd3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x96, 0x16, 0x18, 0xc6, 0x45, 0x03, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -249,6 +260,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.FrontRunningProtection { + i-- + if m.FrontRunningProtection { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x30 + } if len(m.MinBidIncrement) > 0 { for iNdEx := len(m.MinBidIncrement) - 1; iNdEx >= 0; iNdEx-- { { @@ -359,6 +380,9 @@ func (m *Params) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if m.FrontRunningProtection { + n += 2 + } return n } @@ -633,6 +657,26 @@ func (m *Params) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 6: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field FrontRunningProtection", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.FrontRunningProtection = bool(v != 0) default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/x/auction/types/params.go b/x/auction/types/params.go index ffeada2..221bbc3 100644 --- a/x/auction/types/params.go +++ b/x/auction/types/params.go @@ -7,21 +7,23 @@ import ( ) var ( - DefaultMaxBundleSize uint32 = 2 - DefaultEscrowAccountAddress string - DefaultReserveFee = sdk.Coins{} - DefaultMinBuyInFee = sdk.Coins{} - DefaultMinBidIncrement = sdk.Coins{} + DefaultMaxBundleSize uint32 = 2 + DefaultEscrowAccountAddress string + DefaultReserveFee = sdk.Coins{} + DefaultMinBuyInFee = sdk.Coins{} + DefaultMinBidIncrement = sdk.Coins{} + DefaultFrontRunningProtection = true ) // NewParams returns a new Params instance with the provided values. -func NewParams(maxBundleSize uint32, escrowAccountAddress string, reserveFee, minBuyInFee, minBidIncrement sdk.Coins) Params { +func NewParams(maxBundleSize uint32, escrowAccountAddress string, reserveFee, minBuyInFee, minBidIncrement sdk.Coins, frontRunningProtection bool) Params { return Params{ - MaxBundleSize: maxBundleSize, - EscrowAccountAddress: escrowAccountAddress, - ReserveFee: reserveFee, - MinBuyInFee: minBuyInFee, - MinBidIncrement: minBidIncrement, + MaxBundleSize: maxBundleSize, + EscrowAccountAddress: escrowAccountAddress, + ReserveFee: reserveFee, + MinBuyInFee: minBuyInFee, + MinBidIncrement: minBidIncrement, + FrontRunningProtection: frontRunningProtection, } } @@ -33,6 +35,7 @@ func DefaultParams() Params { DefaultReserveFee, DefaultMinBuyInFee, DefaultMinBidIncrement, + DefaultFrontRunningProtection, ) }