examples

package
v0.0.0-...-7c0078d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

开发案例

AMOP 开发案例

AMOP(Advanced Messages Onchain Protocol)即链上信使协议,旨在为联盟链提供一个安全高效的消息信道,联盟链中的各个机构,只要部署了区块链节点,无论是共识节点还是观察节点,均可使用AMOP进行通讯,AMOP有如下优势:

  • 实时:AMOP消息不依赖区块链交易和共识,消息在节点间实时传输,延时在毫秒级。
  • 可靠:AMOP消息传输时,自动寻找区块链网络中所有可行的链路进行通讯,只要收发双方至少有一个链路可用,消息就保证可达。
  • 高效:AMOP消息结构简洁、处理逻辑高效,仅需少量cpu占用,能充分利用网络带宽。
  • 安全:AMOP的所有通讯链路使用SSL加密,加密算法可配置,支持身份认证机制。
  • 易用:使用AMOP时,无需在SDK做任何额外配置。

进一步了解 AMOP,请参考:链上信使协议

初始化

  • 搭建单群组四节点区块链网络,可参考:安装
单播案例

单播 指的是节点从监听相同 Topic 的多个订阅者中随机抽取一个订阅者转发消息,流程详细可参考 单播时序图

  • 启动 AMOP 消息订阅者:

    # go run examples/amop/sub/subscriber.go [endpoint] [topic]
    > go run examples/amop/sub/subscriber.go 127.0.0.1:20201 hello
    
      Subscriber success
      2020/08/11 21:21:50 received: hello, FISCO BCOS, I am unique broadcast publisher! 0
      2020/08/11 21:21:52 received: hello, FISCO BCOS, I am unique broadcast publisher! 1
      2020/08/11 21:21:54 received: hello, FISCO BCOS, I am unique broadcast publisher! 2
      2020/08/11 21:21:56 received: hello, FISCO BCOS, I am unique broadcast publisher! 3
    
  • 运行 AMOP 消息发布者:

    # go run examples/amop/unicast_pub/publisher.go [endpoint] [topic]
    > go run examples/amop/unicast_pub/publisher.go 127.0.0.1:20200 hello
    
      2020/08/11 21:21:50 publish message: hello, FISCO BCOS, I am unique broadcast publisher! 0
      2020/08/11 21:21:52 publish message: hello, FISCO BCOS, I am unique broadcast publisher! 1
      2020/08/11 21:21:54 publish message: hello, FISCO BCOS, I am unique broadcast publisher! 2
      2020/08/11 21:21:56 publish message: hello, FISCO BCOS, I am unique broadcast publisher! 3
    
多播案例

多播 指的是节点向监听相同 Topic 的所有订阅者转发消息。只要网络正常,即使没有监听 Topic 的订阅者,消息发布者也会收到节点消息推送成功的响应包,流程详细可参考 多播时序图

  • 启动 AMOP 消息订阅者:

    # go run examples/amop/sub/subscriber.go [endpoint] [topic]
    > go run examples/amop/sub/subscriber.go 127.0.0.1:20201 hello
    
      Subscriber success
      2020/08/11 21:23:54 received: hello, FISCO BCOS, I am multi broadcast publisher! 0
      2020/08/11 21:23:56 received: hello, FISCO BCOS, I am multi broadcast publisher! 1
      2020/08/11 21:23:58 received: hello, FISCO BCOS, I am multi broadcast publisher! 2
      2020/08/11 21:24:00 received: hello, FISCO BCOS, I am multi broadcast publisher! 3
    
  • 运行 AMOP 消息发布者:

    # go run examples/amop/multicast_pub/publisher.go [endpoint] [topic]
    > go run examples/amop/multicast_pub/publisher.go 127.0.0.1:20200 hello
    
      2020/08/11 21:23:54 publish message: hello, FISCO BCOS, I am multi broadcast publisher! 0
      2020/08/11 21:23:56 publish message: hello, FISCO BCOS, I am multi broadcast publisher! 1
      2020/08/11 21:23:58 publish message: hello, FISCO BCOS, I am multi broadcast publisher! 2
      2020/08/11 21:24:00 publish message: hello, FISCO BCOS, I am multi broadcast publisher! 3
    
