数据结构
为什么这里要相互持有呢?
type Ethereum struct {
accountManager *accounts.Manager
APIBackend *EthAPIBackend
}
type EthAPIBackend struct {
eth *Ethereum
gpo *gasprice.Oracle
}
1.1. go-ethereum/cmd/geth/consolecmd.go
func localConsole(ctx *cli.Context) error {
node := makeFullNode(ctx) //创建Node
startNode(ctx, node) //启动Node
}
1.2. go-ethereum/cmd/geth/main.go
func startNode(ctx *cli.Context, stack *node.Node) {
utils.StartNode(stack)
}
1.3. go-ethereum/cmd/utils/cmd.go
func StartNode(stack *node.Node) {
if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err)
}
}
1.4. go-ethereum/node/node.go
func (n *Node) Start() error {
services := make(map[reflect.Type]Service)
for _, constructor := range n.serviceFuncs {
ctx := &ServiceContext{
config: n.config,
services: make(map[reflect.Type]Service),
EventMux: n.eventmux,
AccountManager: n.accman, //Node的accounts.Manager(给到Ethereum)
}
service, err := constructor(ctx) //run serviceFuncs 2.1(create Ethereum)
services[kind] = service //save servie to services
}
if err := n.startRPC(services); err != nil {
for _, service := range services {
service.Stop()
}
running.Stop()
return err
}
}
1.5. go-ethereum/node/node.go
func (n *Node) startRPC(services map[reflect.Type]Service) error {
apis := n.apis()
for _, service := range services {
apis = append(apis, service.APIs()...) //APIs service is Ethereum
}
}
1.6. go-ethereum/eth/backend.go
func (s *Ethereum) APIs() []rpc.API {
apis := ethapi.GetAPIs(s.APIBackend) //Ethereum的APIBackend
}
1.7. go-ethereum/internal/ethapi/backend.go
func GetAPIs(apiBackend Backend) []rpc.API {
{
Namespace: "personal",
Version: "1.0",
Service: NewPrivateAccountAPI(apiBackend, nonceLock), //Ethereum的APIBackend
Public: false,
}
}
1.8. go-ethereum/internal/ethapi/api.go
//创建PrivateAccountAPI, 从Ethereum的EthAPIBackend获得accounts.Manager
func NewPrivateAccountAPI(b Backend, nonceLock *AddrLocker) *PrivateAccountAPI {
return &PrivateAccountAPI{
am: b.AccountManager(), //Ethereum的EthAPIBackend的Ethereum的accounts.Manager
nonceLock: nonceLock,
b: b, //Ethereum的EthAPIBackend
}
}
1.9. go-ethereum/eth/api_backend.go
func (b *EthAPIBackend) AccountManager() *accounts.Manager {
return b.eth.AccountManager()
}
2.1 go-ethereum/cmd/utils/flags.go
func(ctx *node.ServiceContext) (node.Service, error) {
fullNode, err := eth.New(ctx, cfg) //create Ethereum (is a node.Service)
if fullNode != nil && cfg.LightServ > 0 {
ls, _ := les.NewLesServer(fullNode, cfg)
fullNode.AddLesServer(ls)
}
return fullNode, err
}