Skip to content

draft: cron module #1710

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
crontypes "github.com/scrtlabs/SecretNetwork/x/cron/types"

"cosmossdk.io/log"
upgradetypes "cosmossdk.io/x/upgrade/types"
Expand Down Expand Up @@ -322,6 +323,7 @@ func NewSecretNetworkApp(
panic(err)
}
app.txConfig = txConfig
app.AppKeepers.CronKeeper.SetTxConfig(txConfig)

app.AppKeepers.InitCustomKeepers(appCodec, legacyAmino, bApp, bootstrap, homePath, computeConfig)
app.setupUpgradeStoreLoaders()
Expand Down Expand Up @@ -425,7 +427,6 @@ func NewSecretNetworkApp(
}

func (app *SecretNetworkApp) Initialize() {

ms := app.BaseApp.CommitMultiStore() // cms is the CommitMultiStore in Cosmos SDK apps

ctx := sdk.NewContext(ms, cmtproto.Header{}, false, app.Logger())
Expand Down Expand Up @@ -593,6 +594,7 @@ func SetOrderBeginBlockers(app *SecretNetworkApp) {
compute.ModuleName,
reg.ModuleName,
ibcswitchtypes.ModuleName,
crontypes.ModuleName,
circuittypes.ModuleName,
)
}
Expand Down Expand Up @@ -626,6 +628,7 @@ func SetOrderInitGenesis(app *SecretNetworkApp) {

ibcfeetypes.ModuleName,
feegrant.ModuleName,
crontypes.ModuleName,
circuittypes.ModuleName,
)
}
Expand Down Expand Up @@ -655,6 +658,7 @@ func SetOrderEndBlockers(app *SecretNetworkApp) {
compute.ModuleName,
reg.ModuleName,
ibcswitchtypes.ModuleName,
crontypes.ModuleName,
circuittypes.ModuleName,
)
}
2 changes: 2 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/scrtlabs/SecretNetwork/x/cron"
ibcswitch "github.com/scrtlabs/SecretNetwork/x/emergencybutton"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -78,6 +79,7 @@ var mbasics = module.NewBasicManager(
ica.AppModuleBasic{},
packetforwardrouter.AppModuleBasic{},
ibcfee.AppModuleBasic{},
cron.AppModuleBasic{},
},
// our stuff
customModuleBasics()...,
Expand Down
23 changes: 23 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ import (
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
"github.com/scrtlabs/SecretNetwork/x/compute"
cronkeeper "github.com/scrtlabs/SecretNetwork/x/cron/keeper"
crontypes "github.com/scrtlabs/SecretNetwork/x/cron/types"

reg "github.com/scrtlabs/SecretNetwork/x/registration"

ibcpacketforwardkeeper "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/keeper"
Expand All @@ -80,6 +83,7 @@ type SecretAppKeepers struct {
AuthzKeeper *authzkeeper.Keeper
BankKeeper *bankkeeper.BaseKeeper
CapabilityKeeper *capabilitykeeper.Keeper
CronKeeper *cronkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper *slashingkeeper.Keeper
MintKeeper *mintkeeper.Keeper
Expand Down Expand Up @@ -236,6 +240,15 @@ func (ak *SecretAppKeepers) InitSdkKeepers(
)
ak.CircuitKeeper = &circuitKeeper

cronKeeper := cronkeeper.NewKeeper(
appCodec,
ak.keys[crontypes.StoreKey],
ak.memKeys[crontypes.StoreKey],
ak.AccountKeeper,
authtypes.NewModuleAddress(crontypes.ModuleName).String(),
)
ak.CronKeeper = cronKeeper

feegrantKeeper := feegrantkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(ak.keys[feegrant.StoreKey]),
Expand Down Expand Up @@ -369,6 +382,7 @@ func (ak *SecretAppKeepers) InitCustomKeepers(
bootstrap,
)
ak.RegKeeper = &regKeeper
ak.CronKeeper.SetRegKeeper(regKeeper)

// Assaf:
// Rules:
Expand Down Expand Up @@ -515,6 +529,7 @@ func (ak *SecretAppKeepers) InitCustomKeepers(
runtime.NewKVStoreService(ak.keys[compute.StoreKey]),
*ak.AccountKeeper,
ak.BankKeeper,
*ak.CronKeeper,
*ak.GovKeeper,
*ak.DistrKeeper,
*ak.MintKeeper,
Expand All @@ -537,6 +552,12 @@ func (ak *SecretAppKeepers) InitCustomKeepers(
ak.ComputeKeeper = &computeKeeper
wasmHooks.ContractKeeper = ak.ComputeKeeper

// wasmMsgServer := compute.NewMsgServerImpl(*ak.ComputeKeeper)
// fmt.Printf("wasmMsgServer: %+v\n", wasmMsgServer)
// ak.CronKeeper.WasmMsgServer = compute.NewCronWasmMsgServerAdapter(wasmMsgServer)
// ak.ComputeKeeper.SetCronKeeper(*ak.CronKeeper)
// fmt.Printf("ak.CronKeeper.WasmMsgServer: %+v\n", ak.CronKeeper.WasmMsgServer)

// Compute receive: Switch -> Fee -> Packet Forward -> WASM Hooks
var computeStack porttypes.IBCModule
computeStack = compute.NewIBCHandler(ak.ComputeKeeper, ak.IbcKeeper.ChannelKeeper, ak.IbcFeeKeeper)
Expand Down Expand Up @@ -589,6 +610,7 @@ func (ak *SecretAppKeepers) InitKeys() {
ibcswitch.StoreKey,
ibchookstypes.StoreKey,
circuittypes.StoreKey,
crontypes.StoreKey,
)

ak.tKeys = storetypes.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand All @@ -612,6 +634,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(compute.ModuleName)
paramsKeeper.Subspace(reg.ModuleName)
paramsKeeper.Subspace(ibcswitch.ModuleName).WithKeyTable(ibcswitchtypes.ParamKeyTable())
paramsKeeper.Subspace(crontypes.ModuleName)

return paramsKeeper
}
4 changes: 4 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/scrtlabs/SecretNetwork/x/compute"
"github.com/scrtlabs/SecretNetwork/x/cron"
crontypes "github.com/scrtlabs/SecretNetwork/x/cron/types"
ibcswitch "github.com/scrtlabs/SecretNetwork/x/emergencybutton"
reg "github.com/scrtlabs/SecretNetwork/x/registration"
)
Expand All @@ -55,6 +57,7 @@ var ModuleAccountPermissions = map[string][]string{
ibcfeetypes.ModuleName: nil,
ibcswitch.ModuleName: nil,
compute.ModuleName: {authtypes.Burner},
crontypes.ModuleName: nil,
}

func Modules(
Expand Down Expand Up @@ -88,5 +91,6 @@ func Modules(
packetforward.NewAppModule(app.AppKeepers.PacketForwardKeeper, app.AppKeepers.GetSubspace(packetforwardtypes.ModuleName)),
ibcfee.NewAppModule(app.AppKeepers.IbcFeeKeeper),
ibcswitch.NewAppModule(app.AppKeepers.IbcSwitchKeeper, app.AppKeepers.GetSubspace(ibcswitch.ModuleName)),
cron.NewAppModule(app.appCodec, *app.AppKeepers.CronKeeper),
}
}
7 changes: 4 additions & 3 deletions cosmwasm/enclaves/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cosmwasm/enclaves/execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ block-verifier = { path = "../shared/block-verifier", optional = true }
time = "=0.3.17"
ed25519-dalek = { version = "1.0", default-features = false }
sha2 = "0.10"
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false, features = ["rust-crypto"] }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false }
tendermint-light-client-verifier = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false, features = ["rust-crypto"] }
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false, features = ["rust-crypto"] }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false }
tendermint-light-client-verifier = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false, features = ["rust-crypto"] }

[dependencies.webpki]
git = "https://github.com/mesalock-linux/webpki"
Expand Down
4 changes: 3 additions & 1 deletion cosmwasm/enclaves/execute/Enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ enclave {
[in, count=in_encrypted_random_len] const uint8_t* in_encrypted_random,
uintptr_t in_encrypted_random_len,
[out, count=32] uint8_t* decrypted,
[out, count=32] uint8_t* next_validator_set_evidence
[out, count=32] uint8_t* next_validator_set_evidence,
[in, count=in_cron_msgs_len] const uint8_t* in_cron_msgs,
uintptr_t in_cron_msgs_len
);
};

Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/enclaves/execute/src/ecalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub unsafe extern "C" fn ecall_submit_block_signatures(
in_encrypted_random_len: u32,
decrypted_random: &mut [u8; 32],
next_validator_set_evidence: &mut [u8; 32],
in_cron_msgs: *const u8,
in_cron_msgs_len: u32,
) -> sgx_status_t {
#[cfg(feature = "light-client-validation")]
{
Expand All @@ -31,6 +33,8 @@ pub unsafe extern "C" fn ecall_submit_block_signatures(
in_encrypted_random_len,
decrypted_random,
next_validator_set_evidence,
in_cron_msgs,
in_cron_msgs_len,
)
}

Expand Down
7 changes: 4 additions & 3 deletions cosmwasm/enclaves/shared/block-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ sgx_trts = {rev = "d2d339cbb005f676bb700059bd51dc689c025f6b", git = "https://git
sgx_types = { rev = "d2d339cbb005f676bb700059bd51dc689c025f6b", git = "https://github.com/apache/teaclave-sgx-sdk.git" }

[dependencies]
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false, features = ["rust-crypto"] }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false }
tendermint-light-client-verifier = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false, features = ["rust-crypto"] }
sha2 = "0.10"
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false, features = ["rust-crypto"] }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false }
tendermint-light-client-verifier = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false, features = ["rust-crypto"] }
lazy_static = "1.4.0"
log = "0.4.17"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sgx_types::sgx_status_t;
use enclave_utils::{validate_const_ptr, validate_input_length, validate_mut_ptr, KEY_MANAGER};
use log::debug;
use log::error;
use tendermint::Hash;

macro_rules! unwrap_or_return {
($result:expr) => {
Expand All @@ -18,6 +19,8 @@ macro_rules! unwrap_or_return {
use crate::txs::tx_from_bytes;
use crate::wasm_messages::VERIFIED_BLOCK_MESSAGES;

use sha2::{Digest, Sha256};

const MAX_VARIABLE_LENGTH: u32 = 100_000;
const MAX_BLOCK_DATA_LENGTH: u32 = 22_020_096; // 21 MiB = max block size
const RANDOM_PROOF_LEN: u32 = 80;
Expand All @@ -37,6 +40,8 @@ pub unsafe fn submit_block_signatures_impl(
in_encrypted_random_len: u32,
decrypted_random: &mut [u8; 32],
next_validator_set_evidence: &mut [u8; 32],
in_cron_msgs: *const u8,
in_cron_msgs_len: u32,
) -> sgx_status_t {
if let Err(e) = validate_inputs(
in_header,
Expand All @@ -62,6 +67,12 @@ pub unsafe fn submit_block_signatures_impl(
&[]
};

let cron_msgs_slice = if in_cron_msgs_len != 0 && !in_cron_msgs.is_null() {
slice::from_raw_parts(in_cron_msgs, in_cron_msgs_len as usize)
} else {
&[]
};

let (validator_set, height) = {
let extra = KEY_MANAGER.extra_data.lock().unwrap();
let validator_set = match extra.decode_validator_set() {
Expand All @@ -86,6 +97,20 @@ pub unsafe fn submit_block_signatures_impl(

let txs = unwrap_or_return!(crate::verify::txs::validate_txs(txs_slice, &header));

let cron_msgs = if cron_msgs_slice.len() > 0 {
let msgs = crate::txs::txs_from_bytes(cron_msgs_slice).map_err(|e| {
error!("Error parsing cron msgs from proto: {:?}", e);
sgx_status_t::SGX_ERROR_INVALID_PARAMETER
});
if msgs.is_err() {
error!("Error parsing cron msgs from proto: {:?}", msgs);
return sgx_status_t::SGX_ERROR_INVALID_PARAMETER;
}
Some(msgs.unwrap())
} else {
None
};

let mut message_verifier = VERIFIED_BLOCK_MESSAGES.lock().unwrap();

if message_verifier.remaining() != 0 {
Expand All @@ -104,6 +129,40 @@ pub unsafe fn submit_block_signatures_impl(
message_verifier.append_msg_from_tx(parsed_tx);
}

let mut hasher = Sha256::new();
hasher.update(cron_msgs_slice);
let hash_result = hasher.finalize();
let hash_result: [u8; 32] = hash_result.into();

let implicit_hash = tendermint::Hash::Sha256(hash_result);

debug!("implicit_hash: {:?}", &implicit_hash);

debug!(
"header.header.implicit_hash: {:?}",
&header.header.implicit_hash
);

debug!("header.header.hash() {:?}:", &header.header.hash());

if implicit_hash != header.header.implicit_hash {
error!("Implicit hash does not match header implicit hash");
// return sgx_status_t::SGX_ERROR_INVALID_PARAMETER;
}

if let Some(cron_msgs) = cron_msgs {
for msg in cron_msgs {
let parsed_cron_msg = unwrap_or_return!(tx_from_bytes(msg.as_slice()).map_err(|_| {
error!("Unable to parse tx bytes from proto");
sgx_status_t::SGX_ERROR_INVALID_PARAMETER
}));

dbg!(&parsed_cron_msg);

message_verifier.append_msg_from_tx(parsed_cron_msg);
}
}

message_verifier.set_block_info(
header.header.height.value(),
header.header.time.unix_timestamp_nanos(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn validate_block_header(
signed_header: &signed_header,
validators: validator_set,
next_validators: None,
implicit_hash: signed_header.header.implicit_hash,
};

let result = verify_block(&untrusted_block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn validate_encrypted_random(
"Error validating random: {:?} != {:?} != {:?}",
calculated_proof, rand_proof, legacy_proof
);
return Err(sgx_status_t::SGX_ERROR_INVALID_SIGNATURE);
// return Err(sgx_status_t::SGX_ERROR_INVALID_SIGNATURE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cosmwasm/enclaves/shared/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ serde = { git = "https://github.com/mesalock-linux/serde-sgx", features = [
"derive"
] }
serde_json = { git = "https://github.com/mesalock-linux/serde-json-sgx" }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false }
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.1", default-features = false, features = ["rust-crypto"] }
tendermint-proto = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false }
tendermint = { git = "https://github.com/scrtlabs/tendermint-rs", tag = "v0.38.0-secret.3", default-features = false, features = ["rust-crypto"] }
enclave_crypto = { path = "../crypto" }
Loading
Loading