身份验证单播案例

常规场景中,任何一个监听了某topic的接收者都能接受到发送者推送的消息。但在某些场景下,发送者只希望特定的接收者能接收到消息,不希望无关的接收者能任意的监听此topic。针对此类场景,FISCO BCOS 推出了 topic 认证功能。 认证功能是指对于特定的topic消息,只允许通过认证的接收者接收消息。详细请参考:Topic认证功能

  • 启动 AMOP 消息订阅者:

    # go run examples/amop_auth/sub/subscriber.go [endpoint] [topic]
    > go run examples/amop_auth/sub/subscriber.go 127.0.0.1:20201 hello
    
    Subscriber success
    2020/08/27 15:59:33 received: Hi, FISCO BCOS! 0
    2020/08/27 15:59:35 received: Hi, FISCO BCOS! 1
    2020/08/27 15:59:37 received: Hi, FISCO BCOS! 2
    2020/08/27 15:59:39 received: Hi, FISCO BCOS! 3
    
  • 运行 AMOP 消息发布者:

    # go run examples/amop_auth/unicast_pub/publisher.go [endpoint] [topic]
    > go run examples/amop_auth/unicast_pub/publisher.go 127.0.0.1:20200 hello
    
    publish topic success
    2020/08/27 15:59:33 publish message: Hi, FISCO BCOS! 0
    2020/08/27 15:59:35 publish message: Hi, FISCO BCOS! 1
    2020/08/27 15:59:37 publish message: Hi, FISCO BCOS! 2
    2020/08/27 15:59:39 publish message: Hi, FISCO BCOS! 3
    
身份验证多播案例

同理,FISCO BCOS 支持带身份验证的消息多播功能

  • 启动 AMOP 消息订阅者:

    # go run examples/amop_auth/sub/subscriber.go [endpoint] [topic]
    > go run examples/amop_auth/sub/subscriber.go 127.0.0.1:20201 hello
    
    Subscriber success
    2020/08/27 16:02:39 received: Hi, FISCO BCOS! 1
    2020/08/27 16:02:41 received: Hi, FISCO BCOS! 2
    2020/08/27 16:02:43 received: Hi, FISCO BCOS! 3
    2020/08/27 16:02:45 received: Hi, FISCO BCOS! 4
    
  • 运行 AMOP 消息发布者:

    # go run examples/amop_auth/multicast_pub/publisher.go [endpoint] [topic]
    > go run examples/amop_auth/multicast_pub/publisher.go 127.0.0.1:20200 hello
    
    publish topic success
    2020/08/27 16:02:37 publish message: Hi, FISCO BCOS! 0
    2020/08/27 16:02:39 publish message: Hi, FISCO BCOS! 1
    2020/08/27 16:02:41 publish message: Hi, FISCO BCOS! 2
    2020/08/27 16:02:43 publish message: Hi, FISCO BCOS! 3
    2020/08/27 16:02:45 publish message: Hi, FISCO BCOS! 4
    

合约开发案例

在利用SDK进行项目开发时,对智能合约进行操作需要利用go-sdk的abigen工具将Solidity智能合约转换为Go文件代码。整体上主要包含六个流程:

  • 准备需要编译的智能合约
  • 配置好相应版本的solc编译器
  • 构建go-sdk的合约编译工具abigen
  • 编译生成go文件
  • 准备建立ssl连接需要的证书
  • 使用生成的go文件进行合约部署、调用

详细可参考:合约开发样例

使用同步接口调用合约案例

本案例使用 KVTableTest.sol 合约文件,部署该合约会创建一张 KV 表,表中有 "id","item_price,item_name" 三个字段以及 Set 和 Get 两个方法,有关该合约内容以及使用该合约编译生成 go 模板文件的详细说明可参考:KVTableTest样例

执行以下语句,部署和调用合约:

