feat(accounts): add genesis account initialization (#20642)

Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
testinginprod 2024-06-12 16:19:13 +02:00 committed by GitHub
parent d3d6448eca
commit 40492cdf10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 391 additions and 179 deletions

View File

@ -12,10 +12,61 @@ import (
sync "sync"
)
var _ protoreflect.List = (*_GenesisState_1_list)(nil)
type _GenesisState_1_list struct {
list *[]*GenesisAccount
}
func (x *_GenesisState_1_list) Len() int {
if x.list == nil {
return 0
}
return len(*x.list)
}
func (x *_GenesisState_1_list) Get(i int) protoreflect.Value {
return protoreflect.ValueOfMessage((*x.list)[i].ProtoReflect())
}
func (x *_GenesisState_1_list) Set(i int, value protoreflect.Value) {
valueUnwrapped := value.Message()
concreteValue := valueUnwrapped.Interface().(*GenesisAccount)
(*x.list)[i] = concreteValue
}
func (x *_GenesisState_1_list) Append(value protoreflect.Value) {
valueUnwrapped := value.Message()
concreteValue := valueUnwrapped.Interface().(*GenesisAccount)
*x.list = append(*x.list, concreteValue)
}
func (x *_GenesisState_1_list) AppendMutable() protoreflect.Value {
v := new(GenesisAccount)
*x.list = append(*x.list, v)
return protoreflect.ValueOfMessage(v.ProtoReflect())
}
func (x *_GenesisState_1_list) Truncate(n int) {
for i := n; i < len(*x.list); i++ {
(*x.list)[i] = nil
}
*x.list = (*x.list)[:n]
}
func (x *_GenesisState_1_list) NewElement() protoreflect.Value {
v := new(GenesisAccount)
return protoreflect.ValueOfMessage(v.ProtoReflect())
}
func (x *_GenesisState_1_list) IsValid() bool {
return x.list != nil
}
var _ protoreflect.List = (*_GenesisState_2_list)(nil)
type _GenesisState_2_list struct {
list *[]*GenesisAccount
list *[]*MsgInit
}
func (x *_GenesisState_2_list) Len() int {
@ -31,18 +82,18 @@ func (x *_GenesisState_2_list) Get(i int) protoreflect.Value {
func (x *_GenesisState_2_list) Set(i int, value protoreflect.Value) {
valueUnwrapped := value.Message()
concreteValue := valueUnwrapped.Interface().(*GenesisAccount)
concreteValue := valueUnwrapped.Interface().(*MsgInit)
(*x.list)[i] = concreteValue
}
func (x *_GenesisState_2_list) Append(value protoreflect.Value) {
valueUnwrapped := value.Message()
concreteValue := valueUnwrapped.Interface().(*GenesisAccount)
concreteValue := valueUnwrapped.Interface().(*MsgInit)
*x.list = append(*x.list, concreteValue)
}
func (x *_GenesisState_2_list) AppendMutable() protoreflect.Value {
v := new(GenesisAccount)
v := new(MsgInit)
*x.list = append(*x.list, v)
return protoreflect.ValueOfMessage(v.ProtoReflect())
}
@ -55,7 +106,7 @@ func (x *_GenesisState_2_list) Truncate(n int) {
}
func (x *_GenesisState_2_list) NewElement() protoreflect.Value {
v := new(GenesisAccount)
v := new(MsgInit)
return protoreflect.ValueOfMessage(v.ProtoReflect())
}
@ -64,16 +115,16 @@ func (x *_GenesisState_2_list) IsValid() bool {
}
var (
md_GenesisState protoreflect.MessageDescriptor
fd_GenesisState_account_number protoreflect.FieldDescriptor
fd_GenesisState_accounts protoreflect.FieldDescriptor
md_GenesisState protoreflect.MessageDescriptor
fd_GenesisState_accounts protoreflect.FieldDescriptor
fd_GenesisState_init_account_msgs protoreflect.FieldDescriptor
)
func init() {
file_cosmos_accounts_v1_genesis_proto_init()
md_GenesisState = File_cosmos_accounts_v1_genesis_proto.Messages().ByName("GenesisState")
fd_GenesisState_account_number = md_GenesisState.Fields().ByName("account_number")
fd_GenesisState_accounts = md_GenesisState.Fields().ByName("accounts")
fd_GenesisState_init_account_msgs = md_GenesisState.Fields().ByName("init_account_msgs")
}
var _ protoreflect.Message = (*fastReflection_GenesisState)(nil)
@ -141,15 +192,15 @@ func (x *fastReflection_GenesisState) Interface() protoreflect.ProtoMessage {
// While iterating, mutating operations may only be performed
// on the current field descriptor.
func (x *fastReflection_GenesisState) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
if x.AccountNumber != uint64(0) {
value := protoreflect.ValueOfUint64(x.AccountNumber)
if !f(fd_GenesisState_account_number, value) {
if len(x.Accounts) != 0 {
value := protoreflect.ValueOfList(&_GenesisState_1_list{list: &x.Accounts})
if !f(fd_GenesisState_accounts, value) {
return
}
}
if len(x.Accounts) != 0 {
value := protoreflect.ValueOfList(&_GenesisState_2_list{list: &x.Accounts})
if !f(fd_GenesisState_accounts, value) {
if len(x.InitAccountMsgs) != 0 {
value := protoreflect.ValueOfList(&_GenesisState_2_list{list: &x.InitAccountMsgs})
if !f(fd_GenesisState_init_account_msgs, value) {
return
}
}
@ -168,10 +219,10 @@ func (x *fastReflection_GenesisState) Range(f func(protoreflect.FieldDescriptor,
// a repeated field is populated if it is non-empty.
func (x *fastReflection_GenesisState) Has(fd protoreflect.FieldDescriptor) bool {
switch fd.FullName() {
case "cosmos.accounts.v1.GenesisState.account_number":
return x.AccountNumber != uint64(0)
case "cosmos.accounts.v1.GenesisState.accounts":
return len(x.Accounts) != 0
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
return len(x.InitAccountMsgs) != 0
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.accounts.v1.GenesisState"))
@ -188,10 +239,10 @@ func (x *fastReflection_GenesisState) Has(fd protoreflect.FieldDescriptor) bool
// Clear is a mutating operation and unsafe for concurrent use.
func (x *fastReflection_GenesisState) Clear(fd protoreflect.FieldDescriptor) {
switch fd.FullName() {
case "cosmos.accounts.v1.GenesisState.account_number":
x.AccountNumber = uint64(0)
case "cosmos.accounts.v1.GenesisState.accounts":
x.Accounts = nil
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
x.InitAccountMsgs = nil
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.accounts.v1.GenesisState"))
@ -208,14 +259,17 @@ func (x *fastReflection_GenesisState) Clear(fd protoreflect.FieldDescriptor) {
// of the value; to obtain a mutable reference, use Mutable.
func (x *fastReflection_GenesisState) Get(descriptor protoreflect.FieldDescriptor) protoreflect.Value {
switch descriptor.FullName() {
case "cosmos.accounts.v1.GenesisState.account_number":
value := x.AccountNumber
return protoreflect.ValueOfUint64(value)
case "cosmos.accounts.v1.GenesisState.accounts":
if len(x.Accounts) == 0 {
return protoreflect.ValueOfList(&_GenesisState_1_list{})
}
listValue := &_GenesisState_1_list{list: &x.Accounts}
return protoreflect.ValueOfList(listValue)
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
if len(x.InitAccountMsgs) == 0 {
return protoreflect.ValueOfList(&_GenesisState_2_list{})
}
listValue := &_GenesisState_2_list{list: &x.Accounts}
listValue := &_GenesisState_2_list{list: &x.InitAccountMsgs}
return protoreflect.ValueOfList(listValue)
default:
if descriptor.IsExtension() {
@ -237,12 +291,14 @@ func (x *fastReflection_GenesisState) Get(descriptor protoreflect.FieldDescripto
// Set is a mutating operation and unsafe for concurrent use.
func (x *fastReflection_GenesisState) Set(fd protoreflect.FieldDescriptor, value protoreflect.Value) {
switch fd.FullName() {
case "cosmos.accounts.v1.GenesisState.account_number":
x.AccountNumber = value.Uint()
case "cosmos.accounts.v1.GenesisState.accounts":
lv := value.List()
clv := lv.(*_GenesisState_2_list)
clv := lv.(*_GenesisState_1_list)
x.Accounts = *clv.list
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
lv := value.List()
clv := lv.(*_GenesisState_2_list)
x.InitAccountMsgs = *clv.list
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.accounts.v1.GenesisState"))
@ -267,10 +323,14 @@ func (x *fastReflection_GenesisState) Mutable(fd protoreflect.FieldDescriptor) p
if x.Accounts == nil {
x.Accounts = []*GenesisAccount{}
}
value := &_GenesisState_2_list{list: &x.Accounts}
value := &_GenesisState_1_list{list: &x.Accounts}
return protoreflect.ValueOfList(value)
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
if x.InitAccountMsgs == nil {
x.InitAccountMsgs = []*MsgInit{}
}
value := &_GenesisState_2_list{list: &x.InitAccountMsgs}
return protoreflect.ValueOfList(value)
case "cosmos.accounts.v1.GenesisState.account_number":
panic(fmt.Errorf("field account_number of message cosmos.accounts.v1.GenesisState is not mutable"))
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.accounts.v1.GenesisState"))
@ -284,10 +344,11 @@ func (x *fastReflection_GenesisState) Mutable(fd protoreflect.FieldDescriptor) p
// For lists, maps, and messages, this returns a new, empty, mutable value.
func (x *fastReflection_GenesisState) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value {
switch fd.FullName() {
case "cosmos.accounts.v1.GenesisState.account_number":
return protoreflect.ValueOfUint64(uint64(0))
case "cosmos.accounts.v1.GenesisState.accounts":
list := []*GenesisAccount{}
return protoreflect.ValueOfList(&_GenesisState_1_list{list: &list})
case "cosmos.accounts.v1.GenesisState.init_account_msgs":
list := []*MsgInit{}
return protoreflect.ValueOfList(&_GenesisState_2_list{list: &list})
default:
if fd.IsExtension() {
@ -358,15 +419,18 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods {
var n int
var l int
_ = l
if x.AccountNumber != 0 {
n += 1 + runtime.Sov(uint64(x.AccountNumber))
}
if len(x.Accounts) > 0 {
for _, e := range x.Accounts {
l = options.Size(e)
n += 1 + l + runtime.Sov(uint64(l))
}
}
if len(x.InitAccountMsgs) > 0 {
for _, e := range x.InitAccountMsgs {
l = options.Size(e)
n += 1 + l + runtime.Sov(uint64(l))
}
}
if x.unknownFields != nil {
n += len(x.unknownFields)
}
@ -396,9 +460,9 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods {
i -= len(x.unknownFields)
copy(dAtA[i:], x.unknownFields)
}
if len(x.Accounts) > 0 {
for iNdEx := len(x.Accounts) - 1; iNdEx >= 0; iNdEx-- {
encoded, err := options.Marshal(x.Accounts[iNdEx])
if len(x.InitAccountMsgs) > 0 {
for iNdEx := len(x.InitAccountMsgs) - 1; iNdEx >= 0; iNdEx-- {
encoded, err := options.Marshal(x.InitAccountMsgs[iNdEx])
if err != nil {
return protoiface.MarshalOutput{
NoUnkeyedLiterals: input.NoUnkeyedLiterals,
@ -412,10 +476,21 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods {
dAtA[i] = 0x12
}
}
if x.AccountNumber != 0 {
i = runtime.EncodeVarint(dAtA, i, uint64(x.AccountNumber))
i--
dAtA[i] = 0x8
if len(x.Accounts) > 0 {
for iNdEx := len(x.Accounts) - 1; iNdEx >= 0; iNdEx-- {
encoded, err := options.Marshal(x.Accounts[iNdEx])
if err != nil {
return protoiface.MarshalOutput{
NoUnkeyedLiterals: input.NoUnkeyedLiterals,
Buf: input.Buf,
}, err
}
i -= len(encoded)
copy(dAtA[i:], encoded)
i = runtime.EncodeVarint(dAtA, i, uint64(len(encoded)))
i--
dAtA[i] = 0xa
}
}
if input.Buf != nil {
input.Buf = append(input.Buf, dAtA...)
@ -467,25 +542,6 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods {
}
switch fieldNum {
case 1:
if wireType != 0 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field AccountNumber", wireType)
}
x.AccountNumber = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow
}
if iNdEx >= l {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
x.AccountNumber |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
case 2:
if wireType != 2 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field Accounts", wireType)
}
@ -519,6 +575,40 @@ func (x *fastReflection_GenesisState) ProtoMethods() *protoiface.Methods {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err
}
iNdEx = postIndex
case 2:
if wireType != 2 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field InitAccountMsgs", wireType)
}
var msglen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow
}
if iNdEx >= l {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
msglen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if msglen < 0 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength
}
postIndex := iNdEx + msglen
if postIndex < 0 {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength
}
if postIndex > l {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF
}
x.InitAccountMsgs = append(x.InitAccountMsgs, &MsgInit{})
if err := options.Unmarshal(dAtA[iNdEx:postIndex], x.InitAccountMsgs[len(x.InitAccountMsgs)-1]); err != nil {
return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := runtime.Skip(dAtA[iNdEx:])
@ -1731,10 +1821,10 @@ type GenesisState struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// account_number is the latest account number.
AccountNumber uint64 `protobuf:"varint,1,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty"`
// accounts are the genesis accounts.
Accounts []*GenesisAccount `protobuf:"bytes,2,rep,name=accounts,proto3" json:"accounts,omitempty"`
Accounts []*GenesisAccount `protobuf:"bytes,1,rep,name=accounts,proto3" json:"accounts,omitempty"`
// init_accounts_msgs defines the genesis messages that will be executed to init the account.
InitAccountMsgs []*MsgInit `protobuf:"bytes,2,rep,name=init_account_msgs,json=initAccountMsgs,proto3" json:"init_account_msgs,omitempty"`
}
func (x *GenesisState) Reset() {
@ -1757,13 +1847,6 @@ func (*GenesisState) Descriptor() ([]byte, []int) {
return file_cosmos_accounts_v1_genesis_proto_rawDescGZIP(), []int{0}
}
func (x *GenesisState) GetAccountNumber() uint64 {
if x != nil {
return x.AccountNumber
}
return 0
}
func (x *GenesisState) GetAccounts() []*GenesisAccount {
if x != nil {
return x.Accounts
@ -1771,6 +1854,13 @@ func (x *GenesisState) GetAccounts() []*GenesisAccount {
return nil
}
func (x *GenesisState) GetInitAccountMsgs() []*MsgInit {
if x != nil {
return x.InitAccountMsgs
}
return nil
}
// GenesisAccount defines an account to be initialized in the genesis state.
type GenesisAccount struct {
state protoimpl.MessageState
@ -1887,14 +1977,18 @@ var file_cosmos_accounts_v1_genesis_proto_rawDesc = []byte{
0x0a, 0x20, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74,
0x73, 0x2f, 0x76, 0x31, 0x2f, 0x67, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x12, 0x12, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x63, 0x63, 0x6f, 0x75,
0x6e, 0x74, 0x73, 0x2e, 0x76, 0x31, 0x22, 0x75, 0x0a, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69,
0x73, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x25, 0x0a, 0x0e, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e,
0x74, 0x5f, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0d,
0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x3e, 0x0a,
0x08, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32,
0x22, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74,
0x73, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x41, 0x63, 0x63, 0x6f,
0x75, 0x6e, 0x74, 0x52, 0x08, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x73, 0x22, 0xa6, 0x01,
0x6e, 0x74, 0x73, 0x2e, 0x76, 0x31, 0x1a, 0x1b, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x61,
0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x73, 0x2f, 0x76, 0x31, 0x2f, 0x74, 0x78, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0x97, 0x01, 0x0a, 0x0c, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x53,
0x74, 0x61, 0x74, 0x65, 0x12, 0x3e, 0x0a, 0x08, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x73,
0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e,
0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x73, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x6e, 0x65,
0x73, 0x69, 0x73, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x52, 0x08, 0x61, 0x63, 0x63, 0x6f,
0x75, 0x6e, 0x74, 0x73, 0x12, 0x47, 0x0a, 0x11, 0x69, 0x6e, 0x69, 0x74, 0x5f, 0x61, 0x63, 0x63,
0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x6d, 0x73, 0x67, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32,
0x1b, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74,
0x73, 0x2e, 0x76, 0x31, 0x2e, 0x4d, 0x73, 0x67, 0x49, 0x6e, 0x69, 0x74, 0x52, 0x0f, 0x69, 0x6e,
0x69, 0x74, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x4d, 0x73, 0x67, 0x73, 0x22, 0xa6, 0x01,
0x0a, 0x0e, 0x47, 0x65, 0x6e, 0x65, 0x73, 0x69, 0x73, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74,
0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x61, 0x63,
@ -1941,15 +2035,17 @@ var file_cosmos_accounts_v1_genesis_proto_goTypes = []interface{}{
(*GenesisState)(nil), // 0: cosmos.accounts.v1.GenesisState
(*GenesisAccount)(nil), // 1: cosmos.accounts.v1.GenesisAccount
(*KVPair)(nil), // 2: cosmos.accounts.v1.KVPair
(*MsgInit)(nil), // 3: cosmos.accounts.v1.MsgInit
}
var file_cosmos_accounts_v1_genesis_proto_depIdxs = []int32{
1, // 0: cosmos.accounts.v1.GenesisState.accounts:type_name -> cosmos.accounts.v1.GenesisAccount
2, // 1: cosmos.accounts.v1.GenesisAccount.state:type_name -> cosmos.accounts.v1.KVPair
2, // [2:2] is the sub-list for method output_type
2, // [2:2] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
3, // 1: cosmos.accounts.v1.GenesisState.init_account_msgs:type_name -> cosmos.accounts.v1.MsgInit
2, // 2: cosmos.accounts.v1.GenesisAccount.state:type_name -> cosmos.accounts.v1.KVPair
3, // [3:3] is the sub-list for method output_type
3, // [3:3] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
}
func init() { file_cosmos_accounts_v1_genesis_proto_init() }
@ -1957,6 +2053,7 @@ func file_cosmos_accounts_v1_genesis_proto_init() {
if File_cosmos_accounts_v1_genesis_proto != nil {
return
}
file_cosmos_accounts_v1_tx_proto_init()
if !protoimpl.UnsafeEnabled {
file_cosmos_accounts_v1_genesis_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GenesisState); i {

View File

@ -1,3 +1,48 @@
# x/accounts
The x/accounts module provides module and facilities for writing smart cosmos-sdk accounts.
The x/accounts module provides module and facilities for writing smart cosmos-sdk accounts.
# Genesis
## Creating accounts on genesis
In order to create accounts at genesis, the `x/accounts` module allows developers to provide
a list of genesis `MsgInit` messages that will be executed in the `x/accounts` genesis flow.
The init messages are generated offline. You can also use the following CLI command to generate the
json messages: `simd accounts tx init [account type] [msg] --from me --genesis`. This will generate
a jsonified init message wrapped in an x/accounts `MsgInit`.
This follows the same initialization flow and rules that would happen if the chain is running.
The only concrete difference is that this is happening at the genesis block.
For example, given the following `genesis.json` file:
```json
{
"app_state": {
"accounts": {
"init_account_msgs": [
{
"sender": "account_creator_address",
"account_type": "lockup",
"message": {
"@type": "cosmos.accounts.defaults.lockup.MsgInitLockupAccount",
"owner": "some_owner",
"end_time": "..",
"start_time": ".."
},
"funds": [
{
"denom": "stake",
"amount": "1000"
}
]
}
]
}
}
}
```
The accounts module will run the lockup account initialization message.

View File

@ -75,9 +75,20 @@ func GetTxInitCmd() *cobra.Command {
Message: msgBytes,
}
isGenesis, err := cmd.Flags().GetBool("genesis")
if err != nil {
return err
}
// in case the genesis flag is provided then the init message is printed.
if isGenesis {
return clientCtx.WithOutputFormat(flags.OutputFormatJSON).PrintProto(&msg)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
cmd.Flags().Bool("genesis", false, "if true will print the json init message for genesis")
flags.AddTxFlagsToCmd(cmd)
return cmd
}

View File

@ -11,15 +11,7 @@ import (
func (k Keeper) ExportState(ctx context.Context) (*v1.GenesisState, error) {
genState := &v1.GenesisState{}
// get account number
accountNumber, err := k.AccountNumber.Peek(ctx)
if err != nil {
return nil, err
}
genState.AccountNumber = accountNumber
err = k.AccountsByType.Walk(ctx, nil, func(accAddr []byte, accType string) (stop bool, err error) {
err := k.AccountsByType.Walk(ctx, nil, func(accAddr []byte, accType string) (stop bool, err error) {
accNum, err := k.AccountByNumber.Get(ctx, accAddr)
if err != nil {
return true, err
@ -64,7 +56,7 @@ func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string,
}
func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) error {
var largestNum *uint64
lastAccountNumber := uint64(0)
var err error
// import accounts
for _, acc := range genState.Accounts {
@ -73,19 +65,31 @@ func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) erro
return fmt.Errorf("%w: %s", err, acc.Address)
}
accNum := acc.AccountNumber
if largestNum == nil || *largestNum < accNum {
largestNum = &accNum
// update lastAccountNumber if the current account being processed
// has a bigger account number.
if lastAccountNumber < acc.AccountNumber {
lastAccountNumber = acc.AccountNumber
}
}
if largestNum != nil {
// set the account number to the highest account number to avoid duplicate account number
err = k.AccountNumber.Set(ctx, *largestNum)
// we set the latest account number only if there were any genesis accounts, otherwise
// we leave it unset.
if genState.Accounts != nil {
// due to sequence semantics, we store the next account number.
err = k.AccountNumber.Set(ctx, lastAccountNumber+1)
if err != nil {
return err
}
}
return err
// after this execute account creation msgs.
for index, msgInit := range genState.InitAccountMsgs {
_, _, err = k.initFromMsg(ctx, msgInit)
if err != nil {
return fmt.Errorf("invalid genesis account msg init at index %d, msg %s: %w", index, msgInit, err)
}
}
return nil
}
func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error {

View File

@ -39,6 +39,15 @@ func TestGenesis(t *testing.T) {
acc, err := NewTestAccount(deps)
return testAccountType, acc, err
})
// add to state a genesis account init msg.
initMsg, err := implementation.PackAny(&types.Empty{})
require.NoError(t, err)
state.InitAccountMsgs = append(state.InitAccountMsgs, &v1.MsgInit{
Sender: "sender-2",
AccountType: testAccountType,
Message: initMsg,
Funds: nil,
})
err = k.ImportState(ctx, state)
require.NoError(t, err)
@ -52,6 +61,12 @@ func TestGenesis(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &types.UInt64Value{Value: 20}, resp)
// check initted on genesis account
addr3, err := k.makeAddress(2)
require.NoError(t, err)
resp, err = k.Query(ctx, addr3, &types.DoubleValue{})
require.NoError(t, err)
require.Equal(t, &types.UInt64Value{Value: 0}, resp)
// reset state
k, ctx = newKeeper(t, func(deps implementation.Dependencies) (string, implementation.Account, error) {
acc, err := NewTestAccount(deps)
@ -66,8 +81,9 @@ func TestGenesis(t *testing.T) {
currentAccNum, err := k.AccountNumber.Peek(ctx)
require.NoError(t, err)
// AccountNumber should be set to the highest account number in the genesis state
require.Equal(t, uint64(99), currentAccNum)
// AccountNumber should be set to the highest account number in the genesis state + 2
// (one is the sequence offset, the other is the genesis account being added through init msg)
require.Equal(t, state.Accounts[0].AccountNumber+2, currentAccNum)
}
func TestImportAccountError(t *testing.T) {

View File

@ -15,6 +15,7 @@ import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/internal/implementation"
v1 "cosmossdk.io/x/accounts/v1"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -156,6 +157,23 @@ func (k Keeper) Init(
return initResp, accountAddr, nil
}
// initFromMsg is a helper which inits an account given a v1.MsgInit.
func (k Keeper) initFromMsg(ctx context.Context, initMsg *v1.MsgInit) (implementation.ProtoMsg, []byte, error) {
creator, err := k.addressCodec.StringToBytes(initMsg.Sender)
if err != nil {
return nil, nil, err
}
// decode message bytes into the concrete boxed message type
msg, err := implementation.UnpackAnyRaw(initMsg.Message)
if err != nil {
return nil, nil, err
}
// run account creation logic
return k.Init(ctx, initMsg.AccountType, creator, msg, initMsg.Funds)
}
// init initializes the account, given the type, the creator the newly created account number, its address and the
// initialization message.
func (k Keeper) init(

View File

@ -2,6 +2,7 @@ package accounts
import (
"context"
"fmt"
"cosmossdk.io/core/event"
"cosmossdk.io/x/accounts/internal/implementation"
@ -19,21 +20,9 @@ type msgServer struct {
}
func (m msgServer) Init(ctx context.Context, request *v1.MsgInit) (*v1.MsgInitResponse, error) {
creator, err := m.k.addressCodec.StringToBytes(request.Sender)
resp, accAddr, err := m.k.initFromMsg(ctx, request)
if err != nil {
return nil, err
}
// decode message bytes into the concrete boxed message type
msg, err := implementation.UnpackAnyRaw(request.Message)
if err != nil {
return nil, err
}
// run account creation logic
resp, accAddr, err := m.k.Init(ctx, request.AccountType, creator, msg, request.Funds)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to initialize account: %w", err)
}
// encode the address

View File

@ -4,12 +4,14 @@ package cosmos.accounts.v1;
option go_package = "cosmossdk.io/x/accounts/v1";
import "cosmos/accounts/v1/tx.proto";
// GenesisState defines the accounts' module's genesis state.
message GenesisState {
// account_number is the latest account number.
uint64 account_number = 1;
// accounts are the genesis accounts.
repeated GenesisAccount accounts = 2;
repeated GenesisAccount accounts = 1;
// init_accounts_msgs defines the genesis messages that will be executed to init the account.
repeated cosmos.accounts.v1.MsgInit init_account_msgs = 2;
}
// GenesisAccount defines an account to be initialized in the genesis state.

View File

@ -24,10 +24,10 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
// GenesisState defines the accounts' module's genesis state.
type GenesisState struct {
// account_number is the latest account number.
AccountNumber uint64 `protobuf:"varint,1,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty"`
// accounts are the genesis accounts.
Accounts []*GenesisAccount `protobuf:"bytes,2,rep,name=accounts,proto3" json:"accounts,omitempty"`
Accounts []*GenesisAccount `protobuf:"bytes,1,rep,name=accounts,proto3" json:"accounts,omitempty"`
// init_accounts_msgs defines the genesis messages that will be executed to init the account.
InitAccountMsgs []*MsgInit `protobuf:"bytes,2,rep,name=init_account_msgs,json=initAccountMsgs,proto3" json:"init_account_msgs,omitempty"`
}
func (m *GenesisState) Reset() { *m = GenesisState{} }
@ -63,13 +63,6 @@ func (m *GenesisState) XXX_DiscardUnknown() {
var xxx_messageInfo_GenesisState proto.InternalMessageInfo
func (m *GenesisState) GetAccountNumber() uint64 {
if m != nil {
return m.AccountNumber
}
return 0
}
func (m *GenesisState) GetAccounts() []*GenesisAccount {
if m != nil {
return m.Accounts
@ -77,6 +70,13 @@ func (m *GenesisState) GetAccounts() []*GenesisAccount {
return nil
}
func (m *GenesisState) GetInitAccountMsgs() []*MsgInit {
if m != nil {
return m.InitAccountMsgs
}
return nil
}
// GenesisAccount defines an account to be initialized in the genesis state.
type GenesisAccount struct {
// address is the address of the account.
@ -214,25 +214,28 @@ func init() {
func init() { proto.RegisterFile("cosmos/accounts/v1/genesis.proto", fileDescriptor_409859d32eae9438) }
var fileDescriptor_409859d32eae9438 = []byte{
// 285 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x48, 0xce, 0x2f, 0xce,
0xcd, 0x2f, 0xd6, 0x4f, 0x4c, 0x4e, 0xce, 0x2f, 0xcd, 0x2b, 0x29, 0xd6, 0x2f, 0x33, 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, 0x94, 0x42, 0x1c, 0xc4, 0x13, 0x04, 0xe1,
0x38, 0x99, 0x9c, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13,
0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x14, 0xc4, 0xb6,
0xe2, 0x94, 0x6c, 0xbd, 0xcc, 0x7c, 0xfd, 0x0a, 0xe4, 0x98, 0x4a, 0x62, 0x03, 0x47, 0x91, 0x31,
0x20, 0x00, 0x00, 0xff, 0xff, 0x52, 0xae, 0xf2, 0xc5, 0xc6, 0x01, 0x00, 0x00,
// 331 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xcd, 0x4a, 0xc3, 0x40,
0x14, 0x85, 0x3b, 0xfd, 0x53, 0x6f, 0x63, 0xd5, 0xc1, 0x45, 0x68, 0x21, 0xd4, 0x80, 0xd0, 0x55,
0xd2, 0xaa, 0x6b, 0x41, 0x37, 0x45, 0xa4, 0x22, 0xa3, 0xb8, 0x70, 0x53, 0xd2, 0x76, 0x08, 0x43,
0x6d, 0xa6, 0xe4, 0x4e, 0x4b, 0xfb, 0x16, 0xee, 0x7c, 0x03, 0x9f, 0xc5, 0x65, 0x97, 0x2e, 0xa5,
0x7d, 0x11, 0xc9, 0x4c, 0x22, 0x8a, 0xd9, 0xe5, 0x7e, 0x39, 0xe7, 0xdc, 0xc3, 0x5c, 0x68, 0x8d,
0x24, 0x4e, 0x25, 0xfa, 0xc1, 0x68, 0x24, 0xe7, 0x91, 0x42, 0x7f, 0xd1, 0xf5, 0x43, 0x1e, 0x71,
0x14, 0xe8, 0xcd, 0x62, 0xa9, 0x24, 0xa5, 0x46, 0xe1, 0x65, 0x0a, 0x6f, 0xd1, 0x6d, 0x34, 0x73,
0x5c, 0x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eb, 0x14, 0x9e, 0x1b, 0x66, 0x1b,
0x8e, 0x27, 0x9e, 0x90, 0xfe, 0xf2, 0xf7, 0xcd, 0x86, 0x55, 0x7d, 0xb1, 0xf3, 0xef, 0x00, 0x00,
0x00, 0xff, 0xff, 0x06, 0xd4, 0x9c, 0x6a, 0x06, 0x02, 0x00, 0x00,
}
func (m *GenesisState) Marshal() (dAtA []byte, err error) {
@ -255,6 +258,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) {
_ = i
var l int
_ = l
if len(m.InitAccountMsgs) > 0 {
for iNdEx := len(m.InitAccountMsgs) - 1; iNdEx >= 0; iNdEx-- {
{
size, err := m.InitAccountMsgs[iNdEx].MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarintGenesis(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x12
}
}
if len(m.Accounts) > 0 {
for iNdEx := len(m.Accounts) - 1; iNdEx >= 0; iNdEx-- {
{
@ -266,14 +283,9 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i = encodeVarintGenesis(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x12
dAtA[i] = 0xa
}
}
if m.AccountNumber != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.AccountNumber))
i--
dAtA[i] = 0x8
}
return len(dAtA) - i, nil
}
@ -387,15 +399,18 @@ func (m *GenesisState) Size() (n int) {
}
var l int
_ = l
if m.AccountNumber != 0 {
n += 1 + sovGenesis(uint64(m.AccountNumber))
}
if len(m.Accounts) > 0 {
for _, e := range m.Accounts {
l = e.Size()
n += 1 + l + sovGenesis(uint64(l))
}
}
if len(m.InitAccountMsgs) > 0 {
for _, e := range m.InitAccountMsgs {
l = e.Size()
n += 1 + l + sovGenesis(uint64(l))
}
}
return n
}
@ -478,25 +493,6 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error {
}
switch fieldNum {
case 1:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field AccountNumber", wireType)
}
m.AccountNumber = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.AccountNumber |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Accounts", wireType)
}
@ -530,6 +526,40 @@ 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 InitAccountMsgs", 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
}
m.InitAccountMsgs = append(m.InitAccountMsgs, &MsgInit{})
if err := m.InitAccountMsgs[len(m.InitAccountMsgs)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipGenesis(dAtA[iNdEx:])