Skip to content

Commit

Permalink
Merge pull request #5544 from oasisprotocol/peternose/trivial/ext-sec…
Browse files Browse the repository at this point in the history
…rets

go/keymanager: Move master and ephemeral secrets code to extension
  • Loading branch information
peternose authored Feb 7, 2024
2 parents e9236cb + 0ac48f4 commit 93ecca9
Show file tree
Hide file tree
Showing 50 changed files with 2,126 additions and 1,784 deletions.
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

0 comments on commit 93ecca9

Please sign in to comment.