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

Commit

Permalink
Merge pull request #16 from EOSIO/fix/merge_master
Browse files Browse the repository at this point in the history
Merging release/1.1 to master
  • Loading branch information
larryk85 authored Jul 11, 2018
2 parents c8b57e4 + 77b568f commit e8e687a
Show file tree
Hide file tree
Showing 25 changed files with 488 additions and 91 deletions.
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.5)
project(eosio_contracts VERSION 1.1.0)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(TEST_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
else()
set(TEST_BUILD_TYPE ${CMAKE_BUILD_TYPE})
endif()
if(CXX_COMPILER STREQUAL "" OR NOT CXX_COMPILER)
set(CXX_COMPILER ${CMAKE_CXX_COMPILER})
endif()

if(EOSIO_INSTALL_PREFIX STREQUAL "" OR NOT EOSIO_INSTALL_PREFIX)
set(EOSIO_INSTALL_PREFIX "/usr/local")
endif()

# if no wasm root is given use default path
if(WASM_ROOT STREQUAL "" OR NOT WASM_ROOT)
set(WASM_ROOT ${CMAKE_INSTALL_PREFIX})
endif()

list(APPEND CMAKE_MODULE_PATH ${WASM_ROOT}/lib/cmake)
include(EosioWasmToolchain)

add_subdirectory(eosio.msig)
add_subdirectory(eosio.sudo)
add_subdirectory(eosio.system)
add_subdirectory(eosio.token)

if (APPLE)
set(OPENSSL_ROOT "/usr/local/opt/openssl")
elseif (UNIX)
set(OPENSSL_ROOT "/usr/include/openssl")
endif()
set(SECP256K1_ROOT "/usr/local")

include(UnitTestsExternalProject.txt)
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# eosio.contracts

## Version : 1.0.0
## Version : 1.1.0

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 sudo contracts.

Expand All @@ -16,14 +16,14 @@ The following unpriviledged contract(s) are also part of the system.

Dependencies:
* [eosio v1.0.8](https://github.com/eosio/eos/tree/v1.0.8)
* [eosio.wasmsdk v1.0](https://github.com/eosio/eosio.wasmsdk/tree/v1.0)
* [eosio.wasmsdk v1.0.0](https://github.com/eosio/eosio.wasmsdk/tree/v1.0.0)

To build the contracts and the unit tests:
* First, ensure that your __eosio__ is compiled to the core symbol for the EOSIO blockchain that intend to deploy to.
* Second, make sure that you have ```sudo make install```ed __eosio__.
* Then just run the ```build.sh``` in the top directory to build all the contracts and the unit tests for these contracts.
* Then just run the ```build.sh``` in the top directory to build all the contracts and the unit tests for these contracts.

After build:
* The unit tests executable is placed in the _build/tests_ directory and is named __unit_test__.
* The unit tests executable is placed in the _build/tests_ and is named __unit_test__.
* The contracts are built into a _bin/\<contract name\>_ folder in their respective directories.
* Finally, simply use __cleos__ to _set contract_ by pointing to the previously mentioned directory.
14 changes: 14 additions & 0 deletions UnitTestsExternalProject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(ExternalProject)
find_package(Git REQUIRED)
include(GNUInstallDirs)

ExternalProject_Add(
contracts_unit_tests
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${TEST_BUILD_TYPE} -DROOT_DIR=${CMAKE_SOURCE_DIR} -DEOSIO_INSTALL_PREFIX=${EOSIO_INSTALL_PREFIX} -DOPENSSL_INSTALL_PREFIX=${OPENSSL_ROOT} -DSECP256K1_INSTALL_LIB=${SECP256K1_ROOT} -DBOOST_ROOT=${BOOST_ROOT}/include -DCMAKE_CXX_COMPILER=${CXX_COMPILER}

SOURCE_DIR ${CMAKE_SOURCE_DIR}/tests
BINARY_DIR ${CMAKE_SOURCE_DIR}/build/tests
BUILD_ALWAYS 1
TEST_COMMAND ""
INSTALL_COMMAND ""
)
59 changes: 20 additions & 39 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#! /bin/bash

contracts=( "eosio.token"
"eosio.system"
"eosio.msig"
"eosio.sudo" )
printf "\t=========== Building eosio.contracts ===========\n\n"

RED='\033[0;31m'
NC='\033[0m'

#if [ ! -d "/usr/local/eosio" ]; then
# printf "${RED}Error, please ensure that eosio is installed correctly!\n\n${NC}"
# exit -1
#fi

if [ ! -d "/usr/local/eosio.wasmsdk" ]; then
printf "${RED}Error, please ensure that eosio.wasmsdk is installed correctly!\n\n${NC}"
exit -1
fi

unamestr=`uname`
if [[ "${unamestr}" == 'Darwin' ]]; then
PREFIX=/usr/local
BOOST=/usr/local/include
OPENSSL=/usr/local/opt/openssl
BOOST=/usr/local
CXX_COMPILER=g++
else
PREFIX=~/opt
BOOST=~/opt/boost/include
OPENSSL=/usr/include/openssl
BOOST=~/opt/boost
OS_NAME=$( cat /etc/os-release | grep ^NAME | cut -d'=' -f2 | sed 's/\"//gI' )

case "$OS_NAME" in
Expand Down Expand Up @@ -47,35 +54,9 @@ else
esac
fi

EOSIO_PREFIX=/usr/local/eosio

export BOOST=${BOOST}
export PREFIX=${PREFIX}
export INSTALL_PREFIX=/usr/local/eosio

### Build all the contracts

for contract in "${contracts[@]}"; do
pushd ${contract} &> /dev/null
echo "Building ${contract}..."
CONTRACT_NAME="${contract}"
./build.sh
popd &> /dev/null
done


if [ "$1" == "notests" ]; then
exit 0
fi

### Build the unit tests
root_dir=`pwd`
pushd tests &> /dev/null
CORES=`getconf _NPROCESSORS_ONLN`
mkdir -p build
pushd build &> /dev/null
cmake -DCMAKE_CXX_COMPILER="${CXX_COMPILER}" -DROOT_DIR="${root_dir}" -DEOSIO_INSTALL_PREFIX="${EOSIO_PREFIX}" -DOPENSSL_INSTALL_PREFIX="${OPENSSL}" -DSECP256K1_INSTALL_LIB="${EOSIO_PREFIX}" -DBOOST_ROOT="${BOOST}" ../
make -j8
cp unit_test ../../
popd &> /dev/null
rm -r build
cmake -DCXX_COMPILER="${CXX_COMPILER}" -DBOOST_ROOT="${BOOST}" -DEOSIO_INSTALL_PREFIX=/usr/local ../
make -j${CORES}
popd &> /dev/null
12 changes: 12 additions & 0 deletions eosio.msig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/abi/eosio.msig.abi" "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.msig" COPYONLY)

add_executable(eosio.msig.wasm ${CMAKE_CURRENT_SOURCE_DIR}/src/eosio.msig.cpp)
target_include_directories(eosio.msig.wasm
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)

set_target_properties(eosio.msig.wasm
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.msig")

#install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${WASM_ROOT}/eosio.wasmsdk/include)
2 changes: 2 additions & 0 deletions eosio.msig/bin/eosio.msig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eosio.msig.abi
eosio.msig.wasm
11 changes: 11 additions & 0 deletions eosio.sudo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_executable(eosio.sudo.wasm ${CMAKE_CURRENT_SOURCE_DIR}/src/eosio.sudo.cpp)
target_include_directories(eosio.sudo.wasm
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)

set_target_properties(eosio.sudo.wasm
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.sudo")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/abi/eosio.sudo.abi" "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.sudo" COPYONLY)
#install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${WASM_ROOT}/eosio.wasmsdk/include)
2 changes: 2 additions & 0 deletions eosio.sudo/bin/eosio.sudo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eosio.sudo.abi
eosio.sudo.wasm
13 changes: 13 additions & 0 deletions eosio.system/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_executable(eosio.system.wasm ${CMAKE_CURRENT_SOURCE_DIR}/src/eosio.system.cpp)
target_include_directories(eosio.system.wasm
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../eosio.token/include)

set_target_properties(eosio.system.wasm
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.system")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/abi/eosio.system.abi" "${CMAKE_CURRENT_SOURCE_DIR}/bin/eosio.system" COPYONLY)

#install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${WASM_ROOT}/eosio.wasmsdk/include)
26 changes: 25 additions & 1 deletion eosio.system/abi/eosio.system.abi
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@
{"name":"max_inline_action_size", "type":"uint32"},
{"name":"max_inline_action_depth", "type":"uint16"},
{"name":"max_authority_depth", "type":"uint16"}

]
},{
"name": "eosio_global_state2",
"fields": [
{"name":"new_ram_per_block", "type":"uint16"},
{"name":"last_ram_increase", "type":"block_timestamp_type"},
{"name":"last_block_num", "type":"block_timestamp_type"},
{"name":"reserved", "type":"float64"},
{"name":"revision", "type":"uint8"}
]
},{
"name": "eosio_global_state",
Expand Down Expand Up @@ -294,6 +302,12 @@
"fields": [
{"name":"max_ram_size", "type":"uint64"}
]
},{
"name": "setramrate",
"base": "",
"fields": [
{"name":"bytes_per_block", "type":"uint16"}
]
},{
"name": "regproxy",
"base": "",
Expand Down Expand Up @@ -474,6 +488,10 @@
"name": "setram",
"type": "setram",
"ricardian_contract": ""
},{
"name": "setramrate",
"type": "setramrate",
"ricardian_contract": "Sets the number of new bytes of ram to create per block and resyncs bancor base connector balance"
},{
"name": "bidname",
"type": "bidname",
Expand Down Expand Up @@ -535,6 +553,12 @@
"index_type": "i64",
"key_names" : [],
"key_types" : []
},{
"name": "global2",
"type": "eosio_global_state2",
"index_type": "i64",
"key_names" : [],
"key_types" : []
},{
"name": "voters",
"type": "voter_info",
Expand Down
2 changes: 2 additions & 0 deletions eosio.system/bin/eosio.system/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eosio.system.abi
eosio.system.wasm
27 changes: 24 additions & 3 deletions eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ namespace eosiosystem {
(last_producer_schedule_size)(total_producer_vote_weight)(last_name_close) )
};

/**
* Defines new global state parameters added after version 1.0
*/
struct eosio_global_state2 {
eosio_global_state2(){}

uint16_t new_ram_per_block = 0;
block_timestamp last_ram_increase;
block_timestamp last_block_num;
double reserved = 0;
uint8_t revision = 0; ///< used to track version updates in the future.


EOSLIB_SERIALIZE( eosio_global_state2, (new_ram_per_block)(last_ram_increase)(last_block_num)(reserved)(revision) )
};

struct producer_info {
account_name owner;
double total_votes = 0;
Expand Down Expand Up @@ -120,18 +136,21 @@ namespace eosiosystem {
> producers_table;

typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
typedef eosio::singleton<N(global2), eosio_global_state2> global_state2_singleton;

// static constexpr uint32_t max_inflation_rate = 5; // 5% annual inflation
static constexpr uint32_t seconds_per_day = 24 * 3600;
static constexpr uint64_t system_token_symbol = CORE_SYMBOL;

class system_contract : public native {
private:
voters_table _voters;
producers_table _producers;
global_state_singleton _global;
voters_table _voters;
producers_table _producers;
global_state_singleton _global;
global_state2_singleton _global2;

eosio_global_state _gstate;
eosio_global_state2 _gstate2;
rammarket _rammarket;

public:
Expand Down Expand Up @@ -200,6 +219,7 @@ namespace eosiosystem {
void unregprod( const account_name producer );

void setram( uint64_t max_ram_size );
void setramrate( uint16_t bytes_per_block );

void voteproducer( const account_name voter, const account_name proxy, const std::vector<account_name>& producers );

Expand All @@ -217,6 +237,7 @@ namespace eosiosystem {
void bidname( account_name bidder, account_name newname, asset bid );
private:
void update_elected_producers( block_timestamp timestamp );
void update_ram_supply();

// Implementation details:

Expand Down
10 changes: 7 additions & 3 deletions eosio.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace eosiosystem {
* This action will buy an exact amount of ram and bill the payer the current market price.
*/
void system_contract::buyrambytes( account_name payer, account_name receiver, uint32_t bytes ) {

auto itr = _rammarket.find(S(4,RAMCORE));
auto tmp = *itr;
auto eosout = tmp.convert( asset(bytes,S(0,RAM)), CORE_SYMBOL );
Expand All @@ -105,6 +106,8 @@ namespace eosiosystem {
void system_contract::buyram( account_name payer, account_name receiver, asset quant )
{
require_auth( payer );
update_ram_supply();

eosio_assert( quant.amount > 0, "must purchase a positive amount" );

auto fee = quant;
Expand All @@ -117,7 +120,7 @@ namespace eosiosystem {
// quant_after_fee.amount should be > 0 if quant.amount > 1.
// If quant.amount == 1, then quant_after_fee.amount == 0 and the next inline transfer will fail causing the buyram action to fail.

INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {payer,N(active)},
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {{payer,N(active)},{N(eosio.ram),N(active)}},
{ payer, N(eosio.ram), quant_after_fee, std::string("buy ram") } );

if( fee.amount > 0 ) {
Expand Down Expand Up @@ -161,6 +164,8 @@ namespace eosiosystem {
*/
void system_contract::sellram( account_name account, int64_t bytes ) {
require_auth( account );
update_ram_supply();

eosio_assert( bytes > 0, "cannot sell negative byte" );

user_resources_table userres( _self, account );
Expand Down Expand Up @@ -188,12 +193,11 @@ namespace eosiosystem {
});
set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );

INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.ram),N(active)},
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {{N(eosio.ram),N(active)},{account,N(active)}},
{ N(eosio.ram), account, asset(tokens_out), std::string("sell ram") } );

auto fee = ( tokens_out.amount + 199 ) / 200; /// .5% fee (round up)
// since tokens_out.amount was asserted to be at least 2 earlier, fee.amount < tokens_out.amount

if( fee > 0 ) {
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {account,N(active)},
{ account, N(eosio.ramfee), asset(fee), std::string("sell ram fee") } );
Expand Down
Loading

0 comments on commit e8e687a

Please sign in to comment.