Clean up names of assert ops and evaluation dispatch
This commit is contained in:
parent
c43c71372d
commit
0d02361af0
5 changed files with 70 additions and 32 deletions
|
|
@ -7,6 +7,7 @@ add_library( graphene_chain
|
||||||
|
|
||||||
address.cpp
|
address.cpp
|
||||||
asset.cpp
|
asset.cpp
|
||||||
|
predicate.cpp
|
||||||
|
|
||||||
operations.cpp
|
operations.cpp
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,20 +24,17 @@
|
||||||
|
|
||||||
namespace graphene { namespace chain {
|
namespace graphene { namespace chain {
|
||||||
|
|
||||||
struct predicate_visitor
|
struct predicate_evaluator
|
||||||
{
|
{
|
||||||
typedef bool result_type;
|
typedef bool result_type;
|
||||||
const database& db;
|
const database& db;
|
||||||
|
|
||||||
predicate_visitor( const database& d ):db(d){}
|
predicate_evaluator( const database& d ):db(d){}
|
||||||
|
|
||||||
bool operator()( const verify_account_name& p )const
|
template< typename T >
|
||||||
|
bool operator()( const T& p )const
|
||||||
{
|
{
|
||||||
return p.account_id(db).name == p.account_name;
|
return p.evaluate( db );
|
||||||
}
|
|
||||||
bool operator()( const verify_symbol& p )const
|
|
||||||
{
|
|
||||||
return p.asset_id(db).symbol == p.symbol;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -63,7 +60,7 @@ void_result assert_evaluator::do_evaluate( const assert_operation& o )
|
||||||
|
|
||||||
FC_ASSERT( p.which() >= 0 );
|
FC_ASSERT( p.which() >= 0 );
|
||||||
FC_ASSERT( unsigned(p.which()) < max_predicate_opcode );
|
FC_ASSERT( unsigned(p.which()) < max_predicate_opcode );
|
||||||
FC_ASSERT( p.visit( predicate_visitor( _db ) ) );
|
FC_ASSERT( p.visit( predicate_evaluator( _db ) ) );
|
||||||
}
|
}
|
||||||
return void_result();
|
return void_result();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,53 +21,63 @@
|
||||||
|
|
||||||
namespace graphene { namespace chain {
|
namespace graphene { namespace chain {
|
||||||
|
|
||||||
bool is_valid_symbol( const string& symbol );
|
|
||||||
bool is_valid_name( const string& s );
|
|
||||||
|
|
||||||
class database;
|
class database;
|
||||||
|
|
||||||
|
namespace pred {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to verify that account_id->name is account_name
|
* Used to verify that account_id->name is equal to the given string literal.
|
||||||
*/
|
*/
|
||||||
struct verify_account_name
|
struct account_name_eq_lit
|
||||||
{
|
{
|
||||||
account_id_type account_id;
|
account_id_type account_id;
|
||||||
string account_name;
|
string name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform state independent checks, such as verifying that
|
* Perform state-independent checks. Verify
|
||||||
* account_name is a valid name for an account.
|
* account_name is a valid account name.
|
||||||
*/
|
*/
|
||||||
bool validate()const { return is_valid_name( account_name ); }
|
bool validate()const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate the predicate.
|
||||||
|
*/
|
||||||
|
bool evaluate( const database& db )const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to verify that account_id->name is account_name
|
* Used to verify that asset_id->symbol is equal to the given string literal.
|
||||||
*/
|
*/
|
||||||
struct verify_symbol
|
struct asset_symbol_eq_lit
|
||||||
{
|
{
|
||||||
asset_id_type asset_id;
|
asset_id_type asset_id;
|
||||||
string symbol;
|
string symbol;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform state independent checks, such as verifying that
|
* Perform state independent checks. Verify symbol is a
|
||||||
* account_name is a valid name for an account.
|
* valid asset symbol.
|
||||||
*/
|
*/
|
||||||
bool validate()const { return is_valid_symbol( symbol ); }
|
bool validate()const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate the predicate.
|
||||||
|
*/
|
||||||
|
bool evaluate( const database& db )const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When defining predicates do not make the protocol dependent upon
|
* When defining predicates do not make the protocol dependent upon
|
||||||
* implementation details.
|
* implementation details.
|
||||||
*/
|
*/
|
||||||
typedef static_variant<
|
typedef static_variant<
|
||||||
verify_account_name,
|
pred::account_name_eq_lit,
|
||||||
verify_symbol
|
pred::asset_symbol_eq_lit
|
||||||
> predicate;
|
> predicate;
|
||||||
|
|
||||||
} }
|
} }
|
||||||
|
|
||||||
FC_REFLECT( graphene::chain::verify_account_name, (account_id)(account_name) )
|
FC_REFLECT( graphene::chain::pred::account_name_eq_lit, (account_id)(name) )
|
||||||
FC_REFLECT( graphene::chain::verify_symbol, (asset_id)(symbol) )
|
FC_REFLECT( graphene::chain::pred::asset_symbol_eq_lit, (asset_id)(symbol) )
|
||||||
FC_REFLECT_TYPENAME( graphene::chain::predicate )
|
FC_REFLECT_TYPENAME( graphene::chain::predicate )
|
||||||
|
|
||||||
|
|
|
||||||
30
libraries/chain/predicate.cpp
Normal file
30
libraries/chain/predicate.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
|
||||||
|
#include <graphene/chain/asset_object.hpp>
|
||||||
|
#include <graphene/chain/account_object.hpp>
|
||||||
|
#include <graphene/chain/database.hpp>
|
||||||
|
#include <graphene/chain/operations.hpp>
|
||||||
|
#include <graphene/chain/predicate.hpp>
|
||||||
|
|
||||||
|
namespace graphene { namespace chain { namespace pred {
|
||||||
|
|
||||||
|
bool account_name_eq_lit::validate()const
|
||||||
|
{
|
||||||
|
return is_valid_name( name );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool account_name_eq_lit::evaluate( const database& db )const
|
||||||
|
{
|
||||||
|
return account_id(db).name == name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool asset_symbol_eq_lit::validate()const
|
||||||
|
{
|
||||||
|
return is_valid_symbol( symbol );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool asset_symbol_eq_lit::evaluate( const database& db )const
|
||||||
|
{
|
||||||
|
return asset_id(db).symbol == symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
} } }
|
||||||
|
|
@ -820,7 +820,7 @@ BOOST_AUTO_TEST_CASE( assert_op_test )
|
||||||
op.predicates = vector< vector< char > >();
|
op.predicates = vector< vector< char > >();
|
||||||
op.predicates.push_back(
|
op.predicates.push_back(
|
||||||
fc::raw::pack(
|
fc::raw::pack(
|
||||||
predicate( verify_account_name{ nathan_id, "nathan" } )
|
predicate( pred::account_name_eq_lit{ nathan_id, "nathan" } )
|
||||||
) );
|
) );
|
||||||
trx.operations.push_back(op);
|
trx.operations.push_back(op);
|
||||||
trx.sign( nathan_key_id, nathan_private_key );
|
trx.sign( nathan_key_id, nathan_private_key );
|
||||||
|
|
@ -829,7 +829,7 @@ BOOST_AUTO_TEST_CASE( assert_op_test )
|
||||||
// nathan checks that his public key is not equal to the given value (fail)
|
// nathan checks that his public key is not equal to the given value (fail)
|
||||||
op.predicates.back() =
|
op.predicates.back() =
|
||||||
fc::raw::pack(
|
fc::raw::pack(
|
||||||
predicate( verify_account_name{ nathan_id, "dan" } )
|
predicate( pred::account_name_eq_lit{ nathan_id, "dan" } )
|
||||||
);
|
);
|
||||||
trx.operations.back() = op;
|
trx.operations.back() = op;
|
||||||
trx.sign( nathan_key_id, nathan_private_key );
|
trx.sign( nathan_key_id, nathan_private_key );
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue