summaryrefslogtreecommitdiffstats
path: root/contracts/Governance.sol
diff options
context:
space:
mode:
Diffstat (limited to 'contracts/Governance.sol')
-rw-r--r--contracts/Governance.sol70
1 files changed, 38 insertions, 32 deletions
diff --git a/contracts/Governance.sol b/contracts/Governance.sol
index d29682a..916d525 100644
--- a/contracts/Governance.sol
+++ b/contracts/Governance.sol
@@ -51,76 +51,82 @@ contract Governance {
// 7: stores the array index + 1 of nodes for delgators.
mapping(address => mapping(address => int256)) public delegatorsOffset;
- // 8: CRS.
- bytes32[] public crs;
+ // 8: CRSRound.
+ uint256 public crsRound;
- // 9: dkgMasterPublicKeys
- bytes[][] public dkgMasterPublicKeys;
+ // 9: CRS.
+ bytes32 public crs;
- // 10: dkgComplaints
- bytes[][] public dkgComplaints;
+ // 10: DKGRound.
+ uint256 public dkgRound;
- // 11: dkgMPKReadys
- mapping(address => bool)[] public dkgMPKReadys;
+ // 11: dkgMasterPublicKeys
+ bytes[] public dkgMasterPublicKeys;
- // 12: dkgMPKReadysCount
- uint256[] public dkgMPKReadysCount;
+ // 12: dkgComplaints
+ bytes[] public dkgComplaints;
- // 13: dkgFinalizeds
- mapping(address => bool)[] public dkgFinalizeds;
+ // 13: dkgMPKReadys
+ mapping(address => bool) public dkgMPKReadys;
- // 14: dkgFinalizedsCount
- uint256[] public dkgFinalizedsCount;
+ // 14: dkgMPKReadysCount
+ uint256 public dkgMPKReadysCount;
- // 15: owner address.
+ // 15: dkgFinalizeds
+ mapping(address => bool) public dkgFinalizeds;
+
+ // 16: dkgFinalizedsCount
+ uint256 public dkgFinalizedsCount;
+
+ // 17: owner address.
address public owner;
- // 16: minStake
+ // 18: minStake
uint256 public minStake;
- // 17: lockupPeriod
+ // 19: lockupPeriod
uint256 public lockupPeriod;
- // 18: miningVelocity.
+ // 20: miningVelocity.
uint256 public miningVelocity; // stored as miningVelocity * 10^8
- // 19: nextHalvingSupply.
+ // 21: nextHalvingSupply.
uint256 public nextHalvingSupply;
- // 20: lastHalvedAmount.
+ // 22: lastHalvedAmount.
uint256 public lastHalvedAmount;
- // 21: blockGasLimit.
+ // 23: blockGasLimit.
uint256 public blockGasLimit;
// Lambda related.
- // 22
+ // 24
uint256 public lambdaBA;
- // 23
+ // 25
uint256 public lambdaDKG;
// Set related.
- // 24
+ // 26
uint256 public notarySetSize;
- // 25
+ // 27
uint256 public dkgSetSize;
- // 26: roundLength.
+ // 28: roundLength.
uint256 public roundLength;
- // 27: minBlockInterval.
+ // 29: minBlockInterval.
uint256 public minBlockInterval;
- // 28: Fine value.
+ // 30: Fine value.
uint256[] public fineValues;
- // 29: Fined records.
+ // 31: Fined records.
mapping(bytes32 => bool) public finedRecords;
- // 30: DKG reset count
+ // 32: DKG reset count
uint256[] public DKGResetCount;
- // 31: min gas price.
+ // 33: min gas price.
uint256 public minGasPrice;
// ----------
@@ -178,7 +184,7 @@ contract Governance {
function delegatorsLength(address NodeAddress) view public returns (uint256) {
}
- // ProposeCRS(signedCRS)
+ // ProposeCRS(round, signedCRS)
function proposeCRS(uint256 Round, bytes memory SignedCRS) public {
}
' href='#n269'>269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CodeFragment.cpp
 * @author Gav Wood <i@gavwood.com>
 * @date 2014
 */

#include "CodeFragment.h"

#include <boost/algorithm/string.hpp>
#pragma warning(push)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <boost/spirit/include/support_utree.hpp>
#pragma warning(pop)
#pragma GCC diagnostic pop
#include <libdevcore/CommonIO.h>
#include <libevmasm/Instruction.h>
#include "CompilerState.h"
#include "Parser.h"

using namespace std;
using namespace dev;
using namespace dev::lll;

void CodeFragment::finalise(CompilerState const& _cs)
{
    if (_cs.usedAlloc && _cs.vars.size() && !m_finalised)
    {
        m_finalised = true;
        m_asm.injectStart(Instruction::MSTORE8);
        m_asm.injectStart((u256)((_cs.vars.size() + 2) * 32) - 1);
        m_asm.injectStart((u256)1);
    }
}

namespace
{
/// Returns true iff the instruction is valid in "inline assembly".
bool validAssemblyInstruction(string us)
{
    auto it = c_instructions.find(us);
    return !(
        it == c_instructions.end() ||
        solidity::isPushInstruction(it->second)
    );
}

/// Returns true iff the instruction is valid as a function.
bool validFunctionalInstruction(string us)
{
    auto it = c_instructions.find(us);
    return !(
        it == c_instructions.end() ||
        solidity::isPushInstruction(it->second) ||
        solidity::isDupInstruction(it->second) ||
        solidity::isSwapInstruction(it->second) ||
        it->second == solidity::Instruction::JUMPDEST
    );
}
}

CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, ReadCallback const& _readFile, bool _allowASM):
    m_readFile(_readFile)
{
/*
    std::cout << "CodeFragment. Locals:";
    for (auto const& i: _s.defs)
        std::cout << i.first << ":" << i.second.m_asm.out();
    std::cout << "Args:";
    for (auto const& i: _s.args)
        std::cout << i.first << ":" << i.second.m_asm.out();
    std::cout << "Outers:";
    for (auto const& i: _s.outers)
        std::cout << i.first << ":" << i.second.m_asm.out();
    debugOutAST(std::cout, _t);
    std::cout << endl << flush;
*/
    switch (_t.which())
    {
    case sp::utree_type::list_type:
        constructOperation(_t, _s);
        break;
    case sp::utree_type::string_type:
    {
        auto sr = _t.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
        string s(sr.begin(), sr.end());
        m_asm.append(s);
        break;
    }
    case sp::utree_type::symbol_type:
    {
        auto sr = _t.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
        string s(sr.begin(), sr.end());
        string us = boost::algorithm::to_upper_copy(s);
        if (_allowASM && c_instructions.count(us) && validAssemblyInstruction(us))
            m_asm.append(c_instructions.at(us));
        else if (_s.defs.count(s))
            m_asm.append(_s.defs.at(s).m_asm);
        else if (_s.args.count(s))
            m_asm.append(_s.args.at(s).m_asm);
        else if (_s.outers.count(s))
            m_asm.append(_s.outers.at(s).m_asm);
        else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
        {
            auto it = _s.vars.find(s);
            if (it == _s.vars.end())
                error<InvalidName>(std::string("Symbol not found: ") + s);
            m_asm.append((u256)it->second.first);
        }
        else
            error<BareSymbol>(s);

        break;
    }
    case sp::utree_type::any_type:
    {
        bigint i = *_t.get<bigint*>();
        if (i < 0 || i > bigint(u256(0) - 1))
            error<IntegerOutOfRange>(toString(i));
        m_asm.append((u256)i);
        break;
    }
    default:
        error<CompilerException>("Unexpected fragment type");
        break;
    }
}

void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
{
    if (_t.tag() == 0 && _t.empty())
        error<EmptyList>();
    else if (_t.tag() == 0 && _t.front().which() != sp::utree_type::symbol_type)
        error<DataNotExecutable>();
    else
    {
        string s;
        string us;
        switch (_t.tag())
        {
        case 0:
        {
            auto sr = _t.front().get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
            s = string(sr.begin(), sr.end());
            us = boost::algorithm::to_upper_copy(s);
            break;
        }
        case 1:
            us = "MLOAD";
            break;
        case 2:
            us = "SLOAD";
            break;
        case 3:
            us = "MSTORE";
            break;
        case 4:
            us = "SSTORE";
            break;
        case 5:
            us = "SEQ";
            break;
        case 6:
            us = "CALLDATALOAD";
            break;
        default:;
        }

        auto firstAsString = [&]()
        {
            auto i = *++_t.begin();
            if (i.tag())
                error<InvalidName>(toString(i));
            if (i.which() == sp::utree_type::string_type)
            {
                auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
                return string(sr.begin(), sr.end());
            }
            else if (i.which() == sp::utree_type::symbol_type)
            {
                auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
                return _s.getDef(string(sr.begin(), sr.end())).m_asm.backString();
            }
            return string();
        };

        auto varAddress = [&](string const& n, bool createMissing = false)
        {
            if (n.empty())
                error<InvalidName>("Empty variable name not allowed");
            auto it = _s.vars.find(n);
            if (it == _s.vars.end())
            {
                if (createMissing)
                {
                    // Create new variable
                    bool ok;
                    tie(it, ok) = _s.vars.insert(make_pair(n, make_pair(_s.stackSize, 32)));
                    _s.stackSize += 32;
                }
                else
                    error<InvalidName>(std::string("Symbol not found: ") + n);
            }
            return it->second.first;
        };

        // Operations who args are not standard stack-pushers.
        bool nonStandard = true;
        if (us == "ASM")
        {
            int c = 0;
            for (auto const& i: _t)
                if (c++)
                    m_asm.append(CodeFragment(i, _s, m_readFile, true).m_asm);
        }
        else if (us == "INCLUDE")
        {
            if (_t.size() != 2)
                error<IncorrectParameterCount>(us);
            string fileName = firstAsString();
            if (fileName.empty())
                error<InvalidName>("Empty file name provided");
            if (!m_readFile)
                error<InvalidName>("Import callback not present");
            string contents = m_readFile(fileName);
            if (contents.empty())
                error<InvalidName>(std::string("File not found (or empty): ") + fileName);
            m_asm.append(CodeFragment::compile(contents, _s, m_readFile).m_asm);
        }
        else if (us == "SET")
        {
            if (_t.size() != 3)
                error<IncorrectParameterCount>(us);
            int c = 0;
            for (auto const& i: _t)
                if (c++ == 2)
                    m_asm.append(CodeFragment(i, _s, m_readFile, false).m_asm);
            m_asm.append((u256)varAddress(firstAsString(), true));
            m_asm.append(Instruction::MSTORE);
        }
        else if (us == "GET")
        {
            if (_t.size() != 2)
                error<IncorrectParameterCount>(us);
            m_asm.append((u256)varAddress(firstAsString()));
            m_asm.append(Instruction::MLOAD);
        }
        else if (us == "REF")
            m_asm.append((u256)varAddress(firstAsString()));
        else if (us == "DEF")
        {
            string n;
            unsigned ii = 0;
            if (_t.size() != 3 && _t.size() != 4)
                error<IncorrectParameterCount>(us);
            vector<string> args;
            for (auto const& i: _t)
            {
                if (ii == 1)
                {
                    if (i.tag())
                        error<InvalidName>(toString(i));
                    if (i.which() == sp::utree_type::string_type)
                    {
                        auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
                        n = string(sr.begin(), sr.end());
                    }
                    else if (i.which() == sp::utree_type::symbol_type)
                    {
                        auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
                        n = _s.getDef(string(sr.begin(), sr.end())).m_asm.backString();
                    }
                }
                else if (ii == 2)
                    if (_t.size() == 3)
                    {
                        /// NOTE: some compilers could do the assignment first if this is done in a single line
                        CodeFragment code = CodeFragment(i, _s, m_readFile);
                        _s.defs[n] = code;
                    }
                    else
                        for (auto const& j: i)
                        {
                            if (j.tag() || j.which() != sp::utree_type::symbol_type)
                                error<InvalidMacroArgs>();
                            auto sr = j.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
                            args.push_back(string(sr.begin(), sr.end()));
                        }
                else if (ii == 3)
                {
                    auto k = make_pair(n, args.size());
                    _s.macros[k].code = i;
                    _s.macros[k].env = _s.outers;
                    _s.macros[k].args = args;
                    for (auto const& i: _s.args)
                        _s.macros[k].env[i.first] = i.second;
                    for (auto const& i: _s.defs)
                        _s.macros[k].env[i.first] = i.second;
                }
                ++ii;
            }
        }
        else if (us == "LIT")
        {
            if (_t.size() < 3)
                error<IncorrectParameterCount>(us);
            unsigned ii = 0;
            CodeFragment pos;
            bytes data;
            for (auto const& i: _t)
            {
                if (ii == 0)
                {
                    ii++;
                    continue;
                }
                else if (ii == 1)
                {
                    pos = CodeFragment(i, _s, m_readFile);
                    if (pos.m_asm.deposit() != 1)
                        error<InvalidDeposit>(toString(i));
                }
                else if (i.tag() != 0)
                {
                    error<InvalidLiteral>(toString(i));
                }
                else if (i.which() == sp::utree_type::string_type)
                {
                    auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
                    data.insert(data.end(), (byte const *)sr.begin(), (byte const*)sr.end());
                }
                else if (i.which() == sp::utree_type::any_type)
                {
                    bigint bi = *i.get<bigint*>();
                    if (bi < 0)
                        error<IntegerOutOfRange>(toString(i));
                    else
                    {
                        bytes tmp = toCompactBigEndian(bi);
                        data.insert(data.end(), tmp.begin(), tmp.end());
                    }
                }
                else
                {
                    error<InvalidLiteral>(toString(i));
                }

                ii++;
            }
            m_asm.append((u256)data.size());
            m_asm.append(Instruction::DUP1);
            m_asm.append(data);
            m_asm.append(pos.m_asm, 1);
            m_asm.append(Instruction::CODECOPY);
        }
        else
            nonStandard = false;

        if (nonStandard)
            return;

        std::map<std::string, Instruction> const c_arith = {
            { "+", Instruction::ADD },
            { "-", Instruction::SUB },
            { "*", Instruction::MUL },
            { "/", Instruction::DIV },
            { "%", Instruction::MOD },
            { "&", Instruction::AND },
            { "|", Instruction::OR },
            { "^", Instruction::XOR }
        };
        std::map<std::string, pair<Instruction, bool>> const c_binary = {
            { "<", { Instruction::LT, false } },
            { "<=", { Instruction::GT, true } },
            { ">", { Instruction::GT, false } },
            { ">=", { Instruction::LT, true } },
            { "S<", { Instruction::SLT, false } },
            { "S<=", { Instruction::SGT, true } },
            { "S>", { Instruction::SGT, false } },
            { "S>=", { Instruction::SLT, true } },
            { "=", { Instruction::EQ, false } },
            { "!=", { Instruction::EQ, true } }
        };
        std::map<std::string, Instruction> const c_unary = {
            { "!", Instruction::ISZERO },
            { "~", Instruction::NOT }
        };

        vector<CodeFragment> code;
        CompilerState ns = _s;
        ns.vars.clear();
        ns.usedAlloc = false;
        int c = _t.tag() ? 1 : 0;
        for (auto const& i: _t)
            if (c++)
            {
                if (us == "LLL" && c == 1)
                    code.push_back(CodeFragment(i, ns, m_readFile));
                else
                    code.push_back(CodeFragment(i, _s, m_readFile));
            }
        auto requireSize = [&](unsigned s) { if (code.size() != s) error<IncorrectParameterCount>(us); };
        auto requireMinSize = [&](unsigned s) { if (code.size() < s) error<IncorrectParameterCount>(us); };
        auto requireMaxSize = [&](unsigned s) { if (code.size() > s) error<IncorrectParameterCount>(us); };
        auto requireDeposit = [&](unsigned i, int s) { if (code[i].m_asm.deposit() != s) error<InvalidDeposit>(us); };

        if (_s.macros.count(make_pair(s, code.size())))
        {
            Macro const& m = _s.macros.at(make_pair(s, code.size()));
            CompilerState cs = _s;
            for (auto const& i: m.env)
                cs.outers[i.first] = i.second;
            for (auto const& i: cs.defs)
                cs.outers[i.first] = i.second;
            cs.defs.clear();
            for (unsigned i = 0; i < m.args.size(); ++i)
            {
                //requireDeposit(i, 1);
                cs.args[m.args[i]] = code[i];
            }
            m_asm.append(CodeFragment(m.code, cs, m_readFile).m_asm);
            for (auto const& i: cs.defs)
                _s.defs[i.first] = i.second;
            for (auto const& i: cs.macros)
                _s.macros.insert(i);
        }
        else if (c_instructions.count(us) && validFunctionalInstruction(us))
        {
            auto it = c_instructions.find(us);
            requireSize(instructionInfo(it->second).args);

            for (unsigned i = code.size(); i; --i)
                m_asm.append(code[i - 1].m_asm, 1);
            m_asm.append(it->second);
        }
        else if (c_arith.count(us))
        {
            auto it = c_arith.find(us);
            requireMinSize(1);
            for (unsigned i = code.size(); i; --i)
            {
                requireDeposit(i - 1, 1);
                m_asm.append(code[i - 1].m_asm, 1);
            }
            for (unsigned i = 1; i < code.size(); ++i)
                m_asm.append(it->second);
        }
        else if (c_binary.count(us))
        {
            auto it = c_binary.find(us);
            requireSize(2);
            requireDeposit(0, 1);
            requireDeposit(1, 1);
            m_asm.append(code[1].m_asm, 1);
            m_asm.append(code[0].m_asm, 1);
            m_asm.append(it->second.first);
            if (it->second.second)
                m_asm.append(Instruction::ISZERO);
        }
        else if (c_unary.count(us))
        {
            auto it = c_unary.find(us);
            requireSize(1);
            requireDeposit(0, 1);
            m_asm.append(code[0].m_asm, 1);
            m_asm.append(it->second);
        }
        else if (us == "IF")
        {
            requireSize(3);
            requireDeposit(0, 1);
            int minDep = min(code[1].m_asm.deposit(), code[2].m_asm.deposit());

            m_asm.append(code[0].m_asm);
            auto mainBranch = m_asm.appendJumpI();

            /// The else branch.
            int startDeposit = m_asm.deposit();
            m_asm.append(code[2].m_asm, minDep);
            auto end = m_asm.appendJump();
            int deposit = m_asm.deposit();
            m_asm.setDeposit(startDeposit);

            /// The main branch.
            m_asm << mainBranch.tag();
            m_asm.append(code[1].m_asm, minDep);
            m_asm << end.tag();
            if (m_asm.deposit() != deposit)
                error<InvalidDeposit>(us);
        }
        else if (us == "WHEN" || us == "UNLESS")
        {
            requireSize(2);
            requireDeposit(0, 1);

            m_asm.append(code[0].m_asm);
            if (us == "WHEN")
                m_asm.append(Instruction::ISZERO);
            auto end = m_asm.appendJumpI();
            m_asm.append(code[1].m_asm, 0);
            m_asm << end.tag();
        }
        else if (us == "WHILE" || us == "UNTIL")
        {
            requireSize(2);
            requireDeposit(0, 1);

            auto begin = m_asm.append(m_asm.newTag());
            m_asm.append(code[0].m_asm);
            if (us == "WHILE")
                m_asm.append(Instruction::ISZERO);
            auto end = m_asm.appendJumpI();
            m_asm.append(code[1].m_asm, 0);
            m_asm.appendJump(begin);
            m_asm << end.tag();
        }
        else if (us == "FOR")
        {
            requireSize(4);
            requireDeposit(1, 1);

            m_asm.append(code[0].m_asm, 0);
            auto begin = m_asm.append(m_asm.newTag());
            m_asm.append(code[1].m_asm);
            m_asm.append(Instruction::ISZERO);
            auto end = m_asm.appendJumpI();
            m_asm.append(code[3].m_asm, 0);
            m_asm.append(code[2].m_asm, 0);
            m_asm.appendJump(begin);
            m_asm << end.tag();
        }
        else if (us == "SWITCH")
        {
            requireMinSize(1);

            bool hasDefault = (code.size() % 2 == 1);
            int startDeposit = m_asm.deposit();
            int targetDeposit = hasDefault ? code[code.size() - 1].m_asm.deposit() : 0;

            // The conditions
            eth::AssemblyItems jumpTags;
            for (unsigned i = 0; i < code.size() - 1; i += 2)
            {
                requireDeposit(i, 1);
                m_asm.append(code[i].m_asm);
                jumpTags.push_back(m_asm.appendJumpI());
            }

            // The default, if present
            if (hasDefault)
                m_asm.append(code[code.size() - 1].m_asm);

            // The targets - appending in reverse makes the top case the most efficient.
            if (code.size() > 1)
            {
                auto end = m_asm.appendJump();
                for (int i = 2 * (code.size() / 2 - 1); i >= 0; i -= 2)
                {
                    m_asm << jumpTags[i / 2].tag();
                    requireDeposit(i + 1, targetDeposit);
                    m_asm.append(code[i + 1].m_asm);
                    if (i != 0)
                        m_asm.appendJump(end);
                }
                m_asm << end.tag();
            }

            m_asm.setDeposit(startDeposit + targetDeposit);
        }
        else if (us == "ALLOC")
        {
            requireSize(1);
            requireDeposit(0, 1);

            // (alloc N):
            //  - Evaluates to (msize) before the allocation - the start of the allocated memory
            //  - Does not allocate memory when N is zero
            //  - Size of memory allocated is N bytes rounded up to a multiple of 32
            //  - Uses MLOAD to expand MSIZE to avoid modifying memory.

            auto end = m_asm.newTag();
            m_asm.append(Instruction::MSIZE); // Result will be original top of memory
            m_asm.append(code[0].m_asm, 1);   // The alloc argument N
            m_asm.append(Instruction::DUP1);
            m_asm.append(Instruction::ISZERO);// (alloc 0) does not change MSIZE
            m_asm.appendJumpI(end);
            m_asm.append(u256(1));
            m_asm.append(Instruction::DUP2);  // Copy N
            m_asm.append(Instruction::SUB);   // N-1
            m_asm.append(u256(0x1f));         // Bit mask
            m_asm.append(Instruction::NOT);   // Invert
            m_asm.append(Instruction::AND);   // Align N-1 on 32 byte boundary
            m_asm.append(Instruction::MSIZE); // MSIZE is cheap
            m_asm.append(Instruction::ADD);
            m_asm.append(Instruction::MLOAD); // Updates MSIZE
            m_asm.append(Instruction::POP);   // Discard the result of the MLOAD
            m_asm.append(end);
            m_asm.append(Instruction::POP);   // Discard duplicate N

            _s.usedAlloc = true;
        }
        else if (us == "LLL")
        {
            requireMinSize(2);
            requireMaxSize(3);
            requireDeposit(1, 1);

            auto subPush = m_asm.appendSubroutine(make_shared<eth::Assembly>(code[0].assembly(ns)));
            m_asm.append(Instruction::DUP1);
            if (code.size() == 3)
            {
                requireDeposit(2, 1);
                m_asm.append(code[2].m_asm, 1);
                m_asm.append(Instruction::LT);
                m_asm.append(Instruction::ISZERO);
                m_asm.append(Instruction::MUL);
                m_asm.append(Instruction::DUP1);
            }
            m_asm.append(subPush);
            m_asm.append(code[1].m_asm, 1);
            m_asm.append(Instruction::CODECOPY);
        }
        else if (us == "&&" || us == "||")
        {
            requireMinSize(1);
            for (unsigned i = 0; i < code.size(); ++i)
                requireDeposit(i, 1);

            auto end = m_asm.newTag();
            if (code.size() > 1)
            {
                m_asm.append((u256)(us == "||" ? 1 : 0));
                for (unsigned i = 1; i < code.size(); ++i)
                {
                    // Check if true - predicate
                    m_asm.append(code[i - 1].m_asm, 1);
                    if (us == "&&")
                        m_asm.append(Instruction::ISZERO);
                    m_asm.appendJumpI(end);
                }
                m_asm.append(Instruction::POP);
            }

            // Check if true - predicate
            m_asm.append(code.back().m_asm, 1);

            // At end now.
            m_asm.append(end);
        }
        else if (us == "SEQ")
        {
            unsigned ii = 0;
            for (auto const& i: code)
                if (++ii < code.size())
                    m_asm.append(i.m_asm, 0);
                else
                    m_asm.append(i.m_asm);
        }
        else if (us == "RAW")
        {
            for (auto const& i: code)
                m_asm.append(i.m_asm);
            // Leave only the last item on stack.
            while (m_asm.deposit() > 1)
                m_asm.append(Instruction::POP);
        }
        else if (us == "BYTECODESIZE")
        {
            m_asm.appendProgramSize();
        }
        else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
            m_asm.append((u256)varAddress(s));
        else
            error<InvalidOperation>("Unsupported keyword: '" + us + "'");
    }
}

CodeFragment CodeFragment::compile(string const& _src, CompilerState& _s, ReadCallback const& _readFile)
{
    CodeFragment ret;
    sp::utree o;
    parseTreeLLL(_src, o);
    if (!o.empty())
        ret = CodeFragment(o, _s, _readFile);
    _s.treesToKill.push_back(o);
    return ret;
}