Writing and Reading
Under normal circumstances, developers will use more Writing
and Reading
, because these are basically external read and write interfaces at the business level. So in this section we look at how in yu
use them. The code is here
Defined as follows:
type (
Writing func(ctx *WriteContext) error
Reading func(ctx *ReadContext) error
P2pHandler func([]byte) ([]byte, error)
)
-
Writing
is calledtransaction
in other blockchains such as Ethereum. In the final analysis, it isa write operation to the state on the chain
, so such an operation will be synchronized to the entire network. When you need to modify the state on the chain, you need to callchainEnv.KVDB
to modify the state. Note that the first parameter of theset
,get
,delete
functions inchainEnv.kvdb
must be the currenttripod
Pointer, such ase.State.Set(e, ..., ...)
in Quick Start
currentBlock
is the block that the current transaction is in.
ctx.EmitEvent()
is called whenevent
needs to be sent off-chain. -
Reading
is an on-chain query. It does not modify the on-chain state, so the operation will not be synchronized to the entire network.
Status query callsenv.KVDB.Get()
orenv.KVDB.GetByBlockHash()
. The former is to query the status that the final consensus has been reached on the main chain; the latter is to query the status of a specific block. historical state.
respObj
is used to return the queried status value to the off-chain. -
P2pHandler
is to define and process requests from the P2P network. For example, when a node needs to broadcast or send a processing request to a specific node, this function can be customized to complete the custom function. -
Inject into Tripod
:When building all ourWriting
andReading
, we need to calltripod.SetWritings()
to inject allWriting
intotripod
, And calltripod.SetReadings()
to inject allReading
intotripod
. -
Load into land
:The framework provides a SyncAndStartup entry as long as the startup Load all thetripod
s we built by callingSyncAndStartup(...Tripod)
, thereby telling the framework whichtripod
s we have customized.
About defaultTripod
[defaultTripod](https://github.com/yu-org/yu/blob/master/tripod/ default_tripod.go)
It can help you write a lot less code, you need to put it in the first member variable of the tripod
structure of your custom implementation (you must omit the variable name to achieve the effect of inheriting defaultTripod
).
as follows:
type Example struct {
*tripod.Tripod
}