common

package
v0.0.0-...-56f3d1a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2024 License: Apache-2.0 Imports: 22 Imported by: 23

Documentation

Overview

统一管理系统引擎和链运行上下文

Index

Constants

View Source
const (
	// 完全块广播模式,即直接广播原始块给所有相邻节点
	FullBroadCastMode = iota
	// 问询式块广播模式,即先广播新块的头部给相邻节点
	// 邻节点在没有相同块的情况下通过GetBlock主动获取块数据
	InteractiveBroadCastMode
	// 出块节点将新块用Full_BroadCast_Mode模式广播
	// 其他节点使用Interactive_BroadCast_Mode模式广播区块
	MixedBroadCastMode
)

广播模式

View Source
const (
	// SyncWithNearestBucket 从邻近节点同步区块
	SyncWithNearestBucket = iota
	// SyncWithFactorBucket 从每个bucket中抽取节点同步区块
	SyncWithFactorBucket
)

同步模式

View Source
const (
	// 处理成功类
	ErrStatusSucc = 200
	// 拒绝处理类错误状态
	ErrStatusRefused = 400
	// 内部错误类错误状态
	ErrStatusInternalErr = 500
)
View Source
const (
	// 引擎名
	BCEngineName = "xchain"
)

引擎常量配置

Variables

View Source
var (
	ErrSuccess      = &Error{ErrStatusSucc, 0, "success"}
	ErrInternal     = &Error{ErrStatusInternalErr, 50000, "internal error"}
	ErrUnknown      = &Error{ErrStatusInternalErr, 50001, "unknown error"}
	ErrForbidden    = &Error{ErrStatusRefused, 40000, "forbidden"}
	ErrParameter    = &Error{ErrStatusRefused, 40001, "param error"}
	ErrUnauthorized = &Error{ErrStatusRefused, 40002, "unauthorized"}

	// engine
	ErrNewEngineCtxFailed = &Error{ErrStatusInternalErr, 50100, "create engine context failed"}
	ErrNotEngineType      = &Error{ErrStatusInternalErr, 50101, "transfer engine type failed"}
	ErrLoadEngConfFailed  = &Error{ErrStatusInternalErr, 50102, "load engine config failed"}
	ErrNewLogFailed       = &Error{ErrStatusInternalErr, 50103, "new logger failed"}

	// chain
	ErrNewChainCtxFailed = &Error{ErrStatusInternalErr, 50200, "new chain context failed"}
	ErrLoadChainFailed   = &Error{ErrStatusInternalErr, 50201, "load chain failed"}
	ErrRootChainNotExist = &Error{ErrStatusInternalErr, 50202, "root chain not exist"}
	ErrChainStatus       = &Error{ErrStatusInternalErr, 50203, "chain status error"}
	ErrChainExist        = &Error{ErrStatusInternalErr, 50204, "chain already exists"}
	ErrChainNotExist     = &Error{ErrStatusInternalErr, 50205, "chain not exist"}
	ErrChainAlreadyExist = &Error{ErrStatusInternalErr, 50206, "chain already exist"}

	// block
	ErrBlockNotExist    = &Error{ErrStatusInternalErr, 50300, "block not exist"}
	ErrProcBlockFailed  = &Error{ErrStatusInternalErr, 50301, "process block failed"}
	ErrGenesisBlockDiff = &Error{ErrStatusInternalErr, 50302, "genesis block diff"}

	// tx
	ErrTxVerifyFailed        = &Error{ErrStatusInternalErr, 50400, "verify tx failed"}
	ErrTxAlreadyExist        = &Error{ErrStatusInternalErr, 50401, "tx already exist"}
	ErrTxNotExist            = &Error{ErrStatusInternalErr, 50402, "tx not exist"}
	ErrTxNotEnough           = &Error{ErrStatusInternalErr, 50403, "tx not enough"}
	ErrSubmitTxFailed        = &Error{ErrStatusInternalErr, 50404, "submit tx failed"}
	ErrGenerateTimerTxFailed = &Error{ErrStatusInternalErr, 50405, "generate timer tx failed"}

	// contract
	ErrContractNewCtxFailed     = &Error{ErrStatusInternalErr, 50500, "contract new context failed"}
	ErrContractInvokeFailed     = &Error{ErrStatusInternalErr, 50501, "contract invoke failed"}
	ErrContractNewSandboxFailed = &Error{ErrStatusInternalErr, 50502, "contract new sandbox failed"}

	// net
	ErrNewNetEventFailed = &Error{ErrStatusInternalErr, 50600, "new net event failed"}
	ErrNewNetworkFailed  = &Error{ErrStatusInternalErr, 50601, "new network failed"}
	ErrSendMessageFailed = &Error{ErrStatusInternalErr, 50602, "send message failed"}
	ErrNetworkNoResponse = &Error{ErrStatusInternalErr, 50603, "network no response"}

	// consensus
	ErrConsensusStatus = &Error{ErrStatusInternalErr, 50701, "consensus status error"}
)

define std error 预留xxx9xx的错误码给上层业务扩展用,这里不要使用xxx9xx的错误码

Functions

This section is empty.

Types

type AsyncworkerAgent

type AsyncworkerAgent interface {
	RegisterHandler(contract string, event string, handler TaskHandler)
}

避免循环调用

type Chain

type Chain interface {
	// 获取链上下文
	Context() *ChainCtx
	// 启动链
	Start()
	// 关闭链
	Stop()
	// 合约预执行
	PreExec(xctx.XContext, []*protos.InvokeRequest, string, []string) (*protos.InvokeResponse, error)
	// 提交交易
	SubmitTx(xctx.XContext, *lpb.Transaction) error
	// 处理新区块
	ProcBlock(xctx.XContext, *lpb.InternalBlock) error
	// 设置依赖实例化代理
	SetRelyAgent(ChainRelyAgent) error
}

type ChainCtx

type ChainCtx struct {
	// 基础上下文
	xctx.BaseCtx
	// 引擎上下文
	EngCtx *EngineCtx
	// 链名
	BCName string
	// 账本
	Ledger *ledger.Ledger
	// 状态机
	State *state.State
	// 合约
	Contract contract.Manager
	// 共识
	Consensus consensus.PluggableConsensusInterface
	// 加密
	Crypto cryptoBase.CryptoClient
	// 权限
	Acl aclBase.AclManager
	// 治理代币
	GovernToken governToken.GovManager
	// 提案
	Proposal propose.ProposeManager
	// 定时任务
	TimerTask timerTask.TimerManager
	// 结点账户信息
	Address *xaddress.Address
	// 异步任务
	Asyncworker AsyncworkerAgent
}

链级别上下文,维护链级别上下文,每条平行链各有一个

type ChainManager

type ChainManager interface {
	Get(string) (Chain, error)
	GetChains() []string
	LoadChain(string) error
	Stop(string) error
}

type ChainRelyAgent

type ChainRelyAgent interface {
	CreateLedger() (*ledger.Ledger, error)
	CreateState(*ledger.Ledger, cryptoBase.CryptoClient) (*state.State, error)
	CreateContract(kledger.XMReader) (contract.Manager, error)
	CreateConsensus() (consensus.PluggableConsensusInterface, error)
	CreateCrypto(cryptoType string) (cryptoBase.CryptoClient, error)
	CreateAcl() (aclBase.AclManager, error)
	CreateGovernToken() (governToken.GovManager, error)
	CreateProposal() (propose.ProposeManager, error)
	CreateTimerTask() (timerTask.TimerManager, error)
	CreateXToken() (base.XTokenManager, error)
	CreateUpdateConfig() (chainConfigBase.UpdateCfgManger, error)
	CreateXEvidence() (xevidenceInter.XEvidenceManager, error)
}

定义链对各组件依赖接口约束

type Engine

type Engine interface {
	engines.BCEngine
	ChainManager
	Context() *EngineCtx
	SetRelyAgent(EngineRelyAgent) error
}

定义xuperos引擎对外暴露接口 依赖接口而不是依赖具体实现

type EngineCtx

type EngineCtx struct {
	// 基础上下文
	xctx.BaseCtx
	// 运行环境配置
	EnvCfg *xconf.EnvConf
	// 引擎配置
	EngCfg *engconf.EngineConf
	// 网络组件句柄
	Net network.Network
	// 链管理上下文
	ChainM ChainManager
}

引擎运行上下文环境

type EngineRelyAgent

type EngineRelyAgent interface {
	CreateNetwork(*xconf.EnvConf) (network.Network, error)
}

定义引擎对各组件依赖接口约束

type Error

type Error struct {
	// 用于统计和监控的错误分类(类似http的2xx、4xx、5xx)
	Status int
	// 用于标识具体错误的详细错误码
	Code int
	// 用于说明具体错误的说明信息
	Msg string
}

func CastError

func CastError(err error) *Error

func CastErrorDefault

func CastErrorDefault(err error, defaultErr *Error) *Error

func (*Error) Equal

func (t *Error) Equal(rhs *Error) bool

func (*Error) Error

func (t *Error) Error() string

func (*Error) More

func (t *Error) More(format string, args ...interface{}) *Error

type TaskContext

type TaskContext interface {
	// ParseArgs 用来解析任务参数,参数为对应任务参数类型的指针
	ParseArgs(v interface{}) error
	RetryTimes() int
}

type TaskHandler

type TaskHandler func(ctx TaskContext) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL