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 fe586b2 + 61512d4 commit f6f1c94
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 27 deletions.
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func NewBinanceProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Binance websocket: %w", err)
}
Expand Down Expand Up @@ -366,7 +370,11 @@ func (p *BinanceProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnect to binance websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ func NewCoinbaseProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Coinbase websocket: %w", err)
}
Expand Down Expand Up @@ -486,7 +490,11 @@ func (p *CoinbaseProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Coinbase websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func NewGateProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Gate websocket: %w", err)
}
Expand Down Expand Up @@ -551,7 +555,11 @@ func (p *GateProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Gate websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func NewHuobiProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Huobi websocket: %w", err)
}
Expand Down Expand Up @@ -388,7 +392,11 @@ func (p *HuobiProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Huobi websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func NewKrakenProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to websocket: %w", err)
}
Expand Down Expand Up @@ -492,7 +496,11 @@ func (p *KrakenProvider) reconnect() error {
p.logger.Debug().Msg("trying to reconnect")

wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error connecting to Kraken websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/mexc.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func NewMexcProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to mexc websocket: %w", err)
}
Expand Down Expand Up @@ -396,7 +400,11 @@ func (p *MexcProvider) reconnect() error {

p.logger.Debug().Msg("mexc: reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("mexc: error reconnect to mexc websocket: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions oracle/price-feeder/oracle/provider/okx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func NewOkxProvider(
}

wsConn, response, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return nil, fmt.Errorf("error connecting to Okx websocket: %w", err)
}
Expand Down Expand Up @@ -430,7 +434,11 @@ func (p *OkxProvider) reconnect() error {

p.logger.Debug().Msg("reconnecting websocket")
wsConn, response, err := websocket.DefaultDialer.Dial(p.wsURL.String(), nil)
defer response.Body.Close()
defer func() {
if response != nil {
response.Body.Close()
}
}()
if err != nil {
return fmt.Errorf("error reconnecting to Okx websocket: %w", err)
}
Expand Down
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
}
Loading

0 comments on commit f6f1c94

Please sign in to comment.