-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cheatcodes): base implementation of cheatcode system (#58)
## Description This PR introduces cheatcodes in Kythera. The inspiration for what the system allows is heavily inspired from [Foundry's cheatcodes](https://book.getfoundry.sh/cheatcodes/). ### Changelog **Major** - We now implement most of the components from the `ref-fvm` repo as we need to have granular control over them for the cheatcode system to work. This implementation is heavily inspired from [the conformance test framework](https://github.com/filecoin-project/ref-fvm/tree/master/testing/conformance) from @Stebalian on the `ref-fvm` - `KytheraMachine`: Stores override values. - `KytheraCallManager`: Set override values when detects a `send()` syscall to actor Id 98 (cheatcodes actor). - `KytheraKernel`: Actually override values at call time. - Introducing an `OverrideContext` structure that allows us to store at the `KytheraMachine` level different value to alter information when cheatcodes are invoked. - Deploying a _Cheatcode Actor_ at Actor Id 98. It contains entrypoints corresponding to the different cheatcodes we want to be available to our users. - Updated the way we compile actors to get binaries. Now, all actors can be found under `<project-root>/actors`. In this folder we find both `actors` (available at all time) and `test-actors` (only available if `feature=["testing"]` is enabled). This allows to have some binaries built and used in our `kythera-lib` without actually building everything everytime. **Minor** - Actor are no longer deployed at a random ID. Instead, we first create an address from their new and register it on-chain. - Set default Kythera chain Id to _1312_ ## Links to any relevant issues or pull requests Closes #22 ## Change checklist - [X] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [X] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: João Oliveira <[email protected]> Co-authored-by: PhilippeMts <[email protected]>
- Loading branch information
1 parent
9fc5e2e
commit de142e0
Showing
51 changed files
with
1,917 additions
and
197 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 +1,12 @@ | ||
[package] | ||
name = "kythera_test_actors" | ||
name = "kythera-actors" | ||
description = "Kythera FVM WASM actor builder" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
authors = ["Polyphene"] | ||
publish = false | ||
|
||
[features] | ||
default=[] | ||
testing=[] |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Cheatcodes Test Actor | ||
|
||
This is the actor embedded in our `kythera-fvm` to expose the cheatcodes interface. | ||
|
||
### Cheatcodes | ||
|
||
The following cheatcodes are exposed through the actor: | ||
- `Epoch`: Set the `NetworkContext::epoch` | ||
- `Warp`: Set the `NetworkContext::timestamp` | ||
- `Fee`: Set the `NetworkContext::fee` | ||
- `ChaindId`: Set the `NetworkContext::chain_id` | ||
- `Prank`: Sets the **next implicit message**'s `MessageContext::caller` to be the input address | ||
- `Trick`: Sets the **next implicit message and its sub-implicit messages**' `MessageContext::origin` to be the input address |
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,114 @@ | ||
// Copyright 2023 Polyphene. | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use frc42_dispatch::match_method; | ||
use fvm_ipld_encoding::{de::DeserializeOwned, RawBytes}; | ||
use fvm_sdk::NO_DATA_BLOCK_ID; | ||
use fvm_shared::address::Address; | ||
use fvm_shared::error::ExitCode; | ||
|
||
/// Deserialize message parameters into given struct. | ||
pub fn deserialize_params<D: DeserializeOwned>(params: u32) -> D { | ||
let params = fvm_sdk::message::params_raw(params) | ||
.expect("Could not get message parameters") | ||
.expect("Expected message parameters but got none"); | ||
|
||
let params = RawBytes::new(params.data); | ||
|
||
params | ||
.deserialize() | ||
.expect("Should be able to deserialize message params into arguments of called method") | ||
} | ||
|
||
#[no_mangle] | ||
fn invoke(input: u32) -> u32 { | ||
let method_num = fvm_sdk::message::method_number(); | ||
match_method!( | ||
method_num, | ||
{ | ||
"Warp" => { | ||
// Ensure that the message params can be deserialized. | ||
let new_timestamp: u64 = deserialize_params(input); | ||
|
||
Warp(new_timestamp); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
"Epoch" => { | ||
// Ensure that the message params can be deserialized. | ||
let new_epoch: i64 = deserialize_params(input); | ||
|
||
Epoch(new_epoch); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
"Fee" => { | ||
// Ensure that the message params can be deserialized. | ||
let (lo, hi): (u64, u64) = deserialize_params(input); | ||
|
||
Fee( | ||
fvm_shared::sys::TokenAmount { | ||
lo, | ||
hi | ||
} | ||
); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
"ChainId" => { | ||
// Ensure that the message params can be deserialized. | ||
let new_chain_id: u64 = deserialize_params(input); | ||
|
||
ChainId(new_chain_id); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
"Prank" => { | ||
// Ensure that the message params can be deserialized. | ||
let new_caller: Address = deserialize_params(input); | ||
|
||
Prank(new_caller); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
"Trick" => { | ||
// Ensure that the message params can be deserialized. | ||
let new_origin: Address = deserialize_params(input); | ||
|
||
Trick(new_origin); | ||
|
||
NO_DATA_BLOCK_ID | ||
}, | ||
_ => { | ||
fvm_sdk::vm::abort( | ||
ExitCode::USR_UNHANDLED_MESSAGE.value(), | ||
Some("Unknown method number"), | ||
); | ||
} | ||
} | ||
) | ||
} | ||
|
||
/// Warp the machine context to a given timestamp. | ||
#[allow(non_snake_case)] | ||
fn Warp(_new_timestamp: u64) {} | ||
|
||
/// Update the machine context to a given epoch. | ||
#[allow(non_snake_case)] | ||
fn Epoch(_new_epoch: i64) {} | ||
|
||
/// Update the base fee that's in effect when the machine runs. | ||
#[allow(non_snake_case)] | ||
fn Fee(_new_fee: fvm_shared::sys::TokenAmount) {} | ||
|
||
/// Set a new chain Id in the machine context. | ||
#[allow(non_snake_case)] | ||
fn ChainId(_new_chain_id: u64) {} | ||
|
||
/// Prank the call manager to set a pre-determined caller for the next message sent. | ||
#[allow(non_snake_case)] | ||
fn Prank(_new_caller: Address) {} | ||
|
||
/// Trick the call manager to set a pre-determined origin for the next message sent. | ||
#[allow(non_snake_case)] | ||
fn Trick(_new_origin: Address) {} |
File renamed without changes.
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,4 +1,4 @@ | ||
This crate contains the necessary logic to build some testing actors for our Kythera toolset. | ||
This crate contains the necessary logic to build actors for our Kythera toolset. | ||
|
||
It was heavily inspired and copied from the implementation [over the `ref-fvm`](https://github.com/filecoin-project/ref-fvm/tree/37643fc02f0342256afecff5158c43693b5ee4f0/testing/test_actors) | ||
done by @fridrik01. |
File renamed without changes.
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,28 @@ | ||
// Copyright 2023 Polyphene. | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
// Constants for wasm build artifacts. | ||
|
||
macro_rules! wasm_bin { | ||
($x: expr) => { | ||
concat!( | ||
env!("OUT_DIR"), | ||
"/bundle/wasm32-unknown-unknown/wasm/", | ||
$x, | ||
".wasm" | ||
) | ||
}; | ||
} | ||
|
||
pub const CHEATCODES_ACTOR_BINARY: &[u8] = include_bytes!(wasm_bin!("cheatcodes_actor")); | ||
|
||
#[cfg(feature = "testing")] | ||
pub mod test_actors { | ||
// Integration tests actors. | ||
pub const BASIC_TEST_ACTOR_BINARY: &[u8] = include_bytes!(wasm_bin!("basic_test_actor")); | ||
pub const BASIC_TARGET_ACTOR_BINARY: &[u8] = include_bytes!(wasm_bin!("basic_target_actor")); | ||
pub const BUILTINS_TEST_ACTOR_BINARY: &[u8] = include_bytes!(wasm_bin!("builtins_test_actor")); | ||
pub const CHEATCODES_TEST_ACTOR_BINARY: &[u8] = | ||
include_bytes!(wasm_bin!("cheatcodes_test_actor")); | ||
pub const CONSTRUCTOR_SETUP_TEST_ACTOR_BINARY: &[u8] = | ||
include_bytes!(wasm_bin!("constructor_setup_test_actor")); | ||
} |
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,16 @@ | ||
[package] | ||
name = "basic-target-actor" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish=false | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dependencies] | ||
frc42_dispatch = "3.1.0" | ||
fvm_sdk = { version = "3.0.0" } | ||
fvm_shared = { version = "3.1.0" } | ||
fvm_ipld_encoding = { version = "0.3.3" } | ||
serde = { version = "1.0.136", features = ["derive"] } | ||
thiserror = { version = "1.0.31" } | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
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,6 @@ | ||
## Basic Target Actor | ||
|
||
This is a basic actor that serves as a target actor in some of our tests for Kythera. It is the target actor used against | ||
our `cheatcodes_test_actor` for example. Its entrypoints are: | ||
- `Caller`: Method that returns the value of the `MessageContext.caller` | ||
- `Origin`: Method that returns the value of the `MessageContext.origin` |
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,52 @@ | ||
// Copyright 2023 Polyphene. | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use frc42_dispatch::match_method; | ||
use fvm_ipld_encoding::DAG_CBOR; | ||
use fvm_sdk as sdk; | ||
use fvm_shared::error::ExitCode; | ||
use sdk::sys::ErrorNumber; | ||
use serde::ser; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
enum IpldError { | ||
#[error("ipld encoding error: {0}")] | ||
Encoding(#[from] fvm_ipld_encoding::Error), | ||
#[error("ipld blockstore error: {0}")] | ||
Blockstore(#[from] ErrorNumber), | ||
} | ||
|
||
fn return_ipld<T>(value: &T) -> std::result::Result<u32, IpldError> | ||
where | ||
T: ser::Serialize + ?Sized, | ||
{ | ||
let bytes = fvm_ipld_encoding::to_vec(value)?; | ||
Ok(sdk::ipld::put_block(DAG_CBOR, bytes.as_slice())?) | ||
} | ||
|
||
#[no_mangle] | ||
fn invoke(_input: u32) -> u32 { | ||
let method_num = fvm_sdk::message::method_number(); | ||
match_method!( | ||
method_num, | ||
{ | ||
"Caller" => { | ||
let mc_caller: u64 = unsafe { fvm_sdk::sys::vm::message_context().unwrap().caller }; | ||
|
||
return_ipld(&mc_caller).unwrap() | ||
}, | ||
"Origin" => { | ||
let mc_origin: u64 = unsafe { fvm_sdk::sys::vm::message_context().unwrap().origin }; | ||
|
||
return_ipld(&mc_origin).unwrap() | ||
}, | ||
_ => { | ||
fvm_sdk::vm::abort( | ||
ExitCode::USR_UNHANDLED_MESSAGE.value(), | ||
Some("Unknown method number"), | ||
); | ||
} | ||
} | ||
) | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...actors/actors/basic_test_actor/Cargo.toml → ...s/test_actors/basic_test_actor/Cargo.toml
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,5 +1,5 @@ | ||
[package] | ||
name = "basic_test_actor" | ||
name = "basic-test-actor" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish=false | ||
|
File renamed without changes.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ors/actors/builtins_test_actor/Cargo.toml → ...est_actors/builtins_test_actor/Cargo.toml
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.