Skip to content

Commit

Permalink
go/keymanager: Move master and ephemeral secrets code to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
peternose committed Jan 31, 2024
1 parent 8225aca commit 696f204
Show file tree
Hide file tree
Showing 51 changed files with 2,111 additions and 1,784 deletions.
Empty file added .changelog/5544.trivial.md
Empty file.
6 changes: 6 additions & 0 deletions go/consensus/cometbft/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ type Extension interface {
// ExecuteTx executes a transaction.
ExecuteTx(*Context, *transaction.Transaction) error

// InitChain initializes the blockchain with validators and other
// info from CometBFT.
//
// Note: Errors are irrecoverable and will result in a panic.
InitChain(*Context, cmtabcitypes.RequestInitChain, *genesis.Document) error

// BeginBlock signals the beginning of a block.
//
// Note: Errors are irrecoverable and will result in a panic.
Expand Down
96 changes: 5 additions & 91 deletions go/consensus/cometbft/apps/keymanager/genesis.go
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(&regSt.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

Check warning on line 20 in go/consensus/cometbft/apps/keymanager/genesis.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/genesis.go#L20

Added line #L20 was not covered by tests
}
}

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
}
Loading

0 comments on commit 696f204

Please sign in to comment.