是否可以在执行例程中设置世界状态(分类帐)? [英] Is it possible to set the world state(ledger) in a go routine?

查看:59
本文介绍了是否可以在执行例程中设置世界状态(分类帐)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动一个go例程,以在初始化后每天检查并更改世界状态.

I want to start a go routine to check and change the world state daily after init.

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
    stub.PutState("xiaoming_wallet", []byte("50"))
    stub.PutState("xiaoming_toy", []byte("0"))
    go monthly_check(&stub)
    return nil, nil
}

功能就是这样

func monthly_check(stub *shim.ChaincodeStubInterface)  {
    tc:=time.Tick(24*time.Hour)
    for range tc{
          ...
          ...
          (*stub).PutState(..,..)
          ...
          ...   }}

但是putstate函数返回

but the putstate function returns

输入状态错误无法将状态放入查询上下文

put state error Cannot put state in query context

似乎所有与分类帐的交互都必须是外部交易的一部分,这意味着我不能在go例程中更改存根吗? 我有什么办法可以做到这一点?

It seems all interactions with the ledger have to be part of an external transaction, which means I can't change the stub in a go routine? Is there any way i can achieve this?

推荐答案

智能合约用Go或Java编写,并通过垫片(对等实体中的代理,合约中的存根-通常称为链码)连接到对等实体).链码在其自己的docker容器中运行,并且从代理到存根的反向连接使用gRPC和protobufs.当链码空闲一段时间后,它的容器可以关闭,并在下一个事务或查询到达时再次启动.

A smart contract is written in either Go or Java and connects to the peer via a shim (proxy in the peer, stub in the contract -- generally called chaincode). The chaincode runs in its own docker container and the cannection from proxy to stub and back uses gRPC and protobufs. When a chaincode is idle for a while, its container can shut down, and be started up again when the next transaction or query arrives.

由于这个原因,您不能在内存中存储任何状态.所有状态必须在交易结束时以世界状态存储,并在下一个交易或查询中检索. (注意:在v1架构中,查询是有效的事务,尽管它们不写状态.)显然,这将扩展到定期唤醒线程并轮询世界状态下的值.

For this reason, you cannot store any state in memory. All state must be stored in world state at the end of a transaction and retrieved in the next transaction or query. (Note: in v1 fabric, queries are transactions effectively, although they don't write state.) Obviously, this would extend to periodically waking up a thread and polling a value in world state.

相反,您可以同时使用两种技术:

Instead, you can use two techniques together:

1)让链码从事务中发出一个事件,以近乎实时地馈入您的应用程序.

1) Have the chaincode emit an event from transactions to feed your application in near-real-time.

2)设置轮询间隔作为事件的备份,以一次又一次轮询您要跟踪的状态.

2) Set up a polling interval as a backup to the events to poll the state you want to track now and again.

(2)必须存在,因为尚未保证(1)交货.

(2) must exist because (1) is not yet guaranteed delivery.

这篇关于是否可以在执行例程中设置世界状态(分类帐)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