Skip to content

Commit

Permalink
Application sends next_block_delay
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Nov 25, 2024
1 parent 4eaca4a commit 2b0ce64
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 27 deletions.
10 changes: 10 additions & 0 deletions proto/dydxprotocol/blocktime/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ message DowntimeParams {
repeated google.protobuf.Duration durations = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
}

// SynchronyParams defines the parameters for block synchrony.
message SynchronyParams {
// next_block_delay replaces the locally configured timeout_commit in
// CometBFT. It determines the amount of time the CometBFT waits after the If
// the application sends next_block_delay = 0 to the consensus engine, the
// latter defaults back to using timeout_commit.
google.protobuf.Duration next_block_delay = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
}
17 changes: 17 additions & 0 deletions proto/dydxprotocol/blocktime/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ service Msg {
// UpdateDowntimeParams updates the DowntimeParams in state.
rpc UpdateDowntimeParams(MsgUpdateDowntimeParams)
returns (MsgUpdateDowntimeParamsResponse);
// UpdateSynchronyParams updates the SynchronyParams in state.
rpc UpdateSynchronyParams(MsgUpdateSynchronyParams)
returns (MsgUpdateSynchronyParamsResponse);
}

// MsgUpdateDowntimeParams is the Msg/UpdateDowntimeParams request type.
Expand All @@ -28,3 +31,17 @@ message MsgUpdateDowntimeParams {
// MsgUpdateDowntimeParamsResponse is the Msg/UpdateDowntimeParams response
// type.
message MsgUpdateDowntimeParamsResponse {}

// MsgUpdateSynchronyParams is the Msg/UpdateSynchronyParams request type.
message MsgUpdateSynchronyParams {
// The address that controls the module.
option (cosmos.msg.v1.signer) = "authority";
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// Defines the parameters to update. All parameters must be supplied.
SynchronyParams params = 2 [ (gogoproto.nullable) = false ];
}

// MsgUpdateSynchronyParamsResponse is the Msg/UpdateSynchronyParams response
// type.
message MsgUpdateSynchronyParamsResponse {}
2 changes: 2 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,8 @@ func New(
app.SetPrepareProposal(prepareProposalHandler)
app.SetProcessProposal(processProposalHandler)

app.SetBlockDelayGetter(app.BlockTimeKeeper.GetBlockDelay)

// Note that panics from out of gas errors won't get logged, since the `OutOfGasMiddleware` is added in front of this,
// so error will get handled by that middleware and subsequent middlewares won't get executed.
// Also note that `AddRunTxRecoveryHandler` adds the handler in reverse order, meaning that handlers that appear
Expand Down
6 changes: 4 additions & 2 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ var (
"/dydxprotocol.accountplus.TxExtension": {},

// blocktime
"/dydxprotocol.blocktime.MsgUpdateDowntimeParams": {},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParamsResponse": {},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParams": {},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParamsResponse": {},
"/dydxprotocol.blocktime.MsgUpdateSynchronyParams": {},
"/dydxprotocol.blocktime.MsgUpdateSynchronyParamsResponse": {},

// bridge
"/dydxprotocol.bridge.MsgAcknowledgeBridges": {},
Expand Down
6 changes: 4 additions & 2 deletions protocol/app/msgs/internal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ var (
"/dydxprotocol.accountplus.MsgSetActiveStateResponse": nil,

// blocktime
"/dydxprotocol.blocktime.MsgUpdateDowntimeParams": &blocktime.MsgUpdateDowntimeParams{},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParamsResponse": nil,
"/dydxprotocol.blocktime.MsgUpdateDowntimeParams": &blocktime.MsgUpdateDowntimeParams{},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParamsResponse": nil,
"/dydxprotocol.blocktime.MsgUpdateSynchronyParams": &blocktime.MsgUpdateSynchronyParams{},
"/dydxprotocol.blocktime.MsgUpdateSynchronyParamsResponse": nil,

// bridge
"/dydxprotocol.bridge.MsgCompleteBridge": &bridge.MsgCompleteBridge{},
Expand Down
4 changes: 2 additions & 2 deletions protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ replace (
// Use dYdX fork of Cosmos SDK/store
cosmossdk.io/store => github.com/dydxprotocol/cosmos-sdk/store v1.0.3-0.20240326192503-dd116391188d
// Use dYdX fork of CometBFT
github.com/cometbft/cometbft => github.com/dydxprotocol/cometbft v0.38.6-0.20241120221529-56316dc17261
github.com/cometbft/cometbft => github.com/dydxprotocol/cometbft v0.38.6-0.20241120224121-fc0b273d1fbd
// Use dYdX fork of Cosmos SDK
github.com/cosmos/cosmos-sdk => github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241120185835-38650041ec4d
github.com/cosmos/cosmos-sdk => github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241124185923-7b365908c58e
github.com/cosmos/iavl => github.com/dydxprotocol/iavl v1.1.1-0.20240509161911-1c8b8e787e85
)

Expand Down
8 changes: 4 additions & 4 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,10 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dydxprotocol/cometbft v0.38.6-0.20241120221529-56316dc17261 h1:uspjqDKBvC5I98gKBdZxqX3q0M2UOE/UlGgg+1CLNyI=
github.com/dydxprotocol/cometbft v0.38.6-0.20241120221529-56316dc17261/go.mod h1:XSQX1hQbr54qaJb4/5YNNZGXkAQHHa6bi/KMcN1SQ7w=
github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241120185835-38650041ec4d h1:6Qzg4IuX6LNdciU55jzpGuKxEVgD09rbU2cQaoJFo9Q=
github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241120185835-38650041ec4d/go.mod h1:8EZnLstapHjZ2iGa9nGIhctJ3gU1yCqmRUPN8WI7jD0=
github.com/dydxprotocol/cometbft v0.38.6-0.20241120224121-fc0b273d1fbd h1:3xEX8EOE7MsOAJXTx8m5zHPJyxjBtqMblC7Z4+QYhs4=
github.com/dydxprotocol/cometbft v0.38.6-0.20241120224121-fc0b273d1fbd/go.mod h1:XSQX1hQbr54qaJb4/5YNNZGXkAQHHa6bi/KMcN1SQ7w=
github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241124185923-7b365908c58e h1:MfvcPOS7hrp0BDVuhml6D1Aftbc23GWdgdSTVmuw/gI=
github.com/dydxprotocol/cosmos-sdk v0.50.6-0.20241124185923-7b365908c58e/go.mod h1:K1nHKSbBpLCmgNEv01sYWfvYN7DPUdx5rsrwIxUrqe0=
github.com/dydxprotocol/cosmos-sdk/store v1.0.3-0.20240326192503-dd116391188d h1:HgLu1FD2oDFzlKW6/+SFXlH5Os8cwNTbplQIrQOWx8w=
github.com/dydxprotocol/cosmos-sdk/store v1.0.3-0.20240326192503-dd116391188d/go.mod h1:zMcD3hfNwd0WMTpdRUhS3QxoCoEtBXWeoKsu3iaLBbQ=
github.com/dydxprotocol/iavl v1.1.1-0.20240509161911-1c8b8e787e85 h1:5B/yGZyTBX/OZASQQMnk6Ms/TZja56MYd8OBaVc0Mho=
Expand Down
2 changes: 1 addition & 1 deletion protocol/lib/ante/internal_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func IsInternalMsg(msg sdk.Msg) bool {

// blocktime
*blocktime.MsgUpdateDowntimeParams,

*blocktime.MsgUpdateSynchronyParams,
// bridge
*bridge.MsgCompleteBridge,
*bridge.MsgUpdateEventParams,
Expand Down
34 changes: 34 additions & 0 deletions protocol/x/blocktime/keeper/block_delay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package keeper

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/x/blocktime/types"
)

func (k Keeper) GetSynchronyParams(ctx sdk.Context) types.SynchronyParams {
store := ctx.KVStore(k.storeKey)
bytes := store.Get([]byte(types.SynchronyParamsKey))

if bytes == nil {
fmt.Printf("!! Returning default synchrony params\n")
return types.DefaultSynchronyParams()
}

var params types.SynchronyParams
k.cdc.MustUnmarshal(bytes, &params)
fmt.Printf("Returning un-nil synchrony params: %v\n", params)
return params
}

func (k Keeper) SetSynchronyParams(ctx sdk.Context, params types.SynchronyParams) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(types.SynchronyParamsKey), k.cdc.MustMarshal(&params))
}

func (k Keeper) GetBlockDelay(ctx sdk.Context) time.Duration {
return k.GetSynchronyParams(ctx).NextBlockDelay
}
49 changes: 49 additions & 0 deletions protocol/x/blocktime/keeper/block_delay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package keeper_test

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/x/blocktime/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/blocktime/types"
"github.com/stretchr/testify/require"
)

func Test_Set_GetSynchronyParams_GetBlockDelay(t *testing.T) {
tests := map[string]struct {
setUp func(keeper.Keeper, sdk.Context)
expectedSynchronyParams types.SynchronyParams
expectedBlockDelay time.Duration
}{
"No set-up, empty synchrony params, block_delay = 0": {
setUp: func(k keeper.Keeper, ctx sdk.Context) {
},
expectedSynchronyParams: types.DefaultSynchronyParams(),
expectedBlockDelay: 0,
},
"Non-nil synchrony param": {
setUp: func(k keeper.Keeper, ctx sdk.Context) {
k.SetSynchronyParams(ctx, types.SynchronyParams{
NextBlockDelay: 300 * time.Millisecond,
})
},
expectedSynchronyParams: types.SynchronyParams{
NextBlockDelay: 300 * time.Millisecond,
},
expectedBlockDelay: 300 * time.Millisecond,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.BlockTimeKeeper
tc.setUp(k, ctx)
require.Equal(t, tc.expectedSynchronyParams, k.GetSynchronyParams(ctx))
require.Equal(t, tc.expectedBlockDelay, k.GetBlockDelay(ctx))
})
}
}
18 changes: 18 additions & 0 deletions protocol/x/blocktime/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ func (k msgServer) UpdateDowntimeParams(

return &types.MsgUpdateDowntimeParamsResponse{}, nil
}

func (k msgServer) UpdateSynchronyParams(
goCtx context.Context,
msg *types.MsgUpdateSynchronyParams,
) (*types.MsgUpdateSynchronyParamsResponse, error) {
if !k.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority %s",
msg.Authority,
)
}

ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)
k.SetSynchronyParams(ctx, msg.Params)

return &types.MsgUpdateSynchronyParamsResponse{}, nil
}
5 changes: 5 additions & 0 deletions protocol/x/blocktime/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ var (
402,
"Authority is invalid",
)
ErrNegativeNextBlockDealy = errorsmod.Register(
ModuleName,
403,
"next_block_delay must be non-negative",
)
)
3 changes: 3 additions & 0 deletions protocol/x/blocktime/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ const (

// PreviousBlockInfoKey defines the key for PreviousBlockInfo
PreviousBlockInfoKey = "PreviousBlockInfo"

// SynchronyParamsKey defines the key for the SynchronyParams
SynchronyParamsKey = "SP:"
)
17 changes: 17 additions & 0 deletions protocol/x/blocktime/types/params.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import time "time"

func (m *DowntimeParams) Validate() error {
if m.Durations != nil {
for i := 0; i < len(m.Durations); i++ {
Expand All @@ -16,3 +18,18 @@ func (m *DowntimeParams) Validate() error {
}
return nil
}

func (s SynchronyParams) Validate() error {
if s.NextBlockDelay < 0 {
return ErrNegativeNextBlockDealy
}
return nil
}

func DefaultSynchronyParams() SynchronyParams {
return SynchronyParams{
// CometBFT defaults back to `timeout_commit` if application sends over
// `NextBlockDelay` of 0.
NextBlockDelay: 0 * time.Second,
}
}
Loading

0 comments on commit 2b0ce64

Please sign in to comment.