石墨烯区块链(6)开发实例
音樂家和制作人
1. 三個角色
- 一個用戶購買的音樂嵌入我們的網絡cryptocurrency。
- 一生產商(或工作室)專輯銷售給用戶和品牌的利潤。
- 一位音樂家與制作人協商合同以發行專輯。
2. 音樂家和制作人安全創建智能合約合同的能力
- 創建合同
- 同意合同條款
- 完成合約并分配獎勵
- 處理不完整和過期的合同
- 根據合同銷售音樂專輯和分配利潤的能力
創建音樂合同的一般過程包括創建、簽署合同和發布專輯、合約到期。
3. 步驟
3.1. 添加數據結構
定義可用的數據類型。這些數據將存儲在區塊鏈上的狀態數據庫中。
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 };創建一個數據庫對象索引。此索引僅用于存儲后端。然后我們可以像這樣在數據庫中注冊對象:
void database::initialize_indexes() {. . .add_index< primary_index<music_contract_index>>(); }3.2. 定義操作
前面部分指定了我們網絡的操作和驗證器。
step1. 在網絡上創建智能合約的關鍵參與者和功能。
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. (以類似的方式使用合約創建其他操作)添加評估器來檢查智能合約的參數,eg. 估計的截止日期和一些網絡用戶同時充當音樂家和制作人的能力
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) ) }現在我們有一個功能性智能合約,可以檢查參數并相應地執行操作。不幸的是,無法從源代碼外部使用它。您可以通過兩種方式訪問??智能合約:使用命令行界面或通過 HTTP API。當然,對于真正的項目,您可能希望同時實現這兩個功能,甚至直接使用您的智能合約操作實現 GUI。
3. 3 訪問操作
為智能合約創建一個簡單的 API
step1. 定義一個包含所有可通過 HTTP 訪問的函數的類:
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 宏注冊 API 及其函數:
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
總結
以上是生活随笔為你收集整理的石墨烯区块链(6)开发实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 石墨烯区块链(3)软件升级
- 下一篇: EOS 共识机制 (1)DPOS共识介绍