Skip to content

Commit

Permalink
Merge pull request #131 from eosnetworkfoundation/elmato/ship-receive…
Browse files Browse the repository at this point in the history
…-plugin-test-cases

Add ship-receiver-plugin defer tests
  • Loading branch information
elmato authored Dec 5, 2023
2 parents 6841ae4 + 9ccb787 commit 62393af
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ configure_file(nodeos_eos_evm_ws_test_basic.py . COPYONLY)
configure_file(nodeos_eos_evm_ws_test_fork.py . COPYONLY)
configure_file(nodeos_eos_evm_server.py . COPYONLY)
configure_file(nodeos_eos_evm_test.py . COPYONLY)
configure_file(defertest.wasm . COPYONLY)
configure_file(defertest.abi . COPYONLY)
configure_file(defertest2.wasm . COPYONLY)
configure_file(defertest2.abi . COPYONLY)
97 changes: 97 additions & 0 deletions tests/defertest.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT ",
"version": "eosio::abi/1.2",
"types": [],
"structs": [
{
"name": "pending",
"base": "",
"fields": [
{
"name": "id",
"type": "uint64"
},
{
"name": "account",
"type": "name"
},
{
"name": "miner",
"type": "name"
},
{
"name": "rlptx",
"type": "bytes"
}
]
},
{
"name": "pushdefer",
"base": "",
"fields": [
{
"name": "id",
"type": "uint64"
},
{
"name": "account",
"type": "name"
},
{
"name": "miner",
"type": "name"
},
{
"name": "rlptx",
"type": "bytes"
},
{
"name": "rlptx2",
"type": "bytes"
}
]
},
{
"name": "pushtxinline",
"base": "",
"fields": [
{
"name": "account",
"type": "name"
},
{
"name": "miner",
"type": "name"
},
{
"name": "rlptx",
"type": "bytes"
}
]
}
],
"actions": [
{
"name": "pushdefer",
"type": "pushdefer",
"ricardian_contract": ""
},
{
"name": "pushtxinline",
"type": "pushtxinline",
"ricardian_contract": ""
}
],
"tables": [
{
"name": "pending",
"type": "pending",
"index_type": "i64",
"key_names": [],
"key_types": []
}
],
"ricardian_clauses": [],
"variants": [],
"action_results": []
}
94 changes: 94 additions & 0 deletions tests/defertest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <type_traits>
#include <tuple>
#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>
#include <eosio/contract.hpp>
#include <eosio/system.hpp>
#include <eosio/transaction.hpp>
#include <eosio/serialize.hpp>
#include <eosio/print.hpp>
#include <eosio/name.hpp>

using namespace eosio;
using namespace std;

extern "C" {
__attribute__((eosio_wasm_import))
uint64_t current_time();
}

class [[eosio::contract("defertest")]] defertest : public eosio::contract {

public:
using contract::contract;

int32_t now() {
return current_time() / 1000000;
}

struct [[eosio::table]] pending {
uint64_t id;
eosio::name account;
eosio::name miner;
std::vector<char> rlptx;

uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index<"pending"_n, pending> pending_table_t;

// notify by the contract account of contract "defertest2.cpp"
[[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) {
action act({_self, "active"_n}, account, "pushtx"_n,
std::tuple<eosio::name, std::vector<char> >(miner, rlptx2));
act.send();
}

[[eosio::action]] void pushtxinline(eosio::name account, eosio::name miner, const std::vector<char> &rlptx) {
action act({_self, "active"_n}, account, "pushtx"_n,
std::tuple<eosio::name, std::vector<char> >(miner, rlptx));
act.send();
}

[[eosio::action]] void pushdefer(uint64_t id, eosio::name account, eosio::name miner, const std::vector<char> &rlptx, const std::vector<char> &rlptx2) {

printf("enter defertest.cpp::pushdefer:%d %d", (int)rlptx.size(), (int)rlptx2.size());

action act(permission_level{_self, "active"_n}, account, "pushtx"_n, std::tuple<eosio::name, std::vector<char> >(miner, rlptx));
transaction txn(time_point_sec(current_time() + 3590));
txn.actions.push_back(act);
auto serialize = pack(txn);
::send_deferred((uint128_t)(id), _self, serialize.data(), serialize.size(), true);

if (rlptx2.size()) {
pending_table_t pending_table(_self, _self.value);
pending_table.emplace(_self, [&](auto &v) {
v.id = id;
v.account = account;
v.miner = miner;
v.rlptx = rlptx2;
});
}
}

[[eosio::on_notify("eosio::onerror")]] void onerror() {

pending_table_t pending_table(_self, _self.value);
if (pending_table.begin() != pending_table.end()) {

printf("defertest.cpp::onerror() 1");
auto itr = pending_table.end();
--itr;
action act(permission_level{_self, "active"_n}, itr->account, "pushtx"_n,
std::tuple<eosio::name, std::vector<char> >(itr->miner, itr->rlptx));
act.send();
pending_table.erase(itr);
}
else {
printf("defertest.cpp::onerror() 2");
check(false, "hard-fail");
}
}
};
Binary file added tests/defertest.wasm
Binary file not shown.
44 changes: 44 additions & 0 deletions tests/defertest2.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT ",
"version": "eosio::abi/1.2",
"types": [],
"structs": [
{
"name": "notifytest",
"base": "",
"fields": [
{
"name": "recipient",
"type": "name"
},
{
"name": "account",
"type": "name"
},
{
"name": "miner",
"type": "name"
},
{
"name": "rlptx",
"type": "bytes"
},
{
"name": "rlptx2",
"type": "bytes"
}
]
}
],
"actions": [
{
"name": "notifytest",
"type": "notifytest",
"ricardian_contract": ""
}
],
"tables": [],
"ricardian_clauses": [],
"variants": [],
"action_results": []
}
32 changes: 32 additions & 0 deletions tests/defertest2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <type_traits>
#include <tuple>
#include <eosio/eosio.hpp>
#include <eosio/contract.hpp>
#include <eosio/system.hpp>
#include <eosio/transaction.hpp>
#include <eosio/serialize.hpp>
#include <eosio/print.hpp>
#include <eosio/name.hpp>

using namespace eosio;
using namespace std;

class [[eosio::contract("defertest2")]] defertest2 : public eosio::contract {

public:
using contract::contract;

// recipient is the contract account of contract "defertest.cpp"
[[eosio::action]] void notifytest(eosio::name recipient, eosio::name account, eosio::name miner, const std::vector<char> &rlptx, const std::vector<char> &rlptx2) {

action act({_self, "active"_n}, recipient, "pushtxinline"_n,
std::tuple<eosio::name, eosio::name, std::vector<char> >(account, miner, rlptx));
act.send();

require_recipient(recipient);
}

};
Binary file added tests/defertest2.wasm
Binary file not shown.
Loading

0 comments on commit 62393af

Please sign in to comment.