> go run examples/kvtable_test_sync/main.go

  -------------------starting deploy contract-----------------------
  contract address:  0x2b4173B18Fd0f88Fb5525f1b657e51c18aBf3D82
  transaction hash:  0xee1b9ddc38f15ec0cac55c7ee3edcc4c8a497b9f22db421dca8206febc8a1f33

  -------------------starting invoke Set to insert info-----------------------
  tx sent: 0x57452ea0a231c05a709644b1878179630b9ffb9bd51cad3d00446d3d95aeb9ed
  seted lines: 1

  -------------------starting invoke Get to query info-----------------------
  id: 100010001001, item_price: 6000, item_name: Laptop
使用异步接口调用合约案例

异步合约开发案例指的是通过 sol 合约文件编译、生成 go 模板文件之后,调用 go 文件中提供的异步接口部署合约、修改数据,可以极大的提高交易并发量。详细可阅读源码

执行以下语句,部署和调用合约:

> go run examples/kvtable_test_async/main.go

  -------------------starting deploy contract-----------------------
  transaction hash:  0x39ad5ed85f6493a4c73248fe65196a69316bbbfedebe87bbd5e108a703b74419
  contract address:  0x974f2B3f93a6eDeB1DCB915Fa58b6931E3229F13

  -------------------starting invoke Set to insert info-----------------------
  tx sent: 0x161835e0a379a085b1cd4f046b9668745ff09815142608e32dff6aa75dc26274
  seted lines: 1

  -------------------starting invoke Get to query info-----------------------
  id: 100010001001, item_price: 6000, item_name: Laptop

Documentation

Index

Constants

View Source
const KVTableTestABI = "" /* 813-byte string literal not displayed */

KVTableTestABI is the input ABI used to generate the binding from.

Variables

View Source
var KVTableTestBin = "" /* 8124-byte string literal not displayed */

KVTableTestBin is the compiled bytecode used for deploying new contracts.

Functions

func AsyncDeployKVTableTest

func AsyncDeployKVTableTest(auth *bind.TransactOpts, handler func(*types.Receipt, error), backend bind.ContractBackend) (*types.Transaction, error)

Types

type KVTableTest

type KVTableTest struct {
	KVTableTestCaller     // Read-only binding to the contract
	KVTableTestTransactor // Write-only binding to the contract
	KVTableTestFilterer   // Log filterer for contract events
}

KVTableTest is an auto generated Go binding around a Solidity contract.

func DeployKVTableTest

func DeployKVTableTest(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *KVTableTest, error)

DeployKVTableTest deploys a new contract, binding an instance of KVTableTest to it.

func NewKVTableTest

func NewKVTableTest(address common.Address, backend bind.ContractBackend) (*KVTableTest, error)

NewKVTableTest creates a new instance of KVTableTest, bound to a specific deployed contract.

type KVTableTestCaller

type KVTableTestCaller struct {
	// contains filtered or unexported fields
}

KVTableTestCaller is an auto generated read-only Go binding around a Solidity contract.

func NewKVTableTestCaller

func NewKVTableTestCaller(address common.Address, caller bind.ContractCaller) (*KVTableTestCaller, error)

NewKVTableTestCaller creates a new read-only instance of KVTableTest, bound to a specific deployed contract.

func (*KVTableTestCaller) Get

func (_KVTableTest *KVTableTestCaller) Get(opts *bind.CallOpts, id string) (bool, *big.Int, string, error)

Get is a free data retrieval call binding the contract method 0x693ec85e.

Solidity: function get(string id) constant returns(bool, int256, string)

type KVTableTestCallerRaw

type KVTableTestCallerRaw struct {
	Contract *KVTableTestCaller // Generic read-only contract binding to access the raw methods on
}

KVTableTestCallerRaw is an auto generated low-level read-only Go binding around a Solidity contract.

func (*KVTableTestCallerRaw) Call

