-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5790 from oasisprotocol/peternose/trivial/remove-…
…churp-flag go/registry: Deprecate enable key manager CHURP flag
- Loading branch information
Showing
25 changed files
with
394 additions
and
95 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
go/registry: Deprecate enable key manager CHURP flag | ||
|
||
Removes the code previously necessary to enable the key manager CHURP | ||
extension. |
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,27 +1,89 @@ | ||
package churp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cometbft/cometbft/abci/types" | ||
|
||
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api" | ||
churpState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/keymanager/churp/state" | ||
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/keymanager/common" | ||
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api" | ||
"github.com/oasisprotocol/oasis-core/go/keymanager/churp" | ||
) | ||
|
||
// InitChain implements api.Extension. | ||
func (ext *churpExt) InitChain(ctx *tmapi.Context, _ types.RequestInitChain, _ *genesis.Document) error { | ||
if enabled, err := ext.enabled(ctx); err != nil || !enabled { | ||
func (ext *churpExt) InitChain(ctx *tmapi.Context, _ types.RequestInitChain, doc *genesis.Document) error { | ||
// Ensure compatibility with Eden genesis file. | ||
st := doc.KeyManager.Churp | ||
if st == nil { | ||
return nil | ||
} | ||
|
||
// Insert consensus parameters. | ||
state := churpState.NewMutableState(ctx.State()) | ||
|
||
if err := state.SetConsensusParameters(ctx, &churp.DefaultConsensusParameters); err != nil { | ||
return fmt.Errorf("cometbft/keymanager/churp: failed to set consensus parameters: %w", err) | ||
} | ||
|
||
// Fetch runtimes. | ||
epoch, err := ext.state.GetCurrentEpoch(ctx) | ||
if err != nil { | ||
return fmt.Errorf("cometbft/keymanager/churp: failed to get current epoch: %w", err) | ||
} | ||
runtimes := common.RegistryRuntimes(ctx, doc, epoch) | ||
|
||
// Insert statuses. | ||
for _, status := range st.Statuses { | ||
if _, ok := runtimes[status.RuntimeID]; !ok { | ||
return fmt.Errorf("cometbft/keymanager/churp: unknown key manager runtime: %s", status.RuntimeID) | ||
} | ||
|
||
// Disable handoffs for all instances. | ||
status.NextHandoff = churp.HandoffsDisabled | ||
status.NextChecksum = nil | ||
status.Applications = nil | ||
|
||
// Schedule the next handoff at the beginning of the next epoch. | ||
if status.HandoffInterval != 0 { | ||
status.NextHandoff = epoch + 1 | ||
} | ||
|
||
if err := state.SetStatus(ctx, status); err != nil { | ||
return fmt.Errorf("cometbft/keymanager/churp: failed to set status: %w", err) | ||
} | ||
|
||
ctx.EmitEvent(tmapi.NewEventBuilder(ext.appName).TypedAttribute(&churp.UpdateEvent{ | ||
Status: status, | ||
})) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Genesis implements Query. | ||
func (q *querier) Genesis(ctx context.Context) (*churp.Genesis, error) { | ||
parameters, err := q.state.ConsensusParameters(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
statuses, err := q.state.AllStatuses(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Disable handoffs for all instances. | ||
for _, status := range statuses { | ||
status.NextHandoff = churp.HandoffsDisabled | ||
status.NextChecksum = nil | ||
status.Applications = nil | ||
} | ||
|
||
gen := churp.Genesis{ | ||
Parameters: *parameters, | ||
Statuses: statuses, | ||
} | ||
return &gen, nil | ||
} |
193 changes: 193 additions & 0 deletions
193
go/consensus/cometbft/apps/keymanager/churp/genesis_test.go
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 |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package churp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cometbft/cometbft/abci/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" | ||
"github.com/oasisprotocol/oasis-core/go/common" | ||
"github.com/oasisprotocol/oasis-core/go/common/cbor" | ||
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash" | ||
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature" | ||
"github.com/oasisprotocol/oasis-core/go/common/node" | ||
"github.com/oasisprotocol/oasis-core/go/common/sgx" | ||
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api" | ||
churpState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/keymanager/churp/state" | ||
"github.com/oasisprotocol/oasis-core/go/genesis/api" | ||
keymanager "github.com/oasisprotocol/oasis-core/go/keymanager/api" | ||
"github.com/oasisprotocol/oasis-core/go/keymanager/churp" | ||
registry "github.com/oasisprotocol/oasis-core/go/registry/api" | ||
) | ||
|
||
var ( | ||
kmRuntimeID common.Namespace | ||
_ = kmRuntimeID.UnmarshalHex("c000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff") | ||
) | ||
|
||
func createTestRegistryGenesis() registry.Genesis { | ||
return registry.Genesis{ | ||
Parameters: registry.ConsensusParameters{ | ||
DebugAllowTestRuntimes: true, | ||
EnableRuntimeGovernanceModels: map[registry.RuntimeGovernanceModel]bool{ | ||
registry.GovernanceEntity: true, | ||
}, | ||
}, | ||
Runtimes: []*registry.Runtime{ | ||
{ | ||
Versioned: cbor.NewVersioned(registry.LatestRuntimeDescriptorVersion), | ||
ID: kmRuntimeID, | ||
EntityID: signature.PublicKey{}, | ||
Kind: registry.KindKeyManager, | ||
TEEHardware: node.TEEHardwareIntelSGX, | ||
Deployments: []*registry.VersionInfo{ | ||
{ | ||
TEE: cbor.Marshal(node.SGXConstraints{ | ||
Enclaves: []sgx.EnclaveIdentity{{}}, | ||
}), | ||
}, | ||
}, | ||
AdmissionPolicy: registry.RuntimeAdmissionPolicy{ | ||
EntityWhitelist: ®istry.EntityWhitelistRuntimeAdmissionPolicy{ | ||
Entities: map[signature.PublicKey]registry.EntityWhitelistConfig{}, | ||
}, | ||
}, | ||
GovernanceModel: registry.GovernanceEntity, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createTestChurpGenesis() *churp.Genesis { | ||
return &churp.Genesis{ | ||
Parameters: churp.ConsensusParameters{ | ||
GasCosts: churp.DefaultGasCosts, | ||
}, | ||
Statuses: []*churp.Status{ | ||
{ | ||
Identity: churp.Identity{ | ||
ID: 1, | ||
RuntimeID: kmRuntimeID, | ||
}, | ||
Threshold: 5, | ||
HandoffInterval: 0, | ||
NextHandoff: churp.HandoffsDisabled, | ||
}, | ||
{ | ||
Identity: churp.Identity{ | ||
ID: 2, | ||
RuntimeID: kmRuntimeID, | ||
}, | ||
Threshold: 10, | ||
HandoffInterval: 1, | ||
NextHandoff: 100, | ||
NextChecksum: &hash.Hash{1, 2, 3}, | ||
Applications: map[signature.PublicKey]churp.Application{ | ||
keymanager.InsecureRAK: { | ||
Checksum: hash.Hash{1, 2, 3}, | ||
Reconstructed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestInitChain(t *testing.T) { | ||
appState := abciAPI.NewMockApplicationState(&abciAPI.MockApplicationStateConfig{}) | ||
ctx := appState.NewContext(abciAPI.ContextInitChain) | ||
defer ctx.Close() | ||
|
||
state := churpState.NewMutableState(ctx.State()) | ||
app := &churpExt{ | ||
state: appState, | ||
} | ||
|
||
// Empty state. | ||
doc := api.Document{} | ||
err := app.InitChain(ctx, types.RequestInitChain{}, &doc) | ||
require.NoError(t, err, "failed to initialize empty state") | ||
|
||
params, err := state.ConsensusParameters(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, churp.ConsensusParameters{}, *params) | ||
|
||
statuses, err := state.AllStatuses(ctx) | ||
require.NoError(t, err) | ||
require.Empty(t, statuses) | ||
|
||
// Non-empty state. | ||
doc = api.Document{ | ||
KeyManager: keymanager.Genesis{ | ||
Churp: createTestChurpGenesis(), | ||
}, | ||
Registry: createTestRegistryGenesis(), | ||
} | ||
|
||
err = app.InitChain(ctx, types.RequestInitChain{}, &doc) | ||
require.NoError(t, err, "failed to initialize non-empty state") | ||
|
||
params, err = state.ConsensusParameters(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, churp.DefaultConsensusParameters, *params) | ||
|
||
statuses, err = state.AllStatuses(ctx) | ||
require.NoError(t, err) | ||
require.Len(t, statuses, 2) | ||
|
||
require.Equal(t, uint8(1), statuses[0].ID) | ||
require.Equal(t, uint8(5), statuses[0].Threshold) | ||
require.Equal(t, beacon.EpochTime(0), statuses[0].HandoffInterval) | ||
require.Equal(t, churp.HandoffsDisabled, statuses[0].NextHandoff) // Should be disabled. | ||
require.Nil(t, statuses[0].NextChecksum) | ||
require.Nil(t, statuses[0].Applications) | ||
|
||
require.Equal(t, uint8(2), statuses[1].ID) | ||
require.Equal(t, uint8(10), statuses[1].Threshold) | ||
require.Equal(t, beacon.EpochTime(1), statuses[1].HandoffInterval) | ||
require.Equal(t, beacon.EpochTime(1), statuses[1].NextHandoff) // Should be set. | ||
require.Nil(t, statuses[1].NextChecksum) | ||
require.Nil(t, statuses[1].Applications) | ||
} | ||
|
||
func TestGenesis(t *testing.T) { | ||
appState := abciAPI.NewMockApplicationState(&abciAPI.MockApplicationStateConfig{}) | ||
ctx := appState.NewContext(abciAPI.ContextEndBlock) | ||
defer ctx.Close() | ||
|
||
state := churpState.NewMutableState(ctx.State()) | ||
q := NewQuery(state.ImmutableState) | ||
|
||
// Empty state. | ||
g, err := q.Genesis(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, churp.ConsensusParameters{}, g.Parameters) | ||
require.Len(t, g.Statuses, 0) | ||
|
||
// Prepare state that should be exported into the expected genesis. | ||
genesis := createTestChurpGenesis() | ||
|
||
err = state.SetConsensusParameters(ctx, &genesis.Parameters) | ||
require.NoError(t, err) | ||
|
||
for _, status := range genesis.Statuses { | ||
err = state.SetStatus(ctx, status) | ||
require.NoError(t, err) | ||
} | ||
|
||
// Exported genesis disables handoffs for all instances | ||
for i := range genesis.Statuses { | ||
genesis.Statuses[i].NextHandoff = churp.HandoffsDisabled | ||
genesis.Statuses[i].NextChecksum = nil | ||
genesis.Statuses[i].Applications = nil | ||
} | ||
|
||
// Non-empty state. | ||
g, err = q.Genesis(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, genesis.Parameters, g.Parameters) | ||
require.EqualValues(t, genesis.Statuses, g.Statuses) | ||
} |
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
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
Oops, something went wrong.