Skip to content

Commit 22b6135

Browse files
vote on jit loading
1 parent 68710bc commit 22b6135

File tree

13 files changed

+258
-4
lines changed

13 files changed

+258
-4
lines changed

contracts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_subdirectory(libc++)
1111
add_subdirectory(simple.token)
1212
add_subdirectory(eosio.token)
1313
add_subdirectory(eosio.msig)
14+
add_subdirectory(eosio.prods)
1415
add_subdirectory(eosio.sudo)
1516
add_subdirectory(multi_index_test)
1617
add_subdirectory(eosio.system)

contracts/eosio.prods/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
file(GLOB ABI_FILES "*.abi")
2+
configure_file("${ABI_FILES}" "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
3+
4+
add_wast_executable(TARGET eosio.prods
5+
INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
6+
LIBRARIES libc++ libc eosiolib
7+
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
8+
)
9+
10+
add_library(eosio_prods_native SHARED eosio.prods.cpp)
11+
12+
target_link_libraries(eosio_prods_native PRIVATE eosiolib_native)
13+
14+
target_include_directories(eosio_prods_native PRIVATE ${Boost_INCLUDE_DIR}
15+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/upstream/include
16+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/include/libc++/upstream/include
17+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/musl/include/musl/upstream/include
18+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/musl/upstream/include
19+
PRIVATE ${CMAKE_SOURCE_DIR}/externals/magic_get/include
20+
PRIVATE ${CMAKE_SOURCE_DIR}/contracts
21+
)
22+
23+
24+
add_library(eosio_prods_static STATIC eosio.prods.cpp)
25+
26+
target_link_libraries(eosio_prods_static PRIVATE eosiolib_native)
27+
28+
target_include_directories(eosio_prods_static PRIVATE ${Boost_INCLUDE_DIR}
29+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/upstream/include
30+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/include/libc++/upstream/include
31+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/libc++/musl/include/musl/upstream/include
32+
# PRIVATE ${CMAKE_SOURCE_DIR}/contracts/musl/upstream/include
33+
PRIVATE ${CMAKE_SOURCE_DIR}/externals/magic_get/include
34+
PRIVATE ${CMAKE_SOURCE_DIR}/contracts
35+
)
36+

contracts/eosio.prods/eosio.prods.abi

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"version": "eosio::abi/1.0",
3+
"types": [{
4+
"new_type_name": "account_name",
5+
"type": "name"
6+
}],
7+
"structs": [{
8+
"name": "votejit",
9+
"base": "",
10+
"fields": [
11+
{"name":"producer", "type":"account_name"},
12+
{"name":"account", "type":"account_name"},
13+
{"name":"last_code_update", "type":"int64"}
14+
]
15+
},{
16+
"name": "clearjit",
17+
"base": "",
18+
"fields": [
19+
{"name":"producer", "type":"account_name"},
20+
{"name":"account", "type":"account_name"},
21+
]
22+
}
23+
],
24+
"actions": [{
25+
"name": "votejit",
26+
"type": "votejit",
27+
"ricardian_contract": ""
28+
},{
29+
"name": "clearjit",
30+
"type": "clearjit",
31+
"ricardian_contract": ""
32+
}
33+
],
34+
"tables": [],
35+
"ricardian_clauses": [],
36+
"abi_extensions": []
37+
}

