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

Commit

Permalink
Ref #7: Implement require_authorization, fix tests
Browse files Browse the repository at this point in the history
Require_authorization is now implemented so as the contract executes and
asserts that a particular account approved the transaction, the chain
asserts that this is so and throws if not.

Also, update the tests, since the auth checks now bring to bear the rule
that an account cannot be used in the same block that creates it. The
tests now comply with this rule.

TODO:
 - Check that all declared authorizations get required by the contract
 - Implement the mapping from user permissions to message types
 - Use mapping of permission to message type in
lookup_minimum_permission
  • Loading branch information
nathanielhourt committed Aug 3, 2017
1 parent e89dafe commit 83b309b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
1 change: 0 additions & 1 deletion libraries/chain/chain_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ void chain_controller::check_transaction_authorization(const SignedTransaction&
("auth", declaredAuthority));
}
}
ilog("Auth check passed");
}

ProcessedTransaction chain_controller::apply_transaction(const SignedTransaction& trx, uint32_t skip)
Expand Down
4 changes: 3 additions & 1 deletion libraries/chain/message_handling_contexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace eos { namespace chain {

void apply_context::require_authorization(const types::AccountName& account) {
#warning TODO
auto itr = boost::find_if(msg.authorization, [&account](const auto& auth) { return auth.account == account; });
EOS_ASSERT(itr != msg.authorization.end(), tx_missing_auth,
"Transaction is missing required authorization from ${acct}", ("acct", account));
}

void apply_context::require_scope(const types::AccountName& account)const {
Expand Down
32 changes: 16 additions & 16 deletions tests/common/macro_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
eos::chain::SignedTransaction trx; \
trx.scope = sort_names({ #creator, "eos" }); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, \
vector<types::AccountPermission>{{#creator, "active"}}, \
"newaccount", types::newaccount{#creator, #name, owner, active, recovery, deposit}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Created account " << #name); \
}
#define MKACCT2(chain, name) \
Expand Down Expand Up @@ -80,11 +80,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { #sender, #recipient } ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, "lock", types::lock{#sender, #recipient, amount}); \
trx.emplaceMessage(config::EosContractName, vector<types::AccountPermission>{{#sender, "active"}}, \
"lock", types::lock{#sender, #recipient, amount}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Staked " << amount << " to " << #recipient); \
}
#define STAKE3(chain, account, amount) STAKE4(chain, account, account, amount)
Expand All @@ -94,23 +94,23 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { "eos" } ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, \
vector<types::AccountPermission>{{#account, "active"}}, \
"unlock", types::unlock{#account, amount}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Begin unstake " << amount << " to " << #account); \
}

#define FINISH_UNSTAKE3(chain, account, amount) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { "eos", #account } ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, "claim", types::claim{#account, amount}); \
trx.emplaceMessage(config::EosContractName, vector<types::AccountPermission>{{#account, "active"}}, \
"claim", types::claim{#account, amount}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Finish unstake " << amount << " to " << #account); \
}

Expand All @@ -119,11 +119,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#owner, "eos"} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, \
vector<types::AccountPermission>{{#owner, "active"}}, \
"setproducer", types::setproducer{#owner, key, cfg}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Create producer " << #owner); \
}
#define MKPDCR3(chain, owner, key) MKPDCR4(chain, owner, key, BlockchainConfiguration{});
Expand All @@ -136,11 +136,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#voter, "eos"} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, \
vector<types::AccountPermission>{{#voter, "active"}}, \
"okproducer", types::okproducer{#voter, #producer, approved? 1 : 0}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Set producer approval from " << #voter << " for " << #producer << " to " << approved); \
}

Expand All @@ -149,11 +149,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {owner, "eos"} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{}, \
vector<types::AccountPermission>{{owner, "active"}}, \
"setproducer", types::setproducer{owner, key, cfg}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Update producer " << owner); \
}
#define UPPDCR3(chain, owner, key) UPPDCR4(chain, owner, key, chain.get_producer(owner).configuration)
17 changes: 9 additions & 8 deletions tests/tests/native_contract_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ BOOST_FIXTURE_TEST_CASE(transfer, testing_fixture)
auto unpacked = fc::raw::unpack<UInt64>(packed);
BOOST_CHECK_EQUAL( value, unpacked );
trx.messages[0].type = "transfer";
trx.messages[0].authorization = {{"inita", "active"}};
trx.messages[0].code = config::EosContractName;
trx.setMessage(0, "transfer", trans);
chain.push_transaction(trx);
chain.push_transaction(trx, chain_controller::skip_transaction_signatures);

BOOST_CHECK_EQUAL(chain.get_liquid_balance("inita"), Asset(100000 - 100));
BOOST_CHECK_EQUAL(chain.get_liquid_balance("initb"), Asset(100000 + 100));
Expand All @@ -118,10 +119,10 @@ BOOST_FIXTURE_TEST_CASE(transfer, testing_fixture)
BOOST_FIXTURE_TEST_CASE(producer_creation, testing_fixture)
{ try {
Make_Blockchain(chain)
Make_Account(chain, producer);
chain.produce_blocks();
BOOST_CHECK_EQUAL(chain.head_block_num(), 1);

Make_Account(chain, producer);
Make_Producer(chain, producer, producer_public_key);

while (chain.head_block_num() < 3) {
Expand Down Expand Up @@ -237,10 +238,10 @@ BOOST_FIXTURE_TEST_CASE(producer_voting_parameters_2, testing_fixture)
BOOST_FIXTURE_TEST_CASE(producer_voting_1, testing_fixture) {
try {
Make_Blockchain(chain)
chain.produce_blocks();

Make_Account(chain, joe);
Make_Account(chain, bob);
chain.produce_blocks();

Make_Producer(chain, joe);
Approve_Producer(chain, bob, joe, true);
// Produce blocks up to, but not including, the last block in the round
Expand Down Expand Up @@ -274,10 +275,10 @@ BOOST_FIXTURE_TEST_CASE(producer_voting_1, testing_fixture) {
BOOST_FIXTURE_TEST_CASE(producer_voting_2, testing_fixture) {
try {
Make_Blockchain(chain)
chain.produce_blocks();

Make_Account(chain, joe);
Make_Account(chain, bob);
chain.produce_blocks();

Make_Producer(chain, joe);
Approve_Producer(chain, bob, joe, true);
chain.produce_blocks();
Expand Down Expand Up @@ -332,11 +333,11 @@ BOOST_FIXTURE_TEST_CASE(producer_proxy_voting, testing_fixture) {

auto run = [this](std::vector<Action> actions) {
Make_Blockchain(chain)
chain.produce_blocks();

Make_Account(chain, stakeholder);
Make_Account(chain, proxy);
Make_Account(chain, producer);
chain.produce_blocks();

Make_Producer(chain, producer);

for (auto& action : actions)
Expand Down
12 changes: 8 additions & 4 deletions tests/tests/special_accounts_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ BOOST_FIXTURE_TEST_CASE(producers_authority, testing_fixture)
{ try {

Make_Blockchain(chain)
chain.produce_blocks();

Make_Account(chain, alice);
Make_Account(chain, bob);
Make_Account(chain, charlie);

Make_Account(chain, newproducer1); Make_Producer(chain, newproducer1);
Make_Account(chain, newproducer2); Make_Producer(chain, newproducer2);
Make_Account(chain, newproducer3); Make_Producer(chain, newproducer3);
Make_Account(chain, newproducer1);
Make_Account(chain, newproducer2);
Make_Account(chain, newproducer3);
chain.produce_blocks();

Make_Producer(chain, newproducer1);
Make_Producer(chain, newproducer2);
Make_Producer(chain, newproducer3);

Approve_Producer(chain, alice, newproducer1, true);
Approve_Producer(chain, bob, newproducer2, true);
Expand Down

0 comments on commit 83b309b

Please sign in to comment.