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)
)
-
Writingis calledtransactionin 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.KVDBto modify the state. Note that the first parameter of theset,get,deletefunctions inchainEnv.kvdbmust be the currenttripodPointer, such ase.State.Set(e, ..., ...)in Quick Start
currentBlockis the block that the current transaction is in.
ctx.EmitEvent()is called wheneventneeds to be sent off-chain. -
Readingis 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.
respObjis used to return the queried status value to the off-chain. -
P2pHandleris 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 ourWritingandReading, we need to calltripod.SetWritings()to inject allWritingintotripod, And calltripod.SetReadings()to inject allReadingintotripod. -
Load into land:The framework provides a SyncAndStartup entry as long as the startup Load all thetripods we built by callingSyncAndStartup(...Tripod), thereby telling the framework whichtripods 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
}