func (_KVTableTest *KVTableTestCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error

Call invokes the (constant) contract method with params as input values and sets the output to result. The result type might be a single field for simple returns, a slice of interfaces for anonymous returns and a struct for named returns.

type KVTableTestCallerSession

type KVTableTestCallerSession struct {
	Contract *KVTableTestCaller // Generic contract caller binding to set the session for
	CallOpts bind.CallOpts      // Call options to use throughout this session
}

KVTableTestCallerSession is an auto generated read-only Go binding around a Solidity contract, with pre-set call options.

func (*KVTableTestCallerSession) Get

func (_KVTableTest *KVTableTestCallerSession) Get(id string) (bool, *big.Int, string, error)

Get is a free data retrieval call binding the contract method 0x693ec85e.

Solidity: function get(string id) constant returns(bool, int256, string)

type KVTableTestFilterer

type KVTableTestFilterer struct {
	// contains filtered or unexported fields
}

KVTableTestFilterer is an auto generated log filtering Go binding around a Solidity contract events.

func NewKVTableTestFilterer

func NewKVTableTestFilterer(address common.Address, filterer bind.ContractFilterer) (*KVTableTestFilterer, error)

NewKVTableTestFilterer creates a new log filterer instance of KVTableTest, bound to a specific deployed contract.

func (*KVTableTestFilterer) FilterSetResult

func (_KVTableTest *KVTableTestFilterer) FilterSetResult(opts *bind.FilterOpts) (*KVTableTestSetResultIterator, error)

FilterSetResult is a free log retrieval operation binding the contract event 0xb103249d88cd818b10c5cd6889874103a7699c5834cb078d8f35925dca8a62d6.

Solidity: event SetResult(int256 count)

func (*KVTableTestFilterer) ParseSetResult

func (_KVTableTest *KVTableTestFilterer) ParseSetResult(log types.Log) (*KVTableTestSetResult, error)

ParseSetResult is a log parse operation binding the contract event 0xb103249d88cd818b10c5cd6889874103a7699c5834cb078d8f35925dca8a62d6.

Solidity: event SetResult(int256 count)

func (*KVTableTestFilterer) WatchSetResult

func (_KVTableTest *KVTableTestFilterer) WatchSetResult(opts *bind.WatchOpts, sink chan<- *KVTableTestSetResult) (event.Subscription, error)

WatchSetResult is a free log subscription operation binding the contract event 0xb103249d88cd818b10c5cd6889874103a7699c5834cb078d8f35925dca8a62d6.

Solidity: event SetResult(int256 count)

type KVTableTestRaw

type KVTableTestRaw struct {
	Contract *KVTableTest // Generic contract binding to access the raw methods on
}

KVTableTestRaw is an auto generated low-level Go binding around a Solidity contract.

func (*KVTableTestRaw) Call

func (_KVTableTest *KVTableTestRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error

Call invokes the (constant) contract method with params as input values and sets the output to result. The result type might be a single field for simple returns, a slice of interfaces for anonymous returns and a struct for named returns.

func (*KVTableTestRaw) Transact

func (_KVTableTest *KVTableTestRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, *types.Receipt, error)

Transact invokes the (paid) contract method with params as input values.

func (*KVTableTestRaw) Transfer

func (_KVTableTest *KVTableTestRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, *types.Receipt, error)

Transfer initiates a plain transaction to move funds to the contract, calling its default method if one is available.

type KVTableTestSession

type KVTableTestSession struct {
	Contract     *KVTableTest      // Generic contract binding to set the session for
	CallOpts     bind.CallOpts     // Call options to use throughout this session
	TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
}

KVTableTestSession is an auto generated Go binding around a Solidity contract, with pre-set call and transact options.

func (*KVTableTestSession) AsyncSet

func (_KVTableTest *KVTableTestSession) AsyncSet(handler func(*types.Receipt, error), id string, item_price *big.Int, item_name string) (*types.Transaction, error)

func (*KVTableTestSession) Get

func (_KVTableTest *KVTableTestSession) Get(id string) (bool, *big.Int, string, error)

Get is a free data retrieval call binding the contract method 0x693ec85e.

Solidity: function get(string id) constant returns(bool, int256, string)

func (*KVTableTestSession) Set

func (_KVTableTest *KVTableTestSession) Set(id string, item_price *big.Int, item_name string) (*types.Transaction, *types.Receipt, error)

Set is a paid mutator transaction binding the contract method 0xed0c8b17.

Solidity: function set(string id, int256 item_price, string item_name) returns(int256)

type KVTableTestSetResult

type KVTableTestSetResult struct {
	Count *big.Int
	Raw   types.Log // Blockchain specific contextual infos
}

KVTableTestSetResult represents a SetResult event raised by the KVTableTest contract.

type KVTableTestSetResultIterator

type KVTableTestSetResultIterator struct {
	Event *KVTableTestSetResult // Event containing the contract specifics and raw log
	// contains filtered or unexported fields
}

KVTableTestSetResultIterator is returned from FilterSetResult and is used to iterate over the raw logs and unpacked data for SetResult events raised by the KVTableTest contract.

func (*KVTableTestSetResultIterator) Close

func (it *KVTableTestSetResultIterator) Close() error

Close terminates the iteration process, releasing any pending underlying resources.

func (*KVTableTestSetResultIterator) Error

func (it *KVTableTestSetResultIterator) Error() error

Error returns any retrieval or parsing error occurred during filtering.

func (*KVTableTestSetResultIterator) Next

Next advances the iterator to the subsequent event, returning whether there are any more events found. In case of a retrieval or parsing error, false is returned and Error() can be queried for the exact failure.

type KVTableTestTransactor

type KVTableTestTransactor struct {
	// contains filtered or unexported fields
}

KVTableTestTransactor is an auto generated write-only Go binding around a Solidity contract.

func NewKVTableTestTransactor

func NewKVTableTestTransactor(address common.Address, transactor bind.ContractTransactor) (*KVTableTestTransactor, error)

NewKVTableTestTransactor creates a new write-only instance of KVTableTest, bound to a specific deployed contract.

func (*KVTableTestTransactor) AsyncSet

func (_KVTableTest *KVTableTestTransactor) AsyncSet(handler func(*types.Receipt, error), opts *bind.TransactOpts, id string, item_price *big.Int, item_name string) (*types.Transaction, error)

func (*KVTableTestTransactor) Set

func (_KVTableTest *KVTableTestTransactor) Set(opts *bind.TransactOpts, id string, item_price *big.Int, item_name string) (*types.Transaction, *types.Receipt, error)

Set is a paid mutator transaction binding the contract method 0xed0c8b17.

Solidity: function set(string id, int256 item_price, string item_name) returns(int256)

type KVTableTestTransactorRaw

type KVTableTestTransactorRaw struct {
	Contract *KVTableTestTransactor // Generic write-only contract binding to access the raw methods on
}

KVTableTestTransactorRaw is an auto generated low-level write-only Go binding around a Solidity contract.

func (*KVTableTestTransactorRaw) Transact

func (_KVTableTest *KVTableTestTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, *types.Receipt, error)

Transact invokes the (paid) contract method with params as input values.

func (*KVTableTestTransactorRaw) Transfer

func (_KVTableTest *KVTableTestTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, *types.Receipt, error)

Transfer initiates a plain transaction to move funds to the contract, calling its default method if one is available.

type KVTableTestTransactorSession

type KVTableTestTransactorSession struct {
	Contract     *KVTableTestTransactor // Generic contract transactor binding to set the session for
	TransactOpts bind.TransactOpts      // Transaction auth options to use throughout this session
}

KVTableTestTransactorSession is an auto generated write-only Go binding around a Solidity contract, with pre-set transact options.

func (*KVTableTestTransactorSession) AsyncSet

func (_KVTableTest *KVTableTestTransactorSession) AsyncSet(handler func(*types.Receipt, error), id string, item_price *big.Int, item_name string) (*types.Transaction, error)

func (*KVTableTestTransactorSession) Set

func (_KVTableTest *KVTableTestTransactorSession) Set(id string, item_price *big.Int, item_name string) (*types.Transaction, *types.Receipt, error)

Set is a paid mutator transaction binding the contract method 0xed0c8b17.

Solidity: function set(string id, int256 item_price, string item_name) returns(int256)

Directories

Path Synopsis
amop
sub
amop_auth
sub

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL