-
Notifications
You must be signed in to change notification settings - Fork 8
/
space.go
288 lines (240 loc) · 9.37 KB
/
space.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package keeper
import (
"bytes"
"strconv"
"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/bianjieai/iritamod/modules/side-chain/types"
)
// CreateSpace creates a new space
func (k Keeper) CreateSpace(ctx sdk.Context, name, uri string, sender sdk.AccAddress) (uint64, error) {
// increment the max space id and save it
k.incrSpaceSequence(ctx)
spaceId := k.GetSpaceSequence(ctx)
space := types.Space{
Id: spaceId,
Name: name,
Uri: uri,
Owner: sender.String(),
}
k.setSpace(ctx, spaceId, space)
k.setSpaceOfOwner(ctx, spaceId, sender)
return spaceId, nil
}
// TransferSpace transfer the space ownership
func (k Keeper) TransferSpace(ctx sdk.Context, spaceId uint64, from, to sdk.AccAddress) error {
space, err := k.GetSpace(ctx, spaceId)
if err != nil {
return err
}
if !k.HasSpaceOfOwner(ctx, from, spaceId) {
return sdkerrors.Wrapf(types.ErrInvalidSpaceOwner, "space (%d) is not owned by (%s)", spaceId, from.String())
}
space.Owner = to.String()
k.setSpace(ctx, spaceId, space)
k.deleteSpaceOfOwner(ctx, spaceId, from)
k.setSpaceOfOwner(ctx, spaceId, to)
return nil
}
// CreateBlockHeader creates a layer2 block header record
func (k Keeper) CreateBlockHeader(ctx sdk.Context, spaceId, height uint64, header string, sender sdk.AccAddress) error {
if !k.HasSpaceOfOwner(ctx, sender, spaceId) {
return sdkerrors.Wrapf(types.ErrInvalidSpaceOwner, "space (%d) is not owned by (%s)", spaceId, sender.String())
}
if k.HasBlockHeader(ctx, spaceId, height) {
return sdkerrors.Wrapf(types.ErrBlockHeader, "block header already exists at height (%d) in space (%d)", height, spaceId)
}
k.setBlockHeader(ctx, spaceId, height, header)
k.setBlockHeaderTxHash(ctx, spaceId, height, tmhash.Sum(ctx.TxBytes()))
// update the latest side chain height
latestHeight, exist := k.GetSpaceLatestHeight(ctx, spaceId)
if !exist || latestHeight < height {
k.setSpaceLatestHeight(ctx, spaceId, height)
}
return nil
}
// GetSpaceSequence return the current sequence for space
// <0x01> -> <currSpaceSequence>
func (k Keeper) GetSpaceSequence(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
key := types.SpaceSequenceStoreKey()
bz := store.Get(key)
return sdk.BigEndianToUint64(bz)
}
// setSpaceSequence set the current sequence for space
func (k Keeper) setSpaceSequence(ctx sdk.Context, spaceId uint64) {
store := ctx.KVStore(k.storeKey)
key := types.SpaceSequenceStoreKey()
store.Set(key, sdk.Uint64ToBigEndian(spaceId))
}
// incrSpaceId increment the space sequence id
// NOTE: this function is called when creating a new space
func (k Keeper) incrSpaceSequence(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
key := types.SpaceSequenceStoreKey()
sequence := k.GetSpaceSequence(ctx)
store.Set(key, sdk.Uint64ToBigEndian(sequence+1))
}
func (k Keeper) GetSpaces(ctx sdk.Context) []types.Space {
spaces := make([]types.Space, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixSpace)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var space types.Space
k.cdc.MustUnmarshal(iterator.Value(), &space)
spaces = append(spaces, space)
}
return spaces
}
func (k Keeper) GetSpace(ctx sdk.Context, spaceId uint64) (types.Space, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SpaceStoreKey(spaceId))
if bz == nil {
return types.Space{}, sdkerrors.Wrapf(types.ErrInvalidSpaceId, "space (%d) does not exist", spaceId)
}
var space types.Space
k.cdc.MustUnmarshal(bz, &space)
return space, nil
}
func (k Keeper) HasSpace(ctx sdk.Context, spaceId uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SpaceStoreKey(spaceId))
}
func (k Keeper) setSpace(ctx sdk.Context, spaceId uint64, space types.Space) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&space)
store.Set(types.SpaceStoreKey(spaceId), bz)
}
func (k Keeper) HasSpaceOfOwner(ctx sdk.Context, owner sdk.AccAddress, spaceId uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SpaceOfOwnerStoreKey(owner, spaceId))
}
func (k Keeper) setSpaceOfOwner(ctx sdk.Context, spaceId uint64, owner sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.SpaceOfOwnerStoreKey(owner, spaceId), types.Placeholder)
}
func (k Keeper) deleteSpaceOfOwner(ctx sdk.Context, spaceId uint64, owner sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.SpaceOfOwnerStoreKey(owner, spaceId))
}
func (k Keeper) GetBlockHeaders(ctx sdk.Context) []types.BlockHeader {
headers := make([]types.BlockHeader, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixBlockHeader)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
ret := bytes.Split(iterator.Key(), types.Delimiter)
if len(ret) != 2 {
panic("fail to split layer2 block header key")
}
var BlockHeader types.BlockHeader
ret[0] = bytes.TrimPrefix(ret[0], types.KeyPrefixBlockHeader)
spaceId, err := strconv.ParseUint(string(ret[0]), 10, 64)
if err != nil {
panic("fail to convert spaceId to uint64")
}
height, err := strconv.ParseUint(string(ret[1]), 10, 64)
if err != nil {
panic("fail to convert height to uint64")
}
BlockHeader.SpaceId = spaceId
BlockHeader.Height = height
BlockHeader.Header = string(iterator.Value())
headers = append(headers, BlockHeader)
}
for i := 0; i < len(headers); i++ {
if k.HasBlockHeaderTxHash(ctx, headers[i].SpaceId, headers[i].Height) {
txHash, err := k.GetBlockHeaderTxHash(ctx, headers[i].SpaceId, headers[i].Height)
if err != nil {
panic("fail to get block header tx hash")
}
headers[i].TxHash = txHash
}
}
return headers
}
func (k Keeper) GetBlockHeader(ctx sdk.Context, spaceId, height uint64) (string, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.BlockHeaderStoreKey(spaceId, height))
if bz == nil {
return "", sdkerrors.Wrapf(types.ErrBlockHeader, "block header does not exist at height (%d) in space (%d)", height, spaceId)
}
return string(bz), nil
}
func (k Keeper) HasBlockHeader(ctx sdk.Context, spaceId, height uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.BlockHeaderStoreKey(spaceId, height))
}
func (k Keeper) setBlockHeader(ctx sdk.Context, spaceId, blockHeight uint64, header string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.BlockHeaderStoreKey(spaceId, blockHeight), []byte(header))
}
func (k Keeper) HasBlockHeaderTxHash(ctx sdk.Context, spaceId, blockHeight uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.BlockHeaderTxHashStoreKey(spaceId, blockHeight))
}
func (k Keeper) GetBlockHeaderTxHash(ctx sdk.Context, spaceId, blockHeight uint64) (string, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.BlockHeaderTxHashStoreKey(spaceId, blockHeight))
if bz == nil {
return "", sdkerrors.Wrapf(types.ErrBlockHeader, "create block header tx hash does not exist at height (%d) in space (%d)", blockHeight, spaceId)
}
return string(bz), nil
}
func (k Keeper) setBlockHeaderTxHash(ctx sdk.Context, spaceId, blockHeight uint64, txHash tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Set(types.BlockHeaderTxHashStoreKey(spaceId, blockHeight), []byte(txHash.String()))
}
func (k Keeper) setBlockHeaderTxHashString(ctx sdk.Context, spaceId, blockHeight uint64, txHashStr string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.BlockHeaderTxHashStoreKey(spaceId, blockHeight), []byte(txHashStr))
}
func (k Keeper) GetSpaceLatestHeights(ctx sdk.Context) []types.SpaceLatestHeight {
latestHeights := make([]types.SpaceLatestHeight, 0)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixSpaceLatestHeight)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
ret := bytes.TrimPrefix(iterator.Key(), types.KeyPrefixSpaceLatestHeight)
spaceId, err := strconv.ParseUint(string(ret), 10, 64)
if err != nil {
panic("fail to convert spaceId to uint64")
}
latestHeight := sdk.BigEndianToUint64(iterator.Value())
latestHeights = append(latestHeights, types.SpaceLatestHeight{
SpaceId: spaceId,
Height: latestHeight,
})
}
return latestHeights
}
func (k Keeper) HasSpaceLatestHeight(ctx sdk.Context, spaceId uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyPrefixSpaceLatestHeightStoreKey(spaceId))
}
func (k Keeper) GetSpaceLatestHeight(ctx sdk.Context, spaceId uint64) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefixSpaceLatestHeightStoreKey(spaceId))
if bz == nil {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}
func (k Keeper) setSpaceLatestHeight(ctx sdk.Context, spaceId, blockHeight uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyPrefixSpaceLatestHeightStoreKey(spaceId), sdk.Uint64ToBigEndian(blockHeight))
}
func (k Keeper) getSpaceStore(ctx sdk.Context) prefix.Store {
store := ctx.KVStore(k.storeKey)
return prefix.NewStore(store, types.KeyPrefixSpace)
}
// getSpaceOfOwnerStore returns a prefix store of <0x02><owner><delimiter>
func (k Keeper) getSpaceOfOwnerStore(ctx sdk.Context, owner sdk.AccAddress) prefix.Store {
store := ctx.KVStore(k.storeKey)
key := types.SpaceOfOwnerByOwnerStoreKey(owner)
return prefix.NewStore(store, key)
}