Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/oracle2 #55

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
395 changes: 234 additions & 161 deletions api/opinit/opchild/v1/types.pulsar.go

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions proto/opinit/opchild/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ message Params {
(amino.dont_omitempty) = true,
(gogoproto.moretags) = "yaml:\"bridge_executor\""
];
//the host(l1) chain id.
string host_chain_id = 5;
// the account address of admin who can execute permissioned cosmos messages.
string admin = 6 [
string admin = 5 [
(cosmos_proto.scalar) = "cosmos.AddressString",
(amino.dont_omitempty) = true,
(gogoproto.moretags) = "yaml:\"admin\""
];
// the list of addresses that are allowed to pay zero fee.
repeated string fee_whitelist = 7 [
repeated string fee_whitelist = 6 [
(cosmos_proto.scalar) = "cosmos.AddressString",
(amino.dont_omitempty) = true,
(gogoproto.moretags) = "yaml:\"fee_whitelist\""
Expand Down Expand Up @@ -92,6 +90,10 @@ message BridgeInfo {
// bridge_addr is the address of the bridge on l1.
string bridge_addr = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];

string l1_chain_id = 3;

string l1_client_id = 4;

// bridge_config is the configuration of the bridge.
opinit.ophost.v1.BridgeConfig bridge_config = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
opinit.ophost.v1.BridgeConfig bridge_config = 5 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}
13 changes: 9 additions & 4 deletions x/opchild/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
NewDepositCmd(ac),
NewWithdrawCmd(ac),
NewSetBridgeInfoCmd(ac),
NewUpdateOracleCmd(ac),
)

return opchildTxCmd
Expand Down Expand Up @@ -254,13 +255,13 @@ Where proposal.json contains:
// NewSetBridgeInfoCmd returns a CLI command handler for transaction to setting a bridge info.
func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "set-bridge-info [bridge-id] [bridge-addr] [path/to/bridge-config.json]",
Use: "set-bridge-info [bridge-id] [bridge-addr] [l1-chain-id] [l1-client-id] [path/to/bridge-config.json]",
Short: "send a bridge creating tx",
Long: strings.TrimSpace(
fmt.Sprintf(
`send a tx to set a bridge info with a config file as a json.
Example:
$ %s tx ophost set-bridge-info 1 init10d07y265gmmuvt4z0w9aw880jnsr700j55nka3 path/to/bridge-config.json
$ %s tx ophost set-bridge-info 1 init10d07y265gmmuvt4z0w9aw880jnsr700j55nka3 mahalo-2 07-tendermint-0 path/to/bridge-config.json

Where bridge-config.json contains:
{
Expand All @@ -274,7 +275,7 @@ func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
}`, version.AppName,
),
),
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -287,8 +288,10 @@ func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
}

bridgeAddr := args[1]
l1ChainId := args[2]
l1ClientId := args[3]

configBytes, err := os.ReadFile(args[2])
configBytes, err := os.ReadFile(args[4])
if err != nil {
return err
}
Expand Down Expand Up @@ -335,6 +338,8 @@ func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
msg := types.NewMsgSetBridgeInfo(fromAddr, types.BridgeInfo{
BridgeId: bridgeId,
BridgeAddr: bridgeAddr,
L1ChainId: l1ChainId,
L1ClientId: l1ClientId,
BridgeConfig: bridgeConfig,
})
if err = msg.Validate(ac); err != nil {
Expand Down
26 changes: 22 additions & 4 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ func (k Keeper) setDenomMetadata(ctx context.Context, baseDenom, denom string) {
}

// UpdateHostValidatorSet updates the host validator set.
func (k Keeper) UpdateHostValidatorSet(ctx context.Context, chainID string, height int64, validatorSet *cmtproto.ValidatorSet) error {
if chainID == "" {
func (k Keeper) UpdateHostValidatorSet(ctx context.Context, clientID string, height int64, validatorSet *cmtproto.ValidatorSet) error {
if clientID == "" {
return nil
}

// ignore if the chain ID is not the host chain ID
if hostChainID, err := k.HostChainId(ctx); err != nil {
if l1ClientId, err := k.L1ClientId(ctx); err != nil {
return err
} else if hostChainID != chainID {
} else if l1ClientId != clientID {
return nil
}

Expand All @@ -177,3 +177,21 @@ func (k Keeper) UpdateHostValidatorSet(ctx context.Context, chainID string, heig
func (k Keeper) ApplyOracleUpdate(ctx context.Context, height uint64, extCommitBz []byte) error {
return k.l2OracleHandler.UpdateOracle(ctx, height, extCommitBz)
}

func (k Keeper) L1ClientId(ctx context.Context) (string, error) {
info, err := k.BridgeInfo.Get(ctx)
if err != nil {
return "", err
}

return info.L1ClientId, nil
}

func (k Keeper) L1ChainId(ctx context.Context) (string, error) {
info, err := k.BridgeInfo.Get(ctx)
if err != nil {
return "", err
}

return info.L1ChainId, nil
}
10 changes: 10 additions & 0 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ func (ms MsgServer) SetBridgeInfo(ctx context.Context, req *types.MsgSetBridgeIn
if info.BridgeAddr != req.BridgeInfo.BridgeAddr {
return nil, types.ErrInvalidBridgeInfo.Wrapf("expected bridge addr %s, got %s", info.BridgeAddr, req.BridgeInfo.BridgeAddr)
}

if info.L1ChainId != req.BridgeInfo.L1ChainId {
return nil, types.ErrInvalidBridgeInfo.Wrapf("expected l1 chain id %s, got %s", info.L1ChainId, req.BridgeInfo.L1ChainId)
}

if info.L1ClientId != req.BridgeInfo.L1ClientId {
return nil, types.ErrInvalidBridgeInfo.Wrapf("expected l1 client id %s, got %s", info.L1ClientId, req.BridgeInfo.L1ClientId)
}
}

// set bridge info
Expand All @@ -327,6 +335,8 @@ func (ms MsgServer) SetBridgeInfo(ctx context.Context, req *types.MsgSetBridgeIn
types.EventTypeSetBridgeInfo,
sdk.NewAttribute(types.AttributeKeyBridgeId, strconv.FormatUint(req.BridgeInfo.BridgeId, 10)),
sdk.NewAttribute(types.AttributeKeyBridgeAddr, req.BridgeInfo.BridgeAddr),
sdk.NewAttribute(types.AttributeKeyL1ClientId, req.BridgeInfo.L1ClientId),
sdk.NewAttribute(types.AttributeKeyL1ChainId, req.BridgeInfo.L1ChainId),
),
)

Expand Down
28 changes: 22 additions & 6 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ func Test_MsgServer_SetBridgeInfo(t *testing.T) {
info := types.BridgeInfo{
BridgeId: 1,
BridgeAddr: addrsStr[1],
L1ChainId: "test-chain-id",
L1ClientId: "test-client-id",
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
Expand All @@ -299,6 +301,19 @@ func Test_MsgServer_SetBridgeInfo(t *testing.T) {
_, err = ms.SetBridgeInfo(ctx, types.NewMsgSetBridgeInfo(addrsStr[0], info))
require.NoError(t, err)

// cannot change chain id
info.L1ChainId = "test-chain-id-2"
_, err = ms.SetBridgeInfo(ctx, types.NewMsgSetBridgeInfo(addrsStr[0], info))
require.Error(t, err)

// cannot change client id
info.L1ChainId = "test-chain-id"
info.L1ClientId = "test-client-id-2"
_, err = ms.SetBridgeInfo(ctx, types.NewMsgSetBridgeInfo(addrsStr[0], info))
require.Error(t, err)

info.L1ClientId = "test-client-id"

// invalid bridge id
info.BridgeId = 0

Expand Down Expand Up @@ -408,12 +423,13 @@ func Test_MsgServer_UpdateOracle(t *testing.T) {
opchildKeeper := input.OPChildKeeper
oracleKeeper := input.OracleKeeper

params, err := opchildKeeper.GetParams(ctx)
require.NoError(t, err)

defaultHostChainId := "test-host-1"
params.HostChainId = defaultHostChainId
err = opchildKeeper.SetParams(ctx, params)
defaultClientId := "test-client-id"
bridgeInfo := types.BridgeInfo{
L1ChainId: defaultHostChainId,
L1ClientId: defaultClientId,
}
err := opchildKeeper.BridgeInfo.Set(ctx, bridgeInfo)
require.NoError(t, err)

oracleKeeper.InitGenesis(sdk.UnwrapSDKContext(ctx), oracletypes.GenesisState{
Expand All @@ -439,7 +455,7 @@ func Test_MsgServer_UpdateOracle(t *testing.T) {

cpStrategy, extendedCommitCodec, voteExtensionCodec := getSlinky(oracleKeeper)
valPrivKeys, _, validatorSet := createCmtValidatorSet(t, numVals)
err = opchildKeeper.UpdateHostValidatorSet(ctx, defaultHostChainId, 1, validatorSet)
err = opchildKeeper.UpdateHostValidatorSet(ctx, defaultClientId, 1, validatorSet)
require.NoError(t, err)

eci := cometabci.ExtendedCommitInfo{
Expand Down
3 changes: 2 additions & 1 deletion x/opchild/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (k L2OracleHandler) UpdateOracle(ctx context.Context, height uint64, extCom
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
hostChainID, err := k.HostChainId(ctx)

hostChainID, err := k.L1ChainId(ctx)
if err != nil {
return err
}
Expand Down
75 changes: 39 additions & 36 deletions x/opchild/keeper/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"testing"

"github.com/initia-labs/OPinit/x/opchild/types"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand Down Expand Up @@ -65,42 +66,42 @@ func getSlinky(oracleKeeper *oraclekeeper.Keeper) (currencypair.CurrencyPairStra
}

func Test_UpdateHostValidatorSet(t *testing.T) {
defaultHostChainId := "test-host-1"
defaultClientId := "test-client-id"

testCases := []struct {
name string
hostChainId string
hostHeight int64
numVals int
expectError bool
name string
hostClientId string
hostHeight int64
numVals int
expectError bool
}{
{
name: "empty chain id",
hostChainId: "",
hostHeight: 100,
numVals: 10,
expectError: true,
name: "empty chain id",
hostClientId: "",
hostHeight: 100,
numVals: 10,
expectError: true,
},
{
name: "different chain id",
hostChainId: "test-host-12",
hostHeight: 100,
numVals: 10,
expectError: true,
name: "different chain id",
hostClientId: "test-host-12",
hostHeight: 100,
numVals: 10,
expectError: true,
},
{
name: "zero height",
hostChainId: defaultHostChainId,
hostHeight: 0,
numVals: 10,
expectError: true,
name: "zero height",
hostClientId: defaultClientId,
hostHeight: 0,
numVals: 10,
expectError: true,
},
{
name: "good host chain id, height, validators",
hostChainId: defaultHostChainId,
hostHeight: 100,
numVals: 10,
expectError: false,
name: "good host chain id, height, validators",
hostClientId: defaultClientId,
hostHeight: 100,
numVals: 10,
expectError: false,
},
}

Expand All @@ -110,14 +111,14 @@ func Test_UpdateHostValidatorSet(t *testing.T) {
opchildKeeper := input.OPChildKeeper
hostValidatorStore := opchildKeeper.HostValidatorStore

params, err := opchildKeeper.GetParams(ctx)
require.NoError(t, err)
params.HostChainId = defaultHostChainId
err = opchildKeeper.SetParams(ctx, params)
bridgeInfo := types.BridgeInfo{
L1ClientId: defaultClientId,
}
err := opchildKeeper.BridgeInfo.Set(ctx, bridgeInfo)
require.NoError(t, err)

_, valPubKeys, validatorSet := createCmtValidatorSet(t, tc.numVals)
err = opchildKeeper.UpdateHostValidatorSet(ctx, tc.hostChainId, tc.hostHeight, validatorSet)
err = opchildKeeper.UpdateHostValidatorSet(ctx, tc.hostClientId, tc.hostHeight, validatorSet)
if tc.expectError {
// no error but no validator update
require.NoError(t, err)
Expand All @@ -144,6 +145,7 @@ func Test_UpdateHostValidatorSet(t *testing.T) {
}

func Test_UpdateOracle(t *testing.T) {
defaultClientId := "test-client-id"
defaultHostChainId := "test-host-1"

testCases := []struct {
Expand Down Expand Up @@ -241,10 +243,11 @@ func Test_UpdateOracle(t *testing.T) {
opchildKeeper := input.OPChildKeeper
oracleKeeper := input.OracleKeeper

params, err := opchildKeeper.GetParams(ctx)
require.NoError(t, err)
params.HostChainId = defaultHostChainId
err = opchildKeeper.SetParams(ctx, params)
bridgeInfo := types.BridgeInfo{
L1ChainId: defaultHostChainId,
L1ClientId: defaultClientId,
}
err := opchildKeeper.BridgeInfo.Set(ctx, bridgeInfo)
require.NoError(t, err)

oracleKeeper.InitGenesis(sdk.UnwrapSDKContext(ctx), oracletypes.GenesisState{
Expand All @@ -259,7 +262,7 @@ func Test_UpdateOracle(t *testing.T) {

cpStrategy, extendedCommitCodec, voteExtensionCodec := getSlinky(oracleKeeper)
valPrivKeys, _, validatorSet := createCmtValidatorSet(t, tc.numVals)
err = opchildKeeper.UpdateHostValidatorSet(ctx, defaultHostChainId, 1, validatorSet)
err = opchildKeeper.UpdateHostValidatorSet(ctx, defaultClientId, 1, validatorSet)
require.NoError(t, err)

eci := cometabci.ExtendedCommitInfo{
Expand Down
8 changes: 0 additions & 8 deletions x/opchild/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,3 @@ func (k Keeper) MinGasPrices(ctx context.Context) (sdk.DecCoins, error) {

return params.MinGasPrices, nil
}

func (k Keeper) HostChainId(ctx context.Context) (string, error) {
params, err := k.GetParams(ctx)
if err != nil {
return "", err
}
return params.HostChainId, nil
}
2 changes: 2 additions & 0 deletions x/opchild/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
AttributeKeySender = "sender"
AttributeKeyBridgeId = "bridge_id"
AttributeKeyBridgeAddr = "bridge_addr"
AttributeKeyL1ChainId = "l1_chain_id"
AttributeKeyL1ClientId = "l1_client_id"
AttributeKeyRecipient = "recipient"
AttributeKeyAmount = "amount"
AttributeKeyDenom = "denom"
Expand Down
5 changes: 1 addition & 4 deletions x/opchild/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

var (
DefaultMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyNewDecWithPrec(15, 2))) // 0.15
DefaultHostChainId = "mahalo-2"
)

// DefaultParams returns default move parameters
Expand All @@ -21,20 +20,18 @@ func DefaultParams() Params {
DefaultMaxValidators,
DefaultHistoricalEntries,
DefaultMinGasPrices,
DefaultHostChainId,
[]string{},
)
}

// NewParams creates a new Params instance
func NewParams(admin, bridgeExecutor string, maxValidators, historicalEntries uint32, minGasPrice sdk.DecCoins, hostChainId string, feeWhitelist []string) Params {
func NewParams(admin, bridgeExecutor string, maxValidators, historicalEntries uint32, minGasPrice sdk.DecCoins, feeWhitelist []string) Params {
return Params{
Admin: admin,
BridgeExecutor: bridgeExecutor,
MaxValidators: maxValidators,
HistoricalEntries: historicalEntries,
MinGasPrices: minGasPrice,
HostChainId: hostChainId,
FeeWhitelist: feeWhitelist,
}

Expand Down
Loading
Loading