Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Scale gas limit.
Browse files Browse the repository at this point in the history
Point 2. in #13.

Reference: Zilliqa/scilla#877
  • Loading branch information
vaivaswatha committed Jul 27, 2021
1 parent b9eb21e commit dcdf996
Show file tree
Hide file tree
Showing 214 changed files with 242 additions and 227 deletions.
2 changes: 2 additions & 0 deletions libScillaRTL/SRTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const auto Ecdsa_Pubkey_Uncompressed_Len = 65;

const auto Zilliqa_Address_Len = 20;

const uint64_t GasScaleFactor = 8;

// Fetch the type of a remote field, if it exists.
std::optional<std::string> remoteFieldType(const ScillaExecImpl *SJ,
const std::string &Addr,
Expand Down
7 changes: 5 additions & 2 deletions libScillaRTL/ScillaBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class TransitionState {

public:
TransitionState(const std::string &Balance_P, const std::string &InAmount_P,
uint64_t CurBlock_P, const std::string &SenderAddr_P)
uint64_t CurBlock_P, uint64_t GasLimit_P,
const std::string &SenderAddr_P)
: Balance(Balance_P), Accepted(false), OutJ(Json::objectValue),
CurBlock(CurBlock_P), SenderAddr(SenderAddr_P), InAmount(InAmount_P){};
CurBlock(CurBlock_P), SenderAddr(SenderAddr_P), InAmount(InAmount_P),
GasLimit(GasLimit_P){};

void processSend(Json::Value &M);
void processEvent(Json::Value &M);
Expand All @@ -52,6 +54,7 @@ class TransitionState {
const uint64_t CurBlock;
const std::string SenderAddr;
const SafeUint128 InAmount;
const uint64_t GasLimit;

// Returns the output of the transition execution. Destroys *this*.
Json::Value finalize(uint64_t GasRem);
Expand Down
38 changes: 24 additions & 14 deletions libScillaRTL/ScillaExecImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ ScillaExprExec::ScillaExprExec(const ScillaParams &SPs,
std::string ScillaExprExec::exec(uint64_t GasLimit) {
auto ScillaMainAddr = PImpl->getAddressFor("scilla_main");
auto ScillaMain = reinterpret_cast<void (*)()>(ScillaMainAddr);

// This isn't a transition, but we still setup a state for consistency.
PImpl->TS = std::make_unique<TransitionState>("0", "0", 0, GasLimit, "0x");
// Set gas available in the JIT'ed code and then initialize libraries.
PImpl->initGasAndLibs(GasLimit);

// Clear the output.
PImpl->ScillaStdout.clear();
ScillaMain();
Expand Down Expand Up @@ -163,16 +167,14 @@ void ScillaExecImpl::initContrParams(const Json::Value &CP,
}
}

uint64_t *ScillaExecImpl::initGasAndLibs(uint64_t GasLimit) {
// Set gas limit in the JIT'ed code module.
void ScillaExecImpl::initGasAndLibs(uint64_t GasLimit) {
auto GasRemPtr = reinterpret_cast<uint64_t *>(getAddressFor("_gasrem"));
*GasRemPtr = GasLimit;
// Scale up the gas limit.
*GasRemPtr = GasLimit * GasScaleFactor;

// Call the library initialisation function.
auto initLibs = reinterpret_cast<void (*)()>(getAddressFor("_init_libs"));
initLibs();

return GasRemPtr;
}

Json::Value ScillaExecImpl::deploy(const Json::Value &InitJ, uint64_t GasLimit,
Expand All @@ -181,20 +183,28 @@ Json::Value ScillaExecImpl::deploy(const Json::Value &InitJ, uint64_t GasLimit,
// Initialize contract parameters.
initContrParams(InitJ, true /* DoDynamicTypechecks */);

auto GasRemPtr = initGasAndLibs(GasLimit);

// Let's setup the TransitionState for this transition.
TS = std::make_unique<TransitionState>("0", "0", CurBlock, "");
TS = std::make_unique<TransitionState>("0", "0", CurBlock, GasLimit, "");

initGasAndLibs(GasLimit);

auto fIS = reinterpret_cast<void (*)(void)>(getAddressFor("_init_state"));
fIS();

Json::Value Result = TS->finalize(*GasRemPtr);
Json::Value Result = TS->finalize(getGasRem());
OM.deleteAll();
return Result;
}

uint64_t ScillaExecImpl::getGasRem() const {
return *reinterpret_cast<const uint64_t *>(getAddressFor("_gasrem"));
auto GasRem = *reinterpret_cast<const uint64_t *>(getAddressFor("_gasrem"));
// Scale down
auto GasRemScaledDown = GasRem / GasScaleFactor;
// If no gas was consumed, force at least 1 unit to be consumed.
if (GasRemScaledDown == TS->GasLimit) {
GasRemScaledDown--;
}
return GasRemScaledDown;
}

const ScillaTypes::Typ *
Expand Down Expand Up @@ -233,11 +243,11 @@ Json::Value ScillaExecImpl::execMsg(const std::string &Balance,
!OriginJ.isString() || !AmountJ.isString())
CREATE_ERROR("Invalid Message");

auto GasRemPtr = initGasAndLibs(GasLimit);

// Let's setup the TransitionState for this transition.
TS = std::make_unique<TransitionState>(Balance, AmountJ.asString(), CurBlock,
SenderJ.asString());
GasLimit, SenderJ.asString());

initGasAndLibs(GasLimit);

// Amount and Sender need to be prepended to the parameter list.
Json::Value AmountParam;
Expand Down Expand Up @@ -349,7 +359,7 @@ Json::Value ScillaExecImpl::execMsg(const std::string &Balance,

Transition(Mem);

Json::Value Result = TS->finalize(*GasRemPtr);
Json::Value Result = TS->finalize(getGasRem());
OM.deleteAll();
return Result;
}
Expand Down
2 changes: 1 addition & 1 deletion libScillaRTL/ScillaExecImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ScillaExecImpl {
// Get address for @Symbol inside the compiled IR, ready to be used.
void *getAddressFor(const std::string &Symbol) const;
// Initialize gas-remaining field in the code and initialize libraries.
uint64_t *initGasAndLibs(uint64_t GasRem);
void initGasAndLibs(uint64_t GasRem);
// Execute a message.
Json::Value execMsg(const std::string &Balance, uint64_t GasLimit,
uint64_t CurBlock, const Json::Value &InitJ,
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accept.output_Accept1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999997",
"gas_remaining" : "9999999",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accept.output_Accept2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999875",
"gas_remaining" : "9999984",
"messages" : [
{
"_amount" : "100",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accept.output_Accept3_succ.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999875",
"gas_remaining" : "9999984",
"messages" : [
{
"_amount" : "100",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999863",
"gas_remaining" : "9999982",
"messages" : [
{
"_amount" : "0",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_10.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999558",
"gas_remaining" : "9999944",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_11.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999556",
"gas_remaining" : "9999944",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_12.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999565",
"gas_remaining" : "9999945",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_13.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999959",
"gas_remaining" : "9999994",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_14.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999554",
"gas_remaining" : "9999944",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_15.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999564",
"gas_remaining" : "9999945",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_16.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999959",
"gas_remaining" : "9999994",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_17.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999733",
"gas_remaining" : "9999966",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_18.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999734",
"gas_remaining" : "9999966",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_19.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999658",
"gas_remaining" : "9999957",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999390",
"gas_remaining" : "9999923",
"messages" : [
{
"_amount" : "0",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_20.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999549",
"gas_remaining" : "9999943",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_21.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999740",
"gas_remaining" : "9999967",
"messages" : [
{
"_amount" : "30",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_3.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999802",
"gas_remaining" : "9999975",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_4.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999393",
"gas_remaining" : "9999924",
"messages" : [
{
"_amount" : "0",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_5.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999802",
"gas_remaining" : "9999975",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_6.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999450",
"gas_remaining" : "9999931",
"messages" : [
{
"_amount" : "0",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_7.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999804",
"gas_remaining" : "9999975",
"messages" : [
{
"_amount" : "0",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_8.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999802",
"gas_remaining" : "9999975",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests.output_9.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999527",
"gas_remaining" : "9999940",
"messages" : [
{
"_amount" : "10",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests_support.output_1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999998",
"gas_remaining" : "9999999",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests_support.output_2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "false",
"events" : [],
"gas_remaining" : "9999938",
"gas_remaining" : "9999992",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests_support.output_3.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999946",
"gas_remaining" : "9999993",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests_support.output_4.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999955",
"gas_remaining" : "9999994",
"messages" : [],
"scilla_major_version" : "0"
}
2 changes: 1 addition & 1 deletion testsuite/contr/accounting_tests_support.output_5.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_accepted" : "true",
"events" : [],
"gas_remaining" : "9999917",
"gas_remaining" : "9999989",
"messages" : [],
"scilla_major_version" : "0"
}
Loading

0 comments on commit dcdf996

Please sign in to comment.