Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/keymanager: Move master and ephemeral secrets code to extension #5544

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .changelog/5544.trivial.md
Empty file.
29 changes: 29 additions & 0 deletions go/consensus/cometbft/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,32 @@ type Application interface {
// Commit is omitted because Applications will work on a cache of
// the state bound to the multiplexer.
}

// Extension is the interface implemented by application-specific extensions.
type Extension interface {
// Methods returns the list of supported methods.
Methods() []transaction.MethodName

// OnRegister is the function that is called when the Application
// is registered with the multiplexer instance.
OnRegister(ApplicationState, MessageDispatcher)

// 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.
BeginBlock(*Context) error

// EndBlock signals the end of a block.
//
// Note: Errors are irrecoverable and will result in a panic.
EndBlock(*Context) error
}
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
}
}

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
Loading