作者:高粱_ | 来源:互联网 | 2023-10-16 12:49
btcdMain流程简化版代码1.main包btcd.gopackagemainimport(runtimedebugruntimegithub.combtcsuitebtcdli
btcdMain 流程
简化版代码
1. main包
btcd.go
package main
import (
"runtime/debug"
"runtime"
"github.com/btcsuite/btcd/limits"
"fmt"
"os"
"net"
"btcd-demo/database"
)
var (
cfg *config
)
func winServiceMain()(bool, error) {
fmt.Println("btcd service runing...")
return true, nil
}
func btcdMain(serverChan chan<- *server) error {
// 加载配置,解析命令行选项。
tcfg, _, err := loadConfig()
if err != nil {
return err
}
cfg = tcfg
interrupt := interruptListener()
defer btcdLog.Info("Shutdown complete")
// 打印版本信息
btcdLog.Infof("Version %s", version())
// 开启http服务器性能监测
if cfg.Profile != "" {
go func() {
listenAddr := net.JoinHostPort("", cfg.Profile)
btcdLog.Infof("Profile server listening on %s", listenAddr)
}()
}
// 开启cpu性能监测
if cfg.CPUProfile != "" {
btcdLog.Info("CPU Profile start")
}
// 升级老版本数据库相关文件路径
if err := doUpgrades(); err != nil {
btcdLog.Errorf("%v", err)
return err
}
// 是否有中断信号
if interruptRequested(interrupt) {
return nil
}
// 加载区块数据库
db, err := loadBlockDB()
if err != nil {
btcdLog.Errorf("%v", err)
return err
}
defer func() {
btcdLog.Info("Gracefully shutting down the database...")
db.Close()
}()
// 是否有中断信号
if interruptRequested(interrupt) {
return nil
}
// 删除地址索引,并退出程序
//
// 注意: 在这里删除顺序很重要,因为删除交易索引同时也会删除地址索引。
// 因为交易索引会依赖地址索引
if cfg.DropAddrIndex {
btcdLog.Info("Drop Address index done")
}
// 删除交易索引,并退出程序
if cfg.DropTxIndex {
btcdLog.Info("Drop transaction index done")
}
// 删除交易索引,并退出程序
if cfg.DropCfIndex {
btcdLog.Info("Drop cf(committed filter) index done")
}
// 创建服务器,并启动
server, err := newServer(cfg.Listeners, db, activeNetParams.Params, interrupt)
if err != nil {
btcdLog.Errorf("Failed to start server on %v: %v", cfg.Listeners, err)
return err
}
defer func(){
btcdLog.Infof("Gracefully shutting down the server...")
server.Stop()
server.WaitForShutdown()
}()
server.Start()
if serverChan != nil {
serverChan <- server
}
//挂起主线程,等待中断信号
<-interrupt
return nil
}
func loadBlockDB()(database.DB, error) {
return &database.SimpleDB{}, nil
}
func main() {
//设置cpu最大使用数量,这里全部使用
runtime.GOMAXPROCS(runtime.NumCPU())
//设置触发GC垃圾收集器阀值: 10% , 即 最新分布的内存大小/上次GC之后活动内存大小*100
debug.SetGCPercent(10)
//设置进程能打开的最大文件资源,即file descriptor数量。做linux系统中,所有资源(pipe, socket, file等等)都以文件形式出现。
if err := limits.SetLimits(); err != nil {
fmt.Printf("Failed to set limits: %v \n", err)
os.Exit(1)
}
if runtime.GOOS == "windows" {
isService, err := winServiceMain()
if err != nil {
fmt.Printf("Failed to start windows service: %v \n", err)
}
if isService {
os.Exit(0)
}
}
if err := btcdMain(nil); err != nil {
fmt.Printf("Failed to start btcd server: %v", err)
os.Exit(1)
}
}
config.go
package main
type config struct {
Profile string
CPUProfile string
DropAddrIndex bool
DropTxIndex bool
DropCfIndex bool
Listeners []string
}
// loadConfig 通过配置文件,命令行选项来初始化配置
//
// 配置处理按下面的流程进行:
// 1 ) 启动时,使用默认配置
// 2 ) 预解释命令行,检查是否有另外指定的配置文件
// 3 ) 加载配置文件,并覆盖默认设置
// 4 ) 解释命令行选项,覆盖或添加新指定的配置
//
func loadConfig()(*config, []string, error) {
return &config{}, nil, nil
}
log.go
package main
import "fmt"
type Level uint32
type sLoger struct {
}
var (
btcdLog = new(sLoger)
)
func (l *sLoger)Tracef(format string, params ...interface{}) {
}
func (l *sLoger)Debugf(format string, params ...interface{}) {
}
// Infof formats message according to format specifier and writes to
// log with LevelInfo.
func (l *sLoger)Infof(format string, params ...interface{}) {
fmt.Printf(format, params)
fmt.Println()
}
// Warnf formats message according to format specifier and writes to
// to log with LevelWarn.
func (l* sLoger)Warnf(format string, params ...interface{}) {
}
// Errorf formats message according to format specifier and writes to
// to log with LevelError.
func (l* sLoger)Errorf(format string, params ...interface{}) {
fmt.Printf(format, params)
fmt.Println()
}
// Criticalf formats message according to format specifier and writes to
// log with LevelCritical.
func (l* sLoger)Criticalf(format string, params ...interface{}) {
}
// Trace formats message using the default formats for its operands
// and writes to log with LevelTrace.
func (l* sLoger)Trace(v ...interface{}) {
}
// Debug formats message using the default formats for its operands
// and writes to log with LevelDebug.
func (l* sLoger)Debug(v ...interface{}) {
}
// Info formats message using the default formats for its operands
// and writes to log with LevelInfo.
func (l* sLoger)Info(v ...interface{}) {
fmt.Printf("%s\n", v)
}
// Warn formats message using the default formats for its operands
// and writes to log with LevelWarn.
func (l* sLoger)Warn(v ...interface{}) {
}
// Error formats message using the default formats for its operands
// and writes to log with LevelError.
func (l* sLoger)Error(v ...interface{}) {
}
// Critical formats message using the default formats for its operands
// and writes to log with LevelCritical.
func (l* sLoger)Critical(v ...interface{}) {
}
// Level returns the current logging level.
func (l* sLoger)Level() Level {
return 0
}
// SetLevel changes the logging level to the passed level.
func (l* sLoger)SetLevel(level Level) {
}
params.go
package main
import "btcd-demo/chaincfg"
var activeNetParams = &mainNetParams
type params struct {
* chaincfg.Params
}
var mainNetParams = params{
}
server.go
package main
import (
"btcd-demo/database"
"btcd-demo/chaincfg"
)
type server struct {
}
func newServer(listeners []string, db database.DB, chainParams *chaincfg.Params, interrupt <-chan struct{}) (*server, error) {
return &server{}, nil
}
func (s *server) Stop() error {
btcdLog.Info("Server stop done")
return nil
}
func (s *server) Start() error {
btcdLog.Info("Server start...")
return nil
}
func (s *server) WaitForShutdown() error {
btcdLog.Info("Server wait for shutdown")
return nil
}
singal.go
package main
func interruptListener() <-chan struct{} {
c := make(chan struct{})
return c
}
func interruptRequested(interrupted <-chan struct{}) bool {
select {
case <- interrupted:
return true
default:
}
return false
}
upgrade.go
package main
func doUpgrades() error {
btcdLog.Info("doUpgrades done")
return nil
}
version.go
package main
func version() string {
return "1.0"
}
2. database包
interface.go
package database
type DB interface {
Type() string
Close() error
}
driver.go
package database
import "fmt"
type SimpleDB struct {
}
func (db *SimpleDB) Type() string {
return "SimpleDB"
}
func (db *SimpleDB) Close() error {
fmt.Printf("Close db done \n")
return nil
}
3. chaincfg包
params.go
package chaincfg
type Params struct {
}
项目地址:
https://github.com/hawkit/btcd-demo/tree/v0.0.1