slow_tests.cpp 30.6 KB
Newer Older
N
Nathan Hourt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * Copyright (c) 2017, Respective Authors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <boost/test/unit_test.hpp>

#include <eos/chain/chain_controller.hpp>
#include <eos/chain/exceptions.hpp>
#include <eos/chain/account_object.hpp>
#include <eos/chain/key_value_object.hpp>
#include <eos/chain/block_summary_object.hpp>

#include <eos/utilities/tempdir.hpp>

#include <fc/crypto/digest.hpp>

#include "../common/database_fixture.hpp"

#include <Inline/BasicTypes.h>
#include <IR/Module.h>
#include <IR/Validate.h>
#include <WAST/WAST.h>
#include <WASM/WASM.h>
#include <Runtime/Runtime.h>

using namespace eos;
using namespace chain;

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
   struct OrderID {
      AccountName name;
      uint64_t    number  = 0;
   };

   struct Bid {
      OrderID            buyer;
      fc::uint128_t      price;
      uint64_t           quantity;
      Time               expiration;
   };
   struct Ask {
      OrderID            seller;
      fc::uint128_t      price;
      uint64_t           quantity;
      Time               expiration;
   };
FC_REFLECT( OrderID, (name)(number) );
FC_REFLECT( Bid, (buyer)(price)(quantity)(expiration) );
FC_REFLECT( Ask, (seller)(price)(quantity)(expiration) );

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
   struct record {
      uint64_t a = 0;
      uint64_t b = 0;
      uint64_t c = 0;
      fc::uint128 d = 0;
      fc::uint128 e = 0;
   };

   struct by_abcde;
   struct by_abced;
   using test_index = boost::multi_index_container<
      record,
      indexed_by<
         ordered_unique<tag<by_abcde>, 
            composite_key< record,
               member<record, uint64_t, &record::a>,
               member<record, uint64_t, &record::b>,
               member<record, uint64_t, &record::c>,
               member<record, fc::uint128, &record::d>,
               member<record, fc::uint128, &record::e>
            >
         >,
         ordered_unique<tag<by_abced>, 
            composite_key< record,
               member<record, uint64_t, &record::a>,
               member<record, uint64_t, &record::b>,
               member<record, uint64_t, &record::c>,
               member<record, fc::uint128, &record::e>,
               member<record, fc::uint128, &record::d>
            >
         >
      >
   >;
//   FC_REFLECT( record, (a)(b)(c)(d)(e) );

N
Nathan Hourt 已提交
105 106
BOOST_AUTO_TEST_SUITE(slow_tests)

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

BOOST_FIXTURE_TEST_CASE(multiindex, testing_fixture)
{
   test_index idx;
   idx.emplace( record{1,0,0,1,9} );
   idx.emplace( record{1,1,1,2,8} );
   idx.emplace( record{1,2,3,3,7} );
   idx.emplace( record{1,2,3,5,6} );
   idx.emplace( record{1,2,3,6,5} );
   idx.emplace( record{1,2,3,7,5} );
   idx.emplace( record{1,2,3,8,3} );

   auto& by_de = idx.get<by_abcde>();
   auto& by_ed = idx.get<by_abced>();

   auto itr = by_de.lower_bound( boost::make_tuple(1,2,3,0,0) );
   BOOST_REQUIRE( itr != by_de.end() );
   BOOST_REQUIRE( itr->d == 3 );

   auto itr2 = by_ed.lower_bound( boost::make_tuple(1,2,3,0,0) );
   BOOST_REQUIRE( itr2 != by_ed.end() );
   BOOST_REQUIRE( itr2->e == 3 );
   //BOOST_REQUIRE( itr2 == by_ed.begin() );
   Make_Blockchain(chain)
   auto& db = chain.get_mutable_database();
   db.create<key128x128_value_object>( [&]( auto& obj ) {
        obj.scope = 1;
        obj.code = 1;
        obj.table = 1;
        obj.primary_key = 1;
        obj.secondary_key = 2;
   });
   db.create<key128x128_value_object>( [&]( auto& obj ) {
        obj.scope = 1;
        obj.code = 1;
        obj.table = 1;
        obj.primary_key = 2;
        obj.secondary_key = 3;
   });
   db.create<key128x128_value_object>( [&]( auto& obj ) {
        obj.scope = 1;
        obj.code = 1;
        obj.table = 1;
        obj.primary_key = 3;
        obj.secondary_key = 2;
   });
   {
   auto& sidx = db.get_index< key128x128_value_index, by_scope_secondary> ();
   auto lower = sidx.lower_bound( boost::make_tuple( 1, 1, 1, 0, 0 ) );
   BOOST_REQUIRE( lower != sidx.end() );
   BOOST_REQUIRE( lower->primary_key == 1 );
   BOOST_REQUIRE( lower->secondary_key == 2 );
   }

}

