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 called transaction in other blockchains such as Ethereum. In the final analysis, it is a 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 call chainEnv.KVDB to modify the state. Note that the first parameter of the set, get, delete functions in chainEnv.kvdb must be the current tripod Pointer, such as e.State.Set(e, ..., ...) in Quick Start
    currentBlock is the block that the current transaction is in.
    ctx.EmitEvent() is called when event 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 calls env.KVDB.Get() or env.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 our Writing and Reading, we need to call tripod.SetWritings() to inject all Writing into tripod, And call tripod.SetReadings() to inject all Reading into tripod.

  • Load into land:The framework provides a SyncAndStartup entry as long as the startup Load all the tripods we built by calling SyncAndStartup(...Tripod), thereby telling the framework which tripods 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
}