From 0702b2648a9ca4843a96afaa731c51799374c938 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Tue, 28 May 2024 18:59:42 -0400 Subject: [PATCH 01/15] add an authorization list --- .../zetacore/authority/authorization.proto | 18 + .../zetacore/authority/genesis.proto | 6 +- testutil/sample/authority.go | 25 +- .../zetacore/authority/authorization_pb.d.ts | 68 +++ .../zetacore/authority/genesis_pb.d.ts | 6 + .../zetachain/zetacore/authority/index.d.ts | 1 + x/authority/genesis.go | 5 + x/authority/genesis_test.go | 8 +- x/authority/keeper/authorization_list.go | 25 + x/authority/keeper/authorization_list_test.go | 44 ++ x/authority/types/authorization.pb.go | 543 ++++++++++++++++++ x/authority/types/authorizations.go | 41 ++ x/authority/types/genesis.pb.go | 78 ++- 13 files changed, 854 insertions(+), 14 deletions(-) create mode 100644 proto/zetachain/zetacore/authority/authorization.proto create mode 100644 typescript/zetachain/zetacore/authority/authorization_pb.d.ts create mode 100644 x/authority/keeper/authorization_list.go create mode 100644 x/authority/keeper/authorization_list_test.go create mode 100644 x/authority/types/authorization.pb.go create mode 100644 x/authority/types/authorizations.go diff --git a/proto/zetachain/zetacore/authority/authorization.proto b/proto/zetachain/zetacore/authority/authorization.proto new file mode 100644 index 0000000000..4f5a4330ef --- /dev/null +++ b/proto/zetachain/zetacore/authority/authorization.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package zetachain.zetacore.authority; + +import "gogoproto/gogo.proto"; +import "zetachain/zetacore/authority/policies.proto"; + +option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; + +message Authorization { + // The URL of the message that needs to be authorized + string msg_url = 1; + // The policy that is authorized to access the message + PolicyType authorized_policy = 2; +} +// AuthorizationList holds the list of authorizations on zetachain +message AuthorizationList { + repeated Authorization authorizations = 1 [ (gogoproto.nullable) = false ]; +} \ No newline at end of file diff --git a/proto/zetachain/zetacore/authority/genesis.proto b/proto/zetachain/zetacore/authority/genesis.proto index 93eee1e9fc..e508fa5746 100644 --- a/proto/zetachain/zetacore/authority/genesis.proto +++ b/proto/zetachain/zetacore/authority/genesis.proto @@ -2,9 +2,13 @@ syntax = "proto3"; package zetachain.zetacore.authority; import "zetachain/zetacore/authority/policies.proto"; +import "zetachain/zetacore/authority/authorization.proto"; import "gogoproto/gogo.proto"; option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; // GenesisState defines the authority module's genesis state. -message GenesisState { Policies policies = 1 [ (gogoproto.nullable) = false ]; } +message GenesisState { + Policies policies = 1 [ (gogoproto.nullable) = false ]; + AuthorizationList authorization_list = 2 [ (gogoproto.nullable) = false ]; +} diff --git a/testutil/sample/authority.go b/testutil/sample/authority.go index 9f2535d2b3..b35a49d4b3 100644 --- a/testutil/sample/authority.go +++ b/testutil/sample/authority.go @@ -1,6 +1,10 @@ package sample -import authoritytypes "github.com/zeta-chain/zetacore/x/authority/types" +import ( + "fmt" + + authoritytypes "github.com/zeta-chain/zetacore/x/authority/types" +) func Policies() authoritytypes.Policies { return authoritytypes.Policies{ @@ -20,3 +24,22 @@ func Policies() authoritytypes.Policies { }, } } + +func AuthorizationList(val string) authoritytypes.AuthorizationList { + return authoritytypes.AuthorizationList{ + Authorizations: []authoritytypes.Authorization{ + { + MsgUrl: fmt.Sprintf("/zetachain/%d%s", 0, val), + AuthorizedPolicy: authoritytypes.PolicyType_groupEmergency, + }, + { + MsgUrl: fmt.Sprintf("/zetachain/%d%s", 1, val), + AuthorizedPolicy: authoritytypes.PolicyType_groupAdmin, + }, + { + MsgUrl: fmt.Sprintf("/zetachain/%d%s", 2, val), + AuthorizedPolicy: authoritytypes.PolicyType_groupOperational, + }, + }, + } +} diff --git a/typescript/zetachain/zetacore/authority/authorization_pb.d.ts b/typescript/zetachain/zetacore/authority/authorization_pb.d.ts new file mode 100644 index 0000000000..cf4819d1aa --- /dev/null +++ b/typescript/zetachain/zetacore/authority/authorization_pb.d.ts @@ -0,0 +1,68 @@ +// @generated by protoc-gen-es v1.3.0 with parameter "target=dts" +// @generated from file zetachain/zetacore/authority/authorization.proto (package zetachain.zetacore.authority, syntax proto3) +/* eslint-disable */ +// @ts-nocheck + +import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; +import { Message, proto3 } from "@bufbuild/protobuf"; +import type { PolicyType } from "./policies_pb.js"; + +/** + * @generated from message zetachain.zetacore.authority.Authorization + */ +export declare class Authorization extends Message { + /** + * The URL of the message that needs to be authorized + * + * @generated from field: string msg_url = 1; + */ + msgUrl: string; + + /** + * The policy that is authorized to access the message + * + * @generated from field: zetachain.zetacore.authority.PolicyType authorized_policy = 2; + */ + authorizedPolicy: PolicyType; + + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.Authorization"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): Authorization; + + static fromJson(jsonValue: JsonValue, options?: Partial): Authorization; + + static fromJsonString(jsonString: string, options?: Partial): Authorization; + + static equals(a: Authorization | PlainMessage | undefined, b: Authorization | PlainMessage | undefined): boolean; +} + +/** + * AuthorizationList holds the list of authorizations on zetachain + * + * @generated from message zetachain.zetacore.authority.AuthorizationList + */ +export declare class AuthorizationList extends Message { + /** + * @generated from field: repeated zetachain.zetacore.authority.Authorization authorizations = 1; + */ + authorizations: Authorization[]; + + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.AuthorizationList"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): AuthorizationList; + + static fromJson(jsonValue: JsonValue, options?: Partial): AuthorizationList; + + static fromJsonString(jsonString: string, options?: Partial): AuthorizationList; + + static equals(a: AuthorizationList | PlainMessage | undefined, b: AuthorizationList | PlainMessage | undefined): boolean; +} + diff --git a/typescript/zetachain/zetacore/authority/genesis_pb.d.ts b/typescript/zetachain/zetacore/authority/genesis_pb.d.ts index a20371004b..69c965b7e1 100644 --- a/typescript/zetachain/zetacore/authority/genesis_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/genesis_pb.d.ts @@ -6,6 +6,7 @@ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; import { Message, proto3 } from "@bufbuild/protobuf"; import type { Policies } from "./policies_pb.js"; +import type { AuthorizationList } from "./authorization_pb.js"; /** * GenesisState defines the authority module's genesis state. @@ -18,6 +19,11 @@ export declare class GenesisState extends Message { */ policies?: Policies; + /** + * @generated from field: zetachain.zetacore.authority.AuthorizationList authorization_list = 2; + */ + authorizationList?: AuthorizationList; + constructor(data?: PartialMessage); static readonly runtime: typeof proto3; diff --git a/typescript/zetachain/zetacore/authority/index.d.ts b/typescript/zetachain/zetacore/authority/index.d.ts index d363dc44f4..b129207549 100644 --- a/typescript/zetachain/zetacore/authority/index.d.ts +++ b/typescript/zetachain/zetacore/authority/index.d.ts @@ -1,3 +1,4 @@ +export * from "./authorization_pb"; export * from "./genesis_pb"; export * from "./policies_pb"; export * from "./query_pb"; diff --git a/x/authority/genesis.go b/x/authority/genesis.go index 1391b5954d..e4a0ca8c3f 100644 --- a/x/authority/genesis.go +++ b/x/authority/genesis.go @@ -10,6 +10,7 @@ import ( // InitGenesis initializes the authority module's state from a provided genesis state func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { k.SetPolicies(ctx, genState.Policies) + k.SetAuthorizationList(ctx, genState.AuthorizationList) } // ExportGenesis returns the authority module's exported genesis. @@ -20,6 +21,10 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { if found { genesis.Policies = policies } + authorizationList, found := k.GetAuthorizationList(ctx) + if found { + genesis.AuthorizationList = authorizationList + } return &genesis } diff --git a/x/authority/genesis_test.go b/x/authority/genesis_test.go index ac9c8d2e43..5ab91da960 100644 --- a/x/authority/genesis_test.go +++ b/x/authority/genesis_test.go @@ -14,7 +14,8 @@ import ( func TestGenesis(t *testing.T) { genesisState := types.GenesisState{ - Policies: sample.Policies(), + Policies: sample.Policies(), + AuthorizationList: sample.AuthorizationList("sample"), } // Init @@ -26,6 +27,11 @@ func TestGenesis(t *testing.T) { require.True(t, found) require.Equal(t, genesisState.Policies, policies) + // Check authorization list is set + authorizationList, found := k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, genesisState.AuthorizationList, authorizationList) + // Export got := authority.ExportGenesis(ctx, *k) require.NotNil(t, got) diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go new file mode 100644 index 0000000000..8315aa30a3 --- /dev/null +++ b/x/authority/keeper/authorization_list.go @@ -0,0 +1,25 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/zeta-chain/zetacore/x/authority/types" +) + +// SetAuthorizationList sets the authorization list to the store +func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) { + store := ctx.KVStore(k.storeKey) + b := k.cdc.MustMarshal(&list) + store.Set([]byte{0}, b) +} + +// GetAuthorizationList returns the authorization list from the store +func (k Keeper) GetAuthorizationList(ctx sdk.Context) (val types.AuthorizationList, found bool) { + store := ctx.KVStore(k.storeKey) + b := store.Get([]byte{0}) + if b == nil { + return val, false + } + k.cdc.MustUnmarshal(b, &val) + return val, true +} diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go new file mode 100644 index 0000000000..11bc5e60ca --- /dev/null +++ b/x/authority/keeper/authorization_list_test.go @@ -0,0 +1,44 @@ +package keeper_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" + "github.com/zeta-chain/zetacore/testutil/sample" +) + +func TestKeeper_GetAuthorizationList(t *testing.T) { + t.Run("successfully get authorizations list", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + authorizationList := sample.AuthorizationList("sample") + k.SetAuthorizationList(ctx, authorizationList) + list, found := k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, authorizationList, list) + }) + + t.Run("get authorizations list not found", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + _, found := k.GetAuthorizationList(ctx) + require.False(t, found) + }) +} + +func TestKeeper_SetAuthorizationList(t *testing.T) { + t.Run("successfully set authorizations list when a list already exists", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + authorizationList := sample.AuthorizationList("sample") + k.SetAuthorizationList(ctx, authorizationList) + list, found := k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, authorizationList, list) + + newAuthorizationList := sample.AuthorizationList("sample2") + require.NotEqual(t, authorizationList, newAuthorizationList) + k.SetAuthorizationList(ctx, newAuthorizationList) + list, found = k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, newAuthorizationList, list) + }) +} diff --git a/x/authority/types/authorization.pb.go b/x/authority/types/authorization.pb.go new file mode 100644 index 0000000000..c8cce069e1 --- /dev/null +++ b/x/authority/types/authorization.pb.go @@ -0,0 +1,543 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: zetachain/zetacore/authority/authorization.proto + +package types + +import ( + fmt "fmt" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/cosmos/gogoproto/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type Authorization struct { + // The URL of the message that needs to be authorized + MsgUrl string `protobuf:"bytes,1,opt,name=msg_url,json=msgUrl,proto3" json:"msg_url,omitempty"` + // The policy that is authorized to access the message + AuthorizedPolicy PolicyType `protobuf:"varint,2,opt,name=authorized_policy,json=authorizedPolicy,proto3,enum=zetachain.zetacore.authority.PolicyType" json:"authorized_policy,omitempty"` +} + +func (m *Authorization) Reset() { *m = Authorization{} } +func (m *Authorization) String() string { return proto.CompactTextString(m) } +func (*Authorization) ProtoMessage() {} +func (*Authorization) Descriptor() ([]byte, []int) { + return fileDescriptor_b7303e09de7c755a, []int{0} +} +func (m *Authorization) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Authorization) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Authorization.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Authorization) XXX_Merge(src proto.Message) { + xxx_messageInfo_Authorization.Merge(m, src) +} +func (m *Authorization) XXX_Size() int { + return m.Size() +} +func (m *Authorization) XXX_DiscardUnknown() { + xxx_messageInfo_Authorization.DiscardUnknown(m) +} + +var xxx_messageInfo_Authorization proto.InternalMessageInfo + +func (m *Authorization) GetMsgUrl() string { + if m != nil { + return m.MsgUrl + } + return "" +} + +func (m *Authorization) GetAuthorizedPolicy() PolicyType { + if m != nil { + return m.AuthorizedPolicy + } + return PolicyType_groupEmergency +} + +// AuthorizationList holds the list of authorizations on zetachain +type AuthorizationList struct { + Authorizations []Authorization `protobuf:"bytes,1,rep,name=authorizations,proto3" json:"authorizations"` +} + +func (m *AuthorizationList) Reset() { *m = AuthorizationList{} } +func (m *AuthorizationList) String() string { return proto.CompactTextString(m) } +func (*AuthorizationList) ProtoMessage() {} +func (*AuthorizationList) Descriptor() ([]byte, []int) { + return fileDescriptor_b7303e09de7c755a, []int{1} +} +func (m *AuthorizationList) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *AuthorizationList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_AuthorizationList.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *AuthorizationList) XXX_Merge(src proto.Message) { + xxx_messageInfo_AuthorizationList.Merge(m, src) +} +func (m *AuthorizationList) XXX_Size() int { + return m.Size() +} +func (m *AuthorizationList) XXX_DiscardUnknown() { + xxx_messageInfo_AuthorizationList.DiscardUnknown(m) +} + +var xxx_messageInfo_AuthorizationList proto.InternalMessageInfo + +func (m *AuthorizationList) GetAuthorizations() []Authorization { + if m != nil { + return m.Authorizations + } + return nil +} + +func init() { + proto.RegisterType((*Authorization)(nil), "zetachain.zetacore.authority.Authorization") + proto.RegisterType((*AuthorizationList)(nil), "zetachain.zetacore.authority.AuthorizationList") +} + +func init() { + proto.RegisterFile("zetachain/zetacore/authority/authorization.proto", fileDescriptor_b7303e09de7c755a) +} + +var fileDescriptor_b7303e09de7c755a = []byte{ + // 271 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x32, 0xa8, 0x4a, 0x2d, 0x49, + 0x4c, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x07, 0xb3, 0xf2, 0x8b, 0x52, 0xf5, 0x13, 0x4b, 0x4b, 0x32, + 0xf2, 0x8b, 0x32, 0x4b, 0x2a, 0x61, 0xac, 0xaa, 0xc4, 0x92, 0xcc, 0xfc, 0x3c, 0xbd, 0x82, 0xa2, + 0xfc, 0x92, 0x7c, 0x21, 0x19, 0xb8, 0x0e, 0x3d, 0x98, 0x0e, 0x3d, 0xb8, 0x0e, 0x29, 0x91, 0xf4, + 0xfc, 0xf4, 0x7c, 0xb0, 0x42, 0x7d, 0x10, 0x0b, 0xa2, 0x47, 0x4a, 0x1b, 0xaf, 0x2d, 0x05, 0xf9, + 0x39, 0x99, 0xc9, 0x99, 0xa9, 0xc5, 0x10, 0xc5, 0x4a, 0xf5, 0x5c, 0xbc, 0x8e, 0xc8, 0xf6, 0x0a, + 0x89, 0x73, 0xb1, 0xe7, 0x16, 0xa7, 0xc7, 0x97, 0x16, 0xe5, 0x48, 0x30, 0x2a, 0x30, 0x6a, 0x70, + 0x06, 0xb1, 0xe5, 0x16, 0xa7, 0x87, 0x16, 0xe5, 0x08, 0x85, 0x72, 0x09, 0xc2, 0x5c, 0x98, 0x9a, + 0x12, 0x0f, 0x36, 0xa6, 0x52, 0x82, 0x49, 0x81, 0x51, 0x83, 0xcf, 0x48, 0x43, 0x0f, 0x9f, 0x33, + 0xf5, 0x02, 0xc0, 0x6a, 0x43, 0x2a, 0x0b, 0x52, 0x83, 0x04, 0x10, 0x46, 0x40, 0x44, 0x95, 0xf2, + 0xb8, 0x04, 0x51, 0x1c, 0xe0, 0x93, 0x59, 0x5c, 0x22, 0x14, 0xc9, 0xc5, 0x87, 0x12, 0x1a, 0xc5, + 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, 0x46, 0xda, 0xf8, 0x2d, 0x42, 0x31, 0xc8, 0x89, 0xe5, 0xc4, + 0x3d, 0x79, 0x86, 0x20, 0x34, 0x83, 0x9c, 0xbc, 0x4e, 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, 0xca, 0x20, 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f, 0x39, 0x3f, 0x17, 0x1c, 0x70, + 0xba, 0x68, 0x61, 0x58, 0x81, 0x14, 0x8a, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, 0xe0, 0x30, + 0x34, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x88, 0x76, 0x6b, 0x6c, 0xd8, 0x01, 0x00, 0x00, +} + +func (m *Authorization) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Authorization) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Authorization) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.AuthorizedPolicy != 0 { + i = encodeVarintAuthorization(dAtA, i, uint64(m.AuthorizedPolicy)) + i-- + dAtA[i] = 0x10 + } + if len(m.MsgUrl) > 0 { + i -= len(m.MsgUrl) + copy(dAtA[i:], m.MsgUrl) + i = encodeVarintAuthorization(dAtA, i, uint64(len(m.MsgUrl))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *AuthorizationList) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *AuthorizationList) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *AuthorizationList) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Authorizations) > 0 { + for iNdEx := len(m.Authorizations) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Authorizations[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintAuthorization(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintAuthorization(dAtA []byte, offset int, v uint64) int { + offset -= sovAuthorization(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *Authorization) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.MsgUrl) + if l > 0 { + n += 1 + l + sovAuthorization(uint64(l)) + } + if m.AuthorizedPolicy != 0 { + n += 1 + sovAuthorization(uint64(m.AuthorizedPolicy)) + } + return n +} + +func (m *AuthorizationList) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Authorizations) > 0 { + for _, e := range m.Authorizations { + l = e.Size() + n += 1 + l + sovAuthorization(uint64(l)) + } + } + return n +} + +func sovAuthorization(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozAuthorization(x uint64) (n int) { + return sovAuthorization(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *Authorization) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthorization + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Authorization: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Authorization: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MsgUrl", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthorization + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAuthorization + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAuthorization + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.MsgUrl = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field AuthorizedPolicy", wireType) + } + m.AuthorizedPolicy = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthorization + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.AuthorizedPolicy |= PolicyType(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipAuthorization(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAuthorization + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *AuthorizationList) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthorization + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: AuthorizationList: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: AuthorizationList: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Authorizations", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthorization + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthAuthorization + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthAuthorization + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Authorizations = append(m.Authorizations, Authorization{}) + if err := m.Authorizations[len(m.Authorizations)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipAuthorization(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAuthorization + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipAuthorization(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthorization + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthorization + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthorization + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthAuthorization + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupAuthorization + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthAuthorization + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthAuthorization = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowAuthorization = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupAuthorization = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go new file mode 100644 index 0000000000..debe8998ab --- /dev/null +++ b/x/authority/types/authorizations.go @@ -0,0 +1,41 @@ +package types + +func DefaultAuthorizationsList() AuthorizationList { + var authorizations []Authorization + + authorizations = []Authorization{ + // OperationalPolicyMessageList + {MsgUrl: "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateRateLimiterFlags", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgWhitelistERC20", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgDeployFungibleCoinZRC20", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgDeploySystemContracts", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgRemoveForeignCoin", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20LiquidityCap", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20WithdrawFee", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgUnpauseZRC20", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.observer.MsgAddObserver", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.observer.MsgRemoveChainParams", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.observer.MsgResetChainNonces", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", AuthorizedPolicy: PolicyType_groupOperational}, + {MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", AuthorizedPolicy: PolicyType_groupOperational}, + // AdminPolicyMessageList + {MsgUrl: "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", AuthorizedPolicy: PolicyType_groupAdmin}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", AuthorizedPolicy: PolicyType_groupAdmin}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateContractBytecode", AuthorizedPolicy: PolicyType_groupAdmin}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateSystemContract", AuthorizedPolicy: PolicyType_groupAdmin}, + {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateObserver", AuthorizedPolicy: PolicyType_groupAdmin}, + // EmergencyPolicyMessageList + {MsgUrl: "/zetachain.zetacore.crosschain.MsgAddInboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, + {MsgUrl: "/zetachain.zetacore.crosschain.MsgRemoveOutboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, + {MsgUrl: "/zetachain.zetacore.fungible.MsgPauseZRC20", AuthorizedPolicy: PolicyType_groupEmergency}, + {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateKeygen", AuthorizedPolicy: PolicyType_groupEmergency}, + {MsgUrl: "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", AuthorizedPolicy: PolicyType_groupEmergency}, + } + + return AuthorizationList{ + Authorizations: authorizations, + } +} diff --git a/x/authority/types/genesis.pb.go b/x/authority/types/genesis.pb.go index 77857e5b67..923ebb8308 100644 --- a/x/authority/types/genesis.pb.go +++ b/x/authority/types/genesis.pb.go @@ -25,7 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the authority module's genesis state. type GenesisState struct { - Policies Policies `protobuf:"bytes,1,opt,name=policies,proto3" json:"policies"` + Policies Policies `protobuf:"bytes,1,opt,name=policies,proto3" json:"policies"` + AuthorizationList AuthorizationList `protobuf:"bytes,2,opt,name=authorization_list,json=authorizationList,proto3" json:"authorization_list"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -68,6 +69,13 @@ func (m *GenesisState) GetPolicies() Policies { return Policies{} } +func (m *GenesisState) GetAuthorizationList() AuthorizationList { + if m != nil { + return m.AuthorizationList + } + return AuthorizationList{} +} + func init() { proto.RegisterType((*GenesisState)(nil), "zetachain.zetacore.authority.GenesisState") } @@ -77,20 +85,23 @@ func init() { } var fileDescriptor_633475075491b169 = []byte{ - // 202 bytes of a gzipped FileDescriptorProto + // 248 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xd2, 0xaa, 0x4a, 0x2d, 0x49, 0x4c, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x07, 0xb3, 0xf2, 0x8b, 0x52, 0xf5, 0x13, 0x4b, 0x4b, 0x32, 0xf2, 0x8b, 0x32, 0x4b, 0x2a, 0xf5, 0xd3, 0x53, 0xf3, 0x52, 0x8b, 0x33, 0x8b, 0xf5, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0x64, 0xe0, 0x6a, 0xf5, 0x60, 0x6a, 0xf5, 0xe0, 0x6a, 0xa5, 0xb4, 0xf1, - 0x9a, 0x54, 0x90, 0x9f, 0x93, 0x99, 0x9c, 0x99, 0x0a, 0x35, 0x4a, 0x4a, 0x24, 0x3d, 0x3f, 0x3d, - 0x1f, 0xcc, 0xd4, 0x07, 0xb1, 0x20, 0xa2, 0x4a, 0x11, 0x5c, 0x3c, 0xee, 0x10, 0x1b, 0x83, 0x4b, - 0x12, 0x4b, 0x52, 0x85, 0x3c, 0xb8, 0x38, 0x60, 0xfa, 0x24, 0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, - 0xd4, 0xf4, 0xf0, 0xb9, 0x41, 0x2f, 0x00, 0xaa, 0xda, 0x89, 0xe5, 0xc4, 0x3d, 0x79, 0x86, 0x20, - 0xb8, 0x6e, 0x27, 0xaf, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, - 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x48, - 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0x05, 0xbb, 0x5b, 0x17, 0xcd, 0x0b, 0x15, - 0x48, 0x9e, 0x28, 0xa9, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0x3b, 0xd6, 0x18, 0x10, 0x00, 0x00, - 0xff, 0xff, 0xd8, 0x71, 0x7a, 0xaa, 0x3b, 0x01, 0x00, 0x00, + 0x9a, 0x54, 0x90, 0x9f, 0x93, 0x99, 0x9c, 0x99, 0x0a, 0x35, 0x4a, 0xca, 0x00, 0xaf, 0x62, 0x28, + 0xab, 0x2a, 0xb1, 0x24, 0x33, 0x3f, 0x0f, 0xaa, 0x43, 0x24, 0x3d, 0x3f, 0x3d, 0x1f, 0xcc, 0xd4, + 0x07, 0xb1, 0x20, 0xa2, 0x4a, 0xfb, 0x18, 0xb9, 0x78, 0xdc, 0x21, 0x8e, 0x0c, 0x2e, 0x49, 0x2c, + 0x49, 0x15, 0xf2, 0xe0, 0xe2, 0x80, 0x59, 0x25, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x6d, 0xa4, 0xa6, + 0x87, 0xcf, 0xd9, 0x7a, 0x01, 0x50, 0xd5, 0x4e, 0x2c, 0x27, 0xee, 0xc9, 0x33, 0x04, 0xc1, 0x75, + 0x0b, 0xa5, 0x70, 0x09, 0xa1, 0xb8, 0x23, 0x3e, 0x27, 0xb3, 0xb8, 0x44, 0x82, 0x09, 0x6c, 0xa6, + 0x3e, 0x7e, 0x33, 0x1d, 0x91, 0xf5, 0xf9, 0x64, 0x16, 0x97, 0x40, 0x0d, 0x17, 0x4c, 0xc4, 0x90, + 0xf0, 0x3a, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, + 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x83, 0xf4, 0xcc, 0x92, + 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x70, 0x18, 0xe9, 0xa2, 0x05, 0x57, 0x05, 0x52, 0x80, + 0x95, 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0xc3, 0xc4, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, + 0x65, 0xfd, 0x9b, 0x1b, 0xd4, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -113,6 +124,16 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size, err := m.AuthorizationList.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 { size, err := m.Policies.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -145,6 +166,8 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.Policies.Size() n += 1 + l + sovGenesis(uint64(l)) + l = m.AuthorizationList.Size() + n += 1 + l + sovGenesis(uint64(l)) return n } @@ -216,6 +239,39 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AuthorizationList", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.AuthorizationList.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) From fdc09d17c19a1c25a83c7035c7b0d686805eb2e4 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 08:07:27 -0400 Subject: [PATCH 02/15] add update authorization message --- docs/openapi/openapi.swagger.yaml | 20 + docs/spec/authority/messages.md | 10 + proto/zetachain/zetacore/authority/tx.proto | 13 + .../zetachain/zetacore/authority/tx_pb.d.ts | 57 +++ x/authority/genesis.go | 5 +- x/authority/keeper/authorization_list.go | 17 +- x/authority/keeper/authorization_list_test.go | 24 +- .../msg_server_update_authorizations.go | 36 ++ .../msg_server_update_authorizations_test.go | 67 +++ x/authority/types/authorizations.go | 161 +++++- x/authority/types/authorizations_test.go | 60 +++ x/authority/types/errors.go | 5 +- x/authority/types/tx.pb.go | 468 +++++++++++++++++- 13 files changed, 897 insertions(+), 46 deletions(-) create mode 100644 x/authority/keeper/msg_server_update_authorizations.go create mode 100644 x/authority/keeper/msg_server_update_authorizations_test.go create mode 100644 x/authority/types/authorizations_test.go diff --git a/docs/openapi/openapi.swagger.yaml b/docs/openapi/openapi.swagger.yaml index 9067eccc6d..a4d5c9906c 100644 --- a/docs/openapi/openapi.swagger.yaml +++ b/docs/openapi/openapi.swagger.yaml @@ -56703,6 +56703,26 @@ definitions: format: int64 balance: type: string + authorityAuthorization: + type: object + properties: + msg_url: + type: string + title: The URL of the message that needs to be authorized + authorized_policy: + $ref: '#/definitions/authorityPolicyType' + title: The policy that is authorized to access the message + authorityAuthorizationList: + type: object + properties: + authorizations: + type: array + items: + type: object + $ref: '#/definitions/authorityAuthorization' + title: AuthorizationList holds the list of authorizations on zetachain + authorityMsgUpdateAuthorizationsResponse: + type: object authorityMsgUpdatePoliciesResponse: type: object description: MsgUpdatePoliciesResponse defines the MsgUpdatePoliciesResponse service. diff --git a/docs/spec/authority/messages.md b/docs/spec/authority/messages.md index 788e95c6f6..8f6c21ee6a 100644 --- a/docs/spec/authority/messages.md +++ b/docs/spec/authority/messages.md @@ -11,3 +11,13 @@ message MsgUpdatePolicies { } ``` +## MsgUpdateAuthorizations + +```proto +message MsgUpdateAuthorizations { + string signer = 1; + AuthorizationList add_authorization_list = 2; + AuthorizationList remove_authorization_list = 3; +} +``` + diff --git a/proto/zetachain/zetacore/authority/tx.proto b/proto/zetachain/zetacore/authority/tx.proto index 7e8c007005..141f034f56 100644 --- a/proto/zetachain/zetacore/authority/tx.proto +++ b/proto/zetachain/zetacore/authority/tx.proto @@ -2,6 +2,7 @@ syntax = "proto3"; package zetachain.zetacore.authority; import "zetachain/zetacore/authority/policies.proto"; +import "zetachain/zetacore/authority/authorization.proto"; import "gogoproto/gogo.proto"; option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; @@ -9,6 +10,8 @@ option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; // Msg defines the Msg service. service Msg { rpc UpdatePolicies(MsgUpdatePolicies) returns (MsgUpdatePoliciesResponse); + rpc UpdateAuthorizations(MsgUpdateAuthorizations) + returns (MsgUpdateAuthorizationsResponse); } // MsgUpdatePolicies defines the MsgUpdatePolicies service. @@ -19,3 +22,13 @@ message MsgUpdatePolicies { // MsgUpdatePoliciesResponse defines the MsgUpdatePoliciesResponse service. message MsgUpdatePoliciesResponse {} + +// MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It +// accepts a list of authorizations to add and remove. +message MsgUpdateAuthorizations { + string signer = 1; + AuthorizationList add_authorization_list = 2 [ (gogoproto.nullable) = false ]; + AuthorizationList remove_authorization_list = 3 + [ (gogoproto.nullable) = false ]; +} +message MsgUpdateAuthorizationsResponse {} \ No newline at end of file diff --git a/typescript/zetachain/zetacore/authority/tx_pb.d.ts b/typescript/zetachain/zetacore/authority/tx_pb.d.ts index b77e1231dc..1d9a0fe2c6 100644 --- a/typescript/zetachain/zetacore/authority/tx_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/tx_pb.d.ts @@ -6,6 +6,7 @@ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; import { Message, proto3 } from "@bufbuild/protobuf"; import type { Policies } from "./policies_pb.js"; +import type { AuthorizationList } from "./authorization_pb.js"; /** * MsgUpdatePolicies defines the MsgUpdatePolicies service. @@ -59,3 +60,59 @@ export declare class MsgUpdatePoliciesResponse extends Message | undefined, b: MsgUpdatePoliciesResponse | PlainMessage | undefined): boolean; } +/** + * MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It + * accepts a list of authorizations to add and remove. + * + * @generated from message zetachain.zetacore.authority.MsgUpdateAuthorizations + */ +export declare class MsgUpdateAuthorizations extends Message { + /** + * @generated from field: string signer = 1; + */ + signer: string; + + /** + * @generated from field: zetachain.zetacore.authority.AuthorizationList add_authorization_list = 2; + */ + addAuthorizationList?: AuthorizationList; + + /** + * @generated from field: zetachain.zetacore.authority.AuthorizationList remove_authorization_list = 3; + */ + removeAuthorizationList?: AuthorizationList; + + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.MsgUpdateAuthorizations"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): MsgUpdateAuthorizations; + + static fromJson(jsonValue: JsonValue, options?: Partial): MsgUpdateAuthorizations; + + static fromJsonString(jsonString: string, options?: Partial): MsgUpdateAuthorizations; + + static equals(a: MsgUpdateAuthorizations | PlainMessage | undefined, b: MsgUpdateAuthorizations | PlainMessage | undefined): boolean; +} + +/** + * @generated from message zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse + */ +export declare class MsgUpdateAuthorizationsResponse extends Message { + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): MsgUpdateAuthorizationsResponse; + + static fromJson(jsonValue: JsonValue, options?: Partial): MsgUpdateAuthorizationsResponse; + + static fromJsonString(jsonString: string, options?: Partial): MsgUpdateAuthorizationsResponse; + + static equals(a: MsgUpdateAuthorizationsResponse | PlainMessage | undefined, b: MsgUpdateAuthorizationsResponse | PlainMessage | undefined): boolean; +} + diff --git a/x/authority/genesis.go b/x/authority/genesis.go index e4a0ca8c3f..adcaeaed29 100644 --- a/x/authority/genesis.go +++ b/x/authority/genesis.go @@ -10,7 +10,10 @@ import ( // InitGenesis initializes the authority module's state from a provided genesis state func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { k.SetPolicies(ctx, genState.Policies) - k.SetAuthorizationList(ctx, genState.AuthorizationList) + err := k.SetAuthorizationList(ctx, genState.AuthorizationList) + if err != nil { + ctx.Logger().Error("Failed to set authorization list in InitGenesis", "error", err) + } } // ExportGenesis returns the authority module's exported genesis. diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go index 8315aa30a3..cb86472ac8 100644 --- a/x/authority/keeper/authorization_list.go +++ b/x/authority/keeper/authorization_list.go @@ -7,10 +7,15 @@ import ( ) // SetAuthorizationList sets the authorization list to the store -func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) { +func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) error { + err := list.Validate() + if err != nil { + return err + } store := ctx.KVStore(k.storeKey) b := k.cdc.MustMarshal(&list) store.Set([]byte{0}, b) + return nil } // GetAuthorizationList returns the authorization list from the store @@ -23,3 +28,13 @@ func (k Keeper) GetAuthorizationList(ctx sdk.Context) (val types.AuthorizationLi k.cdc.MustUnmarshal(b, &val) return val, true } + +func (k Keeper) UpdateAuthorizationList(ctx sdk.Context, addList types.AuthorizationList, removeList types.AuthorizationList) types.AuthorizationList { + list, found := k.GetAuthorizationList(ctx) + if !found { + return addList + } + list.AddAuthorizations(addList) + list.RemoveAuthorizations(removeList) + return list +} diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index 11bc5e60ca..b2fc7a609a 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -4,15 +4,17 @@ import ( "testing" "github.com/stretchr/testify/require" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/testutil/sample" + "github.com/zeta-chain/zetacore/x/authority/types" ) func TestKeeper_GetAuthorizationList(t *testing.T) { t.Run("successfully get authorizations list", func(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) authorizationList := sample.AuthorizationList("sample") - k.SetAuthorizationList(ctx, authorizationList) + require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) list, found := k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, authorizationList, list) @@ -29,16 +31,32 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { t.Run("successfully set authorizations list when a list already exists", func(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) authorizationList := sample.AuthorizationList("sample") - k.SetAuthorizationList(ctx, authorizationList) + require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) list, found := k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, authorizationList, list) newAuthorizationList := sample.AuthorizationList("sample2") require.NotEqual(t, authorizationList, newAuthorizationList) - k.SetAuthorizationList(ctx, newAuthorizationList) + require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) list, found = k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, newAuthorizationList, list) }) + + t.Run("unable to set invalid authorizations list", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }} + + require.ErrorIs(t, k.SetAuthorizationList(ctx, authorizationsList), types.ErrInValidAuthorizationList) + }) } diff --git a/x/authority/keeper/msg_server_update_authorizations.go b/x/authority/keeper/msg_server_update_authorizations.go new file mode 100644 index 0000000000..c58c5f2e01 --- /dev/null +++ b/x/authority/keeper/msg_server_update_authorizations.go @@ -0,0 +1,36 @@ +package keeper + +import ( + "context" + + errorsmod "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + + "github.com/zeta-chain/zetacore/x/authority/types" +) + +func (k msgServer) UpdateAuthorizations( + goCtx context.Context, + msg *types.MsgUpdateAuthorizations, +) (*types.MsgUpdateAuthorizationsResponse, error) { + ctx := sdk.UnwrapSDKContext(goCtx) + + // check called by governance + if k.govAddr.String() != msg.Signer { + return nil, errorsmod.Wrapf( + govtypes.ErrInvalidSigner, + "invalid authority, expected %s, got %s", + k.govAddr.String(), + msg.Signer, + ) + } + + list := k.UpdateAuthorizationList(ctx, msg.AddAuthorizationList, msg.RemoveAuthorizationList) + err := k.SetAuthorizationList(ctx, list) + if err != nil { + return nil, err + } + + return nil, nil +} diff --git a/x/authority/keeper/msg_server_update_authorizations_test.go b/x/authority/keeper/msg_server_update_authorizations_test.go new file mode 100644 index 0000000000..119166c311 --- /dev/null +++ b/x/authority/keeper/msg_server_update_authorizations_test.go @@ -0,0 +1,67 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/require" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" + "github.com/zeta-chain/zetacore/testutil/sample" + "github.com/zeta-chain/zetacore/x/authority/keeper" + "github.com/zeta-chain/zetacore/x/authority/types" +) + +func TestMsgServer_UpdateAuthorizations(t *testing.T) { + t.Run("can't update authorizations with invalid signer", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + msgServer := keeper.NewMsgServerImpl(*k) + + msg := types.MsgUpdateAuthorizations{ + Signer: sample.AccAddress(), + AuthorizationList: sample.AuthorizationList("sample"), + } + + _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) + require.ErrorIs(t, err, govtypes.ErrInvalidSigner) + }) + + //t.Run("can update authorizations", func(t *testing.T) { + // k, ctx := keepertest.AuthorityKeeper(t) + // msgServer := keeper.NewMsgServerImpl(*k) + // require.NoError(t, k.SetAuthorizationList(ctx, types.DefaultAuthorizationsList())) + // + // authorizationList := sample.AuthorizationList("sample") + // msg := types.MsgUpdateAuthorizations{ + // Signer: keepertest.AuthorityGovAddress.String(), + // AuthorizationList: authorizationList, + // } + // + // _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) + // require.NoError(t, err) + // + // // Check authorization list is set + // got, found := k.GetAuthorizationList(ctx) + // require.True(t, found) + // require.Equal(t, append(types.DefaultAuthorizationsList().Authorizations, authorizationList.Authorizations...), got.Authorizations) + //}) + + t.Run("can add new authorizations when authorizations are not set", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + msgServer := keeper.NewMsgServerImpl(*k) + + authorizationList := sample.AuthorizationList("sample") + msg := types.MsgUpdateAuthorizations{ + Signer: keepertest.AuthorityGovAddress.String(), + AuthorizationList: authorizationList, + } + + _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) + require.NoError(t, err) + + // Check authorization list is set + got, found := k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, authorizationList, got) + }) +} diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index debe8998ab..7820b0eb50 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -1,41 +1,150 @@ package types +import ( + "fmt" + + "cosmossdk.io/errors" +) + func DefaultAuthorizationsList() AuthorizationList { var authorizations []Authorization authorizations = []Authorization{ // OperationalPolicyMessageList - {MsgUrl: "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateRateLimiterFlags", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgWhitelistERC20", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgDeployFungibleCoinZRC20", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgDeploySystemContracts", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgRemoveForeignCoin", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20LiquidityCap", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20WithdrawFee", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgUnpauseZRC20", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.observer.MsgAddObserver", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.observer.MsgRemoveChainParams", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.observer.MsgResetChainNonces", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", AuthorizedPolicy: PolicyType_groupOperational}, - {MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", AuthorizedPolicy: PolicyType_groupOperational}, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", + AuthorizedPolicy: PolicyType_groupOperational}, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateRateLimiterFlags", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgWhitelistERC20", + AuthorizedPolicy: PolicyType_groupOperational}, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgDeployFungibleCoinZRC20", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgDeploySystemContracts", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgRemoveForeignCoin", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20LiquidityCap", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20WithdrawFee", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgUnpauseZRC20", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgAddObserver", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgRemoveChainParams", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgResetChainNonces", + AuthorizedPolicy: PolicyType_groupOperational, + }, + + {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", + AuthorizedPolicy: PolicyType_groupOperational}, + { + MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", + AuthorizedPolicy: PolicyType_groupOperational, + }, // AdminPolicyMessageList - {MsgUrl: "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", AuthorizedPolicy: PolicyType_groupAdmin}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", AuthorizedPolicy: PolicyType_groupAdmin}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateContractBytecode", AuthorizedPolicy: PolicyType_groupAdmin}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateSystemContract", AuthorizedPolicy: PolicyType_groupAdmin}, - {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateObserver", AuthorizedPolicy: PolicyType_groupAdmin}, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", + AuthorizedPolicy: PolicyType_groupAdmin, + }, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", + AuthorizedPolicy: PolicyType_groupAdmin, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateContractBytecode", + AuthorizedPolicy: PolicyType_groupAdmin, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateSystemContract", + AuthorizedPolicy: PolicyType_groupAdmin, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgUpdateObserver", + AuthorizedPolicy: PolicyType_groupAdmin, + }, // EmergencyPolicyMessageList - {MsgUrl: "/zetachain.zetacore.crosschain.MsgAddInboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, - {MsgUrl: "/zetachain.zetacore.crosschain.MsgRemoveOutboundTracker", AuthorizedPolicy: PolicyType_groupEmergency}, - {MsgUrl: "/zetachain.zetacore.fungible.MsgPauseZRC20", AuthorizedPolicy: PolicyType_groupEmergency}, - {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateKeygen", AuthorizedPolicy: PolicyType_groupEmergency}, - {MsgUrl: "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", AuthorizedPolicy: PolicyType_groupEmergency}, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgAddInboundTracker", + AuthorizedPolicy: PolicyType_groupEmergency, + }, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", + AuthorizedPolicy: PolicyType_groupEmergency, + }, + { + MsgUrl: "/zetachain.zetacore.crosschain.MsgRemoveOutboundTracker", + AuthorizedPolicy: PolicyType_groupEmergency, + }, + { + MsgUrl: "/zetachain.zetacore.fungible.MsgPauseZRC20", + AuthorizedPolicy: PolicyType_groupEmergency, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgUpdateKeygen", + AuthorizedPolicy: PolicyType_groupEmergency, + }, + { + MsgUrl: "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", + AuthorizedPolicy: PolicyType_groupEmergency, + }, } return AuthorizationList{ Authorizations: authorizations, } } + +func (a *AuthorizationList) AddAuthorizations(authorizationList AuthorizationList) { + a.Authorizations = append(a.Authorizations, authorizationList.Authorizations...) +} + +func (a *AuthorizationList) RemoveAuthorizations(removeList AuthorizationList) { + for _, removeAuth := range removeList.Authorizations { + for i, auth := range a.Authorizations { + if auth.MsgUrl == removeAuth.MsgUrl { + a.Authorizations = append(a.Authorizations[:i], a.Authorizations[i+1:]...) + } + } + } +} + +func (a *AuthorizationList) Validate() error { + if len(a.Authorizations) == 0 { + return errors.Wrap(ErrInValidAuthorizationList, "empty authorization list") + } + checkMsgUrls := make(map[string]bool) + for _, authorization := range a.Authorizations { + if checkMsgUrls[authorization.MsgUrl] { + return errors.Wrap(ErrInValidAuthorizationList, fmt.Sprintf("duplicate message url: %s", authorization.MsgUrl)) + } + checkMsgUrls[authorization.MsgUrl] = true + } + return nil +} diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go new file mode 100644 index 0000000000..8c6585779d --- /dev/null +++ b/x/authority/types/authorizations_test.go @@ -0,0 +1,60 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/zetacore/testutil/sample" + "github.com/zeta-chain/zetacore/x/authority/types" +) + +func TestAuthorizationList_AddAuthorizations(t *testing.T) { + t.Run("AddAuthorizations", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newList := sample.AuthorizationList("sample") + authorizationsList.AddAuthorizations(newList) + require.ElementsMatch( + t, + append(types.DefaultAuthorizationsList().Authorizations, newList.Authorizations...), + authorizationsList.Authorizations, + ) + }) +} + +func TestAuthorizationList_Validate(t *testing.T) { + t.Run("Validate successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + require.NoError(t, authorizationsList.Validate()) + }) + t.Run("Validate failed with duplicate msg url with different policies", func(t *testing.T) { + authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }} + + require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) + }) + + t.Run("Validate failed with duplicate msg url with same policies", func(t *testing.T) { + authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }} + + require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) + }) + +} diff --git a/x/authority/types/errors.go b/x/authority/types/errors.go index 6df6aa26a5..a471d9e7c5 100644 --- a/x/authority/types/errors.go +++ b/x/authority/types/errors.go @@ -2,4 +2,7 @@ package types import errorsmod "cosmossdk.io/errors" -var ErrUnauthorized = errorsmod.Register(ModuleName, 1102, "sender not authorized") +var ( + ErrUnauthorized = errorsmod.Register(ModuleName, 1102, "sender not authorized") + ErrInValidAuthorizationList = errorsmod.Register(ModuleName, 1103, "invalid authorization list") +) diff --git a/x/authority/types/tx.pb.go b/x/authority/types/tx.pb.go index b27b2cfa20..f16c83de56 100644 --- a/x/authority/types/tx.pb.go +++ b/x/authority/types/tx.pb.go @@ -118,9 +118,109 @@ func (m *MsgUpdatePoliciesResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgUpdatePoliciesResponse proto.InternalMessageInfo +// MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It +// accepts a list of authorizations to add and remove. +type MsgUpdateAuthorizations struct { + Signer string `protobuf:"bytes,1,opt,name=signer,proto3" json:"signer,omitempty"` + AddAuthorizationList AuthorizationList `protobuf:"bytes,2,opt,name=add_authorization_list,json=addAuthorizationList,proto3" json:"add_authorization_list"` + RemoveAuthorizationList AuthorizationList `protobuf:"bytes,3,opt,name=remove_authorization_list,json=removeAuthorizationList,proto3" json:"remove_authorization_list"` +} + +func (m *MsgUpdateAuthorizations) Reset() { *m = MsgUpdateAuthorizations{} } +func (m *MsgUpdateAuthorizations) String() string { return proto.CompactTextString(m) } +func (*MsgUpdateAuthorizations) ProtoMessage() {} +func (*MsgUpdateAuthorizations) Descriptor() ([]byte, []int) { + return fileDescriptor_42e081863c477116, []int{2} +} +func (m *MsgUpdateAuthorizations) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgUpdateAuthorizations) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgUpdateAuthorizations.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgUpdateAuthorizations) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgUpdateAuthorizations.Merge(m, src) +} +func (m *MsgUpdateAuthorizations) XXX_Size() int { + return m.Size() +} +func (m *MsgUpdateAuthorizations) XXX_DiscardUnknown() { + xxx_messageInfo_MsgUpdateAuthorizations.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgUpdateAuthorizations proto.InternalMessageInfo + +func (m *MsgUpdateAuthorizations) GetSigner() string { + if m != nil { + return m.Signer + } + return "" +} + +func (m *MsgUpdateAuthorizations) GetAddAuthorizationList() AuthorizationList { + if m != nil { + return m.AddAuthorizationList + } + return AuthorizationList{} +} + +func (m *MsgUpdateAuthorizations) GetRemoveAuthorizationList() AuthorizationList { + if m != nil { + return m.RemoveAuthorizationList + } + return AuthorizationList{} +} + +type MsgUpdateAuthorizationsResponse struct { +} + +func (m *MsgUpdateAuthorizationsResponse) Reset() { *m = MsgUpdateAuthorizationsResponse{} } +func (m *MsgUpdateAuthorizationsResponse) String() string { return proto.CompactTextString(m) } +func (*MsgUpdateAuthorizationsResponse) ProtoMessage() {} +func (*MsgUpdateAuthorizationsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_42e081863c477116, []int{3} +} +func (m *MsgUpdateAuthorizationsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgUpdateAuthorizationsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgUpdateAuthorizationsResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgUpdateAuthorizationsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgUpdateAuthorizationsResponse.Merge(m, src) +} +func (m *MsgUpdateAuthorizationsResponse) XXX_Size() int { + return m.Size() +} +func (m *MsgUpdateAuthorizationsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_MsgUpdateAuthorizationsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgUpdateAuthorizationsResponse proto.InternalMessageInfo + func init() { proto.RegisterType((*MsgUpdatePolicies)(nil), "zetachain.zetacore.authority.MsgUpdatePolicies") proto.RegisterType((*MsgUpdatePoliciesResponse)(nil), "zetachain.zetacore.authority.MsgUpdatePoliciesResponse") + proto.RegisterType((*MsgUpdateAuthorizations)(nil), "zetachain.zetacore.authority.MsgUpdateAuthorizations") + proto.RegisterType((*MsgUpdateAuthorizationsResponse)(nil), "zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse") } func init() { @@ -128,24 +228,31 @@ func init() { } var fileDescriptor_42e081863c477116 = []byte{ - // 261 bytes of a gzipped FileDescriptorProto + // 371 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0xad, 0x4a, 0x2d, 0x49, 0x4c, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x07, 0xb3, 0xf2, 0x8b, 0x52, 0xf5, 0x13, 0x4b, 0x4b, 0x32, 0xf2, 0x8b, 0x32, 0x4b, 0x2a, 0xf5, 0x4b, 0x2a, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0x64, 0xe0, 0xca, 0xf4, 0x60, 0xca, 0xf4, 0xe0, 0xca, 0xa4, 0xb4, 0xf1, 0x1a, 0x52, 0x90, 0x9f, 0x93, - 0x99, 0x9c, 0x99, 0x5a, 0x0c, 0x31, 0x4a, 0x4a, 0x24, 0x3d, 0x3f, 0x3d, 0x1f, 0xcc, 0xd4, 0x07, - 0xb1, 0x20, 0xa2, 0x4a, 0xa5, 0x5c, 0x82, 0xbe, 0xc5, 0xe9, 0xa1, 0x05, 0x29, 0x89, 0x25, 0xa9, - 0x01, 0x50, 0x0d, 0x42, 0x62, 0x5c, 0x6c, 0xc5, 0x99, 0xe9, 0x79, 0xa9, 0x45, 0x12, 0x8c, 0x0a, - 0x8c, 0x1a, 0x9c, 0x41, 0x50, 0x9e, 0x90, 0x07, 0x17, 0x07, 0xcc, 0x50, 0x09, 0x26, 0x05, 0x46, - 0x0d, 0x6e, 0x23, 0x35, 0x3d, 0x7c, 0x0e, 0xd4, 0x83, 0x99, 0xe8, 0xc4, 0x72, 0xe2, 0x9e, 0x3c, - 0x43, 0x10, 0x5c, 0xb7, 0x92, 0x34, 0x97, 0x24, 0x86, 0xb5, 0x41, 0xa9, 0xc5, 0x05, 0xf9, 0x79, - 0xc5, 0xa9, 0x46, 0x8d, 0x8c, 0x5c, 0xcc, 0xbe, 0xc5, 0xe9, 0x42, 0x55, 0x5c, 0x7c, 0x68, 0x0e, - 0xd3, 0xc7, 0x6f, 0x1d, 0x86, 0x91, 0x52, 0xe6, 0x24, 0x6a, 0x80, 0xb9, 0xc1, 0xc9, 0xeb, 0xc4, - 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, - 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0x0c, 0xd2, 0x33, 0x4b, 0x32, 0x4a, 0x93, - 0xf4, 0x92, 0xf3, 0x73, 0xc1, 0xa1, 0xae, 0x8b, 0x16, 0x01, 0x15, 0xc8, 0xf1, 0x58, 0x59, 0x90, - 0x5a, 0x9c, 0xc4, 0x06, 0x0e, 0x6a, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xdc, 0x18, 0x8a, - 0x31, 0xf4, 0x01, 0x00, 0x00, + 0x99, 0x9c, 0x99, 0x5a, 0x0c, 0x31, 0x4a, 0xca, 0x00, 0xaf, 0x62, 0x28, 0xab, 0x2a, 0xb1, 0x24, + 0x33, 0x3f, 0x0f, 0xaa, 0x43, 0x24, 0x3d, 0x3f, 0x3d, 0x1f, 0xcc, 0xd4, 0x07, 0xb1, 0x20, 0xa2, + 0x4a, 0xa5, 0x5c, 0x82, 0xbe, 0xc5, 0xe9, 0xa1, 0x05, 0x29, 0x89, 0x25, 0xa9, 0x01, 0x50, 0x2b, + 0x84, 0xc4, 0xb8, 0xd8, 0x8a, 0x33, 0xd3, 0xf3, 0x52, 0x8b, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, + 0x83, 0xa0, 0x3c, 0x21, 0x0f, 0x2e, 0x0e, 0x98, 0x33, 0x24, 0x98, 0x14, 0x18, 0x35, 0xb8, 0x8d, + 0xd4, 0xf4, 0xf0, 0x79, 0x49, 0x0f, 0x66, 0xa2, 0x13, 0xcb, 0x89, 0x7b, 0xf2, 0x0c, 0x41, 0x70, + 0xdd, 0x4a, 0xd2, 0x5c, 0x92, 0x18, 0xd6, 0x06, 0xa5, 0x16, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x2a, + 0x4d, 0x64, 0xe2, 0x12, 0x87, 0xcb, 0x3a, 0x22, 0x7b, 0x05, 0xb7, 0xd3, 0xb2, 0xb9, 0xc4, 0x12, + 0x53, 0x52, 0xe2, 0x51, 0x3c, 0x1e, 0x9f, 0x93, 0x59, 0x5c, 0x02, 0x75, 0xa8, 0x3e, 0x7e, 0x87, + 0xa2, 0xd8, 0xe2, 0x93, 0x59, 0x5c, 0x02, 0x75, 0xb1, 0x48, 0x62, 0x4a, 0x0a, 0x86, 0x9c, 0x50, + 0x21, 0x97, 0x64, 0x51, 0x6a, 0x6e, 0x7e, 0x59, 0x2a, 0x36, 0xfb, 0x98, 0x29, 0xb1, 0x4f, 0x1c, + 0x62, 0x2e, 0x86, 0xb4, 0x92, 0x22, 0x97, 0x3c, 0x8e, 0x20, 0x81, 0x05, 0x9b, 0xd1, 0x04, 0x26, + 0x2e, 0x66, 0xdf, 0xe2, 0x74, 0xa1, 0x2a, 0x2e, 0x3e, 0xb4, 0xf8, 0x24, 0xe0, 0x18, 0x8c, 0x98, + 0x90, 0x32, 0x27, 0x51, 0x03, 0xcc, 0x0d, 0x42, 0x3d, 0x8c, 0x5c, 0x22, 0x58, 0xe3, 0xcd, 0x94, + 0x48, 0x13, 0x51, 0xb5, 0x49, 0xd9, 0x92, 0xa5, 0x0d, 0xe6, 0x1c, 0x27, 0xaf, 0x13, 0x8f, 0xe4, + 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, + 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x48, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, + 0xce, 0xcf, 0x05, 0x67, 0x20, 0x5d, 0xb4, 0xbc, 0x54, 0x81, 0x9c, 0x7f, 0x2b, 0x0b, 0x52, 0x8b, + 0x93, 0xd8, 0xc0, 0x19, 0xc6, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xa6, 0xbe, 0x90, 0x0d, 0xec, + 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -161,6 +268,7 @@ const _ = grpc.SupportPackageIsVersion4 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type MsgClient interface { UpdatePolicies(ctx context.Context, in *MsgUpdatePolicies, opts ...grpc.CallOption) (*MsgUpdatePoliciesResponse, error) + UpdateAuthorizations(ctx context.Context, in *MsgUpdateAuthorizations, opts ...grpc.CallOption) (*MsgUpdateAuthorizationsResponse, error) } type msgClient struct { @@ -180,9 +288,19 @@ func (c *msgClient) UpdatePolicies(ctx context.Context, in *MsgUpdatePolicies, o return out, nil } +func (c *msgClient) UpdateAuthorizations(ctx context.Context, in *MsgUpdateAuthorizations, opts ...grpc.CallOption) (*MsgUpdateAuthorizationsResponse, error) { + out := new(MsgUpdateAuthorizationsResponse) + err := c.cc.Invoke(ctx, "/zetachain.zetacore.authority.Msg/UpdateAuthorizations", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // MsgServer is the server API for Msg service. type MsgServer interface { UpdatePolicies(context.Context, *MsgUpdatePolicies) (*MsgUpdatePoliciesResponse, error) + UpdateAuthorizations(context.Context, *MsgUpdateAuthorizations) (*MsgUpdateAuthorizationsResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. @@ -192,6 +310,9 @@ type UnimplementedMsgServer struct { func (*UnimplementedMsgServer) UpdatePolicies(ctx context.Context, req *MsgUpdatePolicies) (*MsgUpdatePoliciesResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method UpdatePolicies not implemented") } +func (*UnimplementedMsgServer) UpdateAuthorizations(ctx context.Context, req *MsgUpdateAuthorizations) (*MsgUpdateAuthorizationsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateAuthorizations not implemented") +} func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) @@ -215,6 +336,24 @@ func _Msg_UpdatePolicies_Handler(srv interface{}, ctx context.Context, dec func( return interceptor(ctx, in, info, handler) } +func _Msg_UpdateAuthorizations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgUpdateAuthorizations) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).UpdateAuthorizations(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/zetachain.zetacore.authority.Msg/UpdateAuthorizations", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).UpdateAuthorizations(ctx, req.(*MsgUpdateAuthorizations)) + } + return interceptor(ctx, in, info, handler) +} + var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "zetachain.zetacore.authority.Msg", HandlerType: (*MsgServer)(nil), @@ -223,6 +362,10 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "UpdatePolicies", Handler: _Msg_UpdatePolicies_Handler, }, + { + MethodName: "UpdateAuthorizations", + Handler: _Msg_UpdateAuthorizations_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "zetachain/zetacore/authority/tx.proto", @@ -291,6 +434,79 @@ func (m *MsgUpdatePoliciesResponse) MarshalToSizedBuffer(dAtA []byte) (int, erro return len(dAtA) - i, nil } +func (m *MsgUpdateAuthorizations) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgUpdateAuthorizations) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgUpdateAuthorizations) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.RemoveAuthorizationList.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size, err := m.AddAuthorizationList.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Signer) > 0 { + i -= len(m.Signer) + copy(dAtA[i:], m.Signer) + i = encodeVarintTx(dAtA, i, uint64(len(m.Signer))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *MsgUpdateAuthorizationsResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgUpdateAuthorizationsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgUpdateAuthorizationsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + func encodeVarintTx(dAtA []byte, offset int, v uint64) int { offset -= sovTx(v) base := offset @@ -326,6 +542,32 @@ func (m *MsgUpdatePoliciesResponse) Size() (n int) { return n } +func (m *MsgUpdateAuthorizations) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Signer) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.AddAuthorizationList.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.RemoveAuthorizationList.Size() + n += 1 + l + sovTx(uint64(l)) + return n +} + +func (m *MsgUpdateAuthorizationsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + func sovTx(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -497,6 +739,204 @@ func (m *MsgUpdatePoliciesResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *MsgUpdateAuthorizations) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgUpdateAuthorizations: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgUpdateAuthorizations: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Signer", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Signer = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AddAuthorizationList", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.AddAuthorizationList.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field RemoveAuthorizationList", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.RemoveAuthorizationList.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgUpdateAuthorizationsResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgUpdateAuthorizationsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgUpdateAuthorizationsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTx(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From 9a05fb5085c809d2d5b5062dcf7a0c0fb2bb53ee Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 10:49:31 -0400 Subject: [PATCH 03/15] remove messages and add unit tests for AuthorizationList functions in types package --- app/setup_handlers.go | 2 +- docs/openapi/openapi.swagger.yaml | 20 - docs/spec/authority/messages.md | 10 - proto/zetachain/zetacore/authority/tx.proto | 12 - testutil/sample/authority.go | 7 + .../zetachain/zetacore/authority/tx_pb.d.ts | 57 --- x/authority/keeper/authorization_list.go | 10 - .../msg_server_update_authorizations.go | 36 -- .../msg_server_update_authorizations_test.go | 67 --- x/authority/types/authorizations.go | 53 +- x/authority/types/authorizations_test.go | 79 ++- x/authority/types/errors.go | 1 + x/authority/types/tx.pb.go | 457 +----------------- 13 files changed, 128 insertions(+), 683 deletions(-) delete mode 100644 x/authority/keeper/msg_server_update_authorizations.go delete mode 100644 x/authority/keeper/msg_server_update_authorizations_test.go diff --git a/app/setup_handlers.go b/app/setup_handlers.go index 2ca71ca0ae..7fe7bc1f8f 100644 --- a/app/setup_handlers.go +++ b/app/setup_handlers.go @@ -129,7 +129,7 @@ func SetupHandlers(app *App) { app.UpgradeKeeper.SetUpgradeHandler( constant.Version, - func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) { + func(ctx sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) { app.Logger().Info("Running upgrade handler for " + constant.Version) var err error diff --git a/docs/openapi/openapi.swagger.yaml b/docs/openapi/openapi.swagger.yaml index a4d5c9906c..9067eccc6d 100644 --- a/docs/openapi/openapi.swagger.yaml +++ b/docs/openapi/openapi.swagger.yaml @@ -56703,26 +56703,6 @@ definitions: format: int64 balance: type: string - authorityAuthorization: - type: object - properties: - msg_url: - type: string - title: The URL of the message that needs to be authorized - authorized_policy: - $ref: '#/definitions/authorityPolicyType' - title: The policy that is authorized to access the message - authorityAuthorizationList: - type: object - properties: - authorizations: - type: array - items: - type: object - $ref: '#/definitions/authorityAuthorization' - title: AuthorizationList holds the list of authorizations on zetachain - authorityMsgUpdateAuthorizationsResponse: - type: object authorityMsgUpdatePoliciesResponse: type: object description: MsgUpdatePoliciesResponse defines the MsgUpdatePoliciesResponse service. diff --git a/docs/spec/authority/messages.md b/docs/spec/authority/messages.md index 8f6c21ee6a..788e95c6f6 100644 --- a/docs/spec/authority/messages.md +++ b/docs/spec/authority/messages.md @@ -11,13 +11,3 @@ message MsgUpdatePolicies { } ``` -## MsgUpdateAuthorizations - -```proto -message MsgUpdateAuthorizations { - string signer = 1; - AuthorizationList add_authorization_list = 2; - AuthorizationList remove_authorization_list = 3; -} -``` - diff --git a/proto/zetachain/zetacore/authority/tx.proto b/proto/zetachain/zetacore/authority/tx.proto index 141f034f56..6944b80779 100644 --- a/proto/zetachain/zetacore/authority/tx.proto +++ b/proto/zetachain/zetacore/authority/tx.proto @@ -10,8 +10,6 @@ option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; // Msg defines the Msg service. service Msg { rpc UpdatePolicies(MsgUpdatePolicies) returns (MsgUpdatePoliciesResponse); - rpc UpdateAuthorizations(MsgUpdateAuthorizations) - returns (MsgUpdateAuthorizationsResponse); } // MsgUpdatePolicies defines the MsgUpdatePolicies service. @@ -22,13 +20,3 @@ message MsgUpdatePolicies { // MsgUpdatePoliciesResponse defines the MsgUpdatePoliciesResponse service. message MsgUpdatePoliciesResponse {} - -// MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It -// accepts a list of authorizations to add and remove. -message MsgUpdateAuthorizations { - string signer = 1; - AuthorizationList add_authorization_list = 2 [ (gogoproto.nullable) = false ]; - AuthorizationList remove_authorization_list = 3 - [ (gogoproto.nullable) = false ]; -} -message MsgUpdateAuthorizationsResponse {} \ No newline at end of file diff --git a/testutil/sample/authority.go b/testutil/sample/authority.go index b35a49d4b3..ef92db0f94 100644 --- a/testutil/sample/authority.go +++ b/testutil/sample/authority.go @@ -43,3 +43,10 @@ func AuthorizationList(val string) authoritytypes.AuthorizationList { }, } } + +func Authorization() authoritytypes.Authorization { + return authoritytypes.Authorization{ + MsgUrl: "ABC", + AuthorizedPolicy: authoritytypes.PolicyType_groupOperational, + } +} diff --git a/typescript/zetachain/zetacore/authority/tx_pb.d.ts b/typescript/zetachain/zetacore/authority/tx_pb.d.ts index 1d9a0fe2c6..b77e1231dc 100644 --- a/typescript/zetachain/zetacore/authority/tx_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/tx_pb.d.ts @@ -6,7 +6,6 @@ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; import { Message, proto3 } from "@bufbuild/protobuf"; import type { Policies } from "./policies_pb.js"; -import type { AuthorizationList } from "./authorization_pb.js"; /** * MsgUpdatePolicies defines the MsgUpdatePolicies service. @@ -60,59 +59,3 @@ export declare class MsgUpdatePoliciesResponse extends Message | undefined, b: MsgUpdatePoliciesResponse | PlainMessage | undefined): boolean; } -/** - * MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It - * accepts a list of authorizations to add and remove. - * - * @generated from message zetachain.zetacore.authority.MsgUpdateAuthorizations - */ -export declare class MsgUpdateAuthorizations extends Message { - /** - * @generated from field: string signer = 1; - */ - signer: string; - - /** - * @generated from field: zetachain.zetacore.authority.AuthorizationList add_authorization_list = 2; - */ - addAuthorizationList?: AuthorizationList; - - /** - * @generated from field: zetachain.zetacore.authority.AuthorizationList remove_authorization_list = 3; - */ - removeAuthorizationList?: AuthorizationList; - - constructor(data?: PartialMessage); - - static readonly runtime: typeof proto3; - static readonly typeName = "zetachain.zetacore.authority.MsgUpdateAuthorizations"; - static readonly fields: FieldList; - - static fromBinary(bytes: Uint8Array, options?: Partial): MsgUpdateAuthorizations; - - static fromJson(jsonValue: JsonValue, options?: Partial): MsgUpdateAuthorizations; - - static fromJsonString(jsonString: string, options?: Partial): MsgUpdateAuthorizations; - - static equals(a: MsgUpdateAuthorizations | PlainMessage | undefined, b: MsgUpdateAuthorizations | PlainMessage | undefined): boolean; -} - -/** - * @generated from message zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse - */ -export declare class MsgUpdateAuthorizationsResponse extends Message { - constructor(data?: PartialMessage); - - static readonly runtime: typeof proto3; - static readonly typeName = "zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse"; - static readonly fields: FieldList; - - static fromBinary(bytes: Uint8Array, options?: Partial): MsgUpdateAuthorizationsResponse; - - static fromJson(jsonValue: JsonValue, options?: Partial): MsgUpdateAuthorizationsResponse; - - static fromJsonString(jsonString: string, options?: Partial): MsgUpdateAuthorizationsResponse; - - static equals(a: MsgUpdateAuthorizationsResponse | PlainMessage | undefined, b: MsgUpdateAuthorizationsResponse | PlainMessage | undefined): boolean; -} - diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go index cb86472ac8..b50401d6e9 100644 --- a/x/authority/keeper/authorization_list.go +++ b/x/authority/keeper/authorization_list.go @@ -28,13 +28,3 @@ func (k Keeper) GetAuthorizationList(ctx sdk.Context) (val types.AuthorizationLi k.cdc.MustUnmarshal(b, &val) return val, true } - -func (k Keeper) UpdateAuthorizationList(ctx sdk.Context, addList types.AuthorizationList, removeList types.AuthorizationList) types.AuthorizationList { - list, found := k.GetAuthorizationList(ctx) - if !found { - return addList - } - list.AddAuthorizations(addList) - list.RemoveAuthorizations(removeList) - return list -} diff --git a/x/authority/keeper/msg_server_update_authorizations.go b/x/authority/keeper/msg_server_update_authorizations.go deleted file mode 100644 index c58c5f2e01..0000000000 --- a/x/authority/keeper/msg_server_update_authorizations.go +++ /dev/null @@ -1,36 +0,0 @@ -package keeper - -import ( - "context" - - errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" - govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - - "github.com/zeta-chain/zetacore/x/authority/types" -) - -func (k msgServer) UpdateAuthorizations( - goCtx context.Context, - msg *types.MsgUpdateAuthorizations, -) (*types.MsgUpdateAuthorizationsResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - - // check called by governance - if k.govAddr.String() != msg.Signer { - return nil, errorsmod.Wrapf( - govtypes.ErrInvalidSigner, - "invalid authority, expected %s, got %s", - k.govAddr.String(), - msg.Signer, - ) - } - - list := k.UpdateAuthorizationList(ctx, msg.AddAuthorizationList, msg.RemoveAuthorizationList) - err := k.SetAuthorizationList(ctx, list) - if err != nil { - return nil, err - } - - return nil, nil -} diff --git a/x/authority/keeper/msg_server_update_authorizations_test.go b/x/authority/keeper/msg_server_update_authorizations_test.go deleted file mode 100644 index 119166c311..0000000000 --- a/x/authority/keeper/msg_server_update_authorizations_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package keeper_test - -import ( - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - "github.com/stretchr/testify/require" - keepertest "github.com/zeta-chain/zetacore/testutil/keeper" - "github.com/zeta-chain/zetacore/testutil/sample" - "github.com/zeta-chain/zetacore/x/authority/keeper" - "github.com/zeta-chain/zetacore/x/authority/types" -) - -func TestMsgServer_UpdateAuthorizations(t *testing.T) { - t.Run("can't update authorizations with invalid signer", func(t *testing.T) { - k, ctx := keepertest.AuthorityKeeper(t) - msgServer := keeper.NewMsgServerImpl(*k) - - msg := types.MsgUpdateAuthorizations{ - Signer: sample.AccAddress(), - AuthorizationList: sample.AuthorizationList("sample"), - } - - _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) - require.ErrorIs(t, err, govtypes.ErrInvalidSigner) - }) - - //t.Run("can update authorizations", func(t *testing.T) { - // k, ctx := keepertest.AuthorityKeeper(t) - // msgServer := keeper.NewMsgServerImpl(*k) - // require.NoError(t, k.SetAuthorizationList(ctx, types.DefaultAuthorizationsList())) - // - // authorizationList := sample.AuthorizationList("sample") - // msg := types.MsgUpdateAuthorizations{ - // Signer: keepertest.AuthorityGovAddress.String(), - // AuthorizationList: authorizationList, - // } - // - // _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) - // require.NoError(t, err) - // - // // Check authorization list is set - // got, found := k.GetAuthorizationList(ctx) - // require.True(t, found) - // require.Equal(t, append(types.DefaultAuthorizationsList().Authorizations, authorizationList.Authorizations...), got.Authorizations) - //}) - - t.Run("can add new authorizations when authorizations are not set", func(t *testing.T) { - k, ctx := keepertest.AuthorityKeeper(t) - msgServer := keeper.NewMsgServerImpl(*k) - - authorizationList := sample.AuthorizationList("sample") - msg := types.MsgUpdateAuthorizations{ - Signer: keepertest.AuthorityGovAddress.String(), - AuthorizationList: authorizationList, - } - - _, err := msgServer.UpdateAuthorizations(sdk.WrapSDKContext(ctx), &msg) - require.NoError(t, err) - - // Check authorization list is set - got, found := k.GetAuthorizationList(ctx) - require.True(t, found) - require.Equal(t, authorizationList, got) - }) -} diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 7820b0eb50..a1331eb3e4 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -121,28 +121,57 @@ func DefaultAuthorizationsList() AuthorizationList { } } -func (a *AuthorizationList) AddAuthorizations(authorizationList AuthorizationList) { - a.Authorizations = append(a.Authorizations, authorizationList.Authorizations...) +// SetAuthorizations adds the authorization to the list. If the authorization already exists, it updates the policy. +func (a *AuthorizationList) SetAuthorizations(authorization Authorization) { + for i, auth := range a.Authorizations { + if auth.MsgUrl == authorization.MsgUrl { + a.Authorizations[i].AuthorizedPolicy = authorization.AuthorizedPolicy + return + } + } + a.Authorizations = append(a.Authorizations, authorization) } -func (a *AuthorizationList) RemoveAuthorizations(removeList AuthorizationList) { - for _, removeAuth := range removeList.Authorizations { - for i, auth := range a.Authorizations { - if auth.MsgUrl == removeAuth.MsgUrl { - a.Authorizations = append(a.Authorizations[:i], a.Authorizations[i+1:]...) - } +// RemoveAuthorizations removes the authorization from the list. It does not check if the authorization exists or not. +func (a *AuthorizationList) RemoveAuthorizations(authorization Authorization) { + for i, auth := range a.Authorizations { + if auth.MsgUrl == authorization.MsgUrl { + a.Authorizations = append(a.Authorizations[:i], a.Authorizations[i+1:]...) } } } -func (a *AuthorizationList) Validate() error { - if len(a.Authorizations) == 0 { - return errors.Wrap(ErrInValidAuthorizationList, "empty authorization list") +// CheckAuthorizationExists checks if the authorization exists in the list. +func (a *AuthorizationList) CheckAuthorizationExists(authorization Authorization) bool { + for _, auth := range a.Authorizations { + if auth.MsgUrl == authorization.MsgUrl { + return true + } } + return false +} + +// GetAuthorizedPolicy returns the policy for the given message url.If the message url is not found, +// it returns an error and the first value of the enum. +func (a *AuthorizationList) GetAuthorizedPolicy(msgURL string) (PolicyType, error) { + for _, auth := range a.Authorizations { + if auth.MsgUrl == msgURL { + return auth.AuthorizedPolicy, nil + } + } + // Returning first value of enum, can consider adding a default value of `EmptyPolicy` in the enum. + return PolicyType(0), ErrAuthorizationNotFound +} + +// Validate checks if the authorization list is valid. It returns an error if the message url is duplicated with different policies. +func (a *AuthorizationList) Validate() error { checkMsgUrls := make(map[string]bool) for _, authorization := range a.Authorizations { if checkMsgUrls[authorization.MsgUrl] { - return errors.Wrap(ErrInValidAuthorizationList, fmt.Sprintf("duplicate message url: %s", authorization.MsgUrl)) + return errors.Wrap( + ErrInValidAuthorizationList, + fmt.Sprintf("duplicate message url: %s", authorization.MsgUrl), + ) } checkMsgUrls[authorization.MsgUrl] = true } diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 8c6585779d..f4bf380824 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -4,21 +4,62 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/zeta-chain/zetacore/testutil/sample" "github.com/zeta-chain/zetacore/x/authority/types" ) -func TestAuthorizationList_AddAuthorizations(t *testing.T) { - t.Run("AddAuthorizations", func(t *testing.T) { +func TestAuthorizationList_SetAuthorizations(t *testing.T) { + t.Run("Set new authorization successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newAuthorization := sample.Authorization() + require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) + require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) + require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + }) + + t.Run("Update existing authorization successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newAuthorization := sample.Authorization() + require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) + require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) + require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + + newAuthorization.AuthorizedPolicy = types.PolicyType_groupEmergency + authorizationsList.SetAuthorizations(newAuthorization) + require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + policy, err := authorizationsList.GetAuthorizedPolicy(newAuthorization.MsgUrl) + require.NoError(t, err) + require.Equal(t, newAuthorization.AuthorizedPolicy, policy) + }) +} + +func TestAuthorizationList_GetAuthorizedPolicy(t *testing.T) { + t.Run("Get authorized policy successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newAuthorization := sample.Authorization() + authorizationsList.SetAuthorizations(newAuthorization) + policy, err := authorizationsList.GetAuthorizedPolicy(newAuthorization.MsgUrl) + require.NoError(t, err) + require.Equal(t, newAuthorization.AuthorizedPolicy, policy) + }) + + t.Run("Get authorized policy failed with not found", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() - newList := sample.AuthorizationList("sample") - authorizationsList.AddAuthorizations(newList) - require.ElementsMatch( - t, - append(types.DefaultAuthorizationsList().Authorizations, newList.Authorizations...), - authorizationsList.Authorizations, - ) + policy, err := authorizationsList.GetAuthorizedPolicy("ABC") + require.ErrorIs(t, err, types.ErrAuthorizationNotFound) + require.Equal(t, types.PolicyType(0), policy) + }) +} + +func TestAuthorizationList_CheckAuthorizationExists(t *testing.T) { + t.Run("Check authorization exists successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newAuthorization := sample.Authorization() + require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) + require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) }) } @@ -58,3 +99,21 @@ func TestAuthorizationList_Validate(t *testing.T) { }) } + +func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { + t.Run("Remove authorization successfully", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + newAuthorization := sample.Authorization() + authorizationsList.SetAuthorizations(newAuthorization) + require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.RemoveAuthorizations(newAuthorization) + require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)) + }) + + t.Run("do not remove anything if authorization not found", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + authorizationsList.RemoveAuthorizations(sample.Authorization()) + require.ElementsMatch(t, authorizationsList.Authorizations, types.DefaultAuthorizationsList().Authorizations) + }) +} diff --git a/x/authority/types/errors.go b/x/authority/types/errors.go index a471d9e7c5..e23355e729 100644 --- a/x/authority/types/errors.go +++ b/x/authority/types/errors.go @@ -5,4 +5,5 @@ import errorsmod "cosmossdk.io/errors" var ( ErrUnauthorized = errorsmod.Register(ModuleName, 1102, "sender not authorized") ErrInValidAuthorizationList = errorsmod.Register(ModuleName, 1103, "invalid authorization list") + ErrAuthorizationNotFound = errorsmod.Register(ModuleName, 1104, "authorization not found") ) diff --git a/x/authority/types/tx.pb.go b/x/authority/types/tx.pb.go index f16c83de56..fc4d384691 100644 --- a/x/authority/types/tx.pb.go +++ b/x/authority/types/tx.pb.go @@ -118,109 +118,9 @@ func (m *MsgUpdatePoliciesResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgUpdatePoliciesResponse proto.InternalMessageInfo -// MsgUpdateAuthorizations defines the MsgUpdateAuthorizations service.It -// accepts a list of authorizations to add and remove. -type MsgUpdateAuthorizations struct { - Signer string `protobuf:"bytes,1,opt,name=signer,proto3" json:"signer,omitempty"` - AddAuthorizationList AuthorizationList `protobuf:"bytes,2,opt,name=add_authorization_list,json=addAuthorizationList,proto3" json:"add_authorization_list"` - RemoveAuthorizationList AuthorizationList `protobuf:"bytes,3,opt,name=remove_authorization_list,json=removeAuthorizationList,proto3" json:"remove_authorization_list"` -} - -func (m *MsgUpdateAuthorizations) Reset() { *m = MsgUpdateAuthorizations{} } -func (m *MsgUpdateAuthorizations) String() string { return proto.CompactTextString(m) } -func (*MsgUpdateAuthorizations) ProtoMessage() {} -func (*MsgUpdateAuthorizations) Descriptor() ([]byte, []int) { - return fileDescriptor_42e081863c477116, []int{2} -} -func (m *MsgUpdateAuthorizations) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MsgUpdateAuthorizations) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MsgUpdateAuthorizations.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *MsgUpdateAuthorizations) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgUpdateAuthorizations.Merge(m, src) -} -func (m *MsgUpdateAuthorizations) XXX_Size() int { - return m.Size() -} -func (m *MsgUpdateAuthorizations) XXX_DiscardUnknown() { - xxx_messageInfo_MsgUpdateAuthorizations.DiscardUnknown(m) -} - -var xxx_messageInfo_MsgUpdateAuthorizations proto.InternalMessageInfo - -func (m *MsgUpdateAuthorizations) GetSigner() string { - if m != nil { - return m.Signer - } - return "" -} - -func (m *MsgUpdateAuthorizations) GetAddAuthorizationList() AuthorizationList { - if m != nil { - return m.AddAuthorizationList - } - return AuthorizationList{} -} - -func (m *MsgUpdateAuthorizations) GetRemoveAuthorizationList() AuthorizationList { - if m != nil { - return m.RemoveAuthorizationList - } - return AuthorizationList{} -} - -type MsgUpdateAuthorizationsResponse struct { -} - -func (m *MsgUpdateAuthorizationsResponse) Reset() { *m = MsgUpdateAuthorizationsResponse{} } -func (m *MsgUpdateAuthorizationsResponse) String() string { return proto.CompactTextString(m) } -func (*MsgUpdateAuthorizationsResponse) ProtoMessage() {} -func (*MsgUpdateAuthorizationsResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_42e081863c477116, []int{3} -} -func (m *MsgUpdateAuthorizationsResponse) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MsgUpdateAuthorizationsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MsgUpdateAuthorizationsResponse.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *MsgUpdateAuthorizationsResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgUpdateAuthorizationsResponse.Merge(m, src) -} -func (m *MsgUpdateAuthorizationsResponse) XXX_Size() int { - return m.Size() -} -func (m *MsgUpdateAuthorizationsResponse) XXX_DiscardUnknown() { - xxx_messageInfo_MsgUpdateAuthorizationsResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_MsgUpdateAuthorizationsResponse proto.InternalMessageInfo - func init() { proto.RegisterType((*MsgUpdatePolicies)(nil), "zetachain.zetacore.authority.MsgUpdatePolicies") proto.RegisterType((*MsgUpdatePoliciesResponse)(nil), "zetachain.zetacore.authority.MsgUpdatePoliciesResponse") - proto.RegisterType((*MsgUpdateAuthorizations)(nil), "zetachain.zetacore.authority.MsgUpdateAuthorizations") - proto.RegisterType((*MsgUpdateAuthorizationsResponse)(nil), "zetachain.zetacore.authority.MsgUpdateAuthorizationsResponse") } func init() { @@ -228,7 +128,7 @@ func init() { } var fileDescriptor_42e081863c477116 = []byte{ - // 371 bytes of a gzipped FileDescriptorProto + // 274 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0xad, 0x4a, 0x2d, 0x49, 0x4c, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x07, 0xb3, 0xf2, 0x8b, 0x52, 0xf5, 0x13, 0x4b, 0x4b, 0x32, 0xf2, 0x8b, 0x32, 0x4b, 0x2a, 0xf5, 0x4b, 0x2a, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0x64, @@ -239,20 +139,14 @@ var fileDescriptor_42e081863c477116 = []byte{ 0x84, 0xc4, 0xb8, 0xd8, 0x8a, 0x33, 0xd3, 0xf3, 0x52, 0x8b, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xa0, 0x3c, 0x21, 0x0f, 0x2e, 0x0e, 0x98, 0x33, 0x24, 0x98, 0x14, 0x18, 0x35, 0xb8, 0x8d, 0xd4, 0xf4, 0xf0, 0x79, 0x49, 0x0f, 0x66, 0xa2, 0x13, 0xcb, 0x89, 0x7b, 0xf2, 0x0c, 0x41, 0x70, - 0xdd, 0x4a, 0xd2, 0x5c, 0x92, 0x18, 0xd6, 0x06, 0xa5, 0x16, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x2a, - 0x4d, 0x64, 0xe2, 0x12, 0x87, 0xcb, 0x3a, 0x22, 0x7b, 0x05, 0xb7, 0xd3, 0xb2, 0xb9, 0xc4, 0x12, - 0x53, 0x52, 0xe2, 0x51, 0x3c, 0x1e, 0x9f, 0x93, 0x59, 0x5c, 0x02, 0x75, 0xa8, 0x3e, 0x7e, 0x87, - 0xa2, 0xd8, 0xe2, 0x93, 0x59, 0x5c, 0x02, 0x75, 0xb1, 0x48, 0x62, 0x4a, 0x0a, 0x86, 0x9c, 0x50, - 0x21, 0x97, 0x64, 0x51, 0x6a, 0x6e, 0x7e, 0x59, 0x2a, 0x36, 0xfb, 0x98, 0x29, 0xb1, 0x4f, 0x1c, - 0x62, 0x2e, 0x86, 0xb4, 0x92, 0x22, 0x97, 0x3c, 0x8e, 0x20, 0x81, 0x05, 0x9b, 0xd1, 0x04, 0x26, - 0x2e, 0x66, 0xdf, 0xe2, 0x74, 0xa1, 0x2a, 0x2e, 0x3e, 0xb4, 0xf8, 0x24, 0xe0, 0x18, 0x8c, 0x98, - 0x90, 0x32, 0x27, 0x51, 0x03, 0xcc, 0x0d, 0x42, 0x3d, 0x8c, 0x5c, 0x22, 0x58, 0xe3, 0xcd, 0x94, - 0x48, 0x13, 0x51, 0xb5, 0x49, 0xd9, 0x92, 0xa5, 0x0d, 0xe6, 0x1c, 0x27, 0xaf, 0x13, 0x8f, 0xe4, - 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, - 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x48, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, - 0xce, 0xcf, 0x05, 0x67, 0x20, 0x5d, 0xb4, 0xbc, 0x54, 0x81, 0x9c, 0x7f, 0x2b, 0x0b, 0x52, 0x8b, - 0x93, 0xd8, 0xc0, 0x19, 0xc6, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xa6, 0xbe, 0x90, 0x0d, 0xec, - 0x03, 0x00, 0x00, + 0xdd, 0x4a, 0xd2, 0x5c, 0x92, 0x18, 0xd6, 0x06, 0xa5, 0x16, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x1a, + 0x35, 0x32, 0x72, 0x31, 0xfb, 0x16, 0xa7, 0x0b, 0x55, 0x71, 0xf1, 0xa1, 0x39, 0x4c, 0x1f, 0xbf, + 0x75, 0x18, 0x46, 0x4a, 0x99, 0x93, 0xa8, 0x01, 0xe6, 0x06, 0x27, 0xaf, 0x13, 0x8f, 0xe4, 0x18, + 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, + 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x48, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, + 0xcf, 0x05, 0x07, 0xbd, 0x2e, 0x5a, 0x2c, 0x54, 0x20, 0xc7, 0x7c, 0x65, 0x41, 0x6a, 0x71, 0x12, + 0x1b, 0x38, 0xa8, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xa4, 0xf1, 0x27, 0x4c, 0x26, 0x02, + 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -268,7 +162,6 @@ const _ = grpc.SupportPackageIsVersion4 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type MsgClient interface { UpdatePolicies(ctx context.Context, in *MsgUpdatePolicies, opts ...grpc.CallOption) (*MsgUpdatePoliciesResponse, error) - UpdateAuthorizations(ctx context.Context, in *MsgUpdateAuthorizations, opts ...grpc.CallOption) (*MsgUpdateAuthorizationsResponse, error) } type msgClient struct { @@ -288,19 +181,9 @@ func (c *msgClient) UpdatePolicies(ctx context.Context, in *MsgUpdatePolicies, o return out, nil } -func (c *msgClient) UpdateAuthorizations(ctx context.Context, in *MsgUpdateAuthorizations, opts ...grpc.CallOption) (*MsgUpdateAuthorizationsResponse, error) { - out := new(MsgUpdateAuthorizationsResponse) - err := c.cc.Invoke(ctx, "/zetachain.zetacore.authority.Msg/UpdateAuthorizations", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - // MsgServer is the server API for Msg service. type MsgServer interface { UpdatePolicies(context.Context, *MsgUpdatePolicies) (*MsgUpdatePoliciesResponse, error) - UpdateAuthorizations(context.Context, *MsgUpdateAuthorizations) (*MsgUpdateAuthorizationsResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. @@ -310,9 +193,6 @@ type UnimplementedMsgServer struct { func (*UnimplementedMsgServer) UpdatePolicies(ctx context.Context, req *MsgUpdatePolicies) (*MsgUpdatePoliciesResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method UpdatePolicies not implemented") } -func (*UnimplementedMsgServer) UpdateAuthorizations(ctx context.Context, req *MsgUpdateAuthorizations) (*MsgUpdateAuthorizationsResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method UpdateAuthorizations not implemented") -} func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) @@ -336,24 +216,6 @@ func _Msg_UpdatePolicies_Handler(srv interface{}, ctx context.Context, dec func( return interceptor(ctx, in, info, handler) } -func _Msg_UpdateAuthorizations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(MsgUpdateAuthorizations) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(MsgServer).UpdateAuthorizations(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/zetachain.zetacore.authority.Msg/UpdateAuthorizations", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(MsgServer).UpdateAuthorizations(ctx, req.(*MsgUpdateAuthorizations)) - } - return interceptor(ctx, in, info, handler) -} - var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "zetachain.zetacore.authority.Msg", HandlerType: (*MsgServer)(nil), @@ -362,10 +224,6 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "UpdatePolicies", Handler: _Msg_UpdatePolicies_Handler, }, - { - MethodName: "UpdateAuthorizations", - Handler: _Msg_UpdateAuthorizations_Handler, - }, }, Streams: []grpc.StreamDesc{}, Metadata: "zetachain/zetacore/authority/tx.proto", @@ -434,79 +292,6 @@ func (m *MsgUpdatePoliciesResponse) MarshalToSizedBuffer(dAtA []byte) (int, erro return len(dAtA) - i, nil } -func (m *MsgUpdateAuthorizations) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *MsgUpdateAuthorizations) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MsgUpdateAuthorizations) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - { - size, err := m.RemoveAuthorizationList.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTx(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a - { - size, err := m.AddAuthorizationList.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTx(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - if len(m.Signer) > 0 { - i -= len(m.Signer) - copy(dAtA[i:], m.Signer) - i = encodeVarintTx(dAtA, i, uint64(len(m.Signer))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *MsgUpdateAuthorizationsResponse) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *MsgUpdateAuthorizationsResponse) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MsgUpdateAuthorizationsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - return len(dAtA) - i, nil -} - func encodeVarintTx(dAtA []byte, offset int, v uint64) int { offset -= sovTx(v) base := offset @@ -542,32 +327,6 @@ func (m *MsgUpdatePoliciesResponse) Size() (n int) { return n } -func (m *MsgUpdateAuthorizations) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Signer) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) - } - l = m.AddAuthorizationList.Size() - n += 1 + l + sovTx(uint64(l)) - l = m.RemoveAuthorizationList.Size() - n += 1 + l + sovTx(uint64(l)) - return n -} - -func (m *MsgUpdateAuthorizationsResponse) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - return n -} - func sovTx(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -739,204 +498,6 @@ func (m *MsgUpdatePoliciesResponse) Unmarshal(dAtA []byte) error { } return nil } -func (m *MsgUpdateAuthorizations) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: MsgUpdateAuthorizations: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MsgUpdateAuthorizations: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Signer", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Signer = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field AddAuthorizationList", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.AddAuthorizationList.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field RemoveAuthorizationList", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.RemoveAuthorizationList.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTx(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTx - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *MsgUpdateAuthorizationsResponse) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: MsgUpdateAuthorizationsResponse: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MsgUpdateAuthorizationsResponse: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - default: - iNdEx = preIndex - skippy, err := skipTx(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTx - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipTx(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From d8c169a5dae8a8ef5ecc57990f639762d7047d69 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 11:01:38 -0400 Subject: [PATCH 04/15] format DEfault policies --- x/authority/types/authorizations.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index a1331eb3e4..090a08a9a3 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -62,7 +62,8 @@ func DefaultAuthorizationsList() AuthorizationList { AuthorizedPolicy: PolicyType_groupOperational, }, - {MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", + { + MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", AuthorizedPolicy: PolicyType_groupOperational}, { MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", From 953b6c90d9502c1f72fa3ac2b62a8ad40be071c9 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 13:01:47 -0400 Subject: [PATCH 05/15] add unit test for default values of authorization list --- x/authority/keeper/authorization_list_test.go | 2 +- x/authority/types/authorizations_test.go | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index b2fc7a609a..9fd4cea4cf 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -38,7 +38,7 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { newAuthorizationList := sample.AuthorizationList("sample2") require.NotEqual(t, authorizationList, newAuthorizationList) - require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) + require.NoError(t, k.SetAuthorizationList(ctx, newAuthorizationList)) list, found = k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, newAuthorizationList, list) diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index f4bf380824..3a95f5c224 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -3,9 +3,14 @@ package types_test import ( "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/testutil/sample" "github.com/zeta-chain/zetacore/x/authority/types" + crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" + fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" + lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" ) func TestAuthorizationList_SetAuthorizations(t *testing.T) { @@ -117,3 +122,67 @@ func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { require.ElementsMatch(t, authorizationsList.Authorizations, types.DefaultAuthorizationsList().Authorizations) }) } + +func TestDefaultAuthorizationsList(t *testing.T) { + t.Run("Default authorizations list", func(t *testing.T) { + var OperationalPolicyMessageList = []string{ + sdk.MsgTypeURL(&crosschaintypes.MsgRefundAbortedCCTX{}), + sdk.MsgTypeURL(&crosschaintypes.MsgAbortStuckCCTX{}), + sdk.MsgTypeURL(&crosschaintypes.MsgUpdateRateLimiterFlags{}), + sdk.MsgTypeURL(&crosschaintypes.MsgWhitelistERC20{}), + sdk.MsgTypeURL(&fungibletypes.MsgDeployFungibleCoinZRC20{}), + sdk.MsgTypeURL(&fungibletypes.MsgDeploySystemContracts{}), + sdk.MsgTypeURL(&fungibletypes.MsgRemoveForeignCoin{}), + sdk.MsgTypeURL(&fungibletypes.MsgUpdateZRC20LiquidityCap{}), + sdk.MsgTypeURL(&fungibletypes.MsgUpdateZRC20WithdrawFee{}), + sdk.MsgTypeURL(&fungibletypes.MsgUnpauseZRC20{}), + sdk.MsgTypeURL(&observertypes.MsgAddObserver{}), + sdk.MsgTypeURL(&observertypes.MsgRemoveChainParams{}), + sdk.MsgTypeURL(&observertypes.MsgResetChainNonces{}), + sdk.MsgTypeURL(&observertypes.MsgUpdateChainParams{}), + //sdk.MsgTypeURL(&observertypes.MsgEnableCCTX{}), + //sdk.MsgTypeURL(&observertypes.MsgUpdateGasPriceIncreaseFlags{}), + sdk.MsgTypeURL(&lightclienttypes.MsgEnableHeaderVerification{}), + } + + // EmergencyPolicyMessageList is a list of messages that can be authorized by the emergency policy + var EmergencyPolicyMessageList = []string{ + sdk.MsgTypeURL(&crosschaintypes.MsgAddInboundTracker{}), + sdk.MsgTypeURL(&crosschaintypes.MsgAddOutboundTracker{}), + sdk.MsgTypeURL(&crosschaintypes.MsgRemoveOutboundTracker{}), + sdk.MsgTypeURL(&fungibletypes.MsgPauseZRC20{}), + sdk.MsgTypeURL(&observertypes.MsgUpdateKeygen{}), + //sdk.MsgTypeURL(&observertypes.MsgDisableCCTX{}), + sdk.MsgTypeURL(&lightclienttypes.MsgDisableHeaderVerification{}), + } + + // AdminPolicyMessageList is a list of messages that can be authorized by the admin policy + var AdminPolicyMessageList = []string{ + sdk.MsgTypeURL(&crosschaintypes.MsgMigrateTssFunds{}), + sdk.MsgTypeURL(&crosschaintypes.MsgUpdateTssAddress{}), + sdk.MsgTypeURL(&fungibletypes.MsgUpdateContractBytecode{}), + sdk.MsgTypeURL(&fungibletypes.MsgUpdateSystemContract{}), + sdk.MsgTypeURL(&observertypes.MsgUpdateObserver{}), + } + defaultList := types.DefaultAuthorizationsList() + for _, msgUrl := range OperationalPolicyMessageList { + require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + policy, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) + require.Equal(t, types.PolicyType_groupOperational, policy) + } + for _, msgUrl := range EmergencyPolicyMessageList { + require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + policy, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) + require.Equal(t, types.PolicyType_groupEmergency, policy) + } + for _, msgUrl := range AdminPolicyMessageList { + require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + policy, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) + require.Equal(t, types.PolicyType_groupAdmin, policy) + } + require.Len(t, defaultList.Authorizations, len(OperationalPolicyMessageList)+len(EmergencyPolicyMessageList)+len(AdminPolicyMessageList)) + }) +} From 3bce3f1c8922c4c18e4e51d137ddd6b454db0863 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 13:31:25 -0400 Subject: [PATCH 06/15] add new messages to the default list --- x/authority/types/authorizations.go | 15 ++++++++++++++- x/authority/types/authorizations_test.go | 6 +++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 090a08a9a3..3d0dfbf7ac 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -64,7 +64,16 @@ func DefaultAuthorizationsList() AuthorizationList { { MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", - AuthorizedPolicy: PolicyType_groupOperational}, + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgEnableCCTX", + AuthorizedPolicy: PolicyType_groupOperational, + }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgUpdateGasPriceIncreaseFlags", + AuthorizedPolicy: PolicyType_groupOperational, + }, { MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", AuthorizedPolicy: PolicyType_groupOperational, @@ -111,6 +120,10 @@ func DefaultAuthorizationsList() AuthorizationList { MsgUrl: "/zetachain.zetacore.observer.MsgUpdateKeygen", AuthorizedPolicy: PolicyType_groupEmergency, }, + { + MsgUrl: "/zetachain.zetacore.observer.MsgDisableCCTX", + AuthorizedPolicy: PolicyType_groupEmergency, + }, { MsgUrl: "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", AuthorizedPolicy: PolicyType_groupEmergency, diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 116c7f2769..1e40501025 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -141,8 +141,8 @@ func TestDefaultAuthorizationsList(t *testing.T) { sdk.MsgTypeURL(&observertypes.MsgRemoveChainParams{}), sdk.MsgTypeURL(&observertypes.MsgResetChainNonces{}), sdk.MsgTypeURL(&observertypes.MsgUpdateChainParams{}), - //sdk.MsgTypeURL(&observertypes.MsgEnableCCTX{}), - //sdk.MsgTypeURL(&observertypes.MsgUpdateGasPriceIncreaseFlags{}), + sdk.MsgTypeURL(&observertypes.MsgEnableCCTX{}), + sdk.MsgTypeURL(&observertypes.MsgUpdateGasPriceIncreaseFlags{}), sdk.MsgTypeURL(&lightclienttypes.MsgEnableHeaderVerification{}), } @@ -153,7 +153,7 @@ func TestDefaultAuthorizationsList(t *testing.T) { sdk.MsgTypeURL(&crosschaintypes.MsgRemoveOutboundTracker{}), sdk.MsgTypeURL(&fungibletypes.MsgPauseZRC20{}), sdk.MsgTypeURL(&observertypes.MsgUpdateKeygen{}), - //sdk.MsgTypeURL(&observertypes.MsgDisableCCTX{}), + sdk.MsgTypeURL(&observertypes.MsgDisableCCTX{}), sdk.MsgTypeURL(&lightclienttypes.MsgDisableHeaderVerification{}), } From 12ff33d92e3da3e25a236e4081fd6142b48dc658 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 13:56:35 -0400 Subject: [PATCH 07/15] add comments --- x/authority/genesis_test.go | 114 ++++++++++++------ x/authority/keeper/authorization_list.go | 2 +- x/authority/keeper/authorization_list_test.go | 2 + x/authority/types/authorizations.go | 3 + x/authority/types/authorizations_test.go | 25 ++-- 5 files changed, 100 insertions(+), 46 deletions(-) diff --git a/x/authority/genesis_test.go b/x/authority/genesis_test.go index 4bdf82d82c..5a6ae2abf7 100644 --- a/x/authority/genesis_test.go +++ b/x/authority/genesis_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/require" - keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/testutil/nullify" "github.com/zeta-chain/zetacore/testutil/sample" @@ -13,37 +12,84 @@ import ( ) func TestGenesis(t *testing.T) { - genesisState := types.GenesisState{ - Policies: sample.Policies(), - AuthorizationList: sample.AuthorizationList("sample"), - ChainInfo: sample.ChainInfo(42), - } - - // Init - k, ctx := keepertest.AuthorityKeeper(t) - authority.InitGenesis(ctx, *k, genesisState) - - // Check policy is set - policies, found := k.GetPolicies(ctx) - require.True(t, found) - require.Equal(t, genesisState.Policies, policies) - - // Check authorization list is set - authorizationList, found := k.GetAuthorizationList(ctx) - require.True(t, found) - require.Equal(t, genesisState.AuthorizationList, authorizationList) - - // Check chain info is set - chainInfo, found := k.GetChainInfo(ctx) - require.True(t, found) - require.Equal(t, genesisState.ChainInfo, chainInfo) - - // Export - got := authority.ExportGenesis(ctx, *k) - require.NotNil(t, got) - - // Compare genesis after init and export - nullify.Fill(&genesisState) - nullify.Fill(got) - require.Equal(t, genesisState, *got) + t.Run("valid genesis", func(t *testing.T) { + genesisState := types.GenesisState{ + Policies: sample.Policies(), + AuthorizationList: sample.AuthorizationList("sample"), + ChainInfo: sample.ChainInfo(42), + } + + // Init + k, ctx := keepertest.AuthorityKeeper(t) + authority.InitGenesis(ctx, *k, genesisState) + + // Check policy is set + policies, found := k.GetPolicies(ctx) + require.True(t, found) + require.Equal(t, genesisState.Policies, policies) + + // Check authorization list is set + authorizationList, found := k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, genesisState.AuthorizationList, authorizationList) + + // Check chain info is set + chainInfo, found := k.GetChainInfo(ctx) + require.True(t, found) + require.Equal(t, genesisState.ChainInfo, chainInfo) + + // Export + got := authority.ExportGenesis(ctx, *k) + require.NotNil(t, got) + + // Compare genesis after init and export + nullify.Fill(&genesisState) + nullify.Fill(got) + require.Equal(t, genesisState, *got) + }) + + t.Run("set genesis and export works but invalid authorization table is not set", func(t *testing.T) { + genesisState := types.GenesisState{ + Policies: sample.Policies(), + AuthorizationList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }}, + ChainInfo: sample.ChainInfo(42), + } + + // Init + k, ctx := keepertest.AuthorityKeeper(t) + authority.InitGenesis(ctx, *k, genesisState) + + // Check policy is set + policies, found := k.GetPolicies(ctx) + require.True(t, found) + require.Equal(t, genesisState.Policies, policies) + + // Check chain info is set + chainInfo, found := k.GetChainInfo(ctx) + require.True(t, found) + require.Equal(t, genesisState.ChainInfo, chainInfo) + + // Check authorization list is not set + _, found = k.GetAuthorizationList(ctx) + require.False(t, found) + + // Export + got := authority.ExportGenesis(ctx, *k) + require.NotNil(t, got) + + // Compare genesis after init and export + nullify.Fill(&genesisState) + nullify.Fill(got) + require.Equal(t, genesisState, *got) + + }) } diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go index b50401d6e9..3573549370 100644 --- a/x/authority/keeper/authorization_list.go +++ b/x/authority/keeper/authorization_list.go @@ -6,7 +6,7 @@ import ( "github.com/zeta-chain/zetacore/x/authority/types" ) -// SetAuthorizationList sets the authorization list to the store +// SetAuthorizationList sets the authorization list to the store.It returns an error if the list is invalid. func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) error { err := list.Validate() if err != nil { diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index 9fd4cea4cf..e445dfc1a8 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -32,6 +32,7 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) authorizationList := sample.AuthorizationList("sample") require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) + list, found := k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, authorizationList, list) @@ -39,6 +40,7 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { newAuthorizationList := sample.AuthorizationList("sample2") require.NotEqual(t, authorizationList, newAuthorizationList) require.NoError(t, k.SetAuthorizationList(ctx, newAuthorizationList)) + list, found = k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, newAuthorizationList, list) diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 3d0dfbf7ac..1fbb2533f5 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -6,6 +6,8 @@ import ( "cosmossdk.io/errors" ) +// DefaultAuthorizationsList list is the list of authorizations that presently exist in the system. +// This is the minimum set of authorizations that are required to be set when the authorization table is deployed func DefaultAuthorizationsList() AuthorizationList { var authorizations []Authorization @@ -178,6 +180,7 @@ func (a *AuthorizationList) GetAuthorizedPolicy(msgURL string) (PolicyType, erro } // Validate checks if the authorization list is valid. It returns an error if the message url is duplicated with different policies. +// It does not check if the list is empty or not, as an empty list is also considered valid. func (a *AuthorizationList) Validate() error { checkMsgUrls := make(map[string]bool) for _, authorization := range a.Authorizations { diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 1e40501025..08e80a07c6 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -15,19 +15,21 @@ import ( ) func TestAuthorizationList_SetAuthorizations(t *testing.T) { - t.Run("Set new authorization successfully", func(t *testing.T) { + t.Run("set new authorization successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() newAuthorization := sample.Authorization() require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) }) - t.Run("Update existing authorization successfully", func(t *testing.T) { + t.Run("update existing authorization successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() newAuthorization := sample.Authorization() require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) @@ -42,16 +44,17 @@ func TestAuthorizationList_SetAuthorizations(t *testing.T) { } func TestAuthorizationList_GetAuthorizedPolicy(t *testing.T) { - t.Run("Get authorized policy successfully", func(t *testing.T) { + t.Run("get authorized policy successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() newAuthorization := sample.Authorization() authorizationsList.SetAuthorizations(newAuthorization) + policy, err := authorizationsList.GetAuthorizedPolicy(newAuthorization.MsgUrl) require.NoError(t, err) require.Equal(t, newAuthorization.AuthorizedPolicy, policy) }) - t.Run("Get authorized policy failed with not found", func(t *testing.T) { + t.Run("get authorized policy fails when msg not found in list", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() policy, err := authorizationsList.GetAuthorizedPolicy("ABC") require.ErrorIs(t, err, types.ErrAuthorizationNotFound) @@ -60,21 +63,22 @@ func TestAuthorizationList_GetAuthorizedPolicy(t *testing.T) { } func TestAuthorizationList_CheckAuthorizationExists(t *testing.T) { - t.Run("Check authorization exists successfully", func(t *testing.T) { + t.Run("check authorization exists successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() newAuthorization := sample.Authorization() require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) + authorizationsList.SetAuthorizations(newAuthorization) require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) }) } func TestAuthorizationList_Validate(t *testing.T) { - t.Run("Validate successfully", func(t *testing.T) { + t.Run("validate successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() require.NoError(t, authorizationsList.Validate()) }) - t.Run("Validate failed with duplicate msg url with different policies", func(t *testing.T) { + t.Run("validate failed with duplicate msg url with different policies", func(t *testing.T) { authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ { MsgUrl: "ABC", @@ -89,7 +93,7 @@ func TestAuthorizationList_Validate(t *testing.T) { require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) }) - t.Run("Validate failed with duplicate msg url with same policies", func(t *testing.T) { + t.Run("validate failed with duplicate msg url with same policies", func(t *testing.T) { authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ { MsgUrl: "ABC", @@ -103,11 +107,10 @@ func TestAuthorizationList_Validate(t *testing.T) { require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) }) - } func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { - t.Run("Remove authorization successfully", func(t *testing.T) { + t.Run("remove authorization successfully", func(t *testing.T) { authorizationsList := types.DefaultAuthorizationsList() newAuthorization := sample.Authorization() authorizationsList.SetAuthorizations(newAuthorization) @@ -125,7 +128,7 @@ func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { } func TestDefaultAuthorizationsList(t *testing.T) { - t.Run("Default authorizations list", func(t *testing.T) { + t.Run("default authorizations list", func(t *testing.T) { var OperationalPolicyMessageList = []string{ sdk.MsgTypeURL(&crosschaintypes.MsgRefundAbortedCCTX{}), sdk.MsgTypeURL(&crosschaintypes.MsgAbortStuckCCTX{}), From 49eb749d3bfd96bb02d86266f088aa3a8df29a28 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 13:58:23 -0400 Subject: [PATCH 08/15] add changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 584d3c2409..2fa61b953f 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,7 @@ * [2258](https://github.com/zeta-chain/node/pull/2258) - add Optimism and Base in static chain information * [2279](https://github.com/zeta-chain/node/pull/2279) - add a CCTXGateway field to chain static data * [2275](https://github.com/zeta-chain/node/pull/2275) - add ChainInfo singleton state variable in authority +* [2289](https://github.com/zeta-chain/node/pull/2289) - add an authorization list to keep track of all authorizations on the chain ### Refactor From e81d37068a35f8a0f3819c0dc6f2a6ab9c0fc965 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 29 May 2024 14:24:01 -0400 Subject: [PATCH 09/15] generate files --- x/authority/genesis_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/authority/genesis_test.go b/x/authority/genesis_test.go index 5a6ae2abf7..e40344eebf 100644 --- a/x/authority/genesis_test.go +++ b/x/authority/genesis_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/require" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/testutil/nullify" "github.com/zeta-chain/zetacore/testutil/sample" From 1fd660e5fbf39783b37daa8c0d4e2a4c356a0aa7 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 30 May 2024 11:08:51 -0400 Subject: [PATCH 10/15] add panic for init genesis and test case for empty string --- x/authority/genesis.go | 4 ++- x/authority/genesis_test.go | 34 ++++--------------- x/authority/keeper/authorization_list_test.go | 2 +- x/authority/types/authorizations.go | 2 +- x/authority/types/authorizations_test.go | 9 +++-- x/authority/types/errors.go | 2 +- 6 files changed, 20 insertions(+), 33 deletions(-) diff --git a/x/authority/genesis.go b/x/authority/genesis.go index a1bd59abb0..8c943967e5 100644 --- a/x/authority/genesis.go +++ b/x/authority/genesis.go @@ -1,6 +1,8 @@ package authority import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/zeta-chain/zetacore/x/authority/keeper" @@ -13,7 +15,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) k.SetChainInfo(ctx, genState.ChainInfo) err := k.SetAuthorizationList(ctx, genState.AuthorizationList) if err != nil { - ctx.Logger().Error("Failed to set authorization list in InitGenesis", "error", err) + panic(fmt.Sprintf("Failed to set authorization list in InitGenesis: %v", err)) } } diff --git a/x/authority/genesis_test.go b/x/authority/genesis_test.go index e40344eebf..a5ee6ac1b1 100644 --- a/x/authority/genesis_test.go +++ b/x/authority/genesis_test.go @@ -49,16 +49,17 @@ func TestGenesis(t *testing.T) { require.Equal(t, genesisState, *got) }) - t.Run("set genesis and export works but invalid authorization table is not set", func(t *testing.T) { + t.Run("set genesis panics when trying to set invalid values", func(t *testing.T) { + duplicateUrl := "ABC" genesisState := types.GenesisState{ Policies: sample.Policies(), AuthorizationList: types.AuthorizationList{Authorizations: []types.Authorization{ { - MsgUrl: "ABC", + MsgUrl: duplicateUrl, AuthorizedPolicy: types.PolicyType_groupOperational, }, { - MsgUrl: "ABC", + MsgUrl: duplicateUrl, AuthorizedPolicy: types.PolicyType_groupEmergency, }, }}, @@ -67,30 +68,9 @@ func TestGenesis(t *testing.T) { // Init k, ctx := keepertest.AuthorityKeeper(t) - authority.InitGenesis(ctx, *k, genesisState) - - // Check policy is set - policies, found := k.GetPolicies(ctx) - require.True(t, found) - require.Equal(t, genesisState.Policies, policies) - - // Check chain info is set - chainInfo, found := k.GetChainInfo(ctx) - require.True(t, found) - require.Equal(t, genesisState.ChainInfo, chainInfo) - - // Check authorization list is not set - _, found = k.GetAuthorizationList(ctx) - require.False(t, found) - - // Export - got := authority.ExportGenesis(ctx, *k) - require.NotNil(t, got) - - // Compare genesis after init and export - nullify.Fill(&genesisState) - nullify.Fill(got) - require.Equal(t, genesisState, *got) + require.Panics(t, func() { + authority.InitGenesis(ctx, *k, genesisState) + }) }) } diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index e445dfc1a8..287ccc873f 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -59,6 +59,6 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { }, }} - require.ErrorIs(t, k.SetAuthorizationList(ctx, authorizationsList), types.ErrInValidAuthorizationList) + require.ErrorIs(t, k.SetAuthorizationList(ctx, authorizationsList), types.ErrInvalidAuthorizationList) }) } diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 1fbb2533f5..ff6120f9dd 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -186,7 +186,7 @@ func (a *AuthorizationList) Validate() error { for _, authorization := range a.Authorizations { if checkMsgUrls[authorization.MsgUrl] { return errors.Wrap( - ErrInValidAuthorizationList, + ErrInvalidAuthorizationList, fmt.Sprintf("duplicate message url: %s", authorization.MsgUrl), ) } diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 08e80a07c6..237dfafbfa 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -71,6 +71,11 @@ func TestAuthorizationList_CheckAuthorizationExists(t *testing.T) { authorizationsList.SetAuthorizations(newAuthorization) require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) }) + + t.Run("check authorization exists for empty string", func(t *testing.T) { + authorizationsList := types.DefaultAuthorizationsList() + require.False(t, authorizationsList.CheckAuthorizationExists(types.Authorization{})) + }) } func TestAuthorizationList_Validate(t *testing.T) { @@ -90,7 +95,7 @@ func TestAuthorizationList_Validate(t *testing.T) { }, }} - require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) + require.ErrorIs(t, authorizationsList.Validate(), types.ErrInvalidAuthorizationList) }) t.Run("validate failed with duplicate msg url with same policies", func(t *testing.T) { @@ -105,7 +110,7 @@ func TestAuthorizationList_Validate(t *testing.T) { }, }} - require.ErrorIs(t, authorizationsList.Validate(), types.ErrInValidAuthorizationList) + require.ErrorIs(t, authorizationsList.Validate(), types.ErrInvalidAuthorizationList) }) } diff --git a/x/authority/types/errors.go b/x/authority/types/errors.go index e23355e729..88b44b9887 100644 --- a/x/authority/types/errors.go +++ b/x/authority/types/errors.go @@ -4,6 +4,6 @@ import errorsmod "cosmossdk.io/errors" var ( ErrUnauthorized = errorsmod.Register(ModuleName, 1102, "sender not authorized") - ErrInValidAuthorizationList = errorsmod.Register(ModuleName, 1103, "invalid authorization list") + ErrInvalidAuthorizationList = errorsmod.Register(ModuleName, 1103, "invalid authorization list") ErrAuthorizationNotFound = errorsmod.Register(ModuleName, 1104, "authorization not found") ) From 4b198d44901073a5d21399510efc92943d7c01e6 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 30 May 2024 15:14:51 -0400 Subject: [PATCH 11/15] refactor to using test tables --- x/authority/genesis.go | 7 +- x/authority/genesis_test.go | 25 -- x/authority/keeper/authorization_list.go | 7 +- x/authority/keeper/authorization_list_test.go | 27 +- x/authority/types/authorizations.go | 10 - x/authority/types/authorizations_test.go | 321 ++++++++++++------ 6 files changed, 221 insertions(+), 176 deletions(-) diff --git a/x/authority/genesis.go b/x/authority/genesis.go index 8c943967e5..06dbaa7e57 100644 --- a/x/authority/genesis.go +++ b/x/authority/genesis.go @@ -1,8 +1,6 @@ package authority import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/zeta-chain/zetacore/x/authority/keeper" @@ -13,10 +11,7 @@ import ( func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { k.SetPolicies(ctx, genState.Policies) k.SetChainInfo(ctx, genState.ChainInfo) - err := k.SetAuthorizationList(ctx, genState.AuthorizationList) - if err != nil { - panic(fmt.Sprintf("Failed to set authorization list in InitGenesis: %v", err)) - } + k.SetAuthorizationList(ctx, genState.AuthorizationList) } // ExportGenesis returns the authority module's exported genesis. diff --git a/x/authority/genesis_test.go b/x/authority/genesis_test.go index a5ee6ac1b1..59176ae91e 100644 --- a/x/authority/genesis_test.go +++ b/x/authority/genesis_test.go @@ -48,29 +48,4 @@ func TestGenesis(t *testing.T) { nullify.Fill(got) require.Equal(t, genesisState, *got) }) - - t.Run("set genesis panics when trying to set invalid values", func(t *testing.T) { - duplicateUrl := "ABC" - genesisState := types.GenesisState{ - Policies: sample.Policies(), - AuthorizationList: types.AuthorizationList{Authorizations: []types.Authorization{ - { - MsgUrl: duplicateUrl, - AuthorizedPolicy: types.PolicyType_groupOperational, - }, - { - MsgUrl: duplicateUrl, - AuthorizedPolicy: types.PolicyType_groupEmergency, - }, - }}, - ChainInfo: sample.ChainInfo(42), - } - - // Init - k, ctx := keepertest.AuthorityKeeper(t) - require.Panics(t, func() { - authority.InitGenesis(ctx, *k, genesisState) - }) - - }) } diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go index 3573549370..4d85f8dc21 100644 --- a/x/authority/keeper/authorization_list.go +++ b/x/authority/keeper/authorization_list.go @@ -7,15 +7,10 @@ import ( ) // SetAuthorizationList sets the authorization list to the store.It returns an error if the list is invalid. -func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) error { - err := list.Validate() - if err != nil { - return err - } +func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) { store := ctx.KVStore(k.storeKey) b := k.cdc.MustMarshal(&list) store.Set([]byte{0}, b) - return nil } // GetAuthorizationList returns the authorization list from the store diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index 287ccc873f..8a78e8c9c8 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -4,17 +4,17 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/x/authority/types" keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/testutil/sample" - "github.com/zeta-chain/zetacore/x/authority/types" ) func TestKeeper_GetAuthorizationList(t *testing.T) { t.Run("successfully get authorizations list", func(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) authorizationList := sample.AuthorizationList("sample") - require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) + k.SetAuthorizationList(ctx, authorizationList) list, found := k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, authorizationList, list) @@ -22,8 +22,9 @@ func TestKeeper_GetAuthorizationList(t *testing.T) { t.Run("get authorizations list not found", func(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) - _, found := k.GetAuthorizationList(ctx) + list, found := k.GetAuthorizationList(ctx) require.False(t, found) + require.Equal(t, types.AuthorizationList{}, list) }) } @@ -31,7 +32,7 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { t.Run("successfully set authorizations list when a list already exists", func(t *testing.T) { k, ctx := keepertest.AuthorityKeeper(t) authorizationList := sample.AuthorizationList("sample") - require.NoError(t, k.SetAuthorizationList(ctx, authorizationList)) + k.SetAuthorizationList(ctx, authorizationList) list, found := k.GetAuthorizationList(ctx) require.True(t, found) @@ -39,26 +40,10 @@ func TestKeeper_SetAuthorizationList(t *testing.T) { newAuthorizationList := sample.AuthorizationList("sample2") require.NotEqual(t, authorizationList, newAuthorizationList) - require.NoError(t, k.SetAuthorizationList(ctx, newAuthorizationList)) + k.SetAuthorizationList(ctx, newAuthorizationList) list, found = k.GetAuthorizationList(ctx) require.True(t, found) require.Equal(t, newAuthorizationList, list) }) - - t.Run("unable to set invalid authorizations list", func(t *testing.T) { - k, ctx := keepertest.AuthorityKeeper(t) - authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ - { - MsgUrl: "ABC", - AuthorizedPolicy: types.PolicyType_groupOperational, - }, - { - MsgUrl: "ABC", - AuthorizedPolicy: types.PolicyType_groupEmergency, - }, - }} - - require.ErrorIs(t, k.SetAuthorizationList(ctx, authorizationsList), types.ErrInvalidAuthorizationList) - }) } diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index ff6120f9dd..4878992cfb 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -157,16 +157,6 @@ func (a *AuthorizationList) RemoveAuthorizations(authorization Authorization) { } } -// CheckAuthorizationExists checks if the authorization exists in the list. -func (a *AuthorizationList) CheckAuthorizationExists(authorization Authorization) bool { - for _, auth := range a.Authorizations { - if auth.MsgUrl == authorization.MsgUrl { - return true - } - } - return false -} - // GetAuthorizedPolicy returns the policy for the given message url.If the message url is not found, // it returns an error and the first value of the enum. func (a *AuthorizationList) GetAuthorizedPolicy(msgURL string) (PolicyType, error) { diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 237dfafbfa..ba2d0aacb4 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -1,12 +1,13 @@ package types_test import ( + "fmt" "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/pkg/errors" "github.com/stretchr/testify/require" - "github.com/zeta-chain/zetacore/testutil/sample" "github.com/zeta-chain/zetacore/x/authority/types" crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" @@ -15,121 +16,222 @@ import ( ) func TestAuthorizationList_SetAuthorizations(t *testing.T) { - t.Run("set new authorization successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - newAuthorization := sample.Authorization() - require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - - authorizationsList.SetAuthorizations(newAuthorization) - require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) - require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - }) - - t.Run("update existing authorization successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - newAuthorization := sample.Authorization() - require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - - authorizationsList.SetAuthorizations(newAuthorization) - require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)+1) - require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - - newAuthorization.AuthorizedPolicy = types.PolicyType_groupEmergency - authorizationsList.SetAuthorizations(newAuthorization) - require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - policy, err := authorizationsList.GetAuthorizedPolicy(newAuthorization.MsgUrl) - require.NoError(t, err) - require.Equal(t, newAuthorization.AuthorizedPolicy, policy) - }) -} - -func TestAuthorizationList_GetAuthorizedPolicy(t *testing.T) { - t.Run("get authorized policy successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - newAuthorization := sample.Authorization() - authorizationsList.SetAuthorizations(newAuthorization) - - policy, err := authorizationsList.GetAuthorizedPolicy(newAuthorization.MsgUrl) - require.NoError(t, err) - require.Equal(t, newAuthorization.AuthorizedPolicy, policy) - }) - - t.Run("get authorized policy fails when msg not found in list", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - policy, err := authorizationsList.GetAuthorizedPolicy("ABC") - require.ErrorIs(t, err, types.ErrAuthorizationNotFound) - require.Equal(t, types.PolicyType(0), policy) - }) -} - -func TestAuthorizationList_CheckAuthorizationExists(t *testing.T) { - t.Run("check authorization exists successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - newAuthorization := sample.Authorization() - require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - - authorizationsList.SetAuthorizations(newAuthorization) - require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - }) - - t.Run("check authorization exists for empty string", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - require.False(t, authorizationsList.CheckAuthorizationExists(types.Authorization{})) - }) -} - -func TestAuthorizationList_Validate(t *testing.T) { - t.Run("validate successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - require.NoError(t, authorizationsList.Validate()) - }) - t.Run("validate failed with duplicate msg url with different policies", func(t *testing.T) { - authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ - { - MsgUrl: "ABC", + tt := []struct { + name string + oldList types.AuthorizationList + addAuthorization types.Authorization + expectedList types.AuthorizationList + }{ + { + name: "set new authorization successfully", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + addAuthorization: types.Authorization{ + MsgUrl: "XYZ", AuthorizedPolicy: types.PolicyType_groupOperational, }, - { + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, + { + name: "update existing authorization successfully", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + addAuthorization: types.Authorization{ MsgUrl: "ABC", AuthorizedPolicy: types.PolicyType_groupEmergency, }, - }} + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }}, + }, + } - require.ErrorIs(t, authorizationsList.Validate(), types.ErrInvalidAuthorizationList) - }) + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + tc.oldList.SetAuthorizations(tc.addAuthorization) + require.Equal(t, tc.expectedList, tc.oldList) + }) + } +} - t.Run("validate failed with duplicate msg url with same policies", func(t *testing.T) { - authorizationsList := types.AuthorizationList{Authorizations: []types.Authorization{ - { - MsgUrl: "ABC", - AuthorizedPolicy: types.PolicyType_groupOperational, - }, - { - MsgUrl: "ABC", - AuthorizedPolicy: types.PolicyType_groupOperational, - }, - }} +func TestAuthorizationList_GetAuthorizations(t *testing.T) { + tt := []struct { + name string + authorizations types.AuthorizationList + getPolicyMsgUrl string + expectedPolicy types.PolicyType + error error + }{ + { + name: "get authorizations successfully", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + getPolicyMsgUrl: "ABC", + expectedPolicy: types.PolicyType_groupOperational, + error: nil, + }, + { + name: "get authorizations fails when msg not found in list", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{}}, + getPolicyMsgUrl: "ABC", + expectedPolicy: types.PolicyType(0), + error: types.ErrAuthorizationNotFound, + }, + { + name: "get authorizations fails when when queried for empty string", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{}}, + getPolicyMsgUrl: "", + expectedPolicy: types.PolicyType(0), + error: types.ErrAuthorizationNotFound, + }, + } + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + policy, err := tc.authorizations.GetAuthorizedPolicy(tc.getPolicyMsgUrl) + require.ErrorIs(t, err, tc.error) + require.Equal(t, tc.expectedPolicy, policy) + }) + } +} - require.ErrorIs(t, authorizationsList.Validate(), types.ErrInvalidAuthorizationList) - }) +func TestAuthorizationList_Validate(t *testing.T) { + tt := []struct { + name string + authorizations types.AuthorizationList + expectedError error + }{ + { + name: "validate successfully", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + expectedError: nil, + }, + { + name: "validate failed with duplicate msg url with different policies", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }}, + expectedError: errors.Wrap( + types.ErrInvalidAuthorizationList, + fmt.Sprintf("duplicate message url: %s", "ABC")), + }, + { + name: "validate failed with duplicate msg url with same policies", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + expectedError: errors.Wrap( + types.ErrInvalidAuthorizationList, + fmt.Sprintf("duplicate message url: %s", "ABC")), + }} + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + err := tc.authorizations.Validate() + require.ErrorIs(t, err, tc.expectedError) + }) + } } func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { - t.Run("remove authorization successfully", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - newAuthorization := sample.Authorization() - authorizationsList.SetAuthorizations(newAuthorization) - require.True(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - authorizationsList.RemoveAuthorizations(newAuthorization) - require.False(t, authorizationsList.CheckAuthorizationExists(newAuthorization)) - require.Len(t, authorizationsList.Authorizations, len(types.DefaultAuthorizationsList().Authorizations)) - }) - - t.Run("do not remove anything if authorization not found", func(t *testing.T) { - authorizationsList := types.DefaultAuthorizationsList() - authorizationsList.RemoveAuthorizations(sample.Authorization()) - require.ElementsMatch(t, authorizationsList.Authorizations, types.DefaultAuthorizationsList().Authorizations) - }) + tt := []struct { + name string + oldList types.AuthorizationList + removeAuthorization types.Authorization + expectedList types.AuthorizationList + }{ + { + name: "remove authorization successfully", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + removeAuthorization: types.Authorization{ + MsgUrl: "ABC", + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, + { + name: "do not remove anything if authorization not found", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + removeAuthorization: types.Authorization{ + MsgUrl: "XYZ", + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, + } + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + tc.oldList.RemoveAuthorizations(tc.removeAuthorization) + require.Equal(t, tc.expectedList, tc.oldList) + }) + } } func TestDefaultAuthorizationsList(t *testing.T) { @@ -175,19 +277,22 @@ func TestDefaultAuthorizationsList(t *testing.T) { } defaultList := types.DefaultAuthorizationsList() for _, msgUrl := range OperationalPolicyMessageList { - require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + _, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) policy, err := defaultList.GetAuthorizedPolicy(msgUrl) require.NoError(t, err) require.Equal(t, types.PolicyType_groupOperational, policy) } for _, msgUrl := range EmergencyPolicyMessageList { - require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + _, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) policy, err := defaultList.GetAuthorizedPolicy(msgUrl) require.NoError(t, err) require.Equal(t, types.PolicyType_groupEmergency, policy) } for _, msgUrl := range AdminPolicyMessageList { - require.True(t, defaultList.CheckAuthorizationExists(types.Authorization{MsgUrl: msgUrl})) + _, err := defaultList.GetAuthorizedPolicy(msgUrl) + require.NoError(t, err) policy, err := defaultList.GetAuthorizedPolicy(msgUrl) require.NoError(t, err) require.Equal(t, types.PolicyType_groupAdmin, policy) From 4caecc2df209a7bb6512dbe12f1bf77c2594208c Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 30 May 2024 15:20:46 -0400 Subject: [PATCH 12/15] generate files --- x/authority/keeper/authorization_list_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/authority/keeper/authorization_list_test.go b/x/authority/keeper/authorization_list_test.go index 8a78e8c9c8..008be61f25 100644 --- a/x/authority/keeper/authorization_list_test.go +++ b/x/authority/keeper/authorization_list_test.go @@ -4,10 +4,10 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/zeta-chain/zetacore/x/authority/types" keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/testutil/sample" + "github.com/zeta-chain/zetacore/x/authority/types" ) func TestKeeper_GetAuthorizationList(t *testing.T) { From c18bd7d3776754598a3ebc3a527995e71faf7f15 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 30 May 2024 21:42:40 -0400 Subject: [PATCH 13/15] resolve comments --- .../zetacore/authority/authorization.proto | 2 + x/authority/keeper/authorization_list.go | 5 +- x/authority/types/authorizations.go | 176 ++++++------------ x/authority/types/authorizations_test.go | 133 +++++++++++++ x/authority/types/keys.go | 3 +- 5 files changed, 198 insertions(+), 121 deletions(-) diff --git a/proto/zetachain/zetacore/authority/authorization.proto b/proto/zetachain/zetacore/authority/authorization.proto index 4f5a4330ef..3a319c3837 100644 --- a/proto/zetachain/zetacore/authority/authorization.proto +++ b/proto/zetachain/zetacore/authority/authorization.proto @@ -6,12 +6,14 @@ import "zetachain/zetacore/authority/policies.proto"; option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; +// Authorization defines the authorization required to access use a message which needs special permissions message Authorization { // The URL of the message that needs to be authorized string msg_url = 1; // The policy that is authorized to access the message PolicyType authorized_policy = 2; } + // AuthorizationList holds the list of authorizations on zetachain message AuthorizationList { repeated Authorization authorizations = 1 [ (gogoproto.nullable) = false ]; diff --git a/x/authority/keeper/authorization_list.go b/x/authority/keeper/authorization_list.go index 4d85f8dc21..0e7fba9163 100644 --- a/x/authority/keeper/authorization_list.go +++ b/x/authority/keeper/authorization_list.go @@ -1,6 +1,7 @@ package keeper import ( + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/zeta-chain/zetacore/x/authority/types" @@ -8,14 +9,14 @@ import ( // SetAuthorizationList sets the authorization list to the store.It returns an error if the list is invalid. func (k Keeper) SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) { - store := ctx.KVStore(k.storeKey) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuthorizationListKey)) b := k.cdc.MustMarshal(&list) store.Set([]byte{0}, b) } // GetAuthorizationList returns the authorization list from the store func (k Keeper) GetAuthorizationList(ctx sdk.Context) (val types.AuthorizationList, found bool) { - store := ctx.KVStore(k.storeKey) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuthorizationListKey)) b := store.Get([]byte{0}) if b == nil { return val, false diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 4878992cfb..4d590d9d4d 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -6,130 +6,69 @@ import ( "cosmossdk.io/errors" ) +var ( + OperationPolicyMessages = []string{ + "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", + "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", + "/zetachain.zetacore.crosschain.MsgUpdateRateLimiterFlags", + "/zetachain.zetacore.crosschain.MsgWhitelistERC20", + "/zetachain.zetacore.fungible.MsgDeployFungibleCoinZRC20", + "/zetachain.zetacore.fungible.MsgDeploySystemContracts", + "/zetachain.zetacore.fungible.MsgRemoveForeignCoin", + "/zetachain.zetacore.fungible.MsgUpdateZRC20LiquidityCap", + "/zetachain.zetacore.fungible.MsgUpdateZRC20WithdrawFee", + "/zetachain.zetacore.fungible.MsgUnpauseZRC20", + "/zetachain.zetacore.observer.MsgAddObserver", + "/zetachain.zetacore.observer.MsgRemoveChainParams", + "/zetachain.zetacore.observer.MsgResetChainNonces", + "/zetachain.zetacore.observer.MsgUpdateChainParams", + "/zetachain.zetacore.observer.MsgEnableCCTX", + "/zetachain.zetacore.observer.MsgUpdateGasPriceIncreaseFlags", + "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", + } + AdminPolicyMessages = []string{ + "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", + "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", + "/zetachain.zetacore.fungible.MsgUpdateContractBytecode", + "/zetachain.zetacore.fungible.MsgUpdateSystemContract", + "/zetachain.zetacore.observer.MsgUpdateObserver", + } + EmergencyPolicyMessages = []string{ + "/zetachain.zetacore.crosschain.MsgAddInboundTracker", + "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", + "/zetachain.zetacore.crosschain.MsgRemoveOutboundTracker", + "/zetachain.zetacore.fungible.MsgPauseZRC20", + "/zetachain.zetacore.observer.MsgUpdateKeygen", + "/zetachain.zetacore.observer.MsgDisableCCTX", + "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", + } +) + // DefaultAuthorizationsList list is the list of authorizations that presently exist in the system. // This is the minimum set of authorizations that are required to be set when the authorization table is deployed func DefaultAuthorizationsList() AuthorizationList { - var authorizations []Authorization - - authorizations = []Authorization{ - // OperationalPolicyMessageList - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", - AuthorizedPolicy: PolicyType_groupOperational}, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateRateLimiterFlags", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgWhitelistERC20", - AuthorizedPolicy: PolicyType_groupOperational}, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgDeployFungibleCoinZRC20", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgDeploySystemContracts", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgRemoveForeignCoin", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20LiquidityCap", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateZRC20WithdrawFee", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgUnpauseZRC20", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgAddObserver", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgRemoveChainParams", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgResetChainNonces", - AuthorizedPolicy: PolicyType_groupOperational, - }, - - { - MsgUrl: "/zetachain.zetacore.observer.MsgUpdateChainParams", + authorizations := make([]Authorization, len(OperationPolicyMessages)+len(AdminPolicyMessages)+len(EmergencyPolicyMessages)) + index := 0 + for _, msgURL := range OperationPolicyMessages { + authorizations[index] = Authorization{ + MsgUrl: msgURL, AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgEnableCCTX", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgUpdateGasPriceIncreaseFlags", - AuthorizedPolicy: PolicyType_groupOperational, - }, - { - MsgUrl: "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", - AuthorizedPolicy: PolicyType_groupOperational, - }, - // AdminPolicyMessageList - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", - AuthorizedPolicy: PolicyType_groupAdmin, - }, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", - AuthorizedPolicy: PolicyType_groupAdmin, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateContractBytecode", - AuthorizedPolicy: PolicyType_groupAdmin, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgUpdateSystemContract", - AuthorizedPolicy: PolicyType_groupAdmin, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgUpdateObserver", + } + index++ + } + for _, msgURL := range AdminPolicyMessages { + authorizations[index] = Authorization{ + MsgUrl: msgURL, AuthorizedPolicy: PolicyType_groupAdmin, - }, - // EmergencyPolicyMessageList - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgAddInboundTracker", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.crosschain.MsgRemoveOutboundTracker", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.fungible.MsgPauseZRC20", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgUpdateKeygen", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.observer.MsgDisableCCTX", - AuthorizedPolicy: PolicyType_groupEmergency, - }, - { - MsgUrl: "/zetachain.zetacore.lightclient.MsgDisableHeaderVerification", + } + index++ + } + for _, msgURL := range EmergencyPolicyMessages { + authorizations[index] = Authorization{ + MsgUrl: msgURL, AuthorizedPolicy: PolicyType_groupEmergency, - }, + } + index++ } return AuthorizationList{ @@ -165,6 +104,7 @@ func (a *AuthorizationList) GetAuthorizedPolicy(msgURL string) (PolicyType, erro return auth.AuthorizedPolicy, nil } } + fmt.Println("Authorization not found", msgURL) // Returning first value of enum, can consider adding a default value of `EmptyPolicy` in the enum. return PolicyType(0), ErrAuthorizationNotFound } diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index ba2d0aacb4..2ea44c788c 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -45,6 +45,20 @@ func TestAuthorizationList_SetAuthorizations(t *testing.T) { }, }}, }, + { + name: "set new authorization successfully with empty list", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{}}, + addAuthorization: types.Authorization{ + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, { name: "update existing authorization successfully", oldList: types.AuthorizationList{Authorizations: []types.Authorization{ @@ -64,6 +78,41 @@ func TestAuthorizationList_SetAuthorizations(t *testing.T) { }, }}, }, + { + name: "update existing authorization successfully in the middle of the list", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "DEF", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + addAuthorization: types.Authorization{ + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + { + MsgUrl: "DEF", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, } for _, tc := range tt { @@ -94,6 +143,42 @@ func TestAuthorizationList_GetAuthorizations(t *testing.T) { expectedPolicy: types.PolicyType_groupOperational, error: nil, }, + { + name: "get authorizations successfully for admin policy", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupAdmin, + }, + }}, + getPolicyMsgUrl: "ABC", + expectedPolicy: types.PolicyType_groupAdmin, + error: nil, + }, + { + name: "get authorizations successfully for emergency policy", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupEmergency, + }, + }}, + getPolicyMsgUrl: "ABC", + expectedPolicy: types.PolicyType_groupEmergency, + error: nil, + }, + { + name: "get authorizations fails when msg not found in list", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + getPolicyMsgUrl: "XYZ", + expectedPolicy: types.PolicyType(0), + error: types.ErrAuthorizationNotFound, + }, { name: "get authorizations fails when msg not found in list", authorizations: types.AuthorizationList{Authorizations: []types.Authorization{}}, @@ -138,6 +223,16 @@ func TestAuthorizationList_Validate(t *testing.T) { }}, expectedError: nil, }, + { + name: "validate successfully with empty list", + authorizations: types.AuthorizationList{Authorizations: []types.Authorization{}}, + expectedError: nil, + }, + { + name: "validate successfully for default list", + authorizations: types.DefaultAuthorizationsList(), + expectedError: nil, + }, { name: "validate failed with duplicate msg url with different policies", authorizations: types.AuthorizationList{Authorizations: []types.Authorization{ @@ -207,6 +302,44 @@ func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { }, }}, }, + { + name: "remove authorization successfully in the middle of the list", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "XYZ", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "DEF", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + removeAuthorization: types.Authorization{ + MsgUrl: "XYZ", + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{ + { + MsgUrl: "ABC", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + { + MsgUrl: "DEF", + AuthorizedPolicy: types.PolicyType_groupOperational, + }, + }}, + }, + { + name: "do not remove anything when trying to remove from an empty list", + oldList: types.AuthorizationList{Authorizations: []types.Authorization{}}, + removeAuthorization: types.Authorization{ + MsgUrl: "XYZ", + }, + expectedList: types.AuthorizationList{Authorizations: []types.Authorization{}}, + }, { name: "do not remove anything if authorization not found", oldList: types.AuthorizationList{Authorizations: []types.Authorization{ diff --git a/x/authority/types/keys.go b/x/authority/types/keys.go index 7593feed3a..04aad4730d 100644 --- a/x/authority/types/keys.go +++ b/x/authority/types/keys.go @@ -24,7 +24,8 @@ func KeyPrefix(p string) []byte { const ( // PoliciesKey is the key for the policies store - PoliciesKey = "Policies-value-" + PoliciesKey = "Policies-value-" + AuthorizationListKey = "AuthorizationList-value-" // ChainInfoKey is the key for the chain info store ChainInfoKey = "ChainInfo-value-" From 1365d5329eebcb08b3408c814e453009e74e1e47 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 30 May 2024 22:18:50 -0400 Subject: [PATCH 14/15] geenrate files with new comments --- proto/zetachain/zetacore/authority/authorization.proto | 3 ++- .../zetachain/zetacore/authority/authorization_pb.d.ts | 3 +++ x/authority/types/authorization.pb.go | 2 ++ x/authority/types/authorizations.go | 5 ++++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/proto/zetachain/zetacore/authority/authorization.proto b/proto/zetachain/zetacore/authority/authorization.proto index 3a319c3837..914b6b7528 100644 --- a/proto/zetachain/zetacore/authority/authorization.proto +++ b/proto/zetachain/zetacore/authority/authorization.proto @@ -6,7 +6,8 @@ import "zetachain/zetacore/authority/policies.proto"; option go_package = "github.com/zeta-chain/zetacore/x/authority/types"; -// Authorization defines the authorization required to access use a message which needs special permissions +// Authorization defines the authorization required to access use a message +// which needs special permissions message Authorization { // The URL of the message that needs to be authorized string msg_url = 1; diff --git a/typescript/zetachain/zetacore/authority/authorization_pb.d.ts b/typescript/zetachain/zetacore/authority/authorization_pb.d.ts index cf4819d1aa..96178864ea 100644 --- a/typescript/zetachain/zetacore/authority/authorization_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/authorization_pb.d.ts @@ -8,6 +8,9 @@ import { Message, proto3 } from "@bufbuild/protobuf"; import type { PolicyType } from "./policies_pb.js"; /** + * Authorization defines the authorization required to access use a message + * which needs special permissions + * * @generated from message zetachain.zetacore.authority.Authorization */ export declare class Authorization extends Message { diff --git a/x/authority/types/authorization.pb.go b/x/authority/types/authorization.pb.go index c8cce069e1..d8c5fa9292 100644 --- a/x/authority/types/authorization.pb.go +++ b/x/authority/types/authorization.pb.go @@ -23,6 +23,8 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +// Authorization defines the authorization required to access use a message +// which needs special permissions type Authorization struct { // The URL of the message that needs to be authorized MsgUrl string `protobuf:"bytes,1,opt,name=msg_url,json=msgUrl,proto3" json:"msg_url,omitempty"` diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 4d590d9d4d..4de2c7d17b 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -47,7 +47,10 @@ var ( // DefaultAuthorizationsList list is the list of authorizations that presently exist in the system. // This is the minimum set of authorizations that are required to be set when the authorization table is deployed func DefaultAuthorizationsList() AuthorizationList { - authorizations := make([]Authorization, len(OperationPolicyMessages)+len(AdminPolicyMessages)+len(EmergencyPolicyMessages)) + authorizations := make( + []Authorization, + len(OperationPolicyMessages)+len(AdminPolicyMessages)+len(EmergencyPolicyMessages), + ) index := 0 for _, msgURL := range OperationPolicyMessages { authorizations[index] = Authorization{ From 61867bd4ce2aae3839ea777d8340fa89da2646d0 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Fri, 31 May 2024 08:54:07 -0400 Subject: [PATCH 15/15] fix comments 3 --- x/authority/types/authorizations.go | 16 +++++++++------- x/authority/types/authorizations_test.go | 4 ++-- x/authority/types/keys.go | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/x/authority/types/authorizations.go b/x/authority/types/authorizations.go index 4de2c7d17b..0de98a768e 100644 --- a/x/authority/types/authorizations.go +++ b/x/authority/types/authorizations.go @@ -7,6 +7,7 @@ import ( ) var ( + // OperationPolicyMessages keeps track of the message URLs that can, by default, only be executed by operational policy address OperationPolicyMessages = []string{ "/zetachain.zetacore.crosschain.MsgRefundAbortedCCTX", "/zetachain.zetacore.crosschain.MsgAbortStuckCCTX", @@ -26,6 +27,7 @@ var ( "/zetachain.zetacore.observer.MsgUpdateGasPriceIncreaseFlags", "/zetachain.zetacore.lightclient.MsgEnableHeaderVerification", } + // AdminPolicyMessages keeps track of the message URLs that can, by default, only be executed by admin policy address AdminPolicyMessages = []string{ "/zetachain.zetacore.crosschain.MsgMigrateTssFunds", "/zetachain.zetacore.crosschain.MsgUpdateTssAddress", @@ -33,6 +35,7 @@ var ( "/zetachain.zetacore.fungible.MsgUpdateSystemContract", "/zetachain.zetacore.observer.MsgUpdateObserver", } + // EmergencyPolicyMessages keeps track of the message URLs that can, by default, only be executed by emergency policy address EmergencyPolicyMessages = []string{ "/zetachain.zetacore.crosschain.MsgAddInboundTracker", "/zetachain.zetacore.crosschain.MsgAddOutboundTracker", @@ -79,8 +82,8 @@ func DefaultAuthorizationsList() AuthorizationList { } } -// SetAuthorizations adds the authorization to the list. If the authorization already exists, it updates the policy. -func (a *AuthorizationList) SetAuthorizations(authorization Authorization) { +// SetAuthorization adds the authorization to the list. If the authorization already exists, it updates the policy. +func (a *AuthorizationList) SetAuthorization(authorization Authorization) { for i, auth := range a.Authorizations { if auth.MsgUrl == authorization.MsgUrl { a.Authorizations[i].AuthorizedPolicy = authorization.AuthorizedPolicy @@ -90,8 +93,8 @@ func (a *AuthorizationList) SetAuthorizations(authorization Authorization) { a.Authorizations = append(a.Authorizations, authorization) } -// RemoveAuthorizations removes the authorization from the list. It does not check if the authorization exists or not. -func (a *AuthorizationList) RemoveAuthorizations(authorization Authorization) { +// RemoveAuthorization removes the authorization from the list. It does not check if the authorization exists or not. +func (a *AuthorizationList) RemoveAuthorization(authorization Authorization) { for i, auth := range a.Authorizations { if auth.MsgUrl == authorization.MsgUrl { a.Authorizations = append(a.Authorizations[:i], a.Authorizations[i+1:]...) @@ -99,15 +102,14 @@ func (a *AuthorizationList) RemoveAuthorizations(authorization Authorization) { } } -// GetAuthorizedPolicy returns the policy for the given message url.If the message url is not found, -// it returns an error and the first value of the enum. +// GetAuthorizedPolicy returns the policy for the given message url. If the message url is not found, + func (a *AuthorizationList) GetAuthorizedPolicy(msgURL string) (PolicyType, error) { for _, auth := range a.Authorizations { if auth.MsgUrl == msgURL { return auth.AuthorizedPolicy, nil } } - fmt.Println("Authorization not found", msgURL) // Returning first value of enum, can consider adding a default value of `EmptyPolicy` in the enum. return PolicyType(0), ErrAuthorizationNotFound } diff --git a/x/authority/types/authorizations_test.go b/x/authority/types/authorizations_test.go index 2ea44c788c..8bb7ed805a 100644 --- a/x/authority/types/authorizations_test.go +++ b/x/authority/types/authorizations_test.go @@ -117,7 +117,7 @@ func TestAuthorizationList_SetAuthorizations(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - tc.oldList.SetAuthorizations(tc.addAuthorization) + tc.oldList.SetAuthorization(tc.addAuthorization) require.Equal(t, tc.expectedList, tc.oldList) }) } @@ -361,7 +361,7 @@ func TestAuthorizationList_RemoveAuthorizations(t *testing.T) { } for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - tc.oldList.RemoveAuthorizations(tc.removeAuthorization) + tc.oldList.RemoveAuthorization(tc.removeAuthorization) require.Equal(t, tc.expectedList, tc.oldList) }) } diff --git a/x/authority/types/keys.go b/x/authority/types/keys.go index 04aad4730d..13365abf77 100644 --- a/x/authority/types/keys.go +++ b/x/authority/types/keys.go @@ -24,9 +24,9 @@ func KeyPrefix(p string) []byte { const ( // PoliciesKey is the key for the policies store - PoliciesKey = "Policies-value-" + PoliciesKey = "Policies-value-" + // AuthorizationListKey is the key for the authorization list store AuthorizationListKey = "AuthorizationList-value-" - // ChainInfoKey is the key for the chain info store ChainInfoKey = "ChainInfo-value-" )