Tripod

Description

tripod is the core of the entire yu framework, and developers define their own business logic by implementing its interface. It consists of three parts:

  1. Custom block verification rules and transaction checking logic.
  2. Control the life cycle of blocks to achieve deep customization development. The life cycle has four phases: initChain, startBlock, endBlock, finalizeBlock. where only initChain It is called only at the time of the genesis block, and the last three are called every block. (Personalized consensus algorithms can be implemented with the block life cycle)
  3. Customize Transaction(Writing), Query(Query) and P2P Request Handling (p2pHandler)

Interface

Code is here

func Name() string
func SetChainEnv(env *ChainEnv)

Name() returns the name of tripod
SetChainEnv(*ChainEnv) loads ChainEnv into tripod, which is convenient for subsequent calls. (ChainEnv is introduced in context and chainEnv)

func CheckTxn(*SignedTxn) error
func VerifyBlock(block *Block) bool

checkTxn() is a developer-defined transaction check logic. As mentioned in the previous chapter, when an external transaction is inserted into txpool, the two steps of BaseCheck and TripodsCheck will be executed. And TripodsCheck is defined here, each tripod can define its own transaction check logic.

VerifyBlock() is a developer-defined block verification logic. Every time the blockchain receives a block from external broadcast, it needs to be verified before it can be processed. Different chains have different verifications for blocks. test method, so Developers can customize the validation logic by themselves. Each tripod can define its own transaction checking logic.

func InitChain() 
func StartBlock(block *Block) 
func EndBlock(block *Block)
func FinalizeBlock(block *Block)

InitChain() will only be called once when the blockchain is started, and it is specially used to define and process the genesis block.
StartBlock() defines the logic of the stage when the block is generated.
EndBlock() defines the logic at the end of the block, where the transaction is generally executed and the block is stored in the blockchain.
FinalizeBlock() defines the logic of the block's finalize phase (i.e. final consensus). If the blockchain is a finalize type chain, you need to customize this part of the logic, otherwise it is not necessary.

Dependency Injection

We may develop many Tripods in yu, and there may be dependencies between different Tripods. For example, when a developer develops a Tripod named staking, it may need to rely on the asset Tripod. In order to facilitate developers to manage Tripod dependencies more conveniently. yu provides the function of dependency injection.

type Staking struct {
	*tripod.Tripod
	asset *asset.Asset `tripod:"asset"`
}

As shown above, it is known that we already have an Asset Tripod. Now we develop the pledge module, which needs to rely on the assets in the asset Some functions, then we only need to declare the asset variable in the Staking structure and add a tag (tripod:"asset"). This is a fixed syntax tripod:<TripodName>, after adding, we just need to start yu normally:

    startup.SyncAndStartup(Staking.New(), asset.NewAsset())

Simple as above. SyncAndStartup() is a necessary step to start yu, you only need to call it once in your code, Startup() will automatically load these Tripods and complete dependency injection at runtime.

land

land is used to load all tripods for the framework to call.

land internal flow chart

image