Yu is a highly customizable blockchain framework.
Blockchain Infrastructure Provider
- Official Website | Twitter
- Built a high-performance, scalable L2 blockchain solutions with Yu framework
Web3 Game platform chain
- Official Website | Twitter
- Built a gaming platform blockchain using Yu framework
Zeroknowledge Proving infrastructure
typeExamplestruct {
*tripod.Tripod
}
// Here is a custom development of an Writingfunc (e*Example) Write(ctx*context.WriteContext) error {
caller:=ctx.GetCaller()
// set this Writing lei costctx.SetLei(100)
// Store data in on-chain state.e.Set([]byte("1"), []byte("yu"))
// Emit an event.ctx.EmitStringEvent(fmt.Printf("execute success, caller: %s", caller.String()))
returnnil
}
// Here is a custom development of a Readingfunc (e*Example) Read(ctx*context.ReadContext) {
key:=ctx.GetString("key")
value, err:=e.Get(key.Bytes())
iferr!=nil {
ctx.JsonOk(err)
return
}
ctx.String(string(value))
returnnil
}Then, register your Writing and Reading and start with main.
funcNewExample() *Example {
tri:=tripod.NewTripod()
e:=&Example{tri}
e.SetWritings(e.Write)
e.SetReadings(e.Read)
returne
}
funcmain() {
yuCfg:=startup.InitDefaultKernelConfig()
poaConf:=poa.DefaultCfg(0)
startup.DefaultStartup(yuCfg, poa.NewPoa(poaConf), NewExample())
}Build and Run
go build -o yu-example
./yu-example By using Yu, you can customize three levels to develop your own blockchain. The Tripod is for developers to
customize their own business.
First level is define Writing and Reading on chain.
Second level is define blockchain lifecycle. ( including customizable Consensus Algorithm )
Third level is define basic components, such as block data structures, blockchain, txdb, txpool.
- Define your
WritingandReadingon chain.WritingisState-Transition, likeTransactionin Ethereum but not only for transfer of Token, it changes the state on the chain and must be consensus on all nodes.Readingis likequeryin Ethereum, it doesn't change state, just query some data from the chain.P2pHandleris a p2p server handler. You can define the services in P2P server. Just like TCP handler.
type (
Writingfunc(ctx*context.WriteContext) errorReadingfunc(ctx*context.ReadContext)
P2pHandlerfunc([]byte) ([]byte, error)
)- Define Your
blockchain lifecycle, this function is inTripodinterface.CheckTxndefines the rules for checking transactions(Writings) before inserting txpool.VerifyBlockdefines the rules for verifying blocks.InitChaindefines business when the blockchain starts up. You should use it to defineGenesis Block.StartBlockdefines business when a new block starts. In this func, you can set some attributes (including pack txns from txpool, mining) in the block.EndBlockdefines business when all nodes accept the new block, usually we execute the txns of new block and append block into the chain.FinalizeBlockdefines business when the block is finalized in the chain by all nodes.
typeTripodinterface {
......CheckTxn(*txn.SignedTxn) errorVerifyBlock(block*types.Block) boolInitChain(genesisBlock*types.Block) StartBlock(block*types.Block) EndBlock(block*types.Block) FinalizeBlock(block*types.Block) }Asset TripodAsset Tripod imitates an Asset function, it has transfer accounts, create accounts.QueryBalance queries someone's account balance. It implements type func Reading.
func (a*Asset) QueryBalance(ctx*context.ReadContext) error {
account:=ctx.GetAddress("account")
if!a.existAccount(account) {
returnnil, AccountNotFound(account)
}
amount:=a.getBalance(account)
returnctx.Json(context.H{"amount": amount})
}CreateAccount creates an account. It implements type func Writing.EmitStringEvent will emit a string event out of the chain.
The error returned will emit out of the chain.
func (a*Asset) CreateAccount(ctx*context.WriteContext) error {
ctx.SetLei(100)
addr:=ctx.Calleramount:=big.NewInt(int64(ctx.GetUint64("amount")))
ifa.existAccount(addr) {
ctx.EmitStringEvent("Account Exists!")
returnnil
}
a.setBalance(addr, amount)
ctx.EmitStringEvent("Account Created Success!")
returnnil
}We need use SetWritings and SetReadings to set Writing and Reading into Asset Tripod.
When we set a Writing, we need declare how much Lei(耜) it consumes. (Lei is the same as gas in ethereum )
funcNewAsset(tokenNamestring) *Asset {
df:=NewDefaultTripod("asset")
a:=&Asset{df, tokenName}
a.SetWritings(a.Transfer, a.CreateAccount)
a.SetReadings(a.QueryBalance)
returna
}Poa Tripod
Consensus Tripod is necessary for a blockchain, so you have to choose or implement one consensus algorithm.Poa Tripod implements a Proof of Authority consensus algorithm. For detailed implementation and usage information, see the PoA README.
Finally set Asset Tripod, Poa Tripod into land in main function.
funcmain() {
startup.InitConfigFromPath("yu_conf/kernel.toml")
startup.DefaultStartup(
poa.NewPoa(poaConf),
asset.NewAsset("YuCoin"),
)
}
