Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Template Sections #254

Merged
merged 95 commits into from Jul 7, 2021
Merged

Template Sections #254

merged 95 commits into from Jul 7, 2021

Conversation

YaronWittenstein
Copy link
Contributor

@YaronWittenstein YaronWittenstein commented Jul 3, 2021

Motivation

This PR rewrites the way a Template is represented and being used.
The main trigger for that change was the decision to store pieces of data that could have been otherwise stored off-chain.

Here are 2 examples:

Schema

Contains the high-level definitions of the persistent storage of a Template.
Each variable has an id, a name and a type (primitive or composite`) associated with it.

The Schema isn't required for the execution of a transaction.
From SVM's Runtime perspective - it only cares about the raw data.
This data contains the raw Layout of raw variables.

A raw variable has an id, offset and byte size. It has neither name nor a type.
Each raw variable represents a primitive. For example, an 5-items Array defined under the Schema will be translated
into 5 raw variables under the Layout.

API

Similarly, SVM's core doesn't need to hold the high-level API of a Template in order to execute a transaction.

It already has the Exports defined under a Wasm program.
Of course, an Export function is very low-level but that info is a non-concern for a user using smapp / similar

Additionally, each deployed Template should store its deployment content.
Details such as the layer of the deployment or the Account that sent the transaction.
This kind of information is a must-have for Transactions Historical Explorers but in theory part of this can be also used for core processes inside SVM (in future versions upon arithmetical changes).

Sections

Generally, SVM needs to retrieves different pieces of data in different contexts.
We can roughly categorize the content as Core and Non-Core.

It's pivotal to be able to fetch only the required data as fast as we can.
Secondly, it's crucial to make the format of a Template easy to evolve in the future.

The solution introduced in this PR is called the Template Sections.
Instead of looking at a Template as a single unit - from now on, we'll consider a Template
as a collection of independent Section. Each Section will serve as single purpose and belong to either the Core or Non-Core category.

The Template is an evolving entity. There is the pre-deploy stage and post-deploy stages.
Instead of having something such as PreDeployTemplate and PostDeployTemplate in the code base - deploying a Template means adding a Deploy Section to a collection of Section.

If in the future we'll add a removal feature for a Template - we could come up with a new Removal Section or similar.

Moving into the Sections design, we can now go further and store groups of Sections in different indexes very naturally.
Upon fetch, we should query the required indexes and get the corresponding Sections back.

This PR also implements a skip Section functionality when loading data.

Each Section is prefixed with a Section Preview, so when decoding the raw fetched data, we can ask to skip Sections that we don't want and only decode the ones we want.

To summarize, we get 2 persistence optimizations:

  • Store different groups of Sections under the same key of the underlying key-value database
  • Given a group of binary fetched Sections we can ask to decode only what the ones we're interested in and skip the rest.

Last notes:

  • SVM will probably consider the Non-Core Sections as optional when retrieving them.
    It means that the client asking for them will have to support a fallback for obtaining that data (from some off-chain service).
    However, for Genesis it's seems fair that each Template will store each optional Section.

  • A Future PR might be using the Sections building blocks for an App as well.

@avive @lrettig This PR is the outcome of our previous Node API changes talks

Implemented the `svm_allocate` host function.

adding some debugging printing to the new `svm_allocate` host function.

WIP: computing the `returns` byte-size for a function (sdk-macros)

* `calldata` is allocated on instance only when non-empty.

The `svm-sdk` relies on the host function for Wasm targeted code and on the default global allocator otherwise.

adding `svm_sdk_std::panic` (aborts for Wasm and unwinds otherwise)

WIP: adding an explicit feature flag for the host static allocation.

svm-codec is using the default global-alloactor (not the "static allocation")

codec - js tests pass (commenting the `verify_data` for now)

tests pass

CI - running only on Windows + running the tests 10 times (there seems to be a sporadic failures)

CI - skipping the build stage

svm-sdk-std: exposing Rust `Vec`

svm-sdk-std: using `panic!`

running only the `svm-runtime` crate tests.

Removing `wabt` (using only `wat`) - it takes much less time to build.

using cache action v2

Adding GitHub Action for LLVM

Adding caching to the llvm

LLVM 10

Returning back the original LLVM action for Windows

adding feature-flags `default-cranelift` and `default-llvm`

trying to run CI against `llvm`

running tests with a single thread

runtime - removing two ignored tests related to gas-metering

Commenting a test

runtime: ignoring all tests except one

running tests in dev-mode

Building on Linux and macOS

svm-sdk-std: returning back Rust `Vec`

Commenting part of the problematic test

Commenting another part of the problematic test

uncomment part of the problematic test

cbindgen version bump

problemtic test - wasm input file "runtime_calldata.wasm" isn't using the "static-alloc"

problemtic test: input wasm is being compiled with less code and no `.cargo/config` file

Uncommenting all the problematic test

Trying to locate the bug

Trying again to pinpoint the bug

Adding asserts against setting explicitly empty `returndata`

WIP: debugging

WIP: debugging...

WIP: debugging...

skipping the last CI stages for now...

WIP: trying to locate the cause of the bug...

WIP

commenting the "static-alloc" related code from `svm-sdk-alloc`

WIP

Trying again to make the test fail

Trying to figure out whether the #[endpoint] return-type has anything to do with the bug...

Commenting "Storage" from the wasm input of the failing test...

More debugging the root cause of the problem

debugging...

adding "Cache workspace" step

Splitting cargo caching into isloated steps.

adding "Dump GitHub context" step.

CI: changing to `crates/runtime` before running the tests

Try again to run CI...

Try again

CI: disabling LLVM installation for now (Windows)

Trying again to reproduce a broken CI on Windows...

Checking whether the bug has something to do with `svm_sdk::Address`

Trying to find a minimal failing input using `Amount`

WIP: Debugging...

WIP: removing the parameter from the problematic endpoint

This should pass (not using `endpoint`)

This should fail again...

`ExtHost#get_calldata` - returning a static data

Re-compile the wasm input

Empty `endpoint` prologue.

debugging

debugging...

Trying to narrow the bug

WIP

Narrowing the epilogue part

should fail...

Uncommenting part of the epilogue

WIP
@YaronWittenstein YaronWittenstein added this to In progress in SVM 0.3 Jul 3, 2021
@YaronWittenstein YaronWittenstein force-pushed the template-sections branch 2 times, most recently from d2e35d6 to a7d270a Compare July 4, 2021 14:36
@neysofu
Copy link
Contributor

neysofu commented Jul 5, 2021

Reviewing this now, let's see what I can learn.

@YaronWittenstein YaronWittenstein modified the milestones: June 21 Sprint, Aug 2 Sprint, July 5 Sprint Jul 5, 2021
@YaronWittenstein YaronWittenstein moved this from In progress to Pending Review in SVM 0.3 Jul 7, 2021
Copy link
Contributor

@neysofu neysofu left a comment

Choose a reason for hiding this comment

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

Awesome to see these many comments. I haven't looked at everything as it deals with a lot of stuff that I'm not familiar with, but it seems good to me

Comment on lines 16 to 50
///
/// # Example
///
/// ```rust
/// use std::io::Cursor;
///
/// use svm_types::Template;
/// use svm_codec::api::builder::DeployTemplateBuilder;
/// use svm_codec::template;
///
/// let layout = vec![5, 10].into();
/// let ctors = vec!["init".to_string()];
///
/// let bytes = DeployTemplateBuilder::new()
/// .with_version(0)
/// .with_name("My Template")
/// .with_code(&[0xC, 0x0, 0xD, 0xE])
/// .with_layout(&layout)
/// .with_ctors(&ctors)
/// .build();
///
/// let mut cursor = Cursor::new(&bytes[..]);
/// let actual = template::decode_deploy_template(&mut cursor).unwrap();
///
/// let expected = Template {
/// version: 0,
/// name: "My Template".to_string(),
/// code: vec![0xC, 0x0, 0xD, 0xE],
/// layout,
/// ctors: vec!["init".to_string()]
/// };
///
/// assert_eq!(expected, actual);
/// ```
///
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should keep this if possible. It'd still be relevant after updating it with the appropriate naming changes, right?

crates/codec/src/section/kind.rs Show resolved Hide resolved
@YaronWittenstein YaronWittenstein merged commit e661106 into master Jul 7, 2021
@YaronWittenstein YaronWittenstein deleted the template-sections branch July 7, 2021 13:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request svm
Projects
No open projects
SVM 0.3
Pending Review
Development

Successfully merging this pull request may close these issues.

None yet

2 participants