/
chain.go
176 lines (146 loc) · 4.7 KB
/
chain.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
package keeper
import (
"encoding/binary"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/spn/x/launch/types"
)
// CreateNewChain creates a new chain in the store from the provided information
func (k Keeper) CreateNewChain(
ctx sdk.Context,
coordinatorID uint64,
genesisChainID,
sourceURL,
sourceHash string,
initialGenesis types.InitialGenesis,
hasProject bool,
projectID uint64,
isMainnet bool,
accountBalance sdk.Coins,
metadata []byte,
) (uint64, error) {
coord, found := k.profileKeeper.GetCoordinator(ctx, coordinatorID)
if !found {
return 0, fmt.Errorf("the coordinator %d doesn't exist", coordinatorID)
}
chain := types.Chain{
CoordinatorID: coordinatorID,
GenesisChainID: genesisChainID,
CreatedAt: ctx.BlockTime().Unix(),
SourceURL: sourceURL,
SourceHash: sourceHash,
InitialGenesis: initialGenesis,
HasProject: hasProject,
ProjectID: projectID,
IsMainnet: isMainnet,
LaunchTriggered: false,
LaunchTime: time.Unix(0, 0).UTC(),
AccountBalance: accountBalance,
Metadata: metadata,
}
if err := chain.Validate(); err != nil {
return 0, err
}
// If the chain is associated to a project, project existence and coordinator is checked
if hasProject {
project, found := k.projectKeeper.GetProject(ctx, projectID)
if !found {
return 0, fmt.Errorf("project %d doesn't exist", projectID)
}
if project.CoordinatorID != coordinatorID {
return 0, fmt.Errorf(
"chain coordinator %d and project coordinator %d don't match",
coordinatorID,
project.CoordinatorID,
)
}
}
// Append the chain to the store
launchID := k.AppendChain(ctx, chain)
// Register the chain to the project
if hasProject {
if err := k.projectKeeper.AddChainToProject(ctx, projectID, launchID); err != nil {
return 0, err
}
}
return launchID, ctx.EventManager().EmitTypedEvent(&types.EventChainCreated{
LaunchID: launchID,
CoordinatorAddress: coord.Address,
CoordinatorID: coordinatorID,
})
}
// GetChainCounter get the counter for chains
func (k Keeper) GetChainCounter(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.ChainCounterKey)
bz := store.Get(byteKey)
// Counter doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetChainCounter set the counter for chains
func (k Keeper) SetChainCounter(ctx sdk.Context, counter uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.ChainCounterKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, counter)
store.Set(byteKey, bz)
}
// AppendChain appends a chain in the store with a new id and update the counter
func (k Keeper) AppendChain(ctx sdk.Context, chain types.Chain) uint64 {
counter := k.GetChainCounter(ctx)
// Set the ID of the appended value
chain.LaunchID = counter
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainKeyPrefix))
appendedValue := k.cdc.MustMarshal(&chain)
store.Set(types.ChainKey(chain.LaunchID), appendedValue)
// Update chain counter
k.SetChainCounter(ctx, counter+1)
return counter
}
// SetChain set a specific chain in the store from its index
func (k Keeper) SetChain(ctx sdk.Context, chain types.Chain) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainKeyPrefix))
b := k.cdc.MustMarshal(&chain)
store.Set(types.ChainKey(chain.LaunchID), b)
}
// EnableMonitoringConnection sets a chain with MonitoringConnected set to true
func (k Keeper) EnableMonitoringConnection(ctx sdk.Context, launchID uint64) error {
chain, found := k.GetChain(ctx, launchID)
if !found {
return types.ErrChainNotFound
}
if chain.MonitoringConnected {
return types.ErrChainMonitoringConnected
}
chain.MonitoringConnected = true
k.SetChain(ctx, chain)
return nil
}
// GetChain returns a chain from its index
func (k Keeper) GetChain(ctx sdk.Context, launchID uint64) (val types.Chain, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainKeyPrefix))
b := store.Get(types.ChainKey(launchID))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetAllChain returns all chain
func (k Keeper) GetAllChain(ctx sdk.Context) (list []types.Chain) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Chain
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}