N
Nathan Hourt 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
// Test that TaPoS still works after block 65535 (See Issue #55)
BOOST_FIXTURE_TEST_CASE(tapos_wrap, testing_fixture)
{ try {
      Make_Blockchain(chain)
      Make_Account(chain, acct);
      Transfer_Asset(chain, system, acct, Asset(5));
      Stake_Asset(chain, acct, Asset(5).amount);
      wlog("Hang on, this will take a minute...");
      chain.produce_blocks(65536);
      Begin_Unstake_Asset(chain, acct, Asset(1).amount);
} FC_LOG_AND_RETHROW() }

// Verify that staking and unstaking works
BOOST_FIXTURE_TEST_CASE(stake, testing_fixture)
{ try {
   // Create account sam with default balance of 100, and stake 55 of it
   Make_Blockchain(chain);
   Make_Account(chain, sam);
   Transfer_Asset(chain, inita, sam, Asset(55) );

   // MakeAccount should start sam out with some staked balance
   BOOST_REQUIRE_EQUAL(chain.get_staked_balance("sam"), Asset(100).amount);

   Stake_Asset(chain, sam, Asset(55).amount);

   // Check balances
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(155).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(0).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(0).amount);

   chain.produce_blocks();

   // Start unstaking 20, check balances
   BOOST_CHECK_THROW(Begin_Unstake_Asset(chain, sam, Asset(156).amount), chain::message_precondition_exception);
   Begin_Unstake_Asset(chain, sam, Asset(20).amount);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(135).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(20).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(0).amount);

   // Make sure we can't liquidate early
   BOOST_CHECK_THROW(Finish_Unstake_Asset(chain, sam, Asset(10).amount), chain::message_precondition_exception);

   // Fast forward to when we can liquidate
   wlog("Hang on, this will take a minute...");
   chain.produce_blocks(config::StakedBalanceCooldownSeconds / config::BlockIntervalSeconds + 1);

   BOOST_CHECK_THROW(Finish_Unstake_Asset(chain, sam, Asset(21).amount), chain::message_precondition_exception);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(135).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(20).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(0).amount);

   // Liquidate 10 of the 20 unstaking and check balances
   Finish_Unstake_Asset(chain, sam, Asset(10).amount);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(135).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(10).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(10).amount);

   // Liquidate 2 of the 10 left unstaking and check balances
   Finish_Unstake_Asset(chain, sam, Asset(2).amount);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(135).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(8).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(12).amount);

   // Ignore the 8 left in unstaking, and begin unstaking 5, which should restake the 8, and start over unstaking 5
   Begin_Unstake_Asset(chain, sam, Asset(5).amount);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(138).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(5).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(12).amount);

   // Begin unstaking 20, which should only deduct 15 from staked, since 5 was already in unstaking
   Begin_Unstake_Asset(chain, sam, Asset(20).amount);
   BOOST_CHECK_EQUAL(chain.get_staked_balance("sam"), Asset(123).amount);
   BOOST_CHECK_EQUAL(chain.get_unstaking_balance("sam"), Asset(20).amount);
   BOOST_CHECK_EQUAL(chain.get_liquid_balance("sam"), Asset(12).amount);
} FC_LOG_AND_RETHROW() }

