Skip to content

Commit

Permalink
go/consensus/cometbft/apps/keymanager: Fix gas charge
Browse files Browse the repository at this point in the history
  • Loading branch information
peternose committed Apr 5, 2024
1 parent 407339c commit 7d649f9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 98 deletions.
115 changes: 59 additions & 56 deletions go/consensus/cometbft/apps/keymanager/churp/txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ func (ext *churpExt) create(ctx *tmapi.Context, req *churp.CreateRequest) error
// Prepare state.
state := churpState.NewMutableState(ctx.State())

// Charge gas for this operation.
params, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 29 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L29

Added line #L29 was not covered by tests
}
if err = ctx.Gas().UseGas(1, churp.GasOpCreate, params.GasCosts); err != nil {
return err

Check warning on line 32 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L32

Added line #L32 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil

Check warning on line 37 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L37

Added line #L37 was not covered by tests
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, req.RuntimeID)
if err != nil {
Expand Down Expand Up @@ -72,20 +86,6 @@ func (ext *churpExt) create(ctx *tmapi.Context, req *churp.CreateRequest) error
return nil
}

// Charge gas for this operation.
params, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, churp.GasOpCreate, params.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Create a new instance.
status := churp.Status{
Identity: req.Identity,
Expand Down Expand Up @@ -119,6 +119,20 @@ func (ext *churpExt) update(ctx *tmapi.Context, req *churp.UpdateRequest) error
// Prepare state.
state := churpState.NewMutableState(ctx.State())

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 125 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L125

Added line #L125 was not covered by tests
}
if err = ctx.Gas().UseGas(1, churp.GasOpUpdate, kmParams.GasCosts); err != nil {
return err

Check warning on line 128 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L128

Added line #L128 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil

Check warning on line 133 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L133

Added line #L133 was not covered by tests
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, req.RuntimeID)
if err != nil {
Expand Down Expand Up @@ -170,24 +184,11 @@ func (ext *churpExt) update(ctx *tmapi.Context, req *churp.UpdateRequest) error
status.Policy = *req.Policy
}

