Forge Core functionalities and Transaction Protocols

JeanChen

2019/05/16 发布于 技术 分类

文字内容
1. 1 Forge: Core functionalities and Transaction Protocols Brought to you by Tyr Chen
2. Overview 2
3. The GOALs of forge • Vision: "Ruby on Rails" alike powerful framework for building dApps • Everything is pluggable • consensus engine: Tendermint or anything else • state db: MPT or anything else • Decisions are made dynamically • engines: load time (after loading configuration) • wallet: create time (app or even user can choose what kind of algo to use) • transaction protocols: load and deployed dynamically 3
4. The use cases for forge 4 • build a blockchain that with cryptocurrency (like an advanced ERC20/ERC721/... token) • build a dApp that solve a certain problem • decentralized usage tracking and audit trail • decentralized identity • move existing internet application forward to decentralized world (why?) • decentralized docuSign • decentralized EventBrite • ... • build things that cannot be built on centralized solution The baseline: Forge could be a big part on an app that partially on-chain and partially offchain.
5. How things works 5
6. What's our trade-o s - high level • PoS (BFT) v.s. PoW • State DB (Ethereum model) v.s. UTXO (Bitcoin model) • BEAM v.s. other languages / platforms • Fat protocols v.s. smart contract • Full fledged framework ecosystem v.s. node software only 6
7. What's our trade-o s - low level • Protobuf v.s. home-brewed protocol definition • gRPC / GraphQL v.s. JSON RPC • RocksDB v.s. levelDB & others 7
8. The states • Account State • Asset State • Stake State • Protocol State • Forge State 8
9. Account State de nition message AccountState { BigUint balance = 1; uint64 nonce = 2; uint64 num_txs = 3; string address = 4; bytes pk = 5; WalletType type = 6 [ deprecated = true ]; string moniker = 7; StateContext context = 8; string issuer = 9; repeated string migrated_to = 13; repeated string migrated_from = 14; // 16-49 reserve for future StakeContext stake = 16; CircularQueue pinned_files = 17 [deprecated = true]; PokeInfo poke = 18; BigUint deposit_received = 19; google.protobuf.Any data = 50; } 9
10. The Store • MPT • Each state has its own tree • each transaction protocol has its own tree • RocksDB • zstd compression • extensively use of column families 10
11. 11 The transaction protocols
12. 12 Arcblock principle - An app chain shall do one thing and do it well
13. What is transaction protocols? 13 • a list of pipes that handle a certain type of transactions • you can treat a pipe as a lego block, or an x86 microcode • normally several or at most tens of pipes consist of a tx protocol • e.g. a transfer is a tx protocol that handles TransferTx • Hot pluggable and upgradable • each protocol can be installed separately • each protocol can be upgraded by deploy_protocol tx (and yes - deploy_protocol can deploy itself) • code in state DB
14. Run-levels 14
15. 15 How to write a transaction protocol
16. First the protocol de nition message DeclareTx { string moniker = 1; string issuer = 2; // forge won't update data into state if app is interested in this tx. google.protobuf.Any data = 15; } 16
17. Then de ne the pipeline --name: declare check: - pipe: verify_info conditions: - [info.itx.moniker !== "", :insufficient_data] - [ForgeSdk.Util.Validator.valid_moniker?(info.itx.moniker), :invalid_moniker] - pipe: :custom name: check_restricted when: declare.restricted === true verify: - pipe: extract_state from: [:itx, :issuer] to: [:priv, :issuer] status: :invalid_tx when: declare.restricted === true update: - pipe: :custom name: update_tx 17
18. Then write custom pipes defmodule UpdateTx do @moduledoc """ create asset pipe """ use ForgePipe.Builder require Logger alias ForgeAbi.AccountState def init(opts), do: opts def call(%{itx: itx, tx: tx, context: context, db_handler: handler} = info, _opts) do Logger.debug(fn -> "[pipe - declare/update]: from #{tx.from}, moniker: #{itx.moniker}" end) attrs = itx > Map.from_struct() > Map.put(:address, tx.from) > Map.put(:pk, tx.pk) state = CoreState.Account.create(AccountState.new(), attrs, context) :ok = handler.put!(state.address, state) info > put(:sender_state, state) > put status(:ok) 18
19. Then wrap these up with a con guration --name: declare version: 0 namespace: CoreTx description: Declare a wallet to the chain type_urls: fg:t:declare: ForgeAbi.DeclareTx proto: protocol.proto pipeline: protocol.yml sources: - protocol.ex 19
20. Finally compile, install and load • compile the tx protocol with forge-compiler • install the tx protocol with deploy_protocol • once installed successfully forge will load the new protocol • if there's existing protocol forge will migrate it 20
21. Wait...something's missing here... 21
22. 22 The whole pipeline
23. pre pipeline --check: - pipe: - pipe: - pipe: - pipe: - pipe: - pipe: - pipe: - pipe: decode_tx verify_tx verify_blacklist verify_replay decode_itx verify_tx_size verify_signature verify_multisig verify: - pipe: extract_state from: [:tx, :from] to: [:sender_state] status: :ok - pipe: extract_signer to: [:priv, :signers] status: :invalid_tx - pipe: verify_account_migration signers: [:priv, :signers] - pipe: verify_sender update: [] 23
24. post pipeline --check: - pipe: check_halted verify: [] update: - pipe: check_halted - pipe: update_tx_statistics 24
25. nal pipeline --check: [] verify: [] update: - pipe: - pipe: - pipe: - pipe: reset_halted update_receipt_db update_index_db broadcast 25
26. Lessons learned and things to improve 26 • sometimes things drive you crazy • believe it or not, finally those will pass with a little more patience • to make a framework easy to use we need to do lots of dirty work - there's a lot of things to improve • BEAM is a great VM that fits for our needs • Forge is great but only if combined with the SDKs, the starter projects, etc. we may create fantastic user experiences • A lot more need to be provided down this road • a better, trust-worthy compiler, an interactive debugger, better DSLs to write tx protocols and sandboxes for the ring 3 protocols • better UIs to make all these looks and feels better!
27. 27