Skip to content

Commit

Permalink
progress on #123, #133, fix issues of contract script
Browse files Browse the repository at this point in the history
  • Loading branch information
hackfisher committed Jun 4, 2015
1 parent 87b88b5 commit f38f4b1
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 29 deletions.
22 changes: 22 additions & 0 deletions libraries/api/game_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,27 @@
}
],
"prerequisites" : ["wallet_unlocked"]
},
{
"method_name": "game_list_datas",
"description": "Returns stored game datas starting with a given game name upto a the limit provided",
"return_type": "game_data_record_array",
"cached" : true,
"parameters" : [
{
"name" : "game_name",
"type" : "account_name",
"description" : "the game name to include",
"default_value" : ""
},
{
"name" : "limit",
"type" : "uint32_t",
"description" : "the maximum number of items to list",
"default_value" : 20
}
],
"is_const" : true,
"prerequisites" : ["no_prerequisites"]
}
] }
10 changes: 10 additions & 0 deletions libraries/api/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"container_type" : "array",
"contained_type" : "account_record"
},
{
"type_name" : "game_data_record",
"cpp_return_type" : "bts::blockchain::game_data_record",
"cpp_include_file" : "bts/blockchain/types.hpp"
},
{
"type_name" : "game_data_record_array",
"container_type" : "array",
"contained_type" : "game_data_record"
},
{
"type_name" : "optional_account_record",
"cpp_return_type" : "fc::optional<bts::blockchain::account_record>",
Expand Down
30 changes: 27 additions & 3 deletions libraries/blockchain/chain_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,20 +2065,26 @@ namespace bts { namespace blockchain {

ogame_data_record chain_database::get_game_data_record( const game_id_type& game_id, const data_id_type& data_id )const
{
return my->_game_data_db.fetch_optional( std::make_pair(game_id, data_id) );
game_data_index index;
index.game_id = game_id;
index.data_id = data_id;
return my->_game_data_db.fetch_optional( index );
}

void chain_database::store_game_data_record( const game_id_type& game_id, const data_id_type& data_id, const game_data_record& r )
{
game_data_index index;
index.game_id = game_id;
index.data_id = data_id;
try {
ilog( "game data record: ${r}", ("r",r) );
if( r.is_null() )
{
my->_game_data_db.remove( std::make_pair(game_id, data_id) );
my->_game_data_db.remove( index );
}
else
{
my->_game_data_db.store( std::make_pair(game_id, data_id), r );
my->_game_data_db.store( index, r );
}
} FC_RETHROW_EXCEPTIONS( warn, "", ("record", r) ) }

Expand Down Expand Up @@ -3429,6 +3435,24 @@ namespace bts { namespace blockchain {

return results;
} FC_CAPTURE_AND_RETHROW( (account_name) ) }

vector<game_data_record> chain_database::fetch_game_data_records( const string& game_name, uint32_t limit )const
{ try {
vector<game_data_record> results;
const auto opt_game_record = get_game_record( game_name );
FC_ASSERT( opt_game_record.valid() );

auto itr = my->_game_data_db.lower_bound( {opt_game_record->id} );
while( itr.valid() && itr.key().game_id == opt_game_record->id )
{
results.push_back( itr.value() );
++itr;

if( results.size() >= limit ) break;
}

return results;
} FC_CAPTURE_AND_RETHROW( (game_name) ) }

vector<transaction_record> chain_database::fetch_address_transactions( const address& addr )
{ try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace bts { namespace blockchain {
vector<ad_record> fetch_ad_records( const string& account_name )const;

vector<note_record> fetch_note_records( const string& account_name )const;

vector<game_data_record> fetch_game_data_records( const string& game_name, uint32_t limit )const;

unordered_map<balance_id_type, balance_record> get_balances( const balance_id_type& first,
uint32_t limit )const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace bts { namespace blockchain {
bts::db::level_map<slot_index, slot_record> _slot_index_to_record;
bts::db::level_map<time_point_sec, account_id_type> _slot_timestamp_to_delegate;

bts::db::cached_level_map<std::pair<game_id_type, data_id_type>, game_data_record > _game_data_db;
bts::db::cached_level_map<game_data_index, game_data_record > _game_data_db;
bts::db::cached_level_map<uint32_t, std::vector<game_result_transaction> > _game_result_transactions_db;
bts::db::cached_level_map<uint32_t, std::vector<operation_reward_transaction> > _operation_reward_transactions_db;

Expand Down
20 changes: 18 additions & 2 deletions libraries/blockchain/include/bts/blockchain/game_record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,27 @@ namespace bts { namespace blockchain {
virtual void game_erase_from_name_map( const string& ) = 0;
};

struct game_data_index
{
game_id_type game_id;
data_id_type data_id;

friend bool operator < ( const game_data_index& a, const game_data_index& b )
{
return std::tie( a.game_id, a.data_id ) < std::tie( b.game_id, b.data_id );
}

friend bool operator == ( const game_data_index& a, const game_data_index& b )
{
return std::tie( a.game_id, a.data_id ) == std::tie( b.game_id, b.data_id );
}
};

struct game_data_record
{
game_data_record():game_id(0){}

int32_t get_game_data_index()const
data_id_type get_game_data_index()const
{ try {
FC_ASSERT( data.is_object() );
FC_ASSERT( data.get_object().contains( "index" ) );
Expand Down Expand Up @@ -101,7 +117,7 @@ FC_REFLECT( bts::blockchain::game_record,
(registration_date)
(last_update)
)

FC_REFLECT( bts::blockchain::game_data_index, (game_id)(data_id) )
FC_REFLECT( bts::blockchain::game_data_record,
(game_id)
(data)
Expand Down
2 changes: 1 addition & 1 deletion libraries/blockchain/include/bts/blockchain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace bts { namespace blockchain {
typedef int64_t share_type;
typedef uint64_t slate_id_type;
typedef fc::signed_int game_id_type;
typedef uint32_t data_id_type;
typedef fc::signed_int data_id_type;
typedef fc::signed_int issuer_id_type; // Could be {account_id_type, game_id_type}
typedef fc::signed_int operation_id_type;

Expand Down
6 changes: 6 additions & 0 deletions libraries/client/game_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ wallet_transaction_record client_impl::game_play(const std::string& game_name, c
network_broadcast_transaction(record.trx);
return record;
}

vector<game_data_record> client_impl::game_list_datas( const string& game_name, uint32_t limit )const
{ try {
FC_ASSERT( limit > 0 );
return _chain_db->fetch_game_data_records( game_name, limit );
} FC_CAPTURE_AND_RETHROW( (game_name)(limit) ) }
2 changes: 2 additions & 0 deletions libraries/game/include/bts/game/v8_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ namespace bts { namespace game {

static void Get_Game_Data_Record(const v8::FunctionCallbackInfo<Value>& args);

static void Get_Account_Record_By_Name(const v8::FunctionCallbackInfo<Value>& args);

static void Store_Blance_Record(const v8::FunctionCallbackInfo<Value>& args);

static void Store_Asset_Record(const v8::FunctionCallbackInfo<Value>& args);
Expand Down
2 changes: 1 addition & 1 deletion libraries/game/include/bts/game/v8_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace bts { namespace game {

wallet_transaction_record play( game_id_type game_id, chain_database_ptr blockchain, bts::wallet::wallet_ptr w, const variant& var, bool sign );

bool scan_ledger( wallet_transaction_record& trx_rec, bts::wallet::wallet_ptr w, const variant& var);
bool scan_ledger( chain_database_ptr blockchain, bts::wallet::wallet_ptr w, wallet_transaction_record& trx_rec, const variant& var);

bool scan_result( const game_result_transaction& rtrx,
uint32_t block_num,
Expand Down
2 changes: 2 additions & 0 deletions libraries/game/include/bts/game/v8_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace bts { namespace game {
static void trx_id_to_hash_array(const v8::FunctionCallbackInfo<v8::Value>& args);
static void fc_ripemd160_hash(const v8::FunctionCallbackInfo<v8::Value>& args );

static void public_key_to_address(const v8::FunctionCallbackInfo<v8::Value>& args );

template<typename T>
static Handle<Value> cpp_to_json(Isolate* isolate, const T& v )
{
Expand Down
71 changes: 58 additions & 13 deletions libraries/game/v8_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace bts { namespace game {
pendingstate_proto->Set(isolate, "get_balance_record", FunctionTemplate::New(isolate, v8_chainstate::Get_Blance_Record));
pendingstate_proto->Set(isolate, "get_asset_record", FunctionTemplate::New(isolate, v8_chainstate::Get_Asset_Record));
pendingstate_proto->Set(isolate, "get_game_data_record", FunctionTemplate::New(isolate, v8_chainstate::Get_Game_Data_Record));
pendingstate_proto->Set(isolate, "get_account_record_by_name", FunctionTemplate::New(isolate, v8_chainstate::Get_Account_Record_By_Name));

pendingstate_proto->Set(isolate, "set_balance_record", FunctionTemplate::New(isolate, v8_chainstate::Store_Blance_Record));
pendingstate_proto->Set(isolate, "set_asset_record", FunctionTemplate::New(isolate, v8_chainstate::Store_Asset_Record));
Expand Down Expand Up @@ -381,17 +382,22 @@ namespace bts { namespace game {
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
//get member variable value
auto addr = v8_helper::json_to_cpp<bts::blockchain::address>(args.GetIsolate(), args[0] );
auto key = static_cast<v8_wallet*>(ptr)->_wallet->get_wallet_key_for_address( addr );

if ( key.valid() )
{
// return the value
args.GetReturnValue().Set( v8_helper::cpp_to_json(args.GetIsolate(), key) );
} else
{
try {
auto addr = v8_helper::json_to_cpp<bts::blockchain::address>(args.GetIsolate(), args[0] );
auto key = static_cast<v8_wallet*>(ptr)->_wallet->get_wallet_key_for_address( addr );

if ( key.valid() )
{
// return the value
args.GetReturnValue().Set( v8_helper::cpp_to_json(args.GetIsolate(), key) );
} else
{
args.GetReturnValue().Set( v8::Null(args.GetIsolate() ) );
}
} catch ( ... ) {
args.GetReturnValue().Set( v8::Null(args.GetIsolate() ) );
}

}

void v8_wallet::Store_Transaction(const v8::FunctionCallbackInfo<Value>& args)
Expand Down Expand Up @@ -489,6 +495,39 @@ namespace bts { namespace game {
}
}

void v8_chainstate::Get_Account_Record_By_Name(const v8::FunctionCallbackInfo<Value>& args)
{
EscapableHandleScope handle_scope(args.GetIsolate());

Local<Object> self = args.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();

try {
auto account_record = static_cast<v8_chainstate*>(ptr)->_chain_state->get_account_record( v8_helper::ToCString( String::Utf8Value( args[0]->ToString() ) ) );

if ( account_record.valid() )
{
wlog("the account record is ${a}", ("a", *account_record) );
Local<Value> res = v8_helper::cpp_to_json(args.GetIsolate(), *account_record );

if ( res->IsObject() )
{
res->ToObject()->Set(String::NewFromUtf8( args.GetIsolate(), "active_key") , v8_helper::cpp_to_json(args.GetIsolate(), account_record->active_key() ) );
}

args.GetReturnValue().Set( handle_scope.Escape( res ) );
} else {
args.GetReturnValue().Set( v8::Null( args.GetIsolate() ) );
}
} catch ( const fc::exception& e )
{
wlog("Failed to Get_Account_Record_By_Name: ${e}", ("e", e.to_detail_string()));
args.GetReturnValue().Set( v8::Null( args.GetIsolate() ) );

}
}

void v8_blockchain::Get_Game_Data_Record(const v8::FunctionCallbackInfo<Value>& args)
{
EscapableHandleScope handle_scope(args.GetIsolate());
Expand All @@ -497,13 +536,18 @@ namespace bts { namespace game {
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();

Local<Integer> wrapper_type = Local<Integer>::Cast(args[0]);
Local<Integer> wrapper_id = Local<Integer>::Cast(args[1]);
Local<Integer> wrapper_game_id = Local<Integer>::Cast(args[0]);
Local<Integer> wrapper_data_id = Local<Integer>::Cast(args[1]);

int32_t game_id = wrapper_game_id->Int32Value();
int32_t data_id = wrapper_data_id->Int32Value();

auto game_data_record = static_cast<v8_blockchain*>(ptr)->_blockchain->get_game_data_record(wrapper_type->Int32Value(), wrapper_id->Int32Value() );
wlog("Starting Get_Game_Data_Record with game_id:${g} and data_id:${d}", ("g", game_id)("d", data_id));
auto game_data_record = static_cast<v8_blockchain*>(ptr)->_blockchain->get_game_data_record(game_id, data_id );

if ( game_data_record.valid() )
{
wlog("game_data_record:${d}", ("d", *game_data_record));
Local<Value> res = v8_helper::cpp_to_json(args.GetIsolate(), *game_data_record );
args.GetReturnValue().Set( handle_scope.Escape( res ) );
} else {
Expand Down Expand Up @@ -539,7 +583,8 @@ namespace bts { namespace game {

/**
* @brief Method for v8_chainstate
* @return undefine
* @deprecated
* TODO
*/
void v8_chainstate::Store_Game_Data_Record(const v8::FunctionCallbackInfo<Value>& args)
{
Expand Down
6 changes: 3 additions & 3 deletions libraries/game/v8_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ namespace bts { namespace game {
return record;
}

bool v8_game_engine::scan_ledger( wallet_transaction_record& trx_rec, bts::wallet::wallet_ptr w, const variant& var )
bool v8_game_engine::scan_ledger( chain_database_ptr blockchain, bts::wallet::wallet_ptr w, wallet_transaction_record& trx_rec, const variant& var )
{

auto isolate = my->GetIsolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(my->GetIsolate());
Expand All @@ -370,14 +369,15 @@ namespace bts { namespace game {
// Entering the context
Context::Scope context_scope(context);

context->Global()->Set(String::NewFromUtf8(my->GetIsolate(), "$blockchain"), v8_blockchain::New(my->GetIsolate(), blockchain, blockchain->get_head_block_num()));
context->Global()->Set( String::NewFromUtf8(isolate, "$wallet_transaction_record"), v8_helper::cpp_to_json(isolate, trx_rec) );
context->Global()->Set( String::NewFromUtf8(isolate, "$wallet"), v8_wallet::New(isolate, w) );

auto _input = var; // TODO: convert/parse it to a v8 javascript object
context->Global()->Set(String::NewFromUtf8(isolate, "$input"), v8_helper::cpp_to_json(isolate, _input));

v8::TryCatch try_catch(my->GetIsolate());
auto source = "PLAY.scan_ledger($wallet_transaction_record, $wallet, $input);";
auto source = "PLAY.scan_ledger($blockchain, $wallet, $wallet_transaction_record, $input);";

v8::Handle<v8::Script> script = v8::Script::Compile( String::NewFromUtf8( my->GetIsolate(), source) );
if ( script.IsEmpty() )
Expand Down
18 changes: 18 additions & 0 deletions libraries/game/v8_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace bts { namespace game {
auto trx_id = json_to_cpp<bts::blockchain::transaction_id_type>(args.GetIsolate(), args[0]);

// Fill out the values
// the value converted to int32_t
array->Set( 0, Integer::New(args.GetIsolate(), trx_id._hash[0]) );
array->Set( 1, Integer::New(args.GetIsolate(), trx_id._hash[1]) );
array->Set( 2, Integer::New(args.GetIsolate(), trx_id._hash[2]) );
Expand All @@ -86,6 +87,22 @@ namespace bts { namespace game {
}
}

void v8_helper::public_key_to_address(const v8::FunctionCallbackInfo<v8::Value>& args )
{
try {
HandleScope handle_scope(args.GetIsolate());

std::string public_key_str(v8_helper::ToCString(String::Utf8Value(args[0]->ToString(args.GetIsolate()))));
;
bts::blockchain::address addr( (bts::blockchain::public_key_type( public_key_str )) );

args.GetReturnValue().Set( v8_helper::cpp_to_json( args.GetIsolate(), addr ) );
} catch ( ... )
{
args.GetReturnValue().Set( v8::Null( args.GetIsolate() ) );
}
}


// Creates a new execution environment containing the built-in
// functions.
Expand All @@ -112,6 +129,7 @@ namespace bts { namespace game {

global->Set(v8::String::NewFromUtf8(isolate, "trx_id_to_hash_array"), v8::FunctionTemplate::New(isolate, trx_id_to_hash_array) );
global->Set(v8::String::NewFromUtf8(isolate, "fc_ripemd160_hash"), v8::FunctionTemplate::New(isolate, fc_ripemd160_hash) );
global->Set(v8::String::NewFromUtf8(isolate, "public_key_to_address"), v8::FunctionTemplate::New(isolate, public_key_to_address) );

return v8::Context::New(isolate, NULL, global);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/wallet/transaction_ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ bool wallet_impl::scan_game( const game_operation& op, wallet_transaction_record

if ( ogame.valid() )
{
return _game_client->get_v8_engine( ogame->name )->scan_ledger(trx_rec, self->shared_from_this(), op.input.data );
return _game_client->get_v8_engine( ogame->name )->scan_ledger( _blockchain, self->shared_from_this(), trx_rec, op.input.data );
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion programs/qt_wallet
2 changes: 1 addition & 1 deletion programs/web_wallet
Loading

0 comments on commit f38f4b1

Please sign in to comment.