Context and ChainEnv
In Quick Start, we have seen how to customize an Writing
, there are two parameters here, one is context
, the other is ChainEnv
, they represent two different
the use of.
Context represents contextual information for interactions with off-chain. It includes caller address
, passed parameters
、
event
、error
。
- The parameters passed in by the client caller (that is, the person who initiated the transaction) will be serialized in the form of json and stored in
context
in the form ofstring -> interface{}
map. We usecontext
to get thecryptographic address
of the client caller and theparameters
passed in by the caller. fromcontext
When getting parameters, you can use any go basic type to get parameters. If the parameter type is custom, you need to obtaininterface
throughcontext.Get(paramName)
and then convert it yourself. The types of parameters that can be directly obtained are:
common.Hash([32]byte)
common.Address([20]byte)
string
[]byte
bool
int, int8, int16, int32, int64
uint, uint8, uint16, uint32, uint64
float32, float64
- At the same time, if you need to return
event
out of the chain, you also need to complete it throughcontext
(ctx.EmitEvent([]byte)
).
ChainEnv contains all environment components related to all chains, including blockchain
,
txdb
, txpool
, state
, Execute
, P2pNetwork
. Components that can be directly manipulated by developers to the bottom layer of the framework.