vector<uint8_t> assemble_wast( const std::string& wast ) {
   //   std::cout << "\n" << wast << "\n";
   IR::Module module;
   std::vector<WAST::Error> parseErrors;
   WAST::parseModule(wast.c_str(),wast.size(),module,parseErrors);
   if(parseErrors.size())
   {
      // Print any parse errors;
      std::cerr << "Error parsing WebAssembly text file:" << std::endl;
      for(auto& error : parseErrors)
      {
         std::cerr << ":" << error.locus.describe() << ": " << error.message.c_str() << std::endl;
         std::cerr << error.locus.sourceLine << std::endl;
         std::cerr << std::setw(error.locus.column(8)) << "^" << std::endl;
      }
      FC_ASSERT( !"error parsing wast" );
   }

   try
   {
      // Serialize the WebAssembly module.
      Serialization::ArrayOutputStream stream;
      WASM::serialize(stream,module);
      return stream.getBytes();
   }
   catch(Serialization::FatalSerializationException exception)
   {
      std::cerr << "Error serializing WebAssembly binary file:" << std::endl;
      std::cerr << exception.message << std::endl;
      throw;
   }
}

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
void SetCode( testing_blockchain& chain, AccountName account, const char* wast ) {
   try {
      types::setcode handler;
      handler.account = account;

      auto wasm = assemble_wast( wast );
      handler.code.resize(wasm.size());
      memcpy( handler.code.data(), wasm.data(), wasm.size() );

      {
         eos::chain::SignedTransaction trx;
         trx.scope = {account};
         trx.messages.resize(1);
         trx.messages[0].code = config::SystemContractName;
         trx.messages[0].recipients = {account};
         trx.setMessage(0, "setcode", handler);
         trx.expiration = chain.head_block_time() + 100;
         trx.set_reference_block(chain.head_block_id());
         chain.push_transaction(trx);
         chain.produce_blocks(1);
      }
} FC_LOG_AND_RETHROW( ) }

void TransferCurrency( testing_blockchain& chain, AccountName from, AccountName to, uint64_t amount ) {
   eos::chain::SignedTransaction trx;
   trx.scope = sort_names({from,to});
   trx.emplaceMessage("currency", sort_names( {from,to} ),
                      vector<types::AccountPermission>{ {from,"active"} },
                      "transfer", types::transfer{from, to, amount, ""});
   trx.expiration = chain.head_block_time() + 100;
   trx.set_reference_block(chain.head_block_id());
   chain.push_transaction(trx);
}

void WithdrawCurrency( testing_blockchain& chain, AccountName from, AccountName to, uint64_t amount ) {
   eos::chain::SignedTransaction trx;
   trx.scope = sort_names({from,to});
   trx.emplaceMessage("currency", sort_names( {from,to} ),
                      vector<types::AccountPermission>{ {from,"active"},{to,"active"} },
                      "transfer", types::transfer{from, to, amount, ""});
   trx.expiration = chain.head_block_time() + 100;
   trx.set_reference_block(chain.head_block_id());
   chain.push_transaction(trx);
}

N
Nathan Hourt 已提交
317 318 319 320 321
//Test account script processing
BOOST_FIXTURE_TEST_CASE(create_script, testing_fixture)
{ try {
      Make_Blockchain(chain);
      chain.produce_blocks(10);
322
      Make_Account(chain, currency);
N
Nathan Hourt 已提交
323 324
      chain.produce_blocks(1);

325
#include <currency/currency.wast.hpp>
N
Nathan Hourt 已提交
326 327

      types::setcode handler;
328
      handler.account = "currency";
N
Nathan Hourt 已提交
329

330
      auto wasm = assemble_wast( currency_wast );
N
Nathan Hourt 已提交
331 332 333 334 335
      handler.code.resize(wasm.size());
      memcpy( handler.code.data(), wasm.data(), wasm.size() );

      {
         eos::chain::SignedTransaction trx;
336
         trx.scope = {"currency"};
N
Nathan Hourt 已提交
337
         trx.messages.resize(1);
N
Nathan Hourt 已提交
338
         trx.messages[0].code = config::SystemContractName;
339
         trx.messages[0].recipients = {"currency"};
N
Nathan Hourt 已提交
340 341 342 343 344 345 346 347 348
         trx.setMessage(0, "setcode", handler);
         trx.expiration = chain.head_block_time() + 100;
         trx.set_reference_block(chain.head_block_id());
         chain.push_transaction(trx);
         chain.produce_blocks(1);
      }


      auto start = fc::time_point::now();
349
      for (uint32_t i = 0; i < 10000; ++i)
N
Nathan Hourt 已提交
350 351
      {
         eos::chain::SignedTransaction trx;
352
         trx.scope = sort_names({"currency","inita"});
353
         trx.emplaceMessage("currency", vector<AccountName>{"inita","currency"},
354
                            vector<types::AccountPermission>{ {"currency","active"} },
355
                            "transfer", types::transfer{"currency", "inita", 1+i, "hello"});
N
Nathan Hourt 已提交
356 357
         trx.expiration = chain.head_block_time() + 100;
         trx.set_reference_block(chain.head_block_id());
358
         //idump((trx));
N
Nathan Hourt 已提交
359 360 361
         chain.push_transaction(trx);
      }
      auto end = fc::time_point::now();
362
      idump((10000*1000000.0 / (end-start).count()));
N
Nathan Hourt 已提交
363 364 365 366 367 368

      chain.produce_blocks(10);

} FC_LOG_AND_RETHROW() }