// Return early if this is a CheckTx context.
if ctx.IsCheckOnly() {
return nil
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, churp.GasOpUpdate, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

if err := state.SetStatus(ctx, status); err != nil {
ctx.Logger().Error("keymanager: churp: failed to set status",
"err", err,
Expand All @@ -206,6 +207,20 @@ func (ext *churpExt) apply(ctx *tmapi.Context, req *churp.SignedApplicationReque
// Prepare state.
state := churpState.NewMutableState(ctx.State())

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 213 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L213

Added line #L213 was not covered by tests
}
if err = ctx.Gas().UseGas(1, churp.GasOpApply, kmParams.GasCosts); err != nil {
return err

Check warning on line 216 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L216

Added line #L216 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, req.Application.RuntimeID)
if err != nil {
Expand Down Expand Up @@ -252,24 +267,11 @@ func (ext *churpExt) apply(ctx *tmapi.Context, req *churp.SignedApplicationReque
return fmt.Errorf("keymanager: churp: invalid signature: %w", err)
}

// Return early if this is a CheckTx context.
if ctx.IsCheckOnly() {
return nil
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, churp.GasOpApply, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ok, as far as we can tell the application is valid, apply it.
if status.Applications == nil {
status.Applications = make(map[signature.PublicKey]churp.Application)
Expand Down Expand Up @@ -297,6 +299,20 @@ func (ext *churpExt) confirm(ctx *tmapi.Context, req *churp.SignedConfirmationRe
// Prepare state.
state := churpState.NewMutableState(ctx.State())

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, churp.GasOpConfirm, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, req.Confirmation.RuntimeID)
if err != nil {
Expand Down Expand Up @@ -360,24 +376,11 @@ func (ext *churpExt) confirm(ctx *tmapi.Context, req *churp.SignedConfirmationRe
return fmt.Errorf("keymanager: churp: invalid signature: %w", err)
}

// Return early if this is a CheckTx context.
if ctx.IsCheckOnly() {
return nil

Check warning on line 381 in go/consensus/cometbft/apps/keymanager/churp/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/churp/txs.go#L381

Added line #L381 was not covered by tests
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, churp.GasOpConfirm, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Update application.
status.Applications[nodeID] = churp.Application{
Checksum: app.Checksum,
Expand Down
85 changes: 43 additions & 42 deletions go/consensus/cometbft/apps/keymanager/secrets/txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ func (ext *secretsExt) updatePolicy(
state *secretsState.MutableState,
sigPol *secrets.SignedPolicySGX,
) error {
// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 27 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L27

Added line #L27 was not covered by tests
}
if err = ctx.Gas().UseGas(1, secrets.GasOpUpdatePolicy, kmParams.GasCosts); err != nil {
return err

Check warning on line 30 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L30

Added line #L30 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil

Check warning on line 35 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L35

Added line #L35 was not covered by tests
}

// Ensure that the runtime exists and is a key manager.
regState := registryState.NewMutableState(ctx.State())
kmRt, err := common.KeyManagerRuntime(ctx, sigPol.Policy.ID)
Expand Down Expand Up @@ -51,24 +65,11 @@ func (ext *secretsExt) updatePolicy(
return err
}

// Return early if this is a CheckTx context.
if ctx.IsCheckOnly() {
return nil
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, secrets.GasOpUpdatePolicy, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ok, as far as we can tell the new policy is valid, apply it.
//
// Note: The key manager cohort responsible for servicing this ID
Expand Down Expand Up @@ -132,6 +133,20 @@ func (ext *secretsExt) publishMasterSecret(
state *secretsState.MutableState,
secret *secrets.SignedEncryptedMasterSecret,
) error {
// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 139 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L139

Added line #L139 was not covered by tests
}
if err = ctx.Gas().UseGas(1, secrets.GasOpPublishMasterSecret, kmParams.GasCosts); err != nil {
return err

Check warning on line 142 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L142

Added line #L142 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, secret.Secret.ID)
if err != nil {
Expand Down Expand Up @@ -183,20 +198,6 @@ func (ext *secretsExt) publishMasterSecret(
return nil
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, secrets.GasOpPublishMasterSecret, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ok, as far as we can tell the secret is valid, save it.
if err := state.SetMasterSecret(ctx, secret); err != nil {
ctx.Logger().Error("keymanager: failed to set key manager master secret",
Expand Down Expand Up @@ -230,6 +231,20 @@ func (ext *secretsExt) publishEphemeralSecret(
state *secretsState.MutableState,
secret *secrets.SignedEncryptedEphemeralSecret,
) error {
// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err

Check warning on line 237 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L237

Added line #L237 was not covered by tests
}
if err = ctx.Gas().UseGas(1, secrets.GasOpPublishEphemeralSecret, kmParams.GasCosts); err != nil {
return err

Check warning on line 240 in go/consensus/cometbft/apps/keymanager/secrets/txs.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/cometbft/apps/keymanager/secrets/txs.go#L240

Added line #L240 was not covered by tests
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ensure that the runtime exists and is a key manager.
kmRt, err := common.KeyManagerRuntime(ctx, secret.Secret.ID)
if err != nil {
Expand Down Expand Up @@ -274,20 +289,6 @@ func (ext *secretsExt) publishEphemeralSecret(
return nil
}

// Charge gas for this operation.
kmParams, err := state.ConsensusParameters(ctx)
if err != nil {
return err
}
if err = ctx.Gas().UseGas(1, secrets.GasOpPublishEphemeralSecret, kmParams.GasCosts); err != nil {
return err
}

// Return early if simulating since this is just estimating gas.
if ctx.IsSimulation() {
return nil
}

// Ok, as far as we can tell the secret is valid, save it.
if err := state.SetEphemeralSecret(ctx, secret); err != nil {
ctx.Logger().Error("keymanager: failed to set key manager ephemeral secret",
Expand Down

0 comments on commit 7d649f9

Please sign in to comment.