Skip to content

Commit

Permalink
GH-54 Add contract code for eosio.msig, eosio.system, eosio.wrap. Upd…
Browse files Browse the repository at this point in the history
…ate contract code for eosio.bios and eosio.token to match as close as possible what matches the existing abi/wasm.
  • Loading branch information
heifner committed Jan 4, 2023
1 parent 2bf719d commit a9281f5
Show file tree
Hide file tree
Showing 27 changed files with 3,895 additions and 155 deletions.
3 changes: 3 additions & 0 deletions libraries/testing/contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
cmake_minimum_required( VERSION 3.8 )
project(testing_contracts)

if( EOSIO_COMPILE_TEST_CONTRACTS )
set(EOSIO_WASM_OLD_BEHAVIOR "Off")
if(USE_EOSIO_CDT_1_7_X OR USE_EOSIO_CDT_1_8_X)
Expand Down
8 changes: 1 addition & 7 deletions libraries/testing/contracts/eosio.bios/eosio.bios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

namespace eosiobios {

// move this to CDT after this release
extern "C" {
__attribute__((eosio_wasm_import))
void set_parameters_packed(const char*, std::size_t);
}

void bios::setabi( name account, const std::vector<char>& abi ) {
abi_hash_table table(get_self(), get_self().value);
auto itr = table.find( account.value );
Expand Down Expand Up @@ -60,4 +54,4 @@ void bios::reqactivated( const eosio::checksum256& feature_digest ) {
check( is_feature_activated( feature_digest ), "protocol feature is not activated" );
}

}
}
154 changes: 134 additions & 20 deletions libraries/testing/contracts/eosio.bios/eosio.bios.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
#include <eosio/privileged.hpp>
#include <eosio/producer_schedule.hpp>

/**
* EOSIO Contracts
*
* @details The design of the EOSIO blockchain calls for a number of smart contracts that are run at a
* privileged permission level in order to support functions such as block producer registration and
* voting, token staking for CPU and network bandwidth, RAM purchasing, multi-sig, etc. These smart
* contracts are referred to as the system, token, msig and wrap (formerly known as sudo) contracts.
*
* This repository contains examples of these privileged contracts that are useful when deploying,
* managing, and/or using an EOSIO blockchain. They are provided for reference purposes:
* - eosio.bios
* - eosio.system
* - eosio.msig
* - eosio.wrap
*
* The following unprivileged contract(s) are also part of the system.
* - eosio.token
*/

