diff --git a/proto/nitro/params.proto b/proto/nitro/params.proto index e81b01aa5d..f27c13fb84 100644 --- a/proto/nitro/params.proto +++ b/proto/nitro/params.proto @@ -9,4 +9,7 @@ message Params { repeated string whitelisted_tx_senders = 1 [ (gogoproto.jsontag) = "whitelisted_tx_senders" ]; + bool enabled = 2 [ + (gogoproto.jsontag) = "enabled" + ]; } diff --git a/x/nitro/keeper/msg_server.go b/x/nitro/keeper/msg_server.go index c92006aca8..9602ebcb6f 100644 --- a/x/nitro/keeper/msg_server.go +++ b/x/nitro/keeper/msg_server.go @@ -55,6 +55,10 @@ func (server msgServer) RecordTransactionData(goCtx context.Context, msg *types. func (server msgServer) SubmitFraudChallenge(goCtx context.Context, msg *types.MsgSubmitFraudChallenge) (*types.MsgSubmitFraudChallengeResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) + if !server.IsFraudChallengeEnabled(ctx) { + return nil, types.ErrFraudChallengeDisabled + } + if len(msg.FraudStatePubKey) == 0 { return nil, types.ErrInvalidFraudStatePubkey } diff --git a/x/nitro/keeper/msg_server_test.go b/x/nitro/keeper/msg_server_test.go index 097037bd2e..d0649d9e11 100644 --- a/x/nitro/keeper/msg_server_test.go +++ b/x/nitro/keeper/msg_server_test.go @@ -71,7 +71,7 @@ func TestSubmitFraudChallenge(t *testing.T) { server := nitrokeeper.NewMsgServerImpl(*keeper) stateRoot, proof := createMockMerkleProof() // set state root with mock merkle root - keeper.SetParams(ctx, types.Params{WhitelistedTxSenders: []string{"sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m"}}) + keeper.SetParams(ctx, types.Params{WhitelistedTxSenders: []string{"sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m"}, Enabled: true}) _, err := server.RecordTransactionData(sdk.WrapSDKContext(ctx), &types.MsgRecordTransactionData{ Sender: "sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m", Slot: 1, @@ -121,5 +121,13 @@ func TestSubmitFraudChallenge(t *testing.T) { }) require.Equal(t, err, types.ErrInvalidFraudStatePubkey) + keeper.SetParams(ctx, types.Params{WhitelistedTxSenders: []string{"sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m"}}) + _, err = server.SubmitFraudChallenge(sdk.WrapSDKContext(ctx), &types.MsgSubmitFraudChallenge{ + StartSlot: 0, + EndSlot: 2, + FraudStatePubKey: "123", + MerkleProof: proof, + }) + require.Equal(t, err, types.ErrFraudChallengeDisabled) // TODO: add happy path with replayable account states } diff --git a/x/nitro/keeper/params.go b/x/nitro/keeper/params.go index 0da89a7d40..2576c4d6c5 100644 --- a/x/nitro/keeper/params.go +++ b/x/nitro/keeper/params.go @@ -29,3 +29,8 @@ func (k Keeper) IsTxSenderWhitelisted(ctx sdk.Context, addr string) bool { } return false } + +func (k Keeper) IsFraudChallengeEnabled(ctx sdk.Context) bool { + params := k.GetParams(ctx) + return params.Enabled +} diff --git a/x/nitro/types/errors.go b/x/nitro/types/errors.go index e94fde6d6a..2f3077a2a7 100644 --- a/x/nitro/types/errors.go +++ b/x/nitro/types/errors.go @@ -12,4 +12,5 @@ var ( ErrInvalidMerkleProof = sdkerrors.Register(ModuleName, 3, "Error invalid merkle proof") ErrInvalidAccountState = sdkerrors.Register(ModuleName, 4, "Error invalid provided account state") ErrInvalidFraudStatePubkey = sdkerrors.Register(ModuleName, 6, "Error invalid provided fraud state public key") + ErrFraudChallengeDisabled = sdkerrors.Register(ModuleName, 7, "Error fraud challenge is not enabled yet") ) diff --git a/x/nitro/types/params.go b/x/nitro/types/params.go index 59ad64f8f6..e6fb18f32e 100644 --- a/x/nitro/types/params.go +++ b/x/nitro/types/params.go @@ -10,6 +10,7 @@ import ( // Parameter store keys. var ( KeyWhitelistedTxSenders = []byte("WhitelistedTxSenders") + KeyEnabled = []byte("Enabled") ) // ParamTable for gamm module. @@ -17,9 +18,10 @@ func ParamKeyTable() paramtypes.KeyTable { return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) } -func NewParams(whitelistedTxSenders []string) Params { +func NewParams(whitelistedTxSenders []string, enabled bool) Params { return Params{ WhitelistedTxSenders: whitelistedTxSenders, + Enabled: enabled, } } @@ -27,6 +29,7 @@ func NewParams(whitelistedTxSenders []string) Params { func DefaultParams() Params { return Params{ WhitelistedTxSenders: []string{}, + Enabled: false, } } @@ -35,6 +38,9 @@ func (p Params) Validate() error { if err := validateWhitelistedTxSenders(p.WhitelistedTxSenders); err != nil { return err } + if err := validateEnabled(p.Enabled); err != nil { + return err + } return nil } @@ -43,6 +49,7 @@ func (p Params) Validate() error { func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{ paramtypes.NewParamSetPair(KeyWhitelistedTxSenders, &p.WhitelistedTxSenders, validateWhitelistedTxSenders), + paramtypes.NewParamSetPair(KeyEnabled, &p.Enabled, validateEnabled), } } @@ -60,3 +67,11 @@ func validateWhitelistedTxSenders(i interface{}) error { return nil } + +func validateEnabled(i interface{}) error { + _, ok := i.(bool) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + return nil +} diff --git a/x/nitro/types/params.pb.go b/x/nitro/types/params.pb.go index d4dfa2b106..2eb04a44a3 100644 --- a/x/nitro/types/params.pb.go +++ b/x/nitro/types/params.pb.go @@ -25,6 +25,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Params struct { WhitelistedTxSenders []string `protobuf:"bytes,1,rep,name=whitelisted_tx_senders,json=whitelistedTxSenders,proto3" json:"whitelisted_tx_senders"` + Enabled bool `protobuf:"varint,2,opt,name=enabled,proto3" json:"enabled"` } func (m *Params) Reset() { *m = Params{} } @@ -67,6 +68,13 @@ func (m *Params) GetWhitelistedTxSenders() []string { return nil } +func (m *Params) GetEnabled() bool { + if m != nil { + return m.Enabled + } + return false +} + func init() { proto.RegisterType((*Params)(nil), "seiprotocol.seichain.nitro.Params") } @@ -74,20 +82,22 @@ func init() { func init() { proto.RegisterFile("nitro/params.proto", fileDescriptor_511e5e88523341bc) } var fileDescriptor_511e5e88523341bc = []byte{ - // 200 bytes of a gzipped FileDescriptorProto + // 228 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xca, 0xcb, 0x2c, 0x29, 0xca, 0xd7, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x2a, 0x4e, 0xcd, 0x04, 0xb3, 0x92, 0xf3, 0x73, 0xf4, 0x8a, 0x53, 0x33, 0x93, 0x33, 0x12, 0x33, 0xf3, 0xf4, 0xc0, 0x0a, 0xa5, 0x44, 0xd2, 0xf3, 0xd3, 0xf3, 0xc1, 0x92, 0xfa, 0x20, 0x16, 0x44, - 0x87, 0x52, 0x14, 0x17, 0x5b, 0x00, 0xd8, 0x04, 0xa1, 0x00, 0x2e, 0xb1, 0xf2, 0x8c, 0xcc, 0x92, - 0xd4, 0x9c, 0xcc, 0xe2, 0x92, 0xd4, 0x94, 0xf8, 0x92, 0x8a, 0xf8, 0xe2, 0xd4, 0xbc, 0x94, 0xd4, - 0xa2, 0x62, 0x09, 0x46, 0x05, 0x66, 0x0d, 0x4e, 0x27, 0xa9, 0x57, 0xf7, 0xe4, 0x71, 0xa8, 0x08, - 0x12, 0x41, 0x12, 0x0f, 0xa9, 0x08, 0x86, 0x88, 0x3a, 0x79, 0x9e, 0x78, 0x24, 0xc7, 0x78, 0xe1, - 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, - 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x7e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, - 0x7e, 0x71, 0x6a, 0xa6, 0x2e, 0xcc, 0xcd, 0x60, 0x0e, 0xd8, 0xd1, 0xfa, 0x15, 0xfa, 0x10, 0xff, - 0x95, 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0x55, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, - 0xd4, 0xa1, 0xdd, 0x7e, 0xf5, 0x00, 0x00, 0x00, + 0x87, 0x52, 0x23, 0x23, 0x17, 0x5b, 0x00, 0xd8, 0x08, 0xa1, 0x00, 0x2e, 0xb1, 0xf2, 0x8c, 0xcc, + 0x92, 0xd4, 0x9c, 0xcc, 0xe2, 0x92, 0xd4, 0x94, 0xf8, 0x92, 0x8a, 0xf8, 0xe2, 0xd4, 0xbc, 0x94, + 0xd4, 0xa2, 0x62, 0x09, 0x46, 0x05, 0x66, 0x0d, 0x4e, 0x27, 0xa9, 0x57, 0xf7, 0xe4, 0x71, 0xa8, + 0x08, 0x12, 0x41, 0x12, 0x0f, 0xa9, 0x08, 0x86, 0x88, 0x0a, 0xa9, 0x72, 0xb1, 0xa7, 0xe6, 0x25, + 0x26, 0xe5, 0xa4, 0xa6, 0x48, 0x30, 0x29, 0x30, 0x6a, 0x70, 0x38, 0x71, 0xbf, 0xba, 0x27, 0x0f, + 0x13, 0x0a, 0x82, 0x31, 0x9c, 0x3c, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, + 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, + 0x4a, 0x3f, 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f, 0x39, 0x3f, 0x57, 0xbf, 0x38, 0x35, 0x53, + 0x17, 0xe6, 0x37, 0x30, 0x07, 0xec, 0x39, 0xfd, 0x0a, 0x7d, 0x48, 0x38, 0x94, 0x54, 0x16, 0xa4, + 0x16, 0x27, 0xb1, 0x81, 0x55, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xf2, 0xe6, 0x00, 0xb8, + 0x1d, 0x01, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -110,6 +120,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.Enabled { + i-- + if m.Enabled { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x10 + } if len(m.WhitelistedTxSenders) > 0 { for iNdEx := len(m.WhitelistedTxSenders) - 1; iNdEx >= 0; iNdEx-- { i -= len(m.WhitelistedTxSenders[iNdEx]) @@ -145,6 +165,9 @@ func (m *Params) Size() (n int) { n += 1 + l + sovParams(uint64(l)) } } + if m.Enabled { + n += 2 + } return n } @@ -215,6 +238,26 @@ func (m *Params) Unmarshal(dAtA []byte) error { } m.WhitelistedTxSenders = append(m.WhitelistedTxSenders, string(dAtA[iNdEx:postIndex])) iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Enabled", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.Enabled = bool(v != 0) default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index 00e2dbf196..86027fc6b5 100755 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -398,6 +398,7 @@ func (k Keeper) AddPriceSnapshot(ctx sdk.Context, snapshot types.PriceSnapshot) k.SetPriceSnapshot(ctx, snapshot) var lastOutOfRangeSnapshotTimestamp int64 = -1 + timestampsToDelete := []int64{} // we need to evict old snapshots (except for one that is out of range) k.IteratePriceSnapshots(ctx, func(snapshot types.PriceSnapshot) (stop bool) { if snapshot.SnapshotTimestamp+lookbackDuration >= ctx.BlockTime().Unix() { @@ -405,12 +406,15 @@ func (k Keeper) AddPriceSnapshot(ctx sdk.Context, snapshot types.PriceSnapshot) } // delete the previous out of range snapshot if lastOutOfRangeSnapshotTimestamp >= 0 { - k.DeletePriceSnapshot(ctx, lastOutOfRangeSnapshotTimestamp) + timestampsToDelete = append(timestampsToDelete, lastOutOfRangeSnapshotTimestamp) } // update last out of range snapshot lastOutOfRangeSnapshotTimestamp = snapshot.SnapshotTimestamp return false }) + for _, ts := range timestampsToDelete { + k.DeletePriceSnapshot(ctx, ts) + } } func (k Keeper) IteratePriceSnapshots(ctx sdk.Context, handler func(snapshot types.PriceSnapshot) (stop bool)) {