Skip to content

Commit

Permalink
Merge branch 'master' into sei-3149
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonWeng authored Feb 28, 2023
2 parents 347413c + 61512d4 commit 1ae60c8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 13 deletions.
3 changes: 3 additions & 0 deletions proto/nitro/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ message Params {
repeated string whitelisted_tx_senders = 1 [
(gogoproto.jsontag) = "whitelisted_tx_senders"
];
bool enabled = 2 [
(gogoproto.jsontag) = "enabled"
];
}
4 changes: 4 additions & 0 deletions x/nitro/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion x/nitro/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions x/nitro/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions x/nitro/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
17 changes: 16 additions & 1 deletion x/nitro/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ import (
// Parameter store keys.
var (
KeyWhitelistedTxSenders = []byte("WhitelistedTxSenders")
KeyEnabled = []byte("Enabled")
)

// ParamTable for gamm module.
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,
}
}

// default gamm module parameters.
func DefaultParams() Params {
return Params{
WhitelistedTxSenders: []string{},
Enabled: false,
}
}

Expand All @@ -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
}
Expand All @@ -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),
}
}

Expand All @@ -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
}
63 changes: 53 additions & 10 deletions x/nitro/types/params.pb.go

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

6 changes: 5 additions & 1 deletion x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,23 @@ 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() {
return true
}
// 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)) {
Expand Down

0 comments on commit 1ae60c8

Please sign in to comment.