contracts/eosio.prods/eosio.prods.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @file
3+
* @copyright defined in eos/LICENSE.txt
4+
*/
5+
6+
#include "eosio.prods.hpp"
7+
#include <eosio.system/eosio.system.hpp>
8+
namespace eosio {
9+
10+
using namespace eosiosystem;
11+
12+
prods::prods( account_name self ):contract(self),_jit_info(self, self)
13+
{
14+
15+
}
16+
17+
void prods::votejit( account_name producer, account_name account, int64_t last_code_update) {
18+
require_auth(producer);
19+
auto it = _jit_info.find(account);
20+
if (it == _jit_info.end()) {
21+
_jit_info.emplace(_self, [&]( auto& m ){
22+
m.account = account;
23+
m.voted_producers.push_back(producer);
24+
m.last_code_update = last_code_update;
25+
});
26+
return;
27+
}
28+
29+
if (it->last_code_update != last_code_update) {
30+
_jit_info.modify( it, 0, [&]( auto& m ) {
31+
m.voted_producers.clear();
32+
m.voted_producers.push_back(producer);
33+
m.last_code_update = last_code_update;
34+
});
35+
return;
36+
}
37+
38+
global_state_singleton _global(N(eosio), N(eosio));
39+
40+
if (!_global.exists()) {
41+
return;
42+
}
43+
44+
{
45+
if (std::find(it->voted_producers.begin(), it->voted_producers.end(), producer) == it->voted_producers.end()) {
46+
_jit_info.modify( it, 0, [&]( auto& m ) {
47+
m.voted_producers.push_back(producer);
48+
if (m.voted_producers.size() >= _global.get().last_producer_schedule_size/3*2) {
49+
m.activated = true;
50+
}
51+
});
52+
}
53+
}
54+
}
55+
56+
void prods::clearjit( account_name producer, account_name account) {
57+
require_auth(producer);
58+
auto it = _jit_info.find(account);
59+
if (it != _jit_info.end()) {
60+
_jit_info.erase(it);
61+
}
62+
}
63+
64+
activated_jit_info_table _jit_info(N(eosio.prods), N(eosio.prods));
65+
66+
extern "C" bool is_jit_account_activated(uint64_t account) {
67+
auto it = _jit_info.find(account);
68+
if (it == _jit_info.end()) {
69+
return false;
70+
}
71+
return it->activated;
72+
}
73+
74+
extern "C" bool jit_account_deactivate(uint64_t account) {
75+
auto it = _jit_info.find(account);
76+
if (it == _jit_info.end()) {
77+
return false;
78+
}
79+
_jit_info.erase(it);
80+
return true;
81+
}
82+
83+
} /// namespace eosio
84+
85+
EOSIO_ABI( eosio::prods, (votejit)(clearjit) )
86+
87+

contracts/eosio.prods/eosio.prods.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file
3+
* @copyright defined in eos/LICENSE.txt
4+
*/
5+
#pragma once
6+
7+
#include <eosiolib/asset.hpp>
8+
#include <eosiolib/eosio.hpp>
9+
10+
#include <string>
11+
12+
13+
struct activated_jit_info {
14+
account_name account = 0;
15+
bool activated = false;
16+
int64_t last_code_update = 0;
17+
std::vector<account_name> voted_producers; // producer ready list
18+
19+
uint64_t primary_key()const { return account; }
20+
21+
// explicit serialization macro is not necessary, used here only to improve compilation time
22+
EOSLIB_SERIALIZE( activated_jit_info, (account)(activated)(last_code_update)(voted_producers) )
23+
};
24+
25+
typedef eosio::multi_index< N(jitinfo), activated_jit_info> activated_jit_info_table;
26+
27+
28+
namespace eosio {
29+
30+
using std::string;
31+
32+
class prods : public contract {
33+
public:
34+
prods( account_name self );
35+
void votejit( account_name producer, account_name account, int64_t last_code_update);
36+
void clearjit( account_name from, account_name account);
37+
private:
38+
activated_jit_info_table _jit_info;
39+
};
40+
41+
} /// namespace eosio

contracts/eosio.system/eosio.system.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ namespace eosiosystem {
2828
bid.start_bid_time = current_time();
2929
bid.jit_remains = 100; //TODO: max jit resources, should be configurable
3030
_jitbid.set(bid, _self);
31+
_boost.emplace( _self, [&]( auto& b ) {
32+
b.account = N(eosio);
33+
b.expiration = -1;
34+
});
35+
_boost.emplace( _self, [&]( auto& b ) {
36+
b.account = N(eosio.token);
37+
b.expiration = -1;
38+
});
3139
}
3240

3341
auto itr = _rammarket.find(S(4,RAMCORE));

libraries/chain/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
7878
target_compile_options(eosio_chain PUBLIC -DDEBUG)
7979
endif()
8080

81-
target_link_libraries( eosio_chain PUBLIC eos_utilities fc chainbase appbase db_api vm_manager softfloat builtins python3 ${OPENSSL_LIBRARIES})
81+
target_link_libraries( eosio_chain PUBLIC eosio_prods_static eos_utilities fc chainbase appbase db_api vm_manager softfloat builtins python3 ${OPENSSL_LIBRARIES})
8282

8383
#vm_py compiled to shared lib and do not link to eosio_chain directly, and loaded by dlopen,
8484
#but there are header files depend on vm_py,

