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

Commit

Permalink
Ref #15: Break up system -> native contracts
Browse files Browse the repository at this point in the history
Create all three native contracts at genesis, and split the various
messages among them appropriately.
  • Loading branch information
nathanielhourt committed Jun 5, 2017
1 parent 9a69719 commit f89c3ab
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
22 changes: 16 additions & 6 deletions libraries/chain/chain_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,16 +808,26 @@ void chain_controller::initialize_genesis(std::function<genesis_state_type()> ge
} inhibitor(*this);


/// create the system contract
/// create the native contract accounts
_db.create<account_object>([&](account_object& a) {
a.name = "sys";
a.name = config::SystemContractName;
});
#define MACRO(r, data, elem) register_type<types::elem>("sys");
BOOST_PP_SEQ_FOR_EACH(MACRO, x, EOS_SYSTEM_CONTRACT_FUNCTIONS)
_db.create<account_object>([&](account_object& a) {
a.name = config::EosContractName;
});
_db.create<account_object>([&](account_object& a) {
a.name = config::StakedBalanceContractName;
});

// Register native contract message types
#define MACRO(r, data, elem) register_type<types::elem>(data);
BOOST_PP_SEQ_FOR_EACH(MACRO, config::SystemContractName, EOS_SYSTEM_CONTRACT_FUNCTIONS)
BOOST_PP_SEQ_FOR_EACH(MACRO, config::EosContractName, EOS_CONTRACT_FUNCTIONS)
BOOST_PP_SEQ_FOR_EACH(MACRO, config::StakedBalanceContractName, EOS_STAKED_BALANCE_CONTRACT_FUNCTIONS)
#undef MACRO

// Create initial accounts
for (const auto& acct : genesis_state.initial_accounts) {
// Create initial accounts
for (const auto& acct : genesis_state.initial_accounts) {
_db.create<account_object>([&acct](account_object& a) {
a.name = acct.name.c_str();
a.balance = acct.balance;
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/include/eos/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ using types::Asset;

const static char KeyPrefix[] = "EOS";

const static char SystemContractName[] = "sys";
const static char EosContractName[] = "eos";
const static char StakedBalanceContractName[] = "sbc";

const static int BlockIntervalSeconds = 3;

/** Percentages are fixed point with a denominator of 10,000 */
Expand Down
5 changes: 3 additions & 2 deletions libraries/chain/include/eos/chain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
{ c(*this); }
#define OBJECT_CTOR(...) BOOST_PP_OVERLOAD(OBJECT_CTOR, __VA_ARGS__)(__VA_ARGS__)

#define EOS_SYSTEM_CONTRACT_FUNCTIONS (Transfer)(CreateAccount)(CreateProducer)(UpdateProducer) \
(DefineStruct)(SetMessageHandler)
#define EOS_SYSTEM_CONTRACT_FUNCTIONS (CreateAccount)(DefineStruct)(SetMessageHandler)
#define EOS_CONTRACT_FUNCTIONS (Transfer)
#define EOS_STAKED_BALANCE_CONTRACT_FUNCTIONS (CreateProducer)(UpdateProducer)

namespace eos { namespace chain {
using std::map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ void native_system_contract_plugin::plugin_shutdown() {
}

void native_system_contract_plugin::install(chain_controller& db) {
#define SET_HANDLERS(name) \
db.set_validate_handler("sys", "sys", #name, &name::validate); \
db.set_precondition_validate_handler("sys", "sys", #name, &name::validate_preconditions); \
db.set_apply_handler("sys", "sys", #name, &name::apply);
#define FWD_SET_HANDLERS(r, data, elem) SET_HANDLERS(elem)

BOOST_PP_SEQ_FOR_EACH(FWD_SET_HANDLERS, x, EOS_SYSTEM_CONTRACT_FUNCTIONS)
#define SET_HANDLERS(contractname, handlername) \
db.set_validate_handler(contractname, contractname, #handlername, &handlername::validate); \
db.set_precondition_validate_handler(contractname, contractname, #handlername, &handlername::validate_preconditions); \
db.set_apply_handler(contractname, contractname, #handlername, &handlername::apply);
#define FWD_SET_HANDLERS(r, data, elem) SET_HANDLERS(data, elem)

BOOST_PP_SEQ_FOR_EACH(FWD_SET_HANDLERS, config::SystemContractName, EOS_SYSTEM_CONTRACT_FUNCTIONS)
BOOST_PP_SEQ_FOR_EACH(FWD_SET_HANDLERS, config::EosContractName, EOS_CONTRACT_FUNCTIONS)
BOOST_PP_SEQ_FOR_EACH(FWD_SET_HANDLERS, config::StakedBalanceContractName, EOS_STAKED_BALANCE_CONTRACT_FUNCTIONS)
}

}
2 changes: 2 additions & 0 deletions tests/common/database_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ class testing_network {
* Use Make_Account to create an account, including keys. The changes will be applied via a transaction applied to the
* provided database object. The changes will not be incorporated into a block; they will be left in the pending state.
*
* Unless overridden, new accounts are created with a balance of Asset(100)
*
* Example:
* @code{.cpp}
* Make_Account(db, joe)
Expand Down
16 changes: 8 additions & 8 deletions tests/common/macro_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define MKACCT_IMPL(db, name, creator, active, owner, recovery, deposit) \
{ \
eos::chain::SignedTransaction trx; \
trx.emplaceMessage(#creator, "sys", vector<AccountName>{}, "CreateAccount", \
trx.emplaceMessage(#creator, config::SystemContractName, vector<AccountName>{}, "CreateAccount", \
types::CreateAccount{#creator, #name, owner, active, recovery, deposit}); \
trx.expiration = db.head_block_time() + 100; \
trx.set_reference_block(db.head_block_id()); \
Expand All @@ -48,19 +48,19 @@
#define XFER5(db, sender, recipient, amount, memo) \
{ \
eos::chain::SignedTransaction trx; \
trx.emplaceMessage(#sender, "sys", vector<AccountName>{#recipient}, "Transfer", \
trx.emplaceMessage(#sender, config::EosContractName, vector<AccountName>{#recipient}, "Transfer", \
types::Transfer{#sender, #recipient, amount, memo}); \
trx.expiration = db.head_block_time() + 100; \
trx.set_reference_block(db.head_block_id()); \
db.push_transaction(trx); \
}
#define XFER4(db, sender, recipient, amount) XFER5(db, sender, recipient, amount, "")

#define MKPDCR4(db, owner, key, config) \
#define MKPDCR4(db, owner, key, cfg) \
{ \
eos::chain::SignedTransaction trx; \
trx.emplaceMessage(#owner, "sys", vector<AccountName>{}, "CreateProducer", \
types::CreateProducer{#owner, key, config}); \
trx.emplaceMessage(#owner, config::StakedBalanceContractName, vector<AccountName>{}, "CreateProducer", \
types::CreateProducer{#owner, key, cfg}); \
trx.expiration = db.head_block_time() + 100; \
trx.set_reference_block(db.head_block_id()); \
db.push_transaction(trx); \
Expand All @@ -70,11 +70,11 @@
Make_Key(owner ## _producer); \
MKPDCR4(db, owner, owner ## _producer_public_key, BlockchainConfiguration{});

#define UPPDCR4(db, owner, key, config) \
#define UPPDCR4(db, owner, key, cfg) \
{ \
eos::chain::SignedTransaction trx; \
trx.emplaceMessage(owner, "sys", vector<AccountName>{}, "UpdateProducer", \
types::UpdateProducer{owner, key, config}); \
trx.emplaceMessage(owner, config::StakedBalanceContractName, vector<AccountName>{}, "UpdateProducer", \
types::UpdateProducer{owner, key, cfg}); \
trx.expiration = db.head_block_time() + 100; \
trx.set_reference_block(db.head_block_id()); \
db.push_transaction(trx); \
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/block_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ BOOST_FIXTURE_TEST_CASE(create_script, testing_fixture)

types::SetMessageHandler handler;
handler.processor = "init1";
handler.recipient = "sys";
handler.recipient = config::EosContractName;
handler.type = "Transfer";

handler.apply = R"(
Expand All @@ -114,7 +114,7 @@ BOOST_FIXTURE_TEST_CASE(create_script, testing_fixture)
)";

trx.setMessage(0, "SetMessageHandler", handler);

idump((trx));
db.push_transaction(trx);
db.produce_blocks(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/system_contract_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ BOOST_FIXTURE_TEST_CASE(transfer, testing_fixture)
trx.set_reference_block(db.head_block_id());
trx.expiration = db.head_block_time() + 100;
trx.messages[0].sender = "init1";
trx.messages[0].recipient = "sys";
trx.messages[0].recipient = config::EosContractName;
trx.messages[0].type = "Undefined";
BOOST_REQUIRE_THROW( db.push_transaction(trx), message_validate_exception ); // "Type Undefined is not defined"

Expand Down

0 comments on commit f89c3ab

Please sign in to comment.