Skip to content

Commit

Permalink
Chain id as command line argument - small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz-Trela committed Oct 12, 2017
1 parent 3552fa5 commit 7823b48
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 43 deletions.
4 changes: 4 additions & 0 deletions libraries/appbase/include/appbase/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ namespace appbase {

boost::asio::io_service& get_io_service() { return *io_serv; }

const std::string& get_chain_desc() const{ return chain_desc; };
void set_chain_desc( const std::string& str ){ chain_desc = str; };

protected:
template< typename Impl >
friend class plugin;
Expand All @@ -112,6 +115,7 @@ namespace appbase {
vector< abstract_plugin* > running_plugins; ///< stored in the order they were started running
std::shared_ptr< boost::asio::io_service > io_serv;
std::string version_info;
std::string chain_desc;

void set_program_options();
void write_default_config( const bfs::path& cfg_file );
Expand Down
14 changes: 11 additions & 3 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ database_impl::database_impl( database& self )
: _self(self), _evaluator_registry(self) {}

database::database()
: _my( new database_impl(*this) ) {}
: _my( new database_impl(*this) )
{
set_chain_id( STEEM_CHAIN_ID_NAME );
}

database::~database()
{
Expand Down Expand Up @@ -364,7 +367,12 @@ std::vector< block_id_type > database::get_block_ids_on_fork( block_id_type head

chain_id_type database::get_chain_id() const
{
return steem::protocol::chain_id;
return STEEM_CHAIN_ID;
}

void database::set_chain_id( const std::string& _chain_id_name )
{
STEEM_CHAIN_ID = generate_chain_id( _chain_id_name );
}

const witness_object& database::get_witness( const account_name_type& name ) const
Expand Down Expand Up @@ -2861,7 +2869,7 @@ void database::_apply_transaction(const signed_transaction& trx)
trx.validate();

auto& trx_idx = get_index<transaction_index>();
const chain_id_type& chain_id = steem::protocol::chain_id;
const chain_id_type& chain_id = get_chain_id();
auto trx_id = trx.id();
// idump((trx_id)(skip&skip_transaction_dupe_check));
FC_ASSERT( (skip & skip_transaction_dupe_check) ||
Expand Down
4 changes: 3 additions & 1 deletion libraries/chain/include/steem/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ namespace steem { namespace chain {
const signed_transaction get_recent_transaction( const transaction_id_type& trx_id )const;
std::vector<block_id_type> get_block_ids_on_fork(block_id_type head_of_fork) const;

chain_id_type get_chain_id()const;
chain_id_type STEEM_CHAIN_ID;
chain_id_type get_chain_id() const;
void set_chain_id( const std::string& _chain_id_name );


const witness_object& get_witness( const account_name_type& name )const;
Expand Down
3 changes: 3 additions & 0 deletions libraries/net/include/graphene/net/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace graphene { namespace net {
public:
virtual ~node_delegate(){}


virtual steem::protocol::chain_id_type get_chain_id() const = 0;

/**
* If delegate has the item, the network has no need to fetch it.
*/
Expand Down
15 changes: 11 additions & 4 deletions libraries/net/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ namespace graphene { namespace net { namespace detail {
(get_block_time) \
(get_head_block_id) \
(estimate_last_known_fork_from_git_revision_timestamp) \
(error_encountered)
(error_encountered) \
(get_chain_id)


#define DECLARE_ACCUMULATOR(r, data, method_name) \
Expand Down Expand Up @@ -378,6 +379,7 @@ namespace graphene { namespace net { namespace detail {

fc::variant_object get_call_statistics();

steem::protocol::chain_id_type get_chain_id() const override;
bool has_item( const net::item_id& id ) override;
void handle_message( const message& ) override;
bool handle_block( const graphene::net::block_message& block_message, bool sync_mode, std::vector<fc::uint160_t>& contained_transaction_message_ids ) override;
Expand Down Expand Up @@ -1876,7 +1878,7 @@ namespace graphene { namespace net { namespace detail {
if (!_hard_fork_block_numbers.empty())
user_data["last_known_fork_block_number"] = _hard_fork_block_numbers.back();

user_data["chain_id"] = steem::protocol::chain_id;
user_data["chain_id"] = _delegate->get_chain_id();

return user_data;
}
Expand Down Expand Up @@ -1994,10 +1996,10 @@ namespace graphene { namespace net { namespace detail {
}
}
}
if ( !originating_peer->chain_id || *originating_peer->chain_id != steem::protocol::chain_id )
if ( !originating_peer->chain_id || *originating_peer->chain_id != _delegate->get_chain_id() )
{
wlog("Received hello message from peer running a node for different blockchain.",
("my_chain_id", steem::protocol::chain_id)("their_chain_id", originating_peer->chain_id) );
("my_chain_id", _delegate->get_chain_id())("their_chain_id", originating_peer->chain_id) );

std::ostringstream rejection_message;
rejection_message << "Your client is running a different chain id";
Expand Down Expand Up @@ -5522,6 +5524,11 @@ namespace graphene { namespace net { namespace detail {
}, "invoke " BOOST_STRINGIZE(method_name)).wait()
#endif

steem::protocol::chain_id_type statistics_gathering_node_delegate_wrapper::get_chain_id() const
{
INVOKE_AND_COLLECT_STATISTICS(get_chain_id);
}

bool statistics_gathering_node_delegate_wrapper::has_item( const net::item_id& id )
{
INVOKE_AND_COLLECT_STATISTICS(has_item, id);
Expand Down
6 changes: 3 additions & 3 deletions libraries/plugins/apis/database_api/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ DEFINE_API( database_api, get_required_signatures )
DEFINE_API( database_api_impl, get_required_signatures )
{
get_required_signatures_return result;
result.keys = args.trx.get_required_signatures( steem::protocol::chain_id,
result.keys = args.trx.get_required_signatures( _db.get_chain_id(),
args.available_keys,
[&]( string account_name ){ return authority( _db.get< chain::account_authority_object, chain::by_account >( account_name ).active ); },
[&]( string account_name ){ return authority( _db.get< chain::account_authority_object, chain::by_account >( account_name ).owner ); },
Expand All @@ -1744,7 +1744,7 @@ DEFINE_API( database_api_impl, get_potential_signatures )
{
get_potential_signatures_return result;
args.trx.get_required_signatures(
steem::protocol::chain_id,
_db.get_chain_id(),
flat_set< public_key_type >(),
[&]( account_name_type account_name )
{
Expand Down Expand Up @@ -1784,7 +1784,7 @@ DEFINE_API( database_api, verify_authority )

DEFINE_API( database_api_impl, verify_authority )
{
args.trx.verify_authority( steem::protocol::chain_id,
args.trx.verify_authority(_db.get_chain_id(),
[&]( string account_name ){ return authority( _db.get< chain::account_authority_object, chain::by_account >( account_name ).active ); },
[&]( string account_name ){ return authority( _db.get< chain::account_authority_object, chain::by_account >( account_name ).owner ); },
[&]( string account_name ){ return authority( _db.get< chain::account_authority_object, chain::by_account >( account_name ).posting ); },
Expand Down
7 changes: 5 additions & 2 deletions libraries/plugins/chain/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void chain_plugin::set_program_options(options_description& cli, options_descrip
("check-locks", bpo::bool_switch()->default_value(false), "Check correctness of chainbase locking" )
("validate-database-invariants", bpo::bool_switch()->default_value(false), "Validate all supply invariants check out" )
#ifdef IS_TEST_NET
("chain-id", bpo::value< std::string >()->default_value( steem::protocol::chain_id_name ), "chain ID to connect to")
("chain-id", bpo::value< std::string >()->default_value( STEEM_CHAIN_ID_NAME ), "chain ID to connect to")
#endif
;
}
Expand Down Expand Up @@ -104,10 +104,13 @@ void chain_plugin::plugin_initialize(const variables_map& options) {
my->loaded_checkpoints[item.first] = item.second;
}
}

app().set_chain_desc( std::string( my->db.get_chain_id() ) );
#ifdef IS_TEST_NET
if( options.count( "chain-id" ) )
{
steem::protocol::chain_id = steem::protocol::set_chain_id( options.at("chain-id").as< std::string >() );
my->db.set_chain_id( options.at("chain-id").as< std::string >() );
app().set_chain_desc( std::string( my->db.get_chain_id() ) );
}
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/plugins/p2p/p2p_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class p2p_plugin_impl : public graphene::net::node_delegate
virtual ~p2p_plugin_impl() {}

bool is_included_block(const block_id_type& block_id);
chain::chain_id_type get_chain_id()const;
virtual steem::protocol::chain_id_type get_chain_id() const override;

// node_delegate interface
virtual bool has_item( const graphene::net::item_id& ) override;
Expand Down Expand Up @@ -270,9 +270,9 @@ graphene::net::message p2p_plugin_impl::get_item( const graphene::net::item_id&
});
} FC_CAPTURE_AND_RETHROW( (id) ) }

chain::chain_id_type p2p_plugin_impl::get_chain_id() const
steem::protocol::chain_id_type p2p_plugin_impl::get_chain_id() const
{
return steem::protocol::chain_id;
return chain.db().get_chain_id();
}

std::vector< graphene::net::item_hash_t > p2p_plugin_impl::get_blockchain_synopsis( const graphene::net::item_hash_t& reference_point, uint32_t number_of_blocks_after_reference_point )
Expand Down
5 changes: 5 additions & 0 deletions libraries/protocol/include/steem/protocol/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#define STEEM_ACCOUNT_RECOVERY_REQUEST_EXPIRATION_PERIOD fc::seconds(12)
#define STEEM_OWNER_UPDATE_LIMIT fc::seconds(0)
#define STEEM_OWNER_AUTH_HISTORY_TRACKING_START_BLOCK_NUM 1

#define STEEM_CHAIN_ID_NAME "testnet"

#else // IS LIVE STEEM NETWORK

#define STEEM_BLOCKCHAIN_VERSION ( version(0, 19, 3) )
Expand All @@ -56,6 +59,8 @@
#define STEEM_OWNER_UPDATE_LIMIT fc::minutes(60)
#define STEEM_OWNER_AUTH_HISTORY_TRACKING_START_BLOCK_NUM 3186477

#define STEEM_CHAIN_ID_NAME ""

#endif

#define VESTS_SYMBOL (steem::protocol::asset_symbol_type::from_asset_num( STEEM_ASSET_NUM_VESTS ) )
Expand Down
4 changes: 1 addition & 3 deletions libraries/protocol/include/steem/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ namespace steem {
friend bool operator != ( const extended_private_key_type& p1, const extended_private_key_type& p2);
};

extern std::string chain_id_name;
extern chain_id_type chain_id;
chain_id_type set_chain_id( const std::string& _chain_id_name );
chain_id_type generate_chain_id( const std::string& chain_id_name );

} } // steem::protocol

Expand Down
15 changes: 3 additions & 12 deletions libraries/protocol/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,10 @@ namespace steem { namespace protocol {
return p1.key_data != p2.key_data;
}

std::string chain_id_name =
#ifdef IS_TEST_NET
"testnet";
#else
"";
#endif

chain_id_type chain_id = set_chain_id( chain_id_name );

chain_id_type set_chain_id( const std::string& _chain_id_name )
chain_id_type generate_chain_id( const std::string& chain_id_name )
{
if( _chain_id_name.size() )
return fc::sha256::hash( _chain_id_name.c_str() );
if( chain_id_name.size() )
return fc::sha256::hash( chain_id_name.c_str() );
else
return fc::sha256();
}
Expand Down
10 changes: 6 additions & 4 deletions libraries/wallet/include/steem/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ struct brain_key_info

struct wallet_data
{
vector<char> cipher_keys; /** encrypted keys */
vector<char> cipher_keys; /** encrypted keys */

string ws_server = "ws://localhost:8090";
string ws_user;
string ws_password;
string ws_server = "ws://localhost:8090";
string ws_user;
string ws_password;

steem::protocol::chain_id_type steem_chain_id;
};

enum authority_type
Expand Down
5 changes: 3 additions & 2 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class wallet_api_impl
init_prototype_ops();

_wallet.ws_server = initial_data.ws_server;
_wallet.steem_chain_id = initial_data.steem_chain_id;
}
virtual ~wallet_api_impl()
{}
Expand Down Expand Up @@ -668,7 +669,7 @@ class wallet_api_impl
}

auto minimal_signing_keys = tx.minimize_required_signatures(
steem::protocol::chain_id,
_wallet.steem_chain_id,
available_keys,
[&]( const string& account_name ) -> const authority&
{ return (get_account_from_lut( account_name ).active); },
Expand All @@ -683,7 +684,7 @@ class wallet_api_impl
{
auto it = available_private_keys.find(k);
FC_ASSERT( it != available_private_keys.end() );
tx.sign( it->second, steem::protocol::chain_id );
tx.sign( it->second, _wallet.steem_chain_id );
}

if( broadcast ) {
Expand Down
7 changes: 4 additions & 3 deletions programs/cli_wallet/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main( int argc, char** argv )
("rpc-http-allowip", bpo::value<vector<string>>()->multitoken(), "Allows only specified IPs to connect to the HTTP endpoint" )
("wallet-file,w", bpo::value<string>()->implicit_value("wallet.json"), "wallet to load")
#ifdef IS_TEST_NET
("chain-id", bpo::value< std::string >()->default_value( steem::protocol::chain_id_name ), "chain ID to connect to")
("chain-id", bpo::value< std::string >()->implicit_value( STEEM_CHAIN_ID_NAME ), "chain ID to connect to")
#endif
;
vector<string> allowed_ips;
Expand All @@ -100,9 +100,11 @@ int main( int argc, char** argv )
wdump((allowed_ips));
}

wallet_data wdata;

#ifdef IS_TEST_NET
if( options.count("chain-id") )
steem::protocol::chain_id = steem::protocol::set_chain_id( options["chain-id"].as< std::string >() );
wdata.steem_chain_id = generate_chain_id( options["chain-id"].as< std::string >() );
#endif

fc::path data_dir;
Expand Down Expand Up @@ -134,7 +136,6 @@ int main( int argc, char** argv )
// load_wallet_file(). Seems like this could be better
// designed.
//
wallet_data wdata;

fc::path wallet_file( options.count("wallet-file") ? options.at("wallet-file").as<string>() : "wallet.json");
if( fc::exists( wallet_file ) )
Expand Down
4 changes: 2 additions & 2 deletions programs/steemd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ void info()
auto initminer_private_key = steem::utilities::key_to_wif( STEEM_INIT_PRIVATE_KEY );
std::cerr << "initminer public key: " << STEEM_INIT_PUBLIC_KEY_STR << "\n";
std::cerr << "initminer private key: " << initminer_private_key << "\n";
std::cerr << "chain id: " << std::string( steem::protocol::chain_id ) << "\n";
std::cerr << "chain id: " << appbase::app().get_chain_desc() << "\n";
std::cerr << "blockchain version: " << fc::string( STEEM_BLOCKCHAIN_VERSION ) << "\n";
std::cerr << "------------------------------------------------------\n";
#else
std::cerr << "------------------------------------------------------\n\n";
std::cerr << " STARTING STEEM NETWORK\n\n";
std::cerr << "------------------------------------------------------\n";
std::cerr << "initminer public key: " << STEEM_INIT_PUBLIC_KEY_STR << "\n";
std::cerr << "chain id: " << std::string( steem::protocol::chain_id ) << "\n";
std::cerr << "chain id: " << appbase::app().get_chain_desc() << "\n";
std::cerr << "blockchain version: " << fc::string( STEEM_BLOCKCHAIN_VERSION ) << "\n";
std::cerr << "------------------------------------------------------\n";
#endif
Expand Down
4 changes: 3 additions & 1 deletion programs/util/sign_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ FC_REFLECT( tx_signing_result, (digest)(sig_digest)(key)(sig) )

int main(int argc, char** argv, char** envp)
{
steem::protocol::chain_id_type _chain_id = steem::protocol::generate_chain_id( STEEM_CHAIN_ID_NAME );

// hash key pairs on stdin
std::string chain_id, hash, wif;
while( std::cin )
Expand All @@ -49,7 +51,7 @@ int main(int argc, char** argv, char** envp)
tx_signing_result sres;
sres.tx = sreq.tx;
sres.digest = sreq.tx.digest();
sres.sig_digest = sreq.tx.sig_digest(steem::protocol::chain_id);
sres.sig_digest = sreq.tx.sig_digest( _chain_id );

fc::ecc::private_key priv_key = *steem::utilities::wif_to_key( sreq.wif );
sres.sig = priv_key.sign_compact( sres.sig_digest );
Expand Down

0 comments on commit 7823b48

Please sign in to comment.