feat(accounts): add Genesis handling (#17802)

Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
testinginprod 2023-09-21 16:26:08 +02:00 committed by GitHub
parent 5987d0003a
commit 5952ecf515
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 3078 additions and 13 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,31 @@
syntax = "proto3";
package cosmos.accounts.v1;
option go_package = "cosmossdk.io/x/accounts/v1";
// 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;
}
// GenesisAccount defines an account to be initialized in the genesis state.
message GenesisAccount {
// address is the address of the account.
string address = 1;
// account_type is the account type of the account.
string account_type = 2;
// state is the account state represented as a slice of raw key value byte pairs.
repeated KVPair state = 3;
}
// KVPair defines a key value pair.
message KVPair {
// key is the key of the pair.
bytes key = 1;
// value is the value of the pair.
bytes value = 2;
}

View File

@ -9,12 +9,21 @@ import (
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/collections"
"cosmossdk.io/x/accounts/internal/implementation"
)
var _ implementation.Account = (*TestAccount)(nil)
type TestAccount struct{}
func NewTestAccount(sb *collections.SchemaBuilder) *TestAccount {
return &TestAccount{
Counter: collections.NewSequence(sb, collections.NewPrefix(0), "counter"),
}
}
type TestAccount struct {
Counter collections.Sequence
}
func (t TestAccount) RegisterInitHandler(builder *implementation.InitBuilder) {
implementation.RegisterInitHandler(builder, func(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) {
@ -62,6 +71,11 @@ func (t TestAccount) RegisterExecuteHandlers(builder *implementation.ExecuteBuil
return &emptypb.Empty{}, nil
})
// genesis testing
implementation.RegisterExecuteHandler(builder, func(ctx context.Context, req *wrapperspb.UInt64Value) (*emptypb.Empty, error) {
return new(emptypb.Empty), t.Counter.Set(ctx, req.Value)
})
}
func (t TestAccount) RegisterQueryHandlers(builder *implementation.QueryBuilder) {
@ -90,4 +104,14 @@ func (t TestAccount) RegisterQueryHandlers(builder *implementation.QueryBuilder)
}
return wrapperspb.Int64(amt), nil
})
// genesis testing; DoubleValue does not make sense as a request type for this query, but empty is already taken
// and this is only used for testing.
implementation.RegisterQueryHandler(builder, func(ctx context.Context, _ *wrapperspb.DoubleValue) (*wrapperspb.UInt64Value, error) {
v, err := t.Counter.Peek(ctx)
if err != nil {
return nil, err
}
return &wrapperspb.UInt64Value{Value: v}, nil
})
}

94
x/accounts/genesis.go Normal file
View File

