Skip to content

Commit

Permalink
tvm_rawreserve() and handle.deploy_noop() added
Browse files Browse the repository at this point in the history
  • Loading branch information
azhogin authored and akiramenai committed Dec 15, 2020
1 parent 50de68d commit b021b7f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
22 changes: 22 additions & 0 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ inline void tvm_accept() {
inline void tvm_commit() {
__builtin_tvm_commit();
}

/* From tvm RAWRESERVE(x, y) :
Creates an output action which would reserve
exactly x nanograms (if y = 0),
at most x nanograms (if y = 2), or
all but x nanograms (if y = 1 or y = 3),
from the remaining balance of the account.
It is roughly equivalent to creating an outbound message
carrying x nanograms (or b − x nanograms, where b is the remaining
balance) to oneself, so that the subsequent output actions would not
be able to spend more money than the remainder.
Bit +2 in y means that the external action does not fail if the specified amount cannot be
reserved; instead, all remaining balance is reserved. Bit +8 in y means
x ← −x before performing any further actions.
Bit +4 in y means that x is increased by the original balance of the current account (before the
compute phase), including all extra currencies, before performing any
other checks and actions. Currently x must be a non-negative integer,
and y must be in the range 0 ... 15. */

inline void tvm_rawreserve(unsigned x, rawreserve_flag y) {
__builtin_tvm_rawreserve(x, static_cast<unsigned>(y));
}
inline void tvm_setcode(cell new_root_cell) {
__builtin_tvm_setcode(new_root_cell);
}
Expand Down
35 changes: 34 additions & 1 deletion llvm/projects/ton-compiler/cpp-sdk/tvm/contract_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ void contract_call_impl(address addr, schema::Grams amount,
tvm_sendmsg(contract_call_prepare<Func>(addr, amount, args...), flags);
}

// Deploy message with function call (immediately after deploy)
template<auto Func, class... Args>
__always_inline
void contract_deploy_impl(address addr, schema::StateInit init,
Expand Down Expand Up @@ -240,6 +241,32 @@ void contract_deploy_impl(address addr, schema::StateInit init,
tvm_sendmsg(build(out_msg).endc(), flags);
}

// Deploy without any function called (with func_id = 0)
// Deployed contract should support `fallback`
__always_inline
void contract_deploy_noop_impl(address addr, schema::StateInit init,
schema::Grams amount, unsigned flags) {
using namespace schema;

auto hdr = abiv2::internal_msg_header{ uint32(0) };
int_msg_info_relaxed out_info;
out_info.ihr_disabled = true;
out_info.bounce = false;
out_info.bounced = false;
out_info.src = addr_none{};
out_info.dest = addr;
out_info.created_lt = 0;
out_info.created_at = 0;
out_info.value.grams = amount;

message_relaxed<decltype(hdr)> out_msg;
out_msg.info = out_info;
Either<StateInit, ref<StateInit>> init_ref = ref<StateInit>{init};
out_msg.init = init_ref;
out_msg.body = hdr;
tvm_sendmsg(build(out_msg).endc(), flags);
}

template<class RetT>
struct wait_call_result {
__always_inline
Expand Down Expand Up @@ -402,11 +429,17 @@ class contract_handle {
schema::Grams amount = 10000000, unsigned flags = DEFAULT_MSG_FLAGS) const {
return contract_call_configured(addr_, amount, flags);
}
// Deploy message with function call (immediately after deploy)
__always_inline
proxy_deploy deploy(
schema::StateInit init, schema::Grams amount, unsigned flags = DEFAULT_MSG_FLAGS) const {
return proxy_deploy(addr_, init, amount, flags);
}
// Deploy message with func_id = 0 (deploying contract must support fallback)
__always_inline
void deploy_noop(schema::StateInit init, schema::Grams amount, unsigned flags = DEFAULT_MSG_FLAGS) {
contract_deploy_noop_impl(addr_, init, amount, flags);
}
__always_inline
proxy operator()(schema::Grams amount = 10000000, unsigned flags = DEFAULT_MSG_FLAGS) const {
return proxy(addr_, amount, flags);
Expand Down Expand Up @@ -442,7 +475,7 @@ using handle = contract_handle<Interface>;
/*
void example_usage(contract_handle<Interface> handle) {
handle(Grams(10), SENDER_WANTS_TO_PAY_FEES_SEPARATELY).method(arg0, arg1, ...);
handle.deploy(init, 10, SENDER_WANTS_TO_PAY_FEES_SEPARATELY).call<&Interface::method>(arg0, arg1, ...);
handle.deploy(init, 10, SENDER_WANTS_TO_PAY_FEES_SEPARATELY).method(arg0, arg1, ...);
}
*/

Expand Down
32 changes: 31 additions & 1 deletion llvm/projects/ton-compiler/cpp-sdk/tvm/message_flags.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <type_traits>

// Message flags

namespace tvm {
Expand All @@ -13,17 +15,45 @@ constexpr unsigned SENDER_WANTS_TO_PAY_FEES_SEPARATELY = 1;
constexpr unsigned DEFAULT_MSG_FLAGS = SENDER_WANTS_TO_PAY_FEES_SEPARATELY | IGNORE_ACTION_ERRORS;

// RAWRESERVE flags
enum class rawreserve_flag {
none = 0x00,
all_except = 0x01,
// external action does not fail if the specified amount cannot be
// reserved; instead, all remaining balance is reserved
up_to = 0x02,
// x is increased by the original balance of the current account (before the
// compute phase), including all extra currencies, before performing any
// other checks and actions.
add_balance = 0x04,
// x to −x before performing any further actions
minus_value = 0x08
};

__always_inline rawreserve_flag operator | (rawreserve_flag lhs, rawreserve_flag rhs) {
using T = std::underlying_type_t<rawreserve_flag>;
return static_cast<rawreserve_flag>(static_cast<T>(lhs) | static_cast<T>(rhs));
}

__always_inline rawreserve_flag& operator |= (rawreserve_flag& lhs, rawreserve_flag rhs) {
lhs = lhs | rhs;
return lhs;
}

// x to −x before performing any further actions
[[deprecated]]
constexpr unsigned RESERVE_MINUS_VALUE = 8;
// x is increased by the original balance of the current account (before the
// compute phase), including all extra currencies, before performing any
// other checks and actions.
[[deprecated]]
constexpr unsigned RESERVE_ADD_BALANCE = 4;
// external action does not fail if the specified amount cannot be
// reserved; instead, all remaining balance is reserved
[[deprecated]]
constexpr unsigned RESERVE_UP_TO = 2;

constexpr unsigned RESERVE_ALL_EXCEPT = 1;
[[deprecated]]
constexpr unsigned RESERVE_ALL_EXCEPT = 1;

} // namespace tvm

2 changes: 2 additions & 0 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/basics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ struct varuint : boost::operators<varuint<_bitlen>> {
varuint() : val_(0) {}
varuint(unsigned val) : val_(val) {}
unsigned operator()() const { return val_; }
unsigned get() const { return val_; }
void operator()(unsigned val) { val_ = val; }
auto& operator=(unsigned val) { val_ = val; return *this; }
DEFAULT_PROXY_OPERATORS(varuint, unsigned)
Expand All @@ -561,6 +562,7 @@ struct varint : boost::operators<varint<_bitlen>> {
varint() : val_(0) {}
varint(int val) : val_(val) {}
int operator()() const { return val_; }
int get() const { return val_; }
void operator()(int val) { val_ = val; }
auto& operator=(int val) { val_ = val; return *this; }
DEFAULT_PROXY_OPERATORS(varint, int)
Expand Down

0 comments on commit b021b7f

Please sign in to comment.