From bdf4ec7d110fe619e888e69d536eeda12bdb6ef6 Mon Sep 17 00:00:00 2001 From: Bart Wyatt Date: Tue, 6 Feb 2018 18:58:58 -0500 Subject: [PATCH] fixes for bugs related to identity contract tests --- contracts/eosiolib/singleton.hpp | 8 ++--- contracts/eosiolib/table.hpp | 4 +-- libraries/chain/wasm_interface.cpp | 9 +++--- tests/CMakeLists.txt | 2 +- tests/wasm_tests/identity_tests.cpp | 46 ++++++++++++++++++----------- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/contracts/eosiolib/singleton.hpp b/contracts/eosiolib/singleton.hpp index 91e8eeda0..cd935faa0 100644 --- a/contracts/eosiolib/singleton.hpp +++ b/contracts/eosiolib/singleton.hpp @@ -19,14 +19,14 @@ namespace eosio { static bool exists( scope_name scope = Code ) { uint64_t key = SingletonName; - auto read = load_i64( scope, Code, key, (char*)&key, sizeof(key) ); + auto read = load_i64( Code, scope, key, (char*)&key, sizeof(key) ); return read > 0; } static T get( scope_name scope = Code ) { char temp[1024+8]; *reinterpret_cast(temp) = SingletonName; - auto read = load_i64( scope, Code, SingletonName, temp, sizeof(temp) ); + auto read = load_i64( Code, scope, SingletonName, temp, sizeof(temp) ); assert( read > 0, "singleton does not exist" ); return unpack( temp + sizeof(SingletonName), read ); } @@ -34,7 +34,7 @@ namespace eosio { static T get_or_default( scope_name scope = Code, const T& def = T() ) { char temp[1024+8]; *reinterpret_cast(temp) = SingletonName; - auto read = load_i64( scope, Code, SingletonName, temp, sizeof(temp) ); + auto read = load_i64( Code, scope, SingletonName, temp, sizeof(temp) ); if ( read < 0 ) { return def; } @@ -46,7 +46,7 @@ namespace eosio { *reinterpret_cast(temp) = SingletonName; - auto read = load_i64( scope, Code, SingletonName, temp, sizeof(temp) ); + auto read = load_i64( Code, scope, SingletonName, temp, sizeof(temp) ); if( read < 0 ) { set( def, scope ); return def; diff --git a/contracts/eosiolib/table.hpp b/contracts/eosiolib/table.hpp index bdcf62d3e..8082f8936 100644 --- a/contracts/eosiolib/table.hpp +++ b/contracts/eosiolib/table.hpp @@ -93,7 +93,7 @@ namespace eosio { temp[1] = secondary; temp[2] = tertiary; - auto read = lower_bound_primary_i64i64i64( _scope, Code, TableName, + auto read = lower_bound_primary_i64i64i64( Code, _scope, TableName, (char*)temp, sizeof(temp) ); if( read <= 0 ) { return false; @@ -114,7 +114,7 @@ namespace eosio { temp[1] = secondary; temp[2] = tertiary; - auto read = upper_bound_primary_i64i64i64( _scope, Code, TableName, + auto read = upper_bound_primary_i64i64i64( Code, _scope, TableName, (char*)temp, sizeof(temp) ); if( read <= 0 ) { return false; diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index e57659e2b..14bc6c90c 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -527,9 +527,10 @@ class producer_api : public context_aware_api { int get_active_producers(array_ptr producers, size_t datalen) { auto active_producers = context.get_active_producers(); - size_t len = std::min(datalen / sizeof(chain::account_name), active_producers.size()); - memcpy(producers, active_producers.data(), len); - return active_producers.size() * sizeof(chain::account_name); + size_t len = active_producers.size() * sizeof(chain::account_name); + size_t cpy_len = std::min(datalen, len); + memcpy(producers, active_producers.data(), cpy_len); + return len; } }; @@ -779,7 +780,7 @@ class db_index_api : public context_aware_api { int call(ContextMethodType method, const account_name& code, const scope_name& scope, const name& table, array_ptr data, size_t data_len) { - auto maybe_t_id = context.find_table(context.receiver, scope, table); + auto maybe_t_id = context.find_table(code, scope, table); if (maybe_t_id == nullptr) { return -1; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e1635b321..799316964 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,7 +38,7 @@ target_link_libraries( chain_test eosio_testing eosio_chain chainbase eos_utilit if(WASM_TOOLCHAIN) target_include_directories( chain_test PUBLIC ${CMAKE_BINARY_DIR}/contracts ${CMAKE_CURRENT_BINARY_DIR}/tests/contracts ) target_include_directories( chain_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/wasm_tests ) - add_dependencies(chain_test asserter test_api currency proxy) + add_dependencies(chain_test asserter test_api currency proxy identity) endif() diff --git a/tests/wasm_tests/identity_tests.cpp b/tests/wasm_tests/identity_tests.cpp index fc4eef230..f5dee69bc 100644 --- a/tests/wasm_tests/identity_tests.cpp +++ b/tests/wasm_tests/identity_tests.cpp @@ -27,7 +27,7 @@ public: identity_tester() { produce_blocks(2); - create_accounts( {N(identity), N(identitytest), N(alice), N(bob)}, asset::from_string("100000.0000 EOS") ); + create_accounts( {N(identity), N(identitytest), N(alice), N(bob), N(carol)}, asset::from_string("100000.0000 EOS") ); produce_blocks(1000); set_code(N(identity), identity_wast); @@ -45,6 +45,9 @@ public: abi_def abi_test; BOOST_REQUIRE_EQUAL(abi_serializer::to_abi(acnt_test.abi, abi_test), true); abi_ser_test.set_abi(abi_test); + + const global_property_object &gpo = control->get_global_properties(); + producer_name = (string)gpo.active_producers.producers.front().producer_name; } uint64_t get_result_uint64() { @@ -194,6 +197,7 @@ public: public: abi_serializer abi_ser; abi_serializer abi_ser_test; + std::string producer_name; }; constexpr uint64_t identity_val = 0xffffffffffffffff; //64-bit value @@ -455,28 +459,29 @@ BOOST_FIXTURE_TEST_CASE( owner_certified_by_producer, identity_tester ) try { BOOST_REQUIRE_EQUAL(success(), create_identity("alice", identity_val)); // certify owner by a block producer, should result in trusted certification - BOOST_REQUIRE_EQUAL(success(), certify("inita", identity_val, vector{ mutable_variant_object() + BOOST_REQUIRE_EQUAL(success(), certify( producer_name, identity_val, vector{ mutable_variant_object() ("property", "owner") ("type", "account") ("data", to_uint8_vector(N(alice))) ("memo", "") ("confidence", 100) })); - fc::variant certrow = get_certrow(identity_val, "owner", 1, "inita"); + fc::variant certrow = get_certrow(identity_val, "owner", 1, producer_name); BOOST_REQUIRE_EQUAL( true, certrow.is_object() ); BOOST_REQUIRE_EQUAL( "owner", certrow["property"].as_string() ); BOOST_REQUIRE_EQUAL( 1, certrow["trusted"].as_uint64() ); - BOOST_REQUIRE_EQUAL( "inita", certrow["certifier"].as_string() ); + BOOST_REQUIRE_EQUAL( producer_name, certrow["certifier"].as_string() ); BOOST_REQUIRE_EQUAL( N(alice), to_uint64(certrow["data"]) ); //uncertified copy of that row shouldn't exist - BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 0, "inita").is_null()); + BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 0, producer_name).is_null()); //alice still has not claimed the identity - she is not the official owner yet BOOST_REQUIRE_EQUAL(0, get_owner_for_identity(identity_val)); BOOST_REQUIRE_EQUAL(0, get_identity_for_account("alice")); - //alice claims it + + //alice claims it BOOST_REQUIRE_EQUAL(success(), certify("alice", identity_val, vector{ mutable_variant_object() ("property", "owner") ("type", "account") @@ -491,7 +496,7 @@ BOOST_FIXTURE_TEST_CASE( owner_certified_by_producer, identity_tester ) try { BOOST_REQUIRE_EQUAL(identity_val, get_identity_for_account("alice")); //block producer decertifies ownership - BOOST_REQUIRE_EQUAL(success(), certify("inita", identity_val, vector{ mutable_variant_object() + BOOST_REQUIRE_EQUAL(success(), certify(producer_name, identity_val, vector{ mutable_variant_object() ("property", "owner") ("type", "account") ("data", to_uint8_vector(N(alice))) @@ -523,8 +528,8 @@ BOOST_FIXTURE_TEST_CASE( owner_certified_by_trusted_account, identity_tester ) t BOOST_REQUIRE_EQUAL(0, get_identity_for_account("alice")); //block producer trusts bob - BOOST_REQUIRE_EQUAL(success(), settrust("initb", "bob", 1)); - BOOST_REQUIRE_EQUAL(true, get_trust("initb", "bob")); + BOOST_REQUIRE_EQUAL(success(), settrust(producer_name, "bob", 1)); + BOOST_REQUIRE_EQUAL(true, get_trust(producer_name, "bob")); // bob (trusted account) certifies alice's ownership, it should result in trusted certification BOOST_REQUIRE_EQUAL(success(), certify("bob", identity_val, vector{ mutable_variant_object() @@ -543,8 +548,8 @@ BOOST_FIXTURE_TEST_CASE( owner_certified_by_trusted_account, identity_tester ) t BOOST_REQUIRE_EQUAL(identity_val, get_identity_for_account("alice")); //block producer stops trusting bob - BOOST_REQUIRE_EQUAL(success(), settrust("initb", "bob", 0)); - BOOST_REQUIRE_EQUAL(false, get_trust("initb", "bob")); + BOOST_REQUIRE_EQUAL(success(), settrust(producer_name, "bob", 0)); + BOOST_REQUIRE_EQUAL(false, get_trust(producer_name, "bob")); //certification made by bob is still flaged as trusted BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 1, "bob").is_object() ); @@ -584,8 +589,8 @@ BOOST_FIXTURE_TEST_CASE( owner_certification_becomes_trusted, identity_tester ) BOOST_REQUIRE_EQUAL(0, get_identity_for_account("alice")); //block producer trusts bob - BOOST_REQUIRE_EQUAL(success(), settrust("initb", "bob", 1)); - BOOST_REQUIRE_EQUAL(true, get_trust("initb", "bob")); + BOOST_REQUIRE_EQUAL(success(), settrust(producer_name, "bob", 1)); + BOOST_REQUIRE_EQUAL(true, get_trust(producer_name, "bob")); //old certification made by bob still shouldn't be flaged as trusted BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 0, "bob").is_object() ); @@ -609,14 +614,14 @@ BOOST_FIXTURE_TEST_CASE( ownership_contradiction, identity_tester ) try { })); // block producer certifies alice's ownership - BOOST_REQUIRE_EQUAL(success(), certify("inita", identity_val, vector{ mutable_variant_object() + BOOST_REQUIRE_EQUAL(success(), certify(producer_name, identity_val, vector{ mutable_variant_object() ("property", "owner") ("type", "account") ("data", to_uint8_vector(N(alice))) ("memo", "") ("confidence", 100) })); - BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 1, "inita").is_object() ); + BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 1, producer_name).is_object() ); //now alice is the official owner of the identity BOOST_REQUIRE_EQUAL(N(alice), get_owner_for_identity(identity_val)); @@ -631,15 +636,20 @@ BOOST_FIXTURE_TEST_CASE( ownership_contradiction, identity_tester ) try { ("confidence", 100) })); - //another block producer certifies bob's identity (to the identity already certified to alice) - BOOST_REQUIRE_EQUAL(success(), certify("initb", identity_val, vector{ mutable_variant_object() + + //block producer trusts carol + BOOST_REQUIRE_EQUAL(success(), settrust(producer_name, "carol", 1)); + BOOST_REQUIRE_EQUAL(true, get_trust(producer_name, "carol")); + + //another trusted delegate certifies bob's identity (to the identity already certified to alice) + BOOST_REQUIRE_EQUAL(success(), certify("carol", identity_val, vector{ mutable_variant_object() ("property", "owner") ("type", "account") ("data", to_uint8_vector(N(bob))) ("memo", "") ("confidence", 100) })); - BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 1, "initb").is_object() ); + BOOST_REQUIRE_EQUAL( true, get_certrow(identity_val, "owner", 1, producer_name).is_object() ); //now neither alice or bob are official owners, because we have 2 trusted certifications in contradiction to each other BOOST_REQUIRE_EQUAL(0, get_owner_for_identity(identity_val)); -- GitLab