|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <math.h> |
| 4 | +#include <type_traits> |
| 5 | +#include <tuple> |
| 6 | +#include <eosio/eosio.hpp> |
| 7 | +#include <eosio/asset.hpp> |
| 8 | +#include <eosio/contract.hpp> |
| 9 | +#include <eosio/system.hpp> |
| 10 | +#include <eosio/transaction.hpp> |
| 11 | +#include <eosio/serialize.hpp> |
| 12 | +#include <eosio/print.hpp> |
| 13 | +#include <eosio/name.hpp> |
| 14 | + |
| 15 | +using namespace eosio; |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +extern "C" { |
| 19 | + __attribute__((eosio_wasm_import)) |
| 20 | + uint64_t current_time(); |
| 21 | +} |
| 22 | + |
| 23 | +class [[eosio::contract("defertest")]] defertest : public eosio::contract { |
| 24 | + |
| 25 | +public: |
| 26 | + using contract::contract; |
| 27 | + |
| 28 | + int32_t now() { |
| 29 | + return current_time() / 1000000; |
| 30 | + } |
| 31 | + |
| 32 | + struct [[eosio::table]] pending { |
| 33 | + uint64_t id; |
| 34 | + eosio::name account; |
| 35 | + eosio::name miner; |
| 36 | + std::vector<char> rlptx; |
| 37 | + |
| 38 | + uint64_t primary_key() const { return id; } |
| 39 | + }; |
| 40 | + typedef eosio::multi_index<"pending"_n, pending> pending_table_t; |
| 41 | + |
| 42 | + // notify by the contract account of contract "defertest2.cpp" |
| 43 | + [[eosio::on_notify("*::notifytest")]] void notifytest(eosio::name recipient, eosio::name account, eosio::name miner, const std::vector<char> &rlptx, const std::vector<char> &rlptx2) { |
| 44 | + action act({_self, "active"_n}, account, "pushtx"_n, |
| 45 | + std::tuple<eosio::name, std::vector<char> >(miner, rlptx2)); |
| 46 | + act.send(); |
| 47 | + } |
| 48 | + |
| 49 | + [[eosio::action]] void pushtxinline(eosio::name account, eosio::name miner, const std::vector<char> &rlptx) { |
| 50 | + action act({_self, "active"_n}, account, "pushtx"_n, |
| 51 | + std::tuple<eosio::name, std::vector<char> >(miner, rlptx)); |
| 52 | + act.send(); |
| 53 | + } |
| 54 | + |
| 55 | + [[eosio::action]] void pushdefer(uint64_t id, eosio::name account, eosio::name miner, const std::vector<char> &rlptx, const std::vector<char> &rlptx2) { |
| 56 | + |
| 57 | + printf("enter defertest.cpp::pushdefer:%d %d", (int)rlptx.size(), (int)rlptx2.size()); |
| 58 | + |
| 59 | + action act(permission_level{_self, "active"_n}, account, "pushtx"_n, std::tuple<eosio::name, std::vector<char> >(miner, rlptx)); |
| 60 | + transaction txn(time_point_sec(current_time() + 3590)); |
| 61 | + txn.actions.push_back(act); |
| 62 | + auto serialize = pack(txn); |
| 63 | + ::send_deferred((uint128_t)(id), _self, serialize.data(), serialize.size(), true); |
| 64 | + |
| 65 | + if (rlptx2.size()) { |
| 66 | + pending_table_t pending_table(_self, _self.value); |
| 67 | + pending_table.emplace(_self, [&](auto &v) { |
| 68 | + v.id = id; |
| 69 | + v.account = account; |
| 70 | + v.miner = miner; |
| 71 | + v.rlptx = rlptx2; |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + [[eosio::on_notify("eosio::onerror")]] void onerror() { |
| 77 | + |
| 78 | + pending_table_t pending_table(_self, _self.value); |
| 79 | + if (pending_table.begin() != pending_table.end()) { |
| 80 | + |
| 81 | + printf("defertest.cpp::onerror() 1"); |
| 82 | + auto itr = pending_table.end(); |
| 83 | + --itr; |
| 84 | + action act(permission_level{_self, "active"_n}, itr->account, "pushtx"_n, |
| 85 | + std::tuple<eosio::name, std::vector<char> >(itr->miner, itr->rlptx)); |
| 86 | + act.send(); |
| 87 | + pending_table.erase(itr); |
| 88 | + } |
| 89 | + else { |
| 90 | + printf("defertest.cpp::onerror() 2"); |
| 91 | + check(false, "hard-fail"); |
| 92 | + } |
| 93 | + } |
| 94 | +}; |
0 commit comments