-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_keeper.go
186 lines (168 loc) · 5.01 KB
/
account_keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package keeper
import (
"github.com/ci123chain/ci123chain/pkg/abci/codec"
"github.com/ci123chain/ci123chain/pkg/abci/types"
"github.com/ci123chain/ci123chain/pkg/account/exported"
acc_types "github.com/ci123chain/ci123chain/pkg/account/types"
)
type AccountKeeper struct {
key types.StoreKey
proto func() exported.Account
cdc *codec.Codec
}
func NewAccountKeeper(cdc *codec.Codec, key types.StoreKey, proto func() exported.Account) AccountKeeper {
return AccountKeeper{
key: key,
cdc: cdc,
proto: proto,
}
}
// 利用一个地址 新建一个账户
func (ak AccountKeeper) NewAccountWithAddress(ctx types.Context, addr types.AccAddress) exported.Account {
acc := ak.proto()
err := acc.SetAddress(addr)
if err != nil {
panic(err)
}
return ak.NewAccount(ctx, acc)
}
func (ak AccountKeeper) NewAccount(ctx types.Context, acc exported.Account) exported.Account {
if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil {
panic(err)
}
return acc
}
func (ak AccountKeeper) SetAccount(ctx types.Context, acc exported.Account) {
//if acc.String() == "0x4feA76427B8345861e80A3540a8a9D936FD39391" {
// fmt.Println(acc.GetIsModule())
//}
addr := acc.GetAddress()
store := ctx.KVStore(ak.key)
//bz, err := ak.cdc.MarshalBinaryBare(acc)
bz, err := ak.cdc.MarshalBinaryLengthPrefixed(acc)
if err != nil {
panic(err)
}
store.Set(acc_types.AddressStoreKey(addr), bz)
//coins := acc.GetCoins()
//chain_id := ctx.ChainID()
//update := util.HeightUpdate{
// Shard: chain_id,
// Coins: coins,
//}
//h := ctx.BlockHeight()
//v, err := ak.cdc.MarshalBinaryLengthPrefixed(update)
//if err != nil {
// panic(err)
//}
//
//var heights util.Heights
//b := store.Get(acc_types.HeightsUpdateKey(addr))
//if b == nil {
// heights = make(util.Heights, 0)
// heights = append(heights, h)
//}else {
// err := ak.cdc.UnmarshalBinaryLengthPrefixed(b, &heights)
// if err != nil {
// panic(err)
// }
// sort.Sort(heights)
// if heights[len(heights)-1] < h{
// heights = append(heights, h)
// }
//}
//hbz, err := ak.cdc.MarshalBinaryLengthPrefixed(heights)
//if err != nil {
// panic(err)
//}
//
//store.Set(acc_types.HeightUpdateKey(addr, h), v)
//store.Set(acc_types.HeightsUpdateKey(addr), hbz)
}
//func SearchHeight(ctx types.Context, ak AccountKeeper, acc exported.Account, i int64) int64 {
// addr := acc.GetAddress()
// store := ctx.KVStore(ak.key)
// var heights util.Heights
// b := store.Get(acc_types.HeightsUpdateKey(addr))
// err := ak.cdc.UnmarshalBinaryLengthPrefixed(b, &heights)
// if err != nil {
// return -3
// }else {
// return heights.Search(i)
// }
//}
//func GetHistoryBalance(ctx types.Context, ak AccountKeeper, acc exported.Account, i int64) util.HeightUpdate {
// addr := acc.GetAddress()
// store := ctx.KVStore(ak.key)
// var update util.HeightUpdate
// b := store.Get(acc_types.HeightUpdateKey(addr, i))
// err := ak.cdc.UnmarshalBinaryLengthPrefixed(b, & update)
// if err != nil {
// panic(err)
// }
// return update
//}
func (ak AccountKeeper) GetAccount(ctx types.Context, addr types.AccAddress) exported.Account {
store := ctx.KVStore(ak.key)
bz := store.Get(acc_types.AddressStoreKey(addr))
if bz == nil {
return nil
}
acc := ak.decodeAccount(bz)
return acc
}
// GetAllAccounts returns all accounts in the accountKeeper.
func (ak AccountKeeper) GetAllAccounts(ctx types.Context) (accounts []exported.Account) {
ak.IterateAccounts(ctx,
func(acc exported.Account) (stop bool) {
accounts = append(accounts, acc)
return false
})
return accounts
}
func (ak AccountKeeper) RemoveAccount(ctx types.Context, acc exported.Account) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.key)
store.Delete(acc_types.AddressStoreKey(addr))
}
func (ak AccountKeeper) decodeAccount(bz []byte) (acc exported.Account) {
err := ak.cdc.UnmarshalBinaryLengthPrefixed(bz, &acc)
if err != nil {
panic(err)
}
return
}
func (ak AccountKeeper) GetNextAccountNumber(ctx types.Context) uint64 {
var accNumber uint64
store := ctx.KVStore(ak.key)
bz := store.Get(acc_types.GlobalAccountNumberKey)
if bz == nil {
accNumber = 0
} else {
err := ak.cdc.UnmarshalBinaryLengthPrefixed(bz, &accNumber)
if err != nil {
panic(err)
}
}
bz = ak.cdc.MustMarshalBinaryLengthPrefixed(accNumber + 1)
store.Set(acc_types.GlobalAccountNumberKey, bz)
return accNumber
}
// IterateAccounts iterates over all the stored accounts and performs a callback function
func (ak AccountKeeper) IterateAccounts(ctx types.Context, cb func(account exported.Account) (stop bool)) {
store := ctx.KVStore(ak.key)
//
//bz := store.Get(acc_types.AddressStoreKey(types.HexToAddress("0x3F43E75Aaba2c2fD6E227C10C6E7DC125A93DE3c")))
//acc := ak.decodeAccount(bz)
//fmt.Println(acc.GetAddress().String())
prefix := acc_types.AddressStoreKeyPrefix
//iterator := store.RemoteIterator(prefix, types.PrefixEndBytes(prefix))
iterator := types.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
account := ak.decodeAccount(iterator.Value())
if cb(account) {
break
}
}
}