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

Fix transaction original_balance and VmState::jump_to #1202

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/global-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
namespace ton {

// See doc/GlobalVersions.md
const int SUPPORTED_VERSION = 8;
const int SUPPORTED_VERSION = 9;

}
9 changes: 8 additions & 1 deletion crypto/block/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,14 @@ bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) {
// ...
compute_phase = std::make_unique<ComputePhase>();
ComputePhase& cp = *(compute_phase.get());
original_balance -= total_fees;
if (cfg.global_version >= 9) {
original_balance = balance;
if (msg_balance_remaining.is_valid()) {
original_balance -= msg_balance_remaining;
}
} else {
original_balance -= total_fees;
}
if (td::sgn(balance.grams) <= 0) {
// no gas
cp.skip_reason = ComputePhase::sk_no_gas;
Expand Down
93 changes: 47 additions & 46 deletions crypto/vm/continuation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

namespace vm {

int Continuation::jump_w(VmState* st) & {
return static_cast<const Continuation*>(this)->jump(st);
td::Ref<Continuation> Continuation::jump_w(VmState* st, int& exitcode) & {
return static_cast<const Continuation*>(this)->jump(st, exitcode);
}

bool Continuation::has_c0() const {
Expand Down Expand Up @@ -286,15 +286,16 @@ std::string QuitCont::type() const {
return "vmc_quit";
}

int ExcQuitCont::jump(VmState* st) const & {
td::Ref<Continuation> ExcQuitCont::jump(VmState* st, int& exitcode) const& {
int n = 0;
try {
n = st->get_stack().pop_smallint_range(0xffff);
} catch (const VmError& vme) {
n = vme.get_errno();
}
VM_LOG(st) << "default exception handler, terminating vm with exit code " << n;
return ~n;
exitcode = ~n;
return {};
}

std::string ExcQuitCont::type() const {
Expand All @@ -311,16 +312,16 @@ Ref<ExcQuitCont> ExcQuitCont::deserialize(CellSlice& cs, int mode) {
return cs.fetch_ulong(4) == 9 ? Ref<ExcQuitCont>{true} : Ref<ExcQuitCont>{};
}

int PushIntCont::jump(VmState* st) const & {
td::Ref<Continuation> PushIntCont::jump(VmState* st, int& exitcode) const& {
VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)";
st->get_stack().push_smallint(push_val);
return st->jump(next);
return next;
}

int PushIntCont::jump_w(VmState* st) & {
td::Ref<Continuation> PushIntCont::jump_w(VmState* st, int& exitcode) & {
VM_LOG(st) << "execute implicit PUSH " << push_val;
st->get_stack().push_smallint(push_val);
return st->jump(std::move(next));
return std::move(next);
}

std::string PushIntCont::type() const {
Expand All @@ -345,20 +346,20 @@ Ref<PushIntCont> PushIntCont::deserialize(CellSlice& cs, int mode) {
}
}

int ArgContExt::jump(VmState* st) const & {
td::Ref<Continuation> ArgContExt::jump(VmState* st, int& exitcode) const& {
st->adjust_cr(data.save);
if (data.cp != -1) {
st->force_cp(data.cp);
}
return ext->jump(st);
return ext;
}

int ArgContExt::jump_w(VmState* st) & {
td::Ref<Continuation> ArgContExt::jump_w(VmState* st, int& exitcode) & {
st->adjust_cr(std::move(data.save));
if (data.cp != -1) {
st->force_cp(data.cp);
}
return st->jump_to(std::move(ext));
return std::move(ext);
}

bool ArgContExt::serialize(CellBuilder& cb) const {
Expand All @@ -382,32 +383,32 @@ std::string ArgContExt::type() const {
return "vmc_envelope";
}

int RepeatCont::jump(VmState* st) const & {
td::Ref<Continuation> RepeatCont::jump(VmState* st, int& exitcode) const& {
VM_LOG(st) << "repeat " << count << " more times (slow)\n";
if (count <= 0) {
return st->jump(after);
return after;
}
if (body->has_c0()) {
return st->jump(body);
return body;
}
st->set_c0(Ref<RepeatCont>{true, body, after, count - 1});
return st->jump(body);
return body;
}

int RepeatCont::jump_w(VmState* st) & {
td::Ref<Continuation> RepeatCont::jump_w(VmState* st, int& exitcode) & {
VM_LOG(st) << "repeat " << count << " more times\n";
if (count <= 0) {
body.clear();
return st->jump(std::move(after));
return std::move(after);
}
if (body->has_c0()) {
after.clear();
return st->jump(std::move(body));
return std::move(body);
}
// optimization: since this is unique, reuse *this instead of creating new object
--count;
st->set_c0(Ref<RepeatCont>{this});
return st->jump(body);
return body;
}

bool RepeatCont::serialize(CellBuilder& cb) const {
Expand Down Expand Up @@ -443,21 +444,21 @@ int VmState::repeat(Ref<Continuation> body, Ref<Continuation> after, long long c
}
}

int AgainCont::jump(VmState* st) const & {
td::Ref<Continuation> AgainCont::jump(VmState* st, int& exitcode) const& {
VM_LOG(st) << "again an infinite loop iteration (slow)\n";
if (!body->has_c0()) {
st->set_c0(Ref<AgainCont>{this});
}
return st->jump(body);
return body;
}

int AgainCont::jump_w(VmState* st) & {
td::Ref<Continuation> AgainCont::jump_w(VmState* st, int& exitcode) & {
VM_LOG(st) << "again an infinite loop iteration\n";
if (!body->has_c0()) {
st->set_c0(Ref<AgainCont>{this});
return st->jump(body);
return body;
} else {
return st->jump(std::move(body));
return std::move(body);
}
}

Expand Down Expand Up @@ -485,31 +486,31 @@ int VmState::again(Ref<Continuation> body) {
return jump(Ref<AgainCont>{true, std::move(body)});
}

int UntilCont::jump(VmState* st) const & {
td::Ref<Continuation> UntilCont::jump(VmState* st, int& exitcode) const& {
VM_LOG(st) << "until loop body end (slow)\n";
if (st->get_stack().pop_bool()) {
VM_LOG(st) << "until loop terminated\n";
return st->jump(after);
return after;
}
if (!body->has_c0()) {
st->set_c0(Ref<UntilCont>{this});
}
return st->jump(body);
return body;
}

int UntilCont::jump_w(VmState* st) & {
td::Ref<Continuation> UntilCont::jump_w(VmState* st, int& exitcode) & {
VM_LOG(st) << "until loop body end\n";
if (st->get_stack().pop_bool()) {
VM_LOG(st) << "until loop terminated\n";
body.clear();
return st->jump(std::move(after));
return std::move(after);
}
if (!body->has_c0()) {
st->set_c0(Ref<UntilCont>{this});
return st->jump(body);
return body;
} else {
after.clear();
return st->jump(std::move(body));
return std::move(body);
}
}

Expand Down Expand Up @@ -541,54 +542,54 @@ int VmState::until(Ref<Continuation> body, Ref<Continuation> after) {
return jump(std::move(body));
}

int WhileCont::jump(VmState* st) const & {
td::Ref<Continuation> WhileCont::jump(VmState* st, int& exitcode) const& {
if (chkcond) {
VM_LOG(st) << "while loop condition end (slow)\n";
if (!st->get_stack().pop_bool()) {
VM_LOG(st) << "while loop terminated\n";
return st->jump(after);
return after;
}
if (!body->has_c0()) {
st->set_c0(Ref<WhileCont>{true, cond, body, after, false});
}
return st->jump(body);
return body;
} else {
VM_LOG(st) << "while loop body end (slow)\n";
if (!cond->has_c0()) {
st->set_c0(Ref<WhileCont>{true, cond, body, after, true});
}
return st->jump(cond);
return cond;
}
}

int WhileCont::jump_w(VmState* st) & {
td::Ref<Continuation> WhileCont::jump_w(VmState* st, int& exitcode) & {
if (chkcond) {
VM_LOG(st) << "while loop condition end\n";
if (!st->get_stack().pop_bool()) {
VM_LOG(st) << "while loop terminated\n";
cond.clear();
body.clear();
return st->jump(std::move(after));
return std::move(after);
}
if (!body->has_c0()) {
chkcond = false; // re-use current object since we hold the unique pointer to it
st->set_c0(Ref<WhileCont>{this});
return st->jump(body);
return body;
} else {
cond.clear();
after.clear();
return st->jump(std::move(body));
return std::move(body);
}
} else {
VM_LOG(st) << "while loop body end\n";
if (!cond->has_c0()) {
chkcond = true; // re-use current object
st->set_c0(Ref<WhileCont>{this});
return st->jump(cond);
return cond;
} else {
body.clear();
after.clear();
return st->jump(std::move(cond));
return std::move(cond);
}
}
}
Expand Down Expand Up @@ -627,16 +628,16 @@ int VmState::loop_while(Ref<Continuation> cond, Ref<Continuation> body, Ref<Cont
return jump(std::move(cond));
}

int OrdCont::jump(VmState* st) const & {
td::Ref<Continuation> OrdCont::jump(VmState* st, int& exitcode) const& {
st->adjust_cr(data.save);
st->set_code(code, data.cp);
return 0;
return {};
}

int OrdCont::jump_w(VmState* st) & {
td::Ref<Continuation> OrdCont::jump_w(VmState* st, int& exitcode) & {
st->adjust_cr(std::move(data.save));
st->set_code(std::move(code), data.cp);
return 0;
return {};
}

bool OrdCont::serialize(CellBuilder& cb) const {
Expand Down
Loading
Loading