1 Star 0 Fork 0

艾鸥科技 / go-aiou

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
chainbase.go 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
张卓 提交于 2020-05-10 14:55 . init
package chainbase
import (
"fmt"
"gitee.com/aiou-official/go-aiou/common/config"
"sync"
"gitee.com/aiou-official/go-aiou/core/block"
"gitee.com/aiou-official/go-aiou/core/blockcache"
"gitee.com/aiou-official/go-aiou/core/txpool"
"gitee.com/aiou-official/go-aiou/db"
"gitee.com/aiou-official/go-aiou/ilog"
)
// ChainBase will maintain blockchain data for memory and hard disk.
type ChainBase struct {
config *config.Config
bChain block.Chain
bCache blockcache.BlockCache
stateDB db.MVCCDB
txPool txpool.TxPool
quitCh chan struct{}
done *sync.WaitGroup
}
// New will return a ChainBase.
func New(conf *config.Config) (*ChainBase, error) {
bChain, err := block.NewBlockChain(conf.DB.LdbPath + "BlockChainDB")
if err != nil {
return nil, fmt.Errorf("new blockchain failed, stop the program. err: %v", err)
}
stateDB, err := db.NewMVCCDB(conf.DB.LdbPath + "StateDB")
if err != nil {
return nil, fmt.Errorf("new statedb failed, stop the program. err: %v", err)
}
c := &ChainBase{
config: conf,
bChain: bChain,
stateDB: stateDB,
quitCh: make(chan struct{}),
done: new(sync.WaitGroup),
}
if err := c.checkGenesis(conf); err != nil {
return nil, fmt.Errorf("check genesis failed: %v", err)
}
if err := c.recoverDB(conf); err != nil {
return nil, fmt.Errorf("recover database failed: %v", err)
}
bCache, err := blockcache.NewBlockCache(conf, bChain, stateDB)
if err != nil {
return nil, fmt.Errorf("initialize blockcache failed: %v", err)
}
c.bCache = bCache
txPool, err := txpool.NewTxPoolImpl(bChain, bCache)
if err != nil {
return nil, fmt.Errorf("initialize txpool failed: %v", err)
}
c.txPool = txPool
if err := c.recoverBlockCache(); err != nil {
return nil, fmt.Errorf("recover chainbase failed: %v", err)
}
c.done.Add(1)
go c.metricsController()
return c, nil
}
// Close will close the chainbase.
func (c *ChainBase) Close() {
close(c.quitCh)
c.done.Wait()
c.txPool.Close()
c.stateDB.Close()
c.bChain.Close()
ilog.Infof("Closed chainbase.")
}
// =============== Temporarily compatible ===============
// StateDB return the state database.
func (c *ChainBase) StateDB() db.MVCCDB {
return c.stateDB
}
// BlockChain return the block chain database.
func (c *ChainBase) BlockChain() block.Chain {
return c.bChain
}
// BlockCache return the block cache.
func (c *ChainBase) BlockCache() blockcache.BlockCache {
return c.bCache
}
// TxPool will return the tx pool.
func (c *ChainBase) TxPool() txpool.TxPool {
return c.txPool
}
// NewMock will return the chainbase composed of blockchain and blockcache.
func NewMock(bChain block.Chain, bCache blockcache.BlockCache) *ChainBase {
return &ChainBase{
bChain: bChain,
bCache: bCache,
}
}
1
https://gitee.com/aiou-official/go-aiou.git
git@gitee.com:aiou-official/go-aiou.git
aiou-official
go-aiou
go-aiou
376a44096468

搜索帮助