libraries/chain/controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ struct controller_impl {
10751075

10761076
update_permission( authorization.get_permission({config::producers_account_name,
10771077
config::single_producers_permission_name}),
1078-
calculate_threshold( 1, 21 ) /* more than one-third */ );
1078+
calculate_threshold( 1, 21 ) /* 1/21 */ );
10791079
//TODO: Add tests
10801080
}
10811081

libraries/chain/eosio_contract.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace eosio { namespace chain {
3333
void apply_eosio_setcode_py(apply_context& context);
3434
void apply_eosio_setcode_evm(apply_context& context);
3535
void apply_eosio_setcode_rpc(apply_context& context);
36+
extern "C" bool jit_account_deactivate(uint64_t account);
3637

3738
uint128_t transaction_id_to_sender_id( const transaction_id_type& tid ) {
3839
fc::uint128_t _id(tid._hash[3], tid._hash[2]);
@@ -152,11 +153,12 @@ void apply_eosio_setcode(apply_context& context) {
152153
if (context.control.pending_block_time().sec_since_epoch() - account.last_code_update.sec_since_epoch() < 10*60) {
153154
throw FC_EXCEPTION( fc::exception, "code update in less than 10minutes from the previous update");
154155
}
155-
156156
}
157157

158158
check_account_lock_status( context, act.account );
159159

160+
jit_account_deactivate(act.account.value);
161+
160162
if (act.vmtype == 1) {
161163
apply_eosio_setcode_py(context);
162164
return;

libraries/vm_manager/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ add_library( vm_manager
88
vm_manager.cpp
99
)
1010

11-
target_link_libraries( vm_manager PRIVATE fc appbase db_api)
11+
target_link_libraries( vm_manager PRIVATE fc appbase db_api eosio_prods_static)
1212

1313
target_include_directories( vm_manager
1414
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"

libraries/vm_manager/vm_manager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ typedef struct vm_wasm_api* (*fn_get_wasm_vm_api)();
5151
typedef uint64_t (*fn_wasm_call)(const char* act, uint64_t* args, int argc);
5252
bool is_boost_account(uint64_t account, bool& expired);
5353

54+
//eosio.prods.cpp
55+
extern "C" bool is_jit_account_activated(uint64_t account);
56+
57+
5458
extern "C" bool is_server_mode();
5559

5660
void _on_boost_account(void* v, uint64_t account, uint64_t expiration) {
@@ -561,6 +565,10 @@ int vm_manager::local_apply(int type, uint64_t receiver, uint64_t account, uint6
561565
break;
562566
}
563567

568+
if (!is_jit_account_activated(receiver)) {
569+
break;
570+
}
571+
564572
auto itr = preload_account_map.find(receiver);
565573
if (itr != preload_account_map.end()) {
566574
return itr->second->apply(receiver, account, act);

programs/pyeos/config.py

Whitespace-only changes.

programs/pyeos/tests/eosio_prods/t.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import time
3+
import struct
4+
5+
import wallet
6+
import eosapi
7+
import initeos
8+
9+
from eosapi import N
10+
from tools import cpp2wast
11+
12+
from common import prepare, producer
13+
14+
print('please make sure you are running the following command before test')
15+
print('./pyeos/pyeos --manual-gen-block --debug -i')
16+
17+
def init(wasm=True):
18+
def init_decorator(func):
19+
def func_wrapper(wasm=True, *args, **kwargs):
20+
eosapi.push_action('eosio', 'setpriv', {'account':'eosio.prods', 'is_priv':1}, {'eosio':'active'})
21+
r = eosapi.set_contract('eosio.prods', '../contracts/eosio.prods/eosio.prods.wast', '../contracts/eosio.prods/eosio.prods.abi', 0)
22+
return func(*args, **kwargs)
23+
return func_wrapper
24+
return init_decorator
25+
26+
@init()
27+
def test(msg='hello,world'):
28+
msg = {"producer":"prod.aa", "account":"eosio"}
29+
r = eosapi.push_action('eosio.prods', 'votejit', {"producer":"prod.aa", "account":"eosio", 'last_code_update':0}, {'prod.aa':'active'})
30+
31+
@init()
32+
def testclear(msg='hello,world'):
33+
msg = {"producer":"prod.aa", "account":"eosio"}
34+
r = eosapi.push_action('eosio.prods', 'clearjit', {"producer":"prod.aa", "account":"eosio"}, {'prod.aa':'active'})

0 commit comments

Comments
 (0)