Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Conversation

@bkchr
Copy link
Member

@bkchr bkchr commented Mar 5, 2019

Adds prerequisites for implementing validate_block in Cumulus.

@bkchr bkchr changed the title Implement validate_block for Cumulus Prerequisites for validate_block in Cumulus Mar 7, 2019
@bkchr bkchr requested a review from rphmeier March 7, 2019 13:02
@bkchr bkchr marked this pull request as ready for review March 7, 2019 13:02
Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

lgtm

memory.get(offset, length)
.map_err(|_| ErrorKind::Runtime.into())
Ok(val) => {
match filter_result(val, &memory)? {
Copy link
Contributor

Choose a reason for hiding this comment

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

outer block is not necessary anymore

@gavofyork gavofyork added A0-please_review Pull request needs code review. and removed A4-gotissues labels Mar 13, 2019
@gavofyork
Copy link
Member

will need impl_version bumping (assuming logic of runtime modules remains unchanged)

/// Convert into a signed extrinsic.
#[cfg(feature = "std")]
pub fn into_signed_tx(self) -> Extrinsic {
let signature = keyring::Keyring::from_raw_public(
Copy link
Member

Choose a reason for hiding this comment

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

should just be keyring::Keyring::from_public(self.from). If you're type-running something's wrong.

@gavofyork gavofyork added A3-needsresolving and removed A0-please_review Pull request needs code review. labels Mar 13, 2019
@gavofyork
Copy link
Member

@rphmeier you ok to sign off on this?

@bkchr
Copy link
Member Author

bkchr commented Mar 13, 2019

Why do I need to increase the impl_version here?

|res, memory| {
if let Some(I64(r)) = res {
let offset = r as u32;
let length = (r >> 32) as usize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I wonder if this is correct. Can you check my logic?

  1. r is i64.
  2. According to the reference, right shift is logical for unsinged values but arithmetic for signed values.
  3. So 0x8000_0000_0000_0000 >> 32 is an arithmetic shift and will give you 0xFFFF_FFFF_8000_0000.

Copy link
Member Author

Choose a reason for hiding this comment

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

@tomusdrw FYI. We both need to go back and learn shifting :D

type Target = T;

fn deref(&self) -> &Self::Target {
unsafe { &(*self.0.as_ptr()).0 }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a safe implementation, though.

// let STATIC_FN = ExchangeableFunction { ... };
let y = STATIC_FN.deref();
STATIC_FN.replace_implementation(...); // violates borrowing rules and introduces UB
y();

Copy link
Contributor

Choose a reason for hiding this comment

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

Basically: it's only safe to use this struct if we can ensure that borrows through deref do not last longer than a single invocation of the internal value. The public API should ensure this. I'm not sure Deref is the right choice with that in mind. Maybe a fn with_inner<F: FnOnce(&T)>(&self, f: F).

}

// WASM does not support threads, so this is safe; qed.
unsafe impl<T> Sync for ExchangeableFunction<T> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens when we have a native-linked runtime? and if we would do runtime calls in multiple threads?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is never compiled in native. Only for WASM.

/// # Returns
///
/// Returns the original implementation wrapped in [`RestoreImplementation`].
pub unsafe fn replace_implementation(&'static self, new_impl: T) -> RestoreImplementation<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you mention in the documentation why this is unsafe?

/// A function which implementation can be exchanged.
///
/// Internally this works by swapping function pointers.
pub struct ExchangeableFunction<T>(Cell<(T, bool)>);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you mention what is this bool stands for?

}

/// Restores a function implementation on drop.
pub struct RestoreImplementation<T: 'static>(&'static ExchangeableFunction<T>, Option<T>);
Copy link
Contributor

@pepyakin pepyakin Mar 14, 2019

Choose a reason for hiding this comment

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

Can you briefly describe what each member is? (In comments)

@bkchr bkchr merged commit 6945bdf into master Mar 14, 2019
@bkchr bkchr deleted the bkchr-validate_block branch March 14, 2019 20:29
MTDK1 pushed a commit to bdevux/substrate that referenced this pull request Apr 12, 2019
* Adds benchmark for direct/indirect wasm function calls

* Store the benchmark function pointer in a `Cell`

* Add some documentation

* Make function implementations exchangeable

* Add parachain stub

* Add macro for registering the `validate_block` function

* Make all functions replace-able by unimplemented

* Some more refactoring

* Adds tests for executing empty parachain block

* Work on a new test with empty witness data

* Don't exchange `ext_print_*` stuff

* Some cleanup and one more function for `validate_block`

* More tests and more functions

* Fixes after merging master

* Use `parity-codec` `derive` feature

* CHange implementation of `wasm-nice-panic-message`

* Move `parachain` stuff to cumulus

* Updated wasm files

* Integrate feedback

* Switch to `ExchangeableFunction` struct

* More fixes

* Switch to Cell and panic on multiple replaces

* Increase `impl_version`

* Fix shifting

* Make the API more verbose of `ExchangeableFunction`

* Increase `impl_version`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants