石墨烯区块链(6)开发实例
音樂(lè)家和制作人
1. 三個(gè)角色
- 一個(gè)用戶(hù)購(gòu)買(mǎi)的音樂(lè)嵌入我們的網(wǎng)絡(luò)cryptocurrency。
- 一生產(chǎn)商(或工作室)專(zhuān)輯銷(xiāo)售給用戶(hù)和品牌的利潤(rùn)。
- 一位音樂(lè)家與制作人協(xié)商合同以發(fā)行專(zhuān)輯。
2. 音樂(lè)家和制作人安全創(chuàng)建智能合約合同的能力
- 創(chuàng)建合同
- 同意合同條款
- 完成合約并分配獎(jiǎng)勵(lì)
- 處理不完整和過(guò)期的合同
- 根據(jù)合同銷(xiāo)售音樂(lè)專(zhuān)輯和分配利潤(rùn)的能力
創(chuàng)建音樂(lè)合同的一般過(guò)程包括創(chuàng)建、簽署合同和發(fā)布專(zhuān)輯、合約到期。
3. 步驟
3.1. 添加數(shù)據(jù)結(jié)構(gòu)
定義可用的數(shù)據(jù)類(lèi)型。這些數(shù)據(jù)將存儲(chǔ)在區(qū)塊鏈上的狀態(tài)數(shù)據(jù)庫(kù)中。
class music_contract_object: public graphene::db::abstract_object < music_contract_object > { public: static const uint8_t space_id = implementation_ids; static const uint8_t type_id = impl_ music_contract_object_type; uint32_t contract_id = 10; account_id_type producer; // the account id of the producer account_id_type musician; // the account id of the musician asset amount; // contract amount time_point_sec ratification_deadline; // deadline till the contract must be signed time_point_sec contract_expiration; // contract expiration time asset pending_fee; // fee for creating the contract bool musician_signed = false; // whether the musician signed the contract };創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)對(duì)象索引。此索引僅用于存儲(chǔ)后端。然后我們可以像這樣在數(shù)據(jù)庫(kù)中注冊(cè)對(duì)象:
void database::initialize_indexes() {. . .add_index< primary_index<music_contract_index>>(); }3.2. 定義操作
前面部分指定了我們網(wǎng)絡(luò)的操作和驗(yàn)證器。
step1. 在網(wǎng)絡(luò)上創(chuàng)建智能合約的關(guān)鍵參與者和功能。
struct music_contract_create_operation : public base_operation {struct fee_parameters_type {uint64_t fee = 1 * GRAPHENE_BLOCKCHAIN_PRECISION;};asset fee;account_id_type producer;account_id_type musician;asset amount;uint32_t contract_id=0;string details;time_point_sec ratification_deadline;time_point_sec contract_expiration;void validate()const {FC_ASSERT( amount.amount > 0 );FC_ASSERT( producer != musician );}void get_required_active_authorities( flat_set<account_id_type>& a )const{ a.insert(producer); }account_id_type fee_payer()const { return producer; } };step2. (以類(lèi)似的方式使用合約創(chuàng)建其他操作)添加評(píng)估器來(lái)檢查智能合約的參數(shù),eg. 估計(jì)的截止日期和一些網(wǎng)絡(luò)用戶(hù)同時(shí)充當(dāng)音樂(lè)家和制作人的能力
class contract_create_evaluator : public evaluator< contract_create_evaluator> {public:typedef music_contract_create_operation operation_type;void_result do_evaluate( const music_contract_create_operation& o );object_id_type do_apply( const music_contract_create_operation& o ); };void_result contract_create_evaluator::do_evaluate(const music_contract_create_operation& o) {FC_ASSERT( o.ratification_deadline > db().head_block_time() );FC_ASSERT( o.contract_expiration > db().head_block_time() );if(o.amount.asset_id == asset_id_type())FC_ASSERT( db().get_balance( o.producer, o.amount.asset_id ) >= (o.amount + o.fee) );return void_result(); }object_id_type contract_create_evaluator::do_apply(const music_contract_create_operation& o) {try {db().adjust_balance( o.producer, -o.amount );const music_contract_object& ctr = db().create<music_contract_object>([&]( music_contract_object& ctr) {ctr.contract_id = o.contract_id;ctr.producer = o.producer;ctr.musician = o.musician;ctr.amount = o.amount;ctr. pending_fee = o.fee;ctr.ratification_deadline = o.ratification_deadline;ctr.contract_expiration = o.contract_expiration;});return ctr.id;} FC_CAPTURE_AND_RETHROW( (o) ) }現(xiàn)在我們有一個(gè)功能性智能合約,可以檢查參數(shù)并相應(yīng)地執(zhí)行操作。不幸的是,無(wú)法從源代碼外部使用它。您可以通過(guò)兩種方式訪問(wèn)??智能合約:使用命令行界面或通過(guò) HTTP API。當(dāng)然,對(duì)于真正的項(xiàng)目,您可能希望同時(shí)實(shí)現(xiàn)這兩個(gè)功能,甚至直接使用您的智能合約操作實(shí)現(xiàn) GUI。
3. 3 訪問(wèn)操作
為智能合約創(chuàng)建一個(gè)簡(jiǎn)單的 API
step1. 定義一個(gè)包含所有可通過(guò) HTTP 訪問(wèn)的函數(shù)的類(lèi):
class music_api{public:music_api(graphene::chain::database& db);~music_api();/* explain */optional<music_contract_object> get_contract( account_id_type publisher, uint32_t contract_id ) const;private:graphene::chain::database& _db;}; . . .optional<music_contract_object> get_contract( account_id_type publisher, uint32_t contract_id ) const{optional< music_contract_object > result;try{result = _db.get_contract( publisher, contract_id );}catch ( ... ) {}return result;}step2. 使用 FC_API 宏注冊(cè) API 及其函數(shù):
FC_API(graphene::app::music_api,(get_contract). . .) It was originally published on https://www.apriorit.com/參考
【1】. https://github.com/cryptonomex/graphene
【2】. https://www.apriorit.com/dev-blog/593-graphene-framework
總結(jié)
以上是生活随笔為你收集整理的石墨烯区块链(6)开发实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 石墨烯区块链(3)软件升级
- 下一篇: EOS 共识机制 (1)DPOS共识介绍