Skip to content

Commit

Permalink
Merge pull request #1364 from scrtlabs/fix-enclave-msg-counter
Browse files Browse the repository at this point in the history
v1.8 emergency upgrade
  • Loading branch information
toml01 authored Mar 4, 2023
2 parents 770004b + a10b181 commit 473b065
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
v1_5 "github.com/scrtlabs/SecretNetwork/app/upgrades/v1.5"
v1_6 "github.com/scrtlabs/SecretNetwork/app/upgrades/v1.6"
v1_7 "github.com/scrtlabs/SecretNetwork/app/upgrades/v1.7"
v1_8 "github.com/scrtlabs/SecretNetwork/app/upgrades/v1.8"
icaauthtypes "github.com/scrtlabs/SecretNetwork/x/mauth/types"

"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -118,7 +119,7 @@ var (
distrtypes.ModuleName: true,
}

Upgrades = []upgrades.Upgrade{v1_3.Upgrade, v1_4.Upgrade, v1_5.Upgrade, v1_6.Upgrade, v1_7.Upgrade}
Upgrades = []upgrades.Upgrade{v1_3.Upgrade, v1_4.Upgrade, v1_5.Upgrade, v1_6.Upgrade, v1_7.Upgrade, v1_8.Upgrade}
)

// Verify app interface at compile time
Expand Down
35 changes: 35 additions & 0 deletions app/upgrades/v1.8/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v1_8

import (
"fmt"

store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/scrtlabs/SecretNetwork/app/keepers"
"github.com/scrtlabs/SecretNetwork/app/upgrades"
)

const upgradeName = "v1.8"

var Upgrade = upgrades.Upgrade{
UpgradeName: upgradeName,
CreateUpgradeHandler: createUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{},
}

func createUpgradeHandler(mm *module.Manager, keepers *keepers.SecretAppKeepers, configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info(` _ _ _____ _____ _____ _____ ______ `)
ctx.Logger().Info(`| | | | __ \ / ____| __ \ /\ | __ \| ____|`)
ctx.Logger().Info(`| | | | |__) | | __| |__) | / \ | | | | |__ `)
ctx.Logger().Info(`| | | | ___/| | |_ | _ / / /\ \ | | | | __| `)
ctx.Logger().Info(`| |__| | | | |__| | | \ \ / ____ \| |__| | |____ `)
ctx.Logger().Info(` \____/|_| \_____|_| \_\/_/ \_\_____/|______|`)

ctx.Logger().Info(fmt.Sprintf("Running module migrations for %s...", upgradeName))
return mm.RunMigrations(ctx, configurator, vm)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ pub fn init(
debug!("New random: {:?}", versioned_env.get_random());
}

update_msg_counter(block_height);
//let start = Instant::now();
let result = engine.init(&versioned_env, validated_msg);
// let duration = start.elapsed();
// trace!("Time elapsed in engine.init: {:?}", duration);

update_msg_counter(block_height);
*used_gas = engine.gas_used();

let output = result?;
Expand Down Expand Up @@ -322,10 +322,10 @@ pub fn handle(

versioned_env.set_contract_hash(&contract_hash);

update_msg_counter(block_height);
let result = engine.handle(&versioned_env, validated_msg, &parsed_handle_type);

*used_gas = engine.gas_used();
update_msg_counter(block_height);

let mut output = result?;

Expand Down
8 changes: 6 additions & 2 deletions cosmwasm/enclaves/shared/contract-engine/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ pub fn derive_random(seed: &Binary, contract_key: &ContractKey, height: u64) ->
pub fn update_msg_counter(height: u64) {
let mut counter = MSG_COUNTER.lock().unwrap();

counter.height = height;
counter.counter += 1;
if counter.height != height {
counter.height = height;
counter.counter = 0;
} else {
counter.counter += 1;
}

trace!("counter incremented to: {:?}", counter);
}
13 changes: 13 additions & 0 deletions docs/proposals/v1.8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This proposal proposes that the chain elect to do an **EMERGENCY** software upgrade to the v1.8 software version of the Secret Network codebase on block **7,760,000**, which is estimated to occur on **Saturday, March 4, 2023 at ~3pm UTC**. Block times have high variance, so please monitor the chain for more precise time estimates.

## Emergency Upgrade Reason

After the upgrade to v1.7 on March 1, many node runners experienced node crashes with an apphash error either when starting from state sync or after restarting their node's process. Upon conducting a root cause analysis, SCRT Labs discovered a bug in our v1.7 code.

The bug was caused by the introduction of a new encryption scheme in v1.7. Specifically, a unique salt was added to the encryption of each storage write, which includes a counter of contract calls within each block. This counter only exists in the enclave's memory, and the bug caused it to never reset when a new block began. As a result, restarting a node or starting from state sync caused the counter to become out of sync with all the nodes that had not been restarted after the v1.7 upgrade. Consequently, this resulted in a different encryption output which is part of consensus.

While this bug still exists, existing nodes cannot restart their process and new nodes cannot join the network.

## Upgrade Instructions

See [docs.scrt.network](https://docs.scrt.network/secret-network-documentation/infrastructure/upgrade-instructions/v1.8) for upgrade instructions.

0 comments on commit 473b065

Please sign in to comment.