-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/keymanager: Move master and ephemeral secrets code to extension
- Loading branch information
Showing
51 changed files
with
2,111 additions
and
1,784 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,25 @@ | ||
package keymanager | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/cometbft/cometbft/abci/types" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api" | ||
keymanagerState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/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, _ types.RequestInitChain, doc *genesis.Document) error { | ||
st := doc.KeyManager | ||
|
||
b, _ := json.Marshal(st) | ||
func (app *keymanagerApplication) InitChain(ctx *tmapi.Context, req types.RequestInitChain, doc *genesis.Document) error { | ||
b, _ := json.Marshal(doc.KeyManager) | ||
ctx.Logger().Debug("InitChain: Genesis state", | ||
"state", string(b), | ||
) | ||
|
||
state := keymanagerState.NewMutableState(ctx.State()) | ||
|
||
if err := state.SetConsensusParameters(ctx, &st.Parameters); err != nil { | ||
return fmt.Errorf("cometbft/keymanager: failed to set consensus parameters: %w", err) | ||
} | ||
|
||
epoch, err := app.state.GetCurrentEpoch(ctx) | ||
if err != nil { | ||
return fmt.Errorf("cometbft/keymanager: couldn't get current epoch: %w", err) | ||
} | ||
|
||
// 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, epoch) | ||
if err != nil { | ||
ctx.Logger().Error("InitChain: Invalid runtime", | ||
"err", err, | ||
) | ||
continue | ||
} | ||
|
||
if rt.Kind == registry.KindKeyManager { | ||
rtMap[rt.ID] = rt | ||
for _, ext := range app.exts { | ||
if err := ext.InitChain(ctx, req, doc); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
var toEmit []*keymanager.Status | ||
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("cometbft/keymanager: genesis key manager has nodes") | ||
} | ||
|
||
// Set, enqueue for emit. | ||
if err := state.SetStatus(ctx, v); err != nil { | ||
return fmt.Errorf("cometbft/keymanager: failed to set status: %w", err) | ||
} | ||
toEmit = append(toEmit, v) | ||
} | ||
|
||
if len(toEmit) > 0 { | ||
ctx.EmitEvent(tmapi.NewEventBuilder(app.Name()).TypedAttribute(&keymanager.StatusUpdateEvent{ | ||
Statuses: 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 | ||
} |
Oops, something went wrong.