@ -0,0 +1,94 @@
package accounts
import (
"context"
"fmt"
"cosmossdk.io/collections"
v1 "cosmossdk.io/x/accounts/v1"
)
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(key []byte, value string) (stop bool, err error) {
accState, err := k.exportAccount(ctx, key, value)
if err != nil {
return true, err
}
genState.Accounts = append(genState.Accounts, accState)
return false, nil
})
if err != nil {
return nil, err
}
return genState, nil
}
func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string) (*v1.GenesisAccount, error) {
addrString, err := k.addressCodec.BytesToString(addr)
if err != nil {
return nil, err
}
account := &v1.GenesisAccount{
Address: addrString,
AccountType: accType,
}
rng := new(collections.Range[[]byte]).
Prefix(addr)
err = k.AccountsState.Walk(ctx, rng, func(key, value []byte) (stop bool, err error) {
account.State = append(account.State, &v1.KVPair{
Key: key,
Value: value,
})
return false, nil
})
if err != nil {
return nil, err
}
return account, nil
}
func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) error {
err := k.AccountNumber.Set(ctx, genState.AccountNumber)
if err != nil {
return err
}
// import accounts
for _, acc := range genState.Accounts {
err = k.importAccount(ctx, acc)
if err != nil {
return fmt.Errorf("%w: %s", err, acc.Address)
}
}
return nil
}
func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error {
// TODO: maybe check if impl exists?
addrBytes, err := k.addressCodec.StringToBytes(acc.Address)
if err != nil {
return err
}
err = k.AccountsByType.Set(ctx, addrBytes, acc.AccountType)
if err != nil {
return err
}
for _, kv := range acc.State {
err = k.AccountsState.Set(ctx, kv.Key, kv.Value)
if err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1,61 @@
package accounts
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
"cosmossdk.io/collections"
"cosmossdk.io/collections/colltest"
"cosmossdk.io/x/accounts/internal/implementation"
)
func TestGenesis(t *testing.T) {
sb := collections.NewSchemaBuilderFromAccessor(implementation.OpenKVStore)
acc := NewTestAccount(sb)
k, ctx := newKeeper(t, map[string]implementation.Account{
"test": acc,
})
k.queryModuleFunc = func(ctx context.Context, _ proto.Message) (proto.Message, error) {
return &bankv1beta1.QueryBalanceResponse{}, nil
}
// we init two accounts of the same type
// we set counter to 10
_, addr1, err := k.Init(ctx, "test", []byte("sender"), &emptypb.Empty{})
require.NoError(t, err)
_, err = k.Execute(ctx, addr1, []byte("sender"), &wrapperspb.UInt64Value{Value: 10})
require.NoError(t, err)
// we set counter to 20
_, addr2, err := k.Init(ctx, "test", []byte("sender"), &emptypb.Empty{})
require.NoError(t, err)
_, err = k.Execute(ctx, addr2, []byte("sender"), &wrapperspb.UInt64Value{Value: 20})
require.NoError(t, err)
// export state
state, err := k.ExportState(ctx)
require.NoError(t, err)
// reset state
_, ctx = colltest.MockStore()
err = k.ImportState(ctx, state)
require.NoError(t, err)
// if genesis import went fine, we should be able to query the accounts
// and get the expected values.
resp, err := k.Query(ctx, addr1, &wrapperspb.DoubleValue{})
require.NoError(t, err)
require.Equal(t, &wrapperspb.UInt64Value{Value: 10}, resp)
resp, err = k.Query(ctx, addr2, &wrapperspb.DoubleValue{})
require.NoError(t, err)
require.Equal(t, &wrapperspb.UInt64Value{Value: 20}, resp)
}

View File

@ -46,7 +46,7 @@ func (t TestAccount) RegisterExecuteHandlers(builder *ExecuteBuilder) {
})
}
func (TestAccount) RegisterQueryHandlers(builder *QueryBuilder) {
func (t TestAccount) RegisterQueryHandlers(builder *QueryBuilder) {
RegisterQueryHandler(builder, func(_ context.Context, req *wrapperspb.StringValue) (*wrapperspb.StringValue, error) {
return &wrapperspb.StringValue{Value: req.Value + "query-echo"}, nil
})

View File

@ -8,22 +8,26 @@ import (
"google.golang.org/protobuf/proto"
"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"cosmossdk.io/x/accounts/internal/prefixstore"
)
var errUnauthorized = errors.New("unauthorized")
var (
errUnauthorized = errors.New("unauthorized")
AccountStatePrefix = collections.NewPrefix(255)
)
type contextKey struct{}
type contextValue struct {
store store.KVStore // store is the prefixed store for the account.
sender []byte // sender is the address of the entity invoking the account action.
whoami []byte // whoami is the address of the account being invoked.
originalContext context.Context // originalContext that was used to build the account context.
getExpectedSender func(msg proto.Message) ([]byte, error)
moduleExec func(ctx context.Context, msg proto.Message) (proto.Message, error)
moduleQuery func(ctx context.Context, msg proto.Message) (proto.Message, error)
store store.KVStore // store is the prefixed store for the account.
sender []byte // sender is the address of the entity invoking the account action.
whoami []byte // whoami is the address of the account being invoked.
originalContext context.Context // originalContext that was used to build the account context.
getExpectedSender func(msg proto.Message) ([]byte, error) // getExpectedSender is a function that returns the expected sender for a given message.
moduleExec func(ctx context.Context, msg proto.Message) (proto.Message, error) // moduleExec is a function that executes a module message.
moduleQuery func(ctx context.Context, msg proto.Message) (proto.Message, error) // moduleQuery is a function that queries a module.
}
// MakeAccountContext creates a new account execution context given:
@ -31,6 +35,8 @@ type contextValue struct {
// accountAddr: the address of the account being invoked, which is used to give the
// account a prefixed storage.
// sender: the address of entity invoking the account action.
// moduleExec: a function that executes a module message.
// moduleQuery: a function that queries a module.
func MakeAccountContext(
ctx context.Context,
storeSvc store.KVStoreService,
@ -41,7 +47,7 @@ func MakeAccountContext(
moduleQuery func(ctx context.Context, msg proto.Message) (proto.Message, error),
) context.Context {
return context.WithValue(ctx, contextKey{}, contextValue{
store: prefixstore.New(storeSvc.OpenKVStore(ctx), accountAddr),
store: prefixstore.New(storeSvc.OpenKVStore(ctx), append(AccountStatePrefix, accountAddr...)),
sender: sender,
whoami: accountAddr,
originalContext: ctx,

View File

@ -38,8 +38,8 @@ func TestMakeAccountContext(t *testing.T) {
// this store is the global x/accounts module store.
store := storeService.OpenKVStore(originalContext)
// now we want the value to be store in the following accounts prefix (accountAddr + itemPrefix)
value, err := store.Get(append(accountAddr, itemPrefix...))
// now we want the value to be store in the following accounts prefix (AccountsStatePrefix + accountAddr + itemPrefix)
value, err := store.Get(append(AccountStatePrefix, append(accountAddr, itemPrefix...)...))
require.NoError(t, err)
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 3, 232}, value)

View File

@ -43,6 +43,7 @@ func NewKeeper(
Schema: collections.Schema{},
AccountNumber: collections.NewSequence(sb, AccountNumberKey, "account_number"),
AccountsByType: collections.NewMap(sb, AccountTypeKeyPrefix, "accounts_by_type", collections.BytesKey, collections.StringValue),
AccountsState: collections.NewMap(sb, implementation.AccountStatePrefix, "accounts_state", collections.BytesKey, collections.BytesValue),
}
// make accounts implementation
@ -77,6 +78,12 @@ type Keeper struct {
AccountNumber collections.Sequence
// AccountsByType maps account address to their implementation.
AccountsByType collections.Map[[]byte, string]
// AccountsState keeps track of the state of each account.
// NOTE: this is only used for genesis import and export.
// Contracts set and get their own state but this helps providing a nice mapping
// between: (account address, account state key) => account state value.
AccountsState collections.Map[[]byte, []byte]
}
// Init creates a new account of the given type.

886
x/accounts/v1/genesis.pb.go Normal file
View File

@ -0,0 +1,886 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: cosmos/accounts/v1/genesis.proto
package v1
import (
fmt "fmt"
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
// 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"`
}
func (m *GenesisState) Reset() { *m = GenesisState{} }
func (m *GenesisState) String() string { return proto.CompactTextString(m) }
func (*GenesisState) ProtoMessage() {}
func (*GenesisState) Descriptor() ([]byte, []int) {
return fileDescriptor_409859d32eae9438, []int{0}
}
func (m *GenesisState) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *GenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_GenesisState.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 *GenesisState) XXX_Merge(src proto.Message) {
xxx_messageInfo_GenesisState.Merge(m, src)
}
func (m *GenesisState) XXX_Size() int {
return m.Size()
}
func (m *GenesisState) XXX_DiscardUnknown() {
xxx_messageInfo_GenesisState.DiscardUnknown(m)
}
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
}
return nil
}
// GenesisAccount defines an account to be initialized in the genesis state.
type GenesisAccount struct {
// address is the address of the account.
Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"`
// account_type is the account type of the account.
AccountType string `protobuf:"bytes,2,opt,name=account_type,json=accountType,proto3" json:"account_type,omitempty"`
// state is the account state represented as a slice of raw key value byte pairs.
State []*KVPair `protobuf:"bytes,3,rep,name=state,proto3" json:"state,omitempty"`
}
func (m *GenesisAccount) Reset() { *m = GenesisAccount{} }
func (m *GenesisAccount) String() string { return proto.CompactTextString(m) }
func (*GenesisAccount) ProtoMessage() {}
func (*GenesisAccount) Descriptor() ([]byte, []int) {
return fileDescriptor_409859d32eae9438, []int{1}
}
func (m *GenesisAccount) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *GenesisAccount) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_GenesisAccount.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 *GenesisAccount) XXX_Merge(src proto.Message) {
xxx_messageInfo_GenesisAccount.Merge(m, src)
}
func (m *GenesisAccount) XXX_Size() int {
return m.Size()
}
func (m *GenesisAccount) XXX_DiscardUnknown() {
xxx_messageInfo_GenesisAccount.DiscardUnknown(m)
}
var xxx_messageInfo_GenesisAccount proto.InternalMessageInfo
func (m *GenesisAccount) GetAddress() string {
if m != nil {
return m.Address
}
return ""
}
func (m *GenesisAccount) GetAccountType() string {
if m != nil {
return m.AccountType
}
return ""
}
func (m *GenesisAccount) GetState() []*KVPair {
if m != nil {
return m.State
}
return nil
}
// KVPair defines a key value pair.
type KVPair struct {
// key is the key of the pair.
Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
// value is the value of the pair.
Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
}
func (m *KVPair) Reset() { *m = KVPair{} }
func (m *KVPair) String() string { return proto.CompactTextString(m) }
func (*KVPair) ProtoMessage() {}
func (*KVPair) Descriptor() ([]byte, []int) {
return fileDescriptor_409859d32eae9438, []int{2}
}
func (m *KVPair) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *KVPair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_KVPair.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 *KVPair) XXX_Merge(src proto.Message) {
xxx_messageInfo_KVPair.Merge(m, src)
}
func (m *KVPair) XXX_Size() int {
return m.Size()
}
func (m *KVPair) XXX_DiscardUnknown() {
xxx_messageInfo_KVPair.DiscardUnknown(m)
}
var xxx_messageInfo_KVPair proto.InternalMessageInfo
func (m *KVPair) GetKey() []byte {
if m != nil {
return m.Key
}
return nil
}
func (m *KVPair) GetValue() []byte {
if m != nil {
return m.Value
}
return nil
}
func init() {
proto.RegisterType((*GenesisState)(nil), "cosmos.accounts.v1.GenesisState")
proto.RegisterType((*GenesisAccount)(nil), "cosmos.accounts.v1.GenesisAccount")
proto.RegisterType((*KVPair)(nil), "cosmos.accounts.v1.KVPair")
}
func init() { proto.RegisterFile("cosmos/accounts/v1/genesis.proto", fileDescriptor_409859d32eae9438) }
var fileDescriptor_409859d32eae9438 = []byte{
// 280 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, 0xd4, 0x4f,
0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x82, 0xa8,
0xd0, 0x83, 0xa9, 0xd0, 0x2b, 0x33, 0x54, 0x2a, 0xe5, 0xe2, 0x71, 0x87, 0x28, 0x0a, 0x2e, 0x49,
0x2c, 0x49, 0x15, 0x52, 0xe5, 0xe2, 0x83, 0x4a, 0xc7, 0xe7, 0x95, 0xe6, 0x26, 0xa5, 0x16, 0x49,
0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x04, 0xf1, 0x42, 0x45, 0xfd, 0xc0, 0x82, 0x42, 0x76, 0x5c, 0x1c,
0x30, 0x53, 0x24, 0x98, 0x14, 0x98, 0x35, 0xb8, 0x8d, 0x94, 0xf4, 0x30, 0x4d, 0xd7, 0x83, 0x1a,
0xed, 0x08, 0x11, 0x0a, 0x82, 0xeb, 0x51, 0xaa, 0xe7, 0xe2, 0x43, 0x95, 0x13, 0x92, 0xe0, 0x62,
0x4f, 0x4c, 0x49, 0x29, 0x4a, 0x2d, 0x2e, 0x06, 0xdb, 0xc8, 0x19, 0x04, 0xe3, 0x0a, 0x29, 0x72,
0xf1, 0xc0, 0x9c, 0x54, 0x52, 0x59, 0x90, 0x2a, 0xc1, 0x04, 0x96, 0xe6, 0x86, 0x8a, 0x85, 0x54,
0x16, 0xa4, 0x0a, 0x19, 0x70, 0xb1, 0x16, 0x83, 0x9c, 0x2f, 0xc1, 0x0c, 0x76, 0x8b, 0x14, 0x36,
0xb7, 0x78, 0x87, 0x05, 0x24, 0x66, 0x16, 0x05, 0x41, 0x14, 0x2a, 0x19, 0x70, 0xb1, 0x41, 0x04,
0x84, 0x04, 0xb8, 0x98, 0xb3, 0x53, 0x2b, 0xc1, 0x96, 0xf2, 0x04, 0x81, 0x98, 0x42, 0x22, 0x5c,
0xac, 0x65, 0x89, 0x39, 0xa5, 0x10, 0x9b, 0x78, 0x82, 0x20, 0x1c, 0x27, 0x93, 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, 0x92, 0x82, 0xd8, 0x56, 0x9c, 0x92, 0xad, 0x97, 0x99,
0xaf, 0x5f, 0x81, 0x1c, 0x03, 0x49, 0x6c, 0xe0, 0xa0, 0x37, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff,
0x11, 0x08, 0x28, 0x3a, 0x9e, 0x01, 0x00, 0x00,
}
func (m *GenesisState) 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 *GenesisState) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if len(m.Accounts) > 0 {
for iNdEx := len(m.Accounts) - 1; iNdEx >= 0; iNdEx-- {
{
size, err := m.Accounts[iNdEx].MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarintGenesis(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x12
}
}
if m.AccountNumber != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.AccountNumber))
i--
dAtA[i] = 0x8
}
return len(dAtA) - i, nil
}
func (m *GenesisAccount) 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 *GenesisAccount) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *GenesisAccount) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if len(m.State) > 0 {
for iNdEx := len(m.State) - 1; iNdEx >= 0; iNdEx-- {
{
size, err := m.State[iNdEx].MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarintGenesis(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x1a
}
}
if len(m.AccountType) > 0 {
i -= len(m.AccountType)
copy(dAtA[i:], m.AccountType)
i = encodeVarintGenesis(dAtA, i, uint64(len(m.AccountType)))
i--
dAtA[i] = 0x12
}
if len(m.Address) > 0 {
i -= len(m.Address)
copy(dAtA[i:], m.Address)
i = encodeVarintGenesis(dAtA, i, uint64(len(m.Address)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *KVPair) 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 *KVPair) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *KVPair) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if len(m.Value) > 0 {
i -= len(m.Value)
copy(dAtA[i:], m.Value)
i = encodeVarintGenesis(dAtA, i, uint64(len(m.Value)))
i--
dAtA[i] = 0x12
}
if len(m.Key) > 0 {
i -= len(m.Key)
copy(dAtA[i:], m.Key)
i = encodeVarintGenesis(dAtA, i, uint64(len(m.Key)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int {
offset -= sovGenesis(v)
base := offset
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return base
}
func (m *GenesisState) Size() (n int) {
if m == nil {
return 0
}
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))
}
}
return n
}
func (m *GenesisAccount) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Address)
if l > 0 {
n += 1 + l + sovGenesis(uint64(l))
}
l = len(m.AccountType)
if l > 0 {
n += 1 + l + sovGenesis(uint64(l))
}
if len(m.State) > 0 {
for _, e := range m.State {
l = e.Size()
n += 1 + l + sovGenesis(uint64(l))
}
}
return n
}
func (m *KVPair) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Key)
if l > 0 {
n += 1 + l + sovGenesis(uint64(l))
}
l = len(m.Value)
if l > 0 {
n += 1 + l + sovGenesis(uint64(l))
}
return n
}
func sovGenesis(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
func sozGenesis(x uint64) (n int) {
return sovGenesis(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *GenesisState) 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 ErrIntOverflowGenesis
}
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: GenesisState: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: GenesisState: illegal tag %d (wire type %d)", fieldNum, wire)
}
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)
}
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.Accounts = append(m.Accounts, &GenesisAccount{})
if err := m.Accounts[len(m.Accounts)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipGenesis(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthGenesis
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *GenesisAccount) 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 ErrIntOverflowGenesis
}
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: GenesisAccount: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: GenesisAccount: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Address", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
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 ErrInvalidLengthGenesis
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthGenesis
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Address = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field AccountType", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
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 ErrInvalidLengthGenesis
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthGenesis
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.AccountType = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 3:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field State", 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.State = append(m.State, &KVPair{})
if err := m.State[len(m.State)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipGenesis(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthGenesis
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *KVPair) 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 ErrIntOverflowGenesis
}
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: KVPair: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: KVPair: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType)
}
var byteLen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
byteLen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if byteLen < 0 {
return ErrInvalidLengthGenesis
}
postIndex := iNdEx + byteLen
if postIndex < 0 {
return ErrInvalidLengthGenesis
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...)
if m.Key == nil {
m.Key = []byte{}
}
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Value", wireType)
}
var byteLen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
byteLen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if byteLen < 0 {
return ErrInvalidLengthGenesis
}
postIndex := iNdEx + byteLen
if postIndex < 0 {
return ErrInvalidLengthGenesis
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Value = append(m.Value[:0], dAtA[iNdEx:postIndex]...)
if m.Value == nil {
m.Value = []byte{}
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipGenesis(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthGenesis
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipGenesis(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, ErrIntOverflowGenesis
}
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, ErrIntOverflowGenesis
}
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, ErrIntOverflowGenesis
}
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, ErrInvalidLengthGenesis
}
iNdEx += length
case 3:
depth++
case 4:
if depth == 0 {
return 0, ErrUnexpectedEndOfGroupGenesis
}
depth--
case 5:
iNdEx += 4
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthGenesis
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthGenesis = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowGenesis = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupGenesis = fmt.Errorf("proto: unexpected end of group")
)