-
Notifications
You must be signed in to change notification settings - Fork 109
/
genesis.go
100 lines (84 loc) · 2.68 KB
/
genesis.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
package keymanager
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/tendermint/tendermint/abci/types"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
keymanagerState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/keymanager/state"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
keymanager "github.com/oasisprotocol/oasis-core/go/keymanager/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
)
func (app *keymanagerApplication) InitChain(ctx *tmapi.Context, request types.RequestInitChain, doc *genesis.Document) error {
st := doc.KeyManager
b, _ := json.Marshal(st)
ctx.Logger().Debug("InitChain: Genesis state",
"state", string(b),
)
// TODO: The better thing to do would be to move the registry init
// before the keymanager, and just query the registry for the runtime
// list.
regSt := doc.Registry
rtMap := make(map[common.Namespace]*registry.Runtime)
for _, rt := range regSt.Runtimes {
err := registry.VerifyRuntime(®St.Parameters, ctx.Logger(), rt, true, false)
if err != nil {
ctx.Logger().Error("InitChain: Invalid runtime",
"err", err,
)
continue
}
if rt.Kind == registry.KindKeyManager {
rtMap[rt.ID] = rt
}
}
var toEmit []*keymanager.Status
state := keymanagerState.NewMutableState(ctx.State())
for i, v := range st.Statuses {
if v == nil {
return fmt.Errorf("InitChain: Status index %d is nil", i)
}
rt := rtMap[v.ID]
if rt == nil {
ctx.Logger().Error("InitChain: State for unknown key manager runtime",
"id", v.ID,
)
continue
}
ctx.Logger().Debug("InitChain: Registering genesis key manager",
"id", v.ID,
)
// Make sure the Nodes field is empty when applying genesis state.
if v.Nodes != nil {
ctx.Logger().Error("InitChain: Genesis key manager has nodes",
"id", v.ID,
)
return errors.New("tendermint/keymanager: genesis key manager has nodes")
}
// Set, enqueue for emit.
if err := state.SetStatus(ctx, v); err != nil {
return fmt.Errorf("tendermint/keymanager: failed to set status: %w", err)
}
toEmit = append(toEmit, v)
}
if len(toEmit) > 0 {
ctx.EmitEvent(tmapi.NewEventBuilder(app.Name()).Attribute(KeyStatusUpdate, cbor.Marshal(toEmit)))
}
return nil
}
func (kq *keymanagerQuerier) Genesis(ctx context.Context) (*keymanager.Genesis, error) {
statuses, err := kq.state.Statuses(ctx)
if err != nil {
return nil, err
}
// Remove the Nodes field of each Status.
for _, status := range statuses {
status.Nodes = nil
}
gen := keymanager.Genesis{Statuses: statuses}
return &gen, nil
}