dir : eth/handler.go
handler 인스턴스 생성 (sync 관련)
h := &handler{...}
node의 상황을 보고 sync mode를 적절히 조정한다.
// full sync mode인 경우
if config.Sync == downloader.FullSync {
// full sync, fast sync 각각의 block을 가져옴
fullBlock, fastBlock := h.chian.CurrentBlock(), h.chain.CurrentFastBlock()
// 만약 full sync가 전혀 일어나지 않아 fullBlock의 height이 0이라면
// sync mode를 snap sync로 변경
if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 {
h.snapsync = uint32(1)
// ... log that says sync mode is changed to snap sync from full sync
}
// full sync mode가 아닌 경우
} else {
// full block이 있다면 full sync로 sync mode를 변경
if h.chain.CurrentBlock().NumberU64() > 0 {
// ...log that says sync mode is changed to full sync
} else {
// h.snapSync가 1이라면 snap sync, 그렇지 않다면 full sync mode
h.snapSync = uint32(1)
}
}
chk point가 있다면 이를 반영한다.
if config.Checkpoint != nil {
h.checkpointNumber = (config.Checkpoint.SectionIndex+1)*params.CHTFrequency - 1
h.checkpointHash = config.Checkpoint.SectionHead
}
sync 완료 후 실행할 callback 선언
success := func() {
// snap sync가 종료되었다면 다음 cycle의 sync를 막음
if atomic.LoadUnit32(&h.snapSync) == 1 {
log.Info("Snap sync complete, auto disabling")
atomic.StoreUint32(&h.snapSync, 0)
}
// sync cycle을 잘 완료했고, 요구 chk point를 통과했다면 network로부터
// tx를 받을 수 있도록 한다.
head := h.chain.CurrnetBlock()
if head.NumberU64() >= h.checkpointNumber {
if head.Time() >= uint64(time.Now().AddDate(0, -1, 0).Unix()) {
atomic.StoreUint32(&h.acceptTxs, 1)
}
}
}
downloader 인스턴스 생성
h.downloader = downloader.New(...)
// dir : eth/downloader/downloader.go
ttd 관련 설정
if ttd := ... {
// ...
} else if ... {
// ...
}
header 검증
validator := func(header *types.header) error {
// ...
if beacon, ok := h.chain.Engine().(*beacon.Beacon); ok {
if beacon.IsPoSHeader(header) {
return error.New(...)
}
}
return h.chain.Engine.VerifyHeader(h.chain, header, true)
}
// ...
h 인스턴스 반혼
return h, nil