Skip to content
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

runtime-sdk: Consensus module #92

Merged
merged 2 commits into from
Apr 29, 2021
Merged

runtime-sdk: Consensus module #92

merged 2 commits into from
Apr 29, 2021

Conversation

ptrus
Copy link
Member

@ptrus ptrus commented Apr 12, 2021

This PR:

  • adds generalized consensus message handling support
    • Since consensus emitted messages get executed after the round finishes the results need to be handled in the next round. Modules can now register message hooks that get called at the start of the next round. When emitting a message one now needs to specify a handler to be called after the message is processed (also supports passing arbitrary handler context).
  • adds low level consensus, and higher level consensus-accounts modules to the SDK
  • add a general Context trait so that module methods can support both TxContext and DispatchCtx
  • adds e2e test to test the consensus modules

TODO:

Only low-level staking support (emitting messages) is available currently. Adding a higher level support for staking (e.g. adding staking to consensus-accounts module) will be done later, as it requires oasisprotocol/oasis-core#3862.

Requires:

@codecov
Copy link

codecov bot commented Apr 12, 2021

Codecov Report

Merging #92 (123b667) into main (8baf071) will increase coverage by 3.93%.
The diff coverage is 66.77%.

❗ Current head 123b667 differs from pull request most recent head 48a93bf. Consider uploading reports for the commit 48a93bf to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   59.42%   63.36%   +3.93%     
==========================================
  Files          41       38       -3     
  Lines        1649     2001     +352     
==========================================
+ Hits          980     1268     +288     
- Misses        650      733      +83     
+ Partials       19        0      -19     
Impacted Files Coverage Δ
runtime-sdk/src/crypto/signature/secp256k1.rs 29.03% <0.00%> (ø)
runtime-sdk/src/dispatcher.rs 0.00% <0.00%> (ø)
runtime-sdk/src/module.rs 17.77% <0.00%> (-4.45%) ⬇️
runtime-sdk/src/runtime.rs 0.00% <0.00%> (ø)
tests/runtimes/simple-consensus/src/lib.rs 0.00% <0.00%> (ø)
tests/runtimes/simple-consensus/src/main.rs 0.00% <0.00%> (ø)
tests/runtimes/simple-keyvalue/src/keyvalue.rs 0.00% <ø> (ø)
tests/runtimes/simple-keyvalue/src/lib.rs 0.00% <ø> (ø)
runtime-sdk/src/modules/consensus_accounts/mod.rs 38.59% <38.59%> (ø)
runtime-sdk/src/context.rs 64.59% <51.76%> (-5.90%) ⬇️
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8baf071...48a93bf. Read the comment docs.

@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch from 2d76bcc to 02f3163 Compare April 13, 2021 06:39
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/dispatcher.rs Outdated Show resolved Hide resolved
runtime-sdk/src/dispatcher.rs Outdated Show resolved Hide resolved
runtime-sdk/src/dispatcher.rs Outdated Show resolved Hide resolved
tests/runtimes/simple-bank/Cargo.toml Outdated Show resolved Hide resolved
tests/runtimes/simple-bank/src/lib.rs Outdated Show resolved Hide resolved
tests/runtimes/simple-bank/src/lib.rs Outdated Show resolved Hide resolved
tests/runtimes/simple-bank/src/lib.rs Outdated Show resolved Hide resolved
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 5 times, most recently from afa2283 to 2be49e8 Compare April 14, 2021 11:55
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/module.rs Show resolved Hide resolved
@@ -19,7 +20,11 @@ pub trait Runtime {
/// Runtime version.
const VERSION: version::Version;

type Modules: AuthHandler + MigrationHandler + MethodRegistrationHandler + BlockHandler;
type Modules: AuthHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these are here and not trait bounds on Module. @kostko ?

It could be done either way and we always only use modules here so maybe we can move them (in a subsequent issue/PR of course).

@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 3 times, most recently from aaf3b10 to 9d3a69e Compare April 15, 2021 16:00
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 2 times, most recently from 7b1c9ed to 85de3fd Compare April 19, 2021 06:20
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
Comment on lines 72 to 78
const MODULE: &str = "dispatcher";

/// State schema constants.
pub mod state {
/// Map of message idx to message handlers for messages emitted in previous round.
pub const HANDLERS: &[u8] = &[0x01];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move this under the core module as that one currently has a similar thing for module migration metadata.

runtime-sdk/src/dispatcher.rs Outdated Show resolved Hide resolved
runtime-sdk/src/dispatcher.rs Outdated Show resolved Hide resolved
runtime-sdk/src/module.rs Show resolved Hide resolved
runtime-sdk/src/modules/accounts/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/types/address.rs Outdated Show resolved Hide resolved
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 2 times, most recently from 68d6c6c to 822ad2d Compare April 19, 2021 16:00
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch from 822ad2d to ddee57d Compare April 19, 2021 17:28
@kostko kostko linked an issue Apr 20, 2021 that may be closed by this pull request
@@ -31,21 +27,126 @@ pub enum Mode {
SimulateTx,
}

impl Mode {
/// Mode string representation.
pub fn to_str(&self) -> &'static str {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could implement the Display trait for Mode instead and then directly use mode below.

Copy link
Member Author

@ptrus ptrus Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So afaik to use it in the logger context as i do bellow (so that it's present in all log invocations) it needs to be threadsafe ('static, Send, Sync).

So I'm not sure if the display trait implementation will be useful. However I should probably move these strings into constants and use these to also implement the Display trait and maybe rename this method to to_static_str with a comment on why we need it.

Unless i missed something obvious?

@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 5 times, most recently from cf8e372 to 86b5a75 Compare April 23, 2021 14:30
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 6 times, most recently from 91f5525 to 76bb6bb Compare April 27, 2021 13:01
@ptrus ptrus marked this pull request as ready for review April 27, 2021 13:16
@ptrus ptrus requested a review from pro-wh as a code owner April 27, 2021 13:16
@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 3 times, most recently from 4440e5a to a35720c Compare April 27, 2021 15:43
@pro-wh
Copy link
Contributor

pro-wh commented Apr 27, 2021

ok, I think I'll have the tx_value stuff in my branch similar to how you've done the tx_auth_info and tx_caller_address, with Options

@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch 2 times, most recently from 05e4bf6 to a6f076a Compare April 28, 2021 05:59
runtime-sdk/src/context.rs Outdated Show resolved Hide resolved
runtime-sdk/src/context.rs Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/types/address.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
runtime-sdk/src/modules/consensus_accounts/mod.rs Outdated Show resolved Hide resolved
}
}

/// Runtime sdk context.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Runtime sdk context.
/// Runtime SDK context.

Amount: *quantity.NewFromUint64(50),
Denomination: types.Denomination("TEST"),
}
log.Info("charlie withdrawing")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also test with dave (secp256k1).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, done 👍

@ptrus ptrus force-pushed the ptrus/feature/consensus-sdk branch from aaa78a9 to 48a93bf Compare April 29, 2021 17:28
@ptrus ptrus merged commit 0c7672d into main Apr 29, 2021
@ptrus ptrus deleted the ptrus/feature/consensus-sdk branch April 29, 2021 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consensus layer interaction module
4 participants