namespace eosiobios {

using eosio::action_wrapper;
Expand All @@ -17,6 +36,14 @@ namespace eosiobios {
using eosio::permission_level;
using eosio::public_key;

/**
* A weighted permission.
*
* @details Defines a weighted permission, that is a permission which has a weight associated.
* A permission is defined by an account name plus a permission name. The weight is going to be
* used against a threshold, if the weight is equal or greater than the threshold set then authorization
* will pass.
*/
struct permission_level_weight {
permission_level permission;
uint16_t weight;
Expand All @@ -25,6 +52,11 @@ namespace eosiobios {
EOSLIB_SERIALIZE( permission_level_weight, (permission)(weight) )
};

/**
* Weighted key.
*
* @details A weighted key is defined by a public key and an associated weight.
*/
struct key_weight {
eosio::public_key key;
uint16_t weight;
Expand All @@ -33,6 +65,11 @@ namespace eosiobios {
EOSLIB_SERIALIZE( key_weight, (key)(weight) )
};

/**
* Wait weight.
*
* @details A wait weight is defined by a number of seconds to wait for and a weight.
*/
struct wait_weight {
uint32_t wait_sec;
uint16_t weight;
Expand All @@ -41,6 +78,15 @@ namespace eosiobios {
EOSLIB_SERIALIZE( wait_weight, (wait_sec)(weight) )
};

/**
* Blockchain authority.
*
* @details An authority is defined by:
* - a vector of key_weights (a key_weight is a public key plus a weight),
* - a vector of permission_level_weights, (a permission_level is an account name plus a permission name)
* - a vector of wait_weights (a wait_weight is defined by a number of seconds to wait and a weight)
* - a threshold value
*/
struct authority {
uint32_t threshold = 0;
std::vector<key_weight> keys;
Expand All @@ -51,6 +97,19 @@ namespace eosiobios {
EOSLIB_SERIALIZE( authority, (threshold)(keys)(accounts)(waits) )
};

/**
* Blockchain block header.
*
* @details A block header is defined by:
* - a timestamp,
* - the producer that created it,
* - a confirmed flag default as zero,
* - a link to previous block,
* - a link to the transaction merkel root,
* - a link to action root,
* - a schedule version,
* - and a producers' schedule.
*/
struct block_header {
uint32_t timestamp;
name producer;
Expand All @@ -67,15 +126,29 @@ namespace eosiobios {
};

/**
* The `eosio.bios` is the first sample of system contract. It is a minimalist system contract because it only supplies the actions that are absolutely critical to bootstrap a chain and nothing more. This allows for a chain agnostic approach to bootstrapping a chain.
* @defgroup eosiobios eosio.bios
* @ingroup eosiocontracts
*
* Just like in the `eosio.system` sample contract implementation, there are a few actions which are not implemented at the contract level (`newaccount`, `updateauth`, `deleteauth`, `linkauth`, `unlinkauth`, `canceldelay`, `onerror`, `setabi`, `setcode`), they are just declared in the contract so they will show in the contract's ABI and users will be able to push those actions to the chain via the account holding the `eosio.system` contract, but the implementation is at the core level. They are referred to as native actions.
* eosio.bios is a minimalistic system contract that only supplies the actions that are absolutely
* critical to bootstrap a chain and nothing more.
*
* @{
*/
class [[eosio::contract("eosio.bios")]] bios : public eosio::contract {
public:
using contract::contract;
/**
* New account action, called after a new account is created. This code enforces resource-limits rules
* @{
* These actions map one-on-one with the ones defined in
* [Native Action Handlers](@ref native_action_handlers) section.
* They are present here so they can show up in the abi file and thus user can send them
* to this contract, but they have no specific implementation at this contract level,
* they will execute the implementation at the core level and nothing else.
*/
/**
* New account action
*
* @details Called after a new account is created. This code enforces resource-limits rules
* for new accounts as well as new account naming conventions.
*
* 1. accounts cannot contain '.' symbols which forces all acccounts to be 12
Expand All @@ -92,7 +165,9 @@ namespace eosiobios {
ignore<authority> owner,
ignore<authority> active){}
/**
* Update authorization action updates pemission for an account.
* Update authorization action.
*
* @details Updates pemission for an account.
*
* @param account - the account for which the permission is updated,
* @param pemission - the permission name which is updated,
Expand All @@ -106,7 +181,9 @@ namespace eosiobios {
ignore<authority> auth ) {}

/**
* Delete authorization action deletes the authorization for an account's permission.
* Delete authorization action.
*
* @details Deletes the authorization for an account's permision.
*
* @param account - the account for which the permission authorization is deleted,
* @param permission - the permission name been deleted.
Expand All @@ -116,9 +193,11 @@ namespace eosiobios {
ignore<name> permission ) {}

/**
* Link authorization action assigns a specific action from a contract to a permission you have created. Five system
* Link authorization action.
*
* @details Assigns a specific action from a contract to a permission you have created. Five system
* actions can not be linked `updateauth`, `deleteauth`, `linkauth`, `unlinkauth`, and `canceldelay`.
* This is useful because when doing authorization checks, the Antelope based blockchain starts with the
* This is useful because when doing authorization checks, the EOSIO based blockchain starts with the
* action needed to be authorized (and the contract belonging to), and looks up which permission
* is needed to pass authorization validation. If a link is set, that permission is used for authoraization
* validation otherwise then active is the default, with the exception of `eosio.any`.
Expand All @@ -137,7 +216,9 @@ namespace eosiobios {
ignore<name> requirement ) {}

/**
* Unlink authorization action it's doing the reverse of linkauth action, by unlinking the given action.
* Unlink authorization action.
*
* @details It's doing the reverse of linkauth action, by unlinking the given action.
*
* @param account - the owner of the permission to be unlinked and the receiver of the freed RAM,
* @param code - the owner of the action to be unlinked,
Expand All @@ -149,7 +230,9 @@ namespace eosiobios {
ignore<name> type ) {}

/**
* Cancel delay action cancels a deferred transaction.
* Cancel delay action.
*
* @details Cancels a deferred transaction.
*
* @param canceling_auth - the permission that authorizes this action,
* @param trx_id - the deferred transaction id to be cancelled.
Expand All @@ -158,7 +241,9 @@ namespace eosiobios {
void canceldelay( ignore<permission_level> canceling_auth, ignore<checksum256> trx_id ) {}

/**
* Set code action sets the contract code for an account.
* Set code action.
*
* @details Sets the contract code for an account.
*
* @param account - the account for which to set the contract code.
* @param vmtype - reserved, set it to zero.
Expand All @@ -168,8 +253,12 @@ namespace eosiobios {
[[eosio::action]]
void setcode( name account, uint8_t vmtype, uint8_t vmversion, const std::vector<char>& code ) {}

/** @}*/

/**
* Set abi action sets the abi for contract identified by `account` name. Creates an entry in the abi_hash_table
* Set abi for contract.
*
* @details Set the abi for contract identified by `account` name. Creates an entry in the abi_hash_table
* index, with `account` name as key, if it is not already present and sets its value with the abi hash.
* Otherwise it is updating the current abi hash value for the existing `account` key.
*
Expand All @@ -180,7 +269,9 @@ namespace eosiobios {
void setabi( name account, const std::vector<char>& abi );

/**
* On error action, notification of this action is delivered to the sender of a deferred transaction
* On error action.
*
* @details Notification of this action is delivered to the sender of a deferred transaction
* when an objective error occurs while executing the deferred transaction.
* This action is not meant to be called directly.
*
Expand All @@ -191,15 +282,19 @@ namespace eosiobios {
void onerror( ignore<uint128_t> sender_id, ignore<std::vector<char>> sent_trx );

/**
* Set privilege action allows to set privilege status for an account (turn it on/off).
* Set privilege status for an account.
*
* @details Allows to set privilege status for an account (turn it on/off).
* @param account - the account to set the privileged status for.
* @param is_priv - 0 for false, > 0 for true.
*/
[[eosio::action]]
void setpriv( name account, uint8_t is_priv );

/**
* Sets the resource limits of an account
* Set the resource limits of an account
*
* @details Set the resource limits of an account
*
* @param account - name of the account whose resource limit to be set
* @param ram_bytes - ram limit in absolute bytes
Expand All @@ -210,7 +305,9 @@ namespace eosiobios {
void setalimits( name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight );

/**
* Set producers action, sets a new list of active producers, by proposing a schedule change, once the block that
* Set a new list of active producers, that is, a new producers' schedule.
*
* @details Set a new list of active producers, by proposing a schedule change, once the block that
* contains the proposal becomes irreversible, the schedule is promoted to "pending"
* automatically. Once the block that promotes the schedule is irreversible, the schedule will
* become "active".
Expand All @@ -221,15 +318,19 @@ namespace eosiobios {
void setprods( const std::vector<eosio::producer_authority>& schedule );

/**
* Set params action, sets the blockchain parameters. By tuning these parameters, various degrees of customization can be achieved.
* Set the blockchain parameters
*
* @details Set the blockchain parameters. By tuning these parameters, various degrees of customization can be achieved.
*
* @param params - New blockchain parameters to set
*/
[[eosio::action]]
void setparams( const eosio::blockchain_parameters& params );

/**
* Require authorization action, checks if the account name `from` passed in as param has authorization to access
* Check if an account has authorization to access current action.
*
* @details Checks if the account name `from` passed in as param has authorization to access
* current action, that is, if it is listed in the action’s allowed permissions vector.
*
* @param from - the account name to authorize
Expand All @@ -238,21 +339,30 @@ namespace eosiobios {
void reqauth( name from );

/**
* Activate action, activates a protocol feature
* Activates a protocol feature.
*
* @details Activates a protocol feature
*
* @param feature_digest - hash of the protocol feature to activate.
*/
[[eosio::action]]
void activate( const eosio::checksum256& feature_digest );

/**
* Require activated action, asserts that a protocol feature has been activated
* Asserts that a protocol feature has been activated.
*
* @details Asserts that a protocol feature has been activated
*
* @param feature_digest - hash of the protocol feature to check for activation.
*/
[[eosio::action]]
void reqactivated( const eosio::checksum256& feature_digest );

/**
* Abi hash structure
*
* @details Abi hash structure is defined by contract owner and the contract hash.
*/
struct [[eosio::table]] abi_hash {
name owner;
checksum256 hash;
Expand All @@ -261,6 +371,9 @@ namespace eosiobios {
EOSLIB_SERIALIZE( abi_hash, (owner)(hash) )
};

/**
* Multi index table that stores the contracts' abi index by their owners/accounts.
*/
typedef eosio::multi_index< "abihash"_n, abi_hash > abi_hash_table;

using newaccount_action = action_wrapper<"newaccount"_n, &bios::newaccount>;
Expand All @@ -279,4 +392,5 @@ namespace eosiobios {
using activate_action = action_wrapper<"activate"_n, &bios::activate>;
using reqactivated_action = action_wrapper<"reqactivated"_n, &bios::reqactivated>;
};
}
/** @}*/ // end of @defgroup eosiobios eosio.bios
} /// namespace eosiobios
Loading

0 comments on commit a9281f5

Please sign in to comment.