369 370 371 372 373
static const uint64_t precision = 1000ll*1000ll*1000ll*1000ll*1000ll;
fc::uint128_t to_price( double p ) {
   uint64_t  pi(p);
   fc::uint128_t result(pi);
   result *= precision;
N
Nathan Hourt 已提交
374

375 376 377
   double fract = p - pi;
   result += uint64_t( fract * precision );
   return result;
N
Nathan Hourt 已提交
378 379 380
}


381
void SellCurrency( testing_blockchain& chain, AccountName seller, AccountName exchange, uint64_t ordernum, uint64_t cur_quantity, double price ) {
N
Nathan Hourt 已提交
382

383 384 385 386 387
   Ask b {  OrderID{seller,ordernum}, 
            to_price(price),
            cur_quantity, 
            chain.head_block_time()+fc::days(3) 
         };
N
Nathan Hourt 已提交
388

389 390
   eos::chain::SignedTransaction trx;
   trx.scope = sort_names({"exchange"});
391
   trx.emplaceMessage("exchange", sort_names( {} ),
392 393 394 395 396
                      vector<types::AccountPermission>{ {seller,"active"} },
                      "sell", b );
   trx.expiration = chain.head_block_time() + 100;
   trx.set_reference_block(chain.head_block_id());
   chain.push_transaction(trx);
N
Nathan Hourt 已提交
397 398

}
399 400 401 402 403 404 405 406 407
void BuyCurrency( testing_blockchain& chain, AccountName buyer, AccountName exchange, uint64_t ordernum, uint64_t cur_quantity, double price ) {
   Bid b {  OrderID{buyer,ordernum}, 
            to_price(price),
            cur_quantity, 
            chain.head_block_time()+fc::days(3) 
         };

   eos::chain::SignedTransaction trx;
   trx.scope = sort_names({"exchange"});
408
   trx.emplaceMessage("exchange", sort_names( {} ),
409 410 411 412 413
                      vector<types::AccountPermission>{ {buyer,"active"} },
                      "buy", b );
   trx.expiration = chain.head_block_time() + 100;
   trx.set_reference_block(chain.head_block_id());
   chain.push_transaction(trx);
N
Nathan Hourt 已提交
414 415
}

416 417 418 419 420 421 422 423 424 425
BOOST_FIXTURE_TEST_CASE(create_exchange, testing_fixture) {
#include <currency/currency.wast.hpp>
#include <exchange/exchange.wast.hpp>
  try {
   try {
      Make_Blockchain(chain);
      chain.produce_blocks(2);
      Make_Account(chain, currency);
      Make_Account(chain, exchange);
      chain.produce_blocks(1);
N
Nathan Hourt 已提交
426

427 428
      SetCode(chain, "currency", currency_wast);
      SetCode(chain, "exchange", exchange_wast);
N
Nathan Hourt 已提交
429

430
      chain.produce_blocks(1);
N
Nathan Hourt 已提交
431

432 433 434
      ilog( "transfering currency to the users" );
      TransferCurrency( chain, "currency", "inita", 1000 );
      TransferCurrency( chain, "currency", "initb", 2000 );
N
Nathan Hourt 已提交
435

436 437 438 439 440 441
      Transfer_Asset(chain, system, inita, Asset(50));
      Transfer_Asset(chain, system, initb, Asset(50));
      chain.produce_blocks(1);
      ilog( "transfering funds to the exchange" );
      TransferCurrency( chain, "inita", "exchange", 1000 );
      TransferCurrency( chain, "initb", "exchange", 2000 );
N
Nathan Hourt 已提交
442

443 444
      Transfer_Asset(chain, inita, exchange, Asset(500));
      Transfer_Asset(chain, initb, exchange, Asset(500));
N
Nathan Hourt 已提交
445

446
      BOOST_REQUIRE_THROW( TransferCurrency( chain, "initb", "exchange", 2000 ), fc::exception ); // insufficient funds
N
Nathan Hourt 已提交
447 448


449
      BOOST_REQUIRE_THROW( WithdrawCurrency( chain, "exchange", "initb", 2001 ), fc::exception ); // insufficient funds
N
Nathan Hourt 已提交
450

451
      ilog( "withdrawing from exchange" );
N
Nathan Hourt 已提交
452

453 454
      WithdrawCurrency( chain, "exchange", "initb", 2000 );
      chain.produce_blocks(1);
N
Nathan Hourt 已提交
455

456 457 458
      ilog( "send back to exchange" );
      TransferCurrency( chain, "initb", "exchange", 2000 );
      chain.produce_blocks(1);
N
Nathan Hourt 已提交
459

460
      wlog( "start buy and sell" );
461
      SellCurrency( chain, "initb", "exchange", 1, 100, .5 );
462 463
      SellCurrency( chain, "initb", "exchange", 1, 100, .75 );
      SellCurrency( chain, "initb", "exchange", 1, 100, .85 );
464
      //BOOST_REQUIRE_THROW( SellCurrency( chain, "initb", "exchange", 1, 100, .5 ), fc::exception ); // order id already exists
465
      //SellCurrency( chain, "initb", "exchange", 2, 100, .75 );
N
Nathan Hourt 已提交
466

467 468
      BuyCurrency( chain, "initb", "exchange", 1, 50, .25 ); 
      //BOOST_REQUIRE_THROW( BuyCurrency( chain, "initb", "exchange", 1, 50, .25 ), fc::exception );  // order id already exists
N
Nathan Hourt 已提交
469

470
      /// this should buy 5 from initb order 2 at a price of .75
471
      //BuyCurrency( chain, "initb", "exchange", 2, 50, .8 ); 
N
Nathan Hourt 已提交
472

473 474 475 476
   } FC_LOG_AND_RETHROW() 
  }catch(...) {
     elog( "unexpected exception" );
  }
N
Nathan Hourt 已提交
477 478
}

479 480 481 482 483 484 485 486 487
//Test account script float rejection
BOOST_FIXTURE_TEST_CASE(create_script_w_float, testing_fixture)
{ try {
      Make_Blockchain(chain);
      chain.produce_blocks(10);
      Make_Account(chain, simplecoin);
      chain.produce_blocks(1);


N
Nathan Hourt 已提交
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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
std::string wast_apply =
R"(
(module
  (type $FUNCSIG$vii (func (param i32 i32)))
  (type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
  (type $FUNCSIG$iii (func (param i32 i32) (result i32)))
  (type $FUNCSIG$iiiii (func (param i32 i32 i32 i32) (result i32)))
  (type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32)))
  (import "env" "AccountName_unpack" (func $AccountName_unpack (param i32 i32)))
  (import "env" "Varint_unpack" (func $Varint_unpack (param i32 i32)))
  (import "env" "assert" (func $assert (param i32 i32)))
  (import "env" "load" (func $load (param i32 i32 i32 i32) (result i32)))
  (import "env" "memcpy" (func $memcpy (param i32 i32 i32) (result i32)))
  (import "env" "readMessage" (func $readMessage (param i32 i32) (result i32)))
  (import "env" "remove" (func $remove (param i32 i32) (result i32)))
  (import "env" "store" (func $store (param i32 i32 i32 i32)))
  (table 0 anyfunc)
  (memory $0 1)
  (data (i32.const 8224) "out of memory\00")
  (data (i32.const 8240) "String is longer than account name allows\00")
  (data (i32.const 8288) "read past end of stream\00")
  (data (i32.const 8368) "simplecoin\00")
  (data (i32.const 8608) "no existing balance\00")
  (data (i32.const 8640) "insufficient funds\00")
  (export "memory" (memory $0))
  (export "malloc" (func $malloc))
  (export "DataStream_init" (func $DataStream_init))
  (export "AccountName_initString" (func $AccountName_initString))
  (export "AccountName_initCString" (func $AccountName_initCString))
  (export "uint64_unpack" (func $uint64_unpack))
  (export "String_unpack" (func $String_unpack))
  (export "Transfer_unpack" (func $Transfer_unpack))
  (export "onInit" (func $onInit))
  (export "onApply_Transfer_simplecoin" (func $onApply_Transfer_simplecoin))
  (func $malloc (param $0 i32) (result i32)
    (local $1 i32)
    (i32.store offset=8208
      (i32.const 0)
      (tee_local $0
        (i32.add
          (tee_local $1
            (i32.load offset=8208
              (i32.const 0)
            )
          )
          (i32.and
            (i32.add
              (get_local $0)
              (i32.const 7)
            )
            (i32.const -8)
          )
        )
      )
    )
    (call $assert
      (i32.lt_u
        (get_local $0)
        (i32.const 8192)
      )
      (i32.const 8224)
    )
    (i32.add
      (get_local $1)
      (i32.const 16)
    )
  )
  (func $DataStream_init (param $0 i32) (param $1 i32) (param $2 i32)
    (i32.store
      (get_local $0)
      (get_local $1)
    )
    (i32.store offset=4
      (get_local $0)
      (get_local $1)
    )
    (i32.store offset=8
      (get_local $0)
      (i32.add
        (get_local $1)
        (get_local $2)
      )
    )
  )
  (func $AccountName_initString (param $0 i32) (param $1 i32)
    (call $assert
      (i32.lt_u
        (i32.load
          (get_local $1)
        )
        (i32.const 33)
      )
      (i32.const 8240)
    )
    (drop
      (call $memcpy
        (get_local $0)
        (i32.add
          (get_local $1)
          (i32.const 4)
        )
        (i32.load
          (get_local $1)
        )
      )
    )
  )
  (func $AccountName_initCString (param $0 i32) (param $1 i32) (param $2 i32)
    (call $assert
      (i32.lt_u
        (get_local $2)
        (i32.const 33)
      )
      (i32.const 8240)
    )
    (drop
      (call $memcpy
        (get_local $0)
        (get_local $1)
        (get_local $2)
      )
    )
  )
  (func $uint64_unpack (param $0 i32) (param $1 i32)
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=4
            (get_local $0)
          )
          (i32.const 8)
        )
        (i32.load offset=8
          (get_local $0)
        )
      )
      (i32.const 8288)
    )
    (i64.store align=1
      (get_local $1)
      (i64.load align=1
        (i32.load offset=4
          (get_local $0)
        )
      )
    )
    (i32.store offset=4
      (get_local $0)
      (i32.add
        (i32.load offset=4
          (get_local $0)
        )
        (i32.const 8)
      )
    )
  )
  (func $String_unpack (param $0 i32) (param $1 i32)
    (local $2 i32)
    (local $3 i32)
    (call $Varint_unpack
      (get_local $0)
      (i32.const 8312)
    )
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=4
            (get_local $0)
          )
          (i32.load offset=8312
            (i32.const 0)
          )
        )
        (i32.load offset=8
          (get_local $0)
        )
      )
      (i32.const 8288)
    )
    (i32.store offset=8208
      (i32.const 0)
      (tee_local $3
        (i32.add
          (i32.and
            (i32.add
              (i32.load offset=8312
                (i32.const 0)
              )
              (i32.const 11)
            )
            (i32.const -8)
          )
          (tee_local $2
            (i32.load offset=8208
              (i32.const 0)
            )
          )
        )
      )
    )
    (call $assert
      (i32.lt_u
        (get_local $3)
        (i32.const 8192)
      )
      (i32.const 8224)
    )
    (drop
      (call $memcpy
        (i32.add
          (get_local $2)
          (i32.const 20)
        )
        (i32.load offset=4
          (get_local $0)
        )
        (i32.load offset=8312
          (i32.const 0)
        )
      )
    )
    (i32.store
      (get_local $1)
      (i32.add
        (get_local $2)
        (i32.const 16)
      )
    )
  )
  (func $Transfer_unpack (param $0 i32) (param $1 i32)
    (local $2 i32)
    (local $3 i32)
    (call $AccountName_unpack
      (get_local $0)
      (get_local $1)
    )
    (call $AccountName_unpack
      (get_local $0)
      (i32.add
        (get_local $1)
        (i32.const 32)
      )
    )
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=4
            (get_local $0)
          )
          (i32.const 8)
        )
        (i32.load offset=8
          (get_local $0)
        )
      )
      (i32.const 8288)
    )
    (i64.store offset=64 align=1
      (get_local $1)
      (i64.load align=1
        (i32.load offset=4
          (get_local $0)
        )
      )
    )
    (i32.store offset=4
      (get_local $0)
      (i32.add
        (i32.load offset=4
          (get_local $0)
        )
        (i32.const 8)
      )
    )
    (call $Varint_unpack
      (get_local $0)
      (i32.const 8312)
    )
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=4
            (get_local $0)
          )
          (i32.load offset=8312
            (i32.const 0)
          )
        )
        (i32.load offset=8
          (get_local $0)
        )
      )
      (i32.const 8288)
    )
    (i32.store offset=8208
      (i32.const 0)
      (tee_local $3
        (i32.add
          (i32.and
            (i32.add
              (i32.load offset=8312
                (i32.const 0)
              )
              (i32.const 11)
            )
            (i32.const -8)
          )
          (tee_local $2
            (i32.load offset=8208
              (i32.const 0)
            )
          )
        )
      )
    )
    (call $assert
      (i32.lt_u
        (get_local $3)
        (i32.const 8192)
      )
      (i32.const 8224)
    )
    (drop
      (call $memcpy
        (i32.add
          (get_local $2)
          (i32.const 20)
        )
        (i32.load offset=4
          (get_local $0)
        )
        (i32.load offset=8312
          (i32.const 0)
        )
      )
    )
    (i32.store offset=72
      (get_local $1)
      (i32.add
        (get_local $2)
        (i32.const 16)
      )
    )
  )
  (func $onInit
    (call $assert
      (i32.const 1)
      (i32.const 8240)
    )
    (i64.store offset=8320
      (i32.const 0)
      (i64.const 1000000)
    )
    (i32.store16 offset=8336
      (i32.const 0)
      (i32.load16_u offset=8376 align=1
        (i32.const 0)
      )
    )
    (i64.store offset=8328
      (i32.const 0)
      (i64.load offset=8368 align=1
        (i32.const 0)
      )
    )
    (call $store
      (i32.const 8328)
      (i32.const 32)
      (i32.const 8320)
      (i32.const 8)
    )
  )
  (func $onApply_Transfer_simplecoin
    (local $0 i32)
    (local $1 i32)
    (local $2 i64)
    (set_local $0
      (call $readMessage
        (i32.const 8384)
        (i32.const 100)
      )
    )
    (i32.store offset=8568
      (i32.const 0)
      (i32.const 8384)
    )
    (i32.store offset=8572
      (i32.const 0)
      (i32.const 8384)
    )
    (i32.store offset=8576
      (i32.const 0)
      (i32.add
        (get_local $0)
        (i32.const 8384)
      )
    )
    (call $AccountName_unpack
      (i32.const 8568)
      (i32.const 8488)
    )
    (call $AccountName_unpack
      (i32.const 8568)
      (i32.const 8520)
    )
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=8572
            (i32.const 0)
          )
          (i32.const 8)
        )
        (i32.load offset=8576
          (i32.const 0)
        )
      )
      (i32.const 8288)
    )
    (i64.store offset=8552
      (i32.const 0)
      (i64.load align=1
        (tee_local $0
          (i32.load offset=8572
            (i32.const 0)
          )
        )
      )
    )
    (i32.store offset=8572
      (i32.const 0)
      (i32.add
        (get_local $0)
        (i32.const 8)
      )
    )
    (call $Varint_unpack
      (i32.const 8568)
      (i32.const 8312)
    )
    (call $assert
      (i32.le_u
        (i32.add
          (i32.load offset=8572
            (i32.const 0)
          )
          (i32.load offset=8312
            (i32.const 0)
          )
        )
        (i32.load offset=8576
          (i32.const 0)
        )
      )
      (i32.const 8288)
    )
    (i32.store offset=8208
      (i32.const 0)
      (tee_local $1
        (i32.add
          (i32.and
            (i32.add
              (i32.load offset=8312
                (i32.const 0)
              )
              (i32.const 11)
            )
            (i32.const -8)
          )
          (tee_local $0
            (i32.load offset=8208
              (i32.const 0)
            )
          )
        )
      )
    )
    (call $assert
      (i32.lt_u
        (get_local $1)
        (i32.const 8192)
      )
      (i32.const 8224)
    )
    (drop
      (call $memcpy
        (i32.add
          (get_local $0)
          (i32.const 20)
        )
        (i32.load offset=8572
          (i32.const 0)
        )
        (i32.load offset=8312
          (i32.const 0)
        )
      )
    )
    (i64.store offset=8592
      (i32.const 0)
      (i64.const 0)
    )
    (i32.store offset=8560
      (i32.const 0)
      (i32.add
        (get_local $0)
        (i32.const 16)
      )
    )
    (call $assert
      (i32.eq
        (call $load
          (i32.const 8488)
          (i32.const 32)
          (i32.const 8584)
          (i32.const 8)
        )
        (i32.const 8)
      )
      (i32.const 8608)
    )
    (call $assert
      (i64.ge_s
        (i64.load offset=8584
          (i32.const 0)
        )
        (i64.load offset=8552
          (i32.const 0)
        )
      )
      (i32.const 8640)
    )
    (drop
      (call $load
        (i32.const 8520)
        (i32.const 32)
        (i32.const 8592)
        (i32.const 8)
      )
    )
    (i64.store offset=8592
      (i32.const 0)
      (i64.add
        (i64.load offset=8592
          (i32.const 0)
        )
        (tee_local $2
          (i64.load offset=8552
            (i32.const 0)
          )
        )
      )
    )
    (i64.store offset=8584
      (i32.const 0)
      (tee_local $2
        (i64.sub
          (i64.load offset=8584
            (i32.const 0)
          )
          (get_local $2)
        )
      )
    )
    (block $label$0
      (br_if $label$0
        (f64.lt
          (f64.add
            (f64.convert_s/i64
              (get_local $2)
            )
            (f64.const 0.5)
          )
          (f64.const 50.5)
        )
      )
      (block $label$1
        (br_if $label$1
          (i64.eqz
            (get_local $2)
          )
        )
        (call $store
          (i32.const 8488)
          (i32.const 32)
          (i32.const 8584)
          (i32.const 8)
        )
        (br $label$0)
      )
      (drop
        (call $remove
          (i32.const 8488)
          (i32.const 32)
        )
      )
    )
    (call $store
      (i32.const 8520)
      (i32.const 32)
      (i32.const 8592)
      (i32.const 8)
    )
  )
)

)";

      types::setcode handler;
      handler.account = "simplecoin";

      auto wasm = assemble_wast( wast_apply );
      handler.code.resize(wasm.size());
      memcpy( handler.code.data(), wasm.data(), wasm.size() );

      eos::chain::SignedTransaction trx;
1104
      trx.scope = {"simplecoin"};
N
Nathan Hourt 已提交
1105
      trx.messages.resize(1);
N
Nathan Hourt 已提交
1106 1107
      trx.messages[0].code = config::SystemContractName;
      trx.messages[0].recipients = {"simplecoin"};
N
Nathan Hourt 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
      trx.setMessage(0, "setcode", handler);
      trx.expiration = chain.head_block_time() + 100;
      trx.set_reference_block(chain.head_block_id());
      try {
         chain.push_transaction(trx);
         BOOST_FAIL("floating point instructions should be rejected");
/*      } catch (const Serialization::FatalSerializationException& fse) {
         BOOST_CHECK_EQUAL("float instructions not allowed", fse.message);
*/    } catch (const std::exception& exp) {
        BOOST_FAIL("Serialization::FatalSerializationException does not inherit from std::exception");
      } catch (...) {
        // empty throw expected, since
      }
} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_SUITE_END()