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

Adding Context to the stdlib #369

Closed
wants to merge 16 commits into from
Closed

Adding Context to the stdlib #369

wants to merge 16 commits into from

Conversation

nfurfaro
Copy link
Contributor

@nfurfaro nfurfaro commented Nov 5, 2021

WIP.
A first crack at add a context Struct to the stdLib so that you can do stuff like Context.id() in a contract to get the current contract's id.

It would be nice if:

  • you don't have to instantiate a context struct manually before using it
  • the user could just use the field context.id directly rather thancalling the method (minor detail)

Note that this doesn't work yet.
Trying to build the contract gives the error:

  Compiled library "stdlib" with 14 warnings.
thread 'main' panicked at 'Out of bounds access', core_lang/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs:40:14
stack backtrace:
   0:     0x5608ce36b53c - std::backtrace_rs::backtrace::libunwind::trace::hd3a6cb8e32f9fc43
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x5608ce36b53c - std::backtrace_rs::backtrace::trace_unsynchronized::h6dcceebcd6a9f2ab
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x5608ce36b53c - std::sys_common::backtrace::_print_fmt::h4eacf4e9ad977499
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x5608ce36b53c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1e42c22572a7e7b4
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x5608ce39316c - core::fmt::write::h072b578a8e90aa54
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/core/src/fmt/mod.rs:1150:17
   5:     0x5608ce3641f5 - std::io::Write::write_fmt::hc32d3cabefff1d24
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/io/mod.rs:1667:15
   6:     0x5608ce36d570 - std::sys_common::backtrace::_print::h058b85b6efc15780
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x5608ce36d570 - std::sys_common::backtrace::print::hdb7de10b704104b4
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x5608ce36d570 - std::panicking::default_hook::{{closure}}::h5451997d203bda2f
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:210:50
   9:     0x5608ce36d127 - std::panicking::default_hook::h419b121e68f746a2
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:227:9
  10:     0x5608ce36dc24 - std::panicking::rust_panic_with_hook::h32c44b1364c1c247
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:624:17
  11:     0x5608ce36d700 - std::panicking::begin_panic_handler::{{closure}}::he2e5f48fb16982d1
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:521:13
  12:     0x5608ce36b9e4 - std::sys_common::backtrace::__rust_end_short_backtrace::haea54a33f9e18aba
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/sys_common/backtrace.rs:141:18
  13:     0x5608ce36d669 - rust_begin_unwind
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:517:5
  14:     0x5608cdc6db01 - core::panicking::panic_fmt::h824e0febbdf5e655
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/core/src/panicking.rs:101:14
  15:     0x5608cdc6d9f3 - core::option::expect_failed::h3867f03951c5322d
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/core/src/option.rs:1615:5
  16:     0x5608ce17e171 - core_lang::semantic_analysis::ast_node::expression::typed_expression::method_application::type_check_method_application::h86d3a2259bdb2bdb
  17:     0x5608ce2217a9 - core_lang::semantic_analysis::ast_node::expression::typed_expression::TypedExpression::type_check::h2017d6b3c8d3544a
  18:     0x5608ce1b9f4a - core_lang::semantic_analysis::ast_node::TypedAstNode::type_check::{{closure}}::h49c3713be9fe514c
  19:     0x5608ce1a57bf - core_lang::semantic_analysis::ast_node::TypedAstNode::type_check::h50077c2b778eb933
  20:     0x5608ce0ac52c - core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut::h0be517620c49dd91
  21:     0x5608ce157ff2 - <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter::h3d468b23061aa639
  22:     0x5608ce0c0083 - core_lang::semantic_analysis::ast_node::code_block::TypedCodeBlock::type_check::hb9d924429718c1e0
  23:     0x5608ce0f9565 - core_lang::semantic_analysis::ast_node::declaration::TypedFunctionDeclaration::type_check::hdd7554e4472a541a
  24:     0x5608ce067e2d - core_lang::semantic_analysis::ast_node::impl_trait::type_check_trait_implementation::h144d2d03ad7455e3
  25:     0x5608ce0660f0 - core_lang::semantic_analysis::ast_node::impl_trait::implementation_of_trait::h594f2768ae27886e
  26:     0x5608ce1a7971 - core_lang::semantic_analysis::ast_node::TypedAstNode::type_check::h50077c2b778eb933
  27:     0x5608ce06ea76 - core_lang::semantic_analysis::syntax_tree::TypedParseTree::type_check_nodes::{{closure}}::hb715714e5fa039cf
  28:     0x5608ce14a1d2 - alloc::vec::source_iter_marker::<impl alloc::vec::spec_from_iter::SpecFromIter<T,I> for alloc::vec::Vec<T>>::from_iter::h4255277f74360568
  29:     0x5608ce235b00 - core_lang::semantic_analysis::syntax_tree::TypedParseTree::type_check::hca3860efc63d7b3f
  30:     0x5608ce1879c5 - core_lang::compile_to_asm::{{closure}}::h5246d3bf02574c6d
  31:     0x5608ce18464c - core_lang::compile_to_asm::ha91d8e304f844c74
  32:     0x5608ce187b41 - core_lang::compile_to_bytecode::h40302e1fbda2bf20
  33:     0x5608cdcb797f - forc::ops::forc_build::build::h1568f6f657a18eac
  34:     0x5608cdcaa1d0 - forc::cli::run_cli::{{closure}}::habb5ece9be8d9145
  35:     0x5608cdca75f7 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hb86145d1833bca62
  36:     0x5608cdc8cca2 - tokio::park::thread::CachedParkThread::block_on::h6174feaf988d2e0a
  37:     0x5608cdc8ce12 - tokio::runtime::thread_pool::ThreadPool::block_on::h15aba608d40d9c4d
  38:     0x5608cdcc9795 - tokio::runtime::Runtime::block_on::h69d8dc2268d7a023
  39:     0x5608cdd0f427 - forc::main::hcbd5036a2d174f05
  40:     0x5608cdc86823 - std::sys_common::backtrace::__rust_begin_short_backtrace::h4864e5d8daa0ae6c
  41:     0x5608cdcc0f0d - std::rt::lang_start::{{closure}}::he6c9ed2ed7630e1c
  42:     0x5608ce36e22a - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h97e9d5f21cbee92f
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/core/src/ops/function.rs:259:13
  43:     0x5608ce36e22a - std::panicking::try::do_call::hdfecd950eba8f698
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:403:40
  44:     0x5608ce36e22a - std::panicking::try::h3c77c8ba3eb0735c
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:367:19
  45:     0x5608ce36e22a - std::panic::catch_unwind::h8e0d62076d8d837e
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panic.rs:129:14
  46:     0x5608ce36e22a - std::rt::lang_start_internal::{{closure}}::h647045b4c6a22e67
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/rt.rs:45:48
  47:     0x5608ce36e22a - std::panicking::try::do_call::h905d96e4a757647e
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:403:40
  48:     0x5608ce36e22a - std::panicking::try::hc113426beb3c908b
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:367:19
  49:     0x5608ce36e22a - std::panic::catch_unwind::h74f684ea7038ea12
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panic.rs:129:14
  50:     0x5608ce36e22a - std::rt::lang_start_internal::h5ba185919d396c97
                               at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/rt.rs:45:20
  51:     0x5608cdd0f4f2 - main
  52:     0x7f2fbfef5565 - __libc_start_main
  53:     0x5608cdc6e32e - _start
  54:                0x0 - <unknown>

@nfurfaro nfurfaro requested a review from sezna as a code owner November 5, 2021 19:47
@nfurfaro nfurfaro marked this pull request as draft November 5, 2021 19:47
@nfurfaro nfurfaro changed the title Adding Context tostdlib Adding Context to the stdlib Nov 5, 2021
@sezna
Copy link
Contributor

sezna commented Nov 6, 2021

Now that #371 and #372 are in, are you still seeing errors here?

@nfurfaro
Copy link
Contributor Author

nfurfaro commented Nov 6, 2021

@sezna Still seeing errors.
In fact, I'm seeing errors with the b512 test on master.
The b512_struct_alignment test doesn't build because of mcpi:

mcpi rlo r1 i32;
   |             ^^^^ Unknown opcode: "mcpi".

If I switch it to use mcp it builds, but forc run is retuning false:

[Return { id: ContractId([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), val: 0, pc: 676, is: 464 }]

In addition, trying to use the new() method is still giving me the error:

thread 'main' panicked at 'Out of bounds access',... 

in the empty_method_initializer test.

Is this working for you on master?

@sezna
Copy link
Contributor

sezna commented Nov 6, 2021

Have you pulled from the latest master and updated forc? Unknown opcode: mcpi tells me that something is out of date, because that is definitely in the language.

@nfurfaro
Copy link
Contributor Author

nfurfaro commented Nov 6, 2021

🤦🏼 I did not think to update forc.
I have pulled from master though. trying again with a fresh forc as soon as I get a chance.
Wait... wouldn't pulling master for the sway repo update forc?
cc @sezna
update: just re-red the docs on installing the toolchain, forgot forc was a crate installed seperately.

@nfurfaro nfurfaro force-pushed the furnic/stdlib-context branch from 1af11e8 to fd89a87 Compare November 9, 2021 16:23
@nfurfaro
Copy link
Contributor Author

nfurfaro commented Nov 9, 2021

What else do we want to add to Context as a minimum initial lib?
@adlerjohn was it msg.data that you mentioned we should "replicate"?
Combined with the auth module. this would basically give us the equivalent of Solidity's msg.sender, msg.data and this.address which would be a pretty good start.

@adlerjohn
Copy link
Contributor

adlerjohn commented Nov 10, 2021

Not msg.data but msg.value. Any by extension, msg.color, (or msg.token_id) if you want to take the leap of renaming per-emptively.

@nfurfaro
Copy link
Contributor Author

Not msg.data but msg.value. Any by extension, msg.color, (or msg.token_id) if you want to take the leap of renaming per-emptively.

This sounds good to me. So far I'm thinking of something like this (maybe exposing balance on Context as well):

struct Context {
    id: b256,
    balance: u64,
}

struct Msg {
    value: u64,
    token_id: b256, // new name for `color` ?
}

Trying to get my head around the implications of the UTXO model...
For msg.value, would I need to basically do something like:

// if TransactionType == Script:
//     check outputs (also have access to outputsCount)
//      if OutputType == Coin:
//          for each OutputCoin:
//              if `to` == this.id && if `color` == ETH_COLOR:
//                  return sum of all `amount`s

@nfurfaro nfurfaro force-pushed the furnic/stdlib-context branch from ff2d6fe to fb42b64 Compare November 10, 2021 18:51
@adlerjohn
Copy link
Contributor

To transclude some info from Slack: the equivalent of msg.value (i.e. the amount of coins sent with a call) is found in $bal.

@nfurfaro nfurfaro force-pushed the furnic/stdlib-context branch from e9ac76b to e57b097 Compare November 11, 2021 20:27
@nfurfaro
Copy link
Contributor Author

OK, I think this is getting close.
I'm not sure what the best direction is with regards to the Context and Msg structs.
Specifically, I'm not sure if we're ever going to want to save data (like id or value) on these types (for caching purposes), or simply call the id() or value() methods as needed.
If the methods are enough, why not omit the structs entirely and just expose some functions instead of methods?

It seems we can't define empty (unit) Structs in Sway, so I did this for now:

struct Context {
    // id: b256
    empty: (),
}

Just need some feedback on this before a final cleanup.
cc @sezna @adlerjohn

@sezna
Copy link
Contributor

sezna commented Nov 11, 2021

It seems we can't define empty (unit) Structs in Sway, so I did this for now:

Actually, what you've done is indeed a unit struct. It is zero-sized. It's just a bit more verbose than Rust's struct Foo {}. 😄

@nfurfaro
Copy link
Contributor Author

I guess the bigger question is, do we need the struct at all?
or just use functions...

@adlerjohn
Copy link
Contributor

IMO we don't need a struct at all. I don't actually see something like std::chain::context::value() as being a problem (obviously you wouldn't have to prefix std::chain:: in practice). In Solidity, msg.value is actually a function anyways I think.

@nfurfaro
Copy link
Contributor Author

Replaced by #389.
See notes there for context.

@nfurfaro nfurfaro closed this Nov 12, 2021
@adlerjohn adlerjohn deleted the furnic/stdlib-context branch April 6, 2022 02:51
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.

3 participants