github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/contract/trace_updater.go (about) 1 package contract 2 3 import ( 4 log "github.com/sirupsen/logrus" 5 ) 6 7 var logModule = "tracer" 8 9 type TraceUpdater struct { 10 *TraceService 11 chain ChainService 12 } 13 14 func NewTraceUpdater(traceService *TraceService, chain ChainService) *TraceUpdater { 15 return &TraceUpdater{ 16 TraceService: traceService, 17 chain: chain, 18 } 19 } 20 21 func (t *TraceUpdater) Sync() { 22 for { 23 block, _ := t.chain.GetBlockByHeight(t.BestHeight() + 1) 24 if block == nil { 25 t.walletBlockWaiter() 26 continue 27 } 28 29 if bestHash := t.BestHash(); block.PreviousBlockHash != bestHash { 30 block, err := t.chain.GetBlockByHash(&bestHash) 31 if err != nil { 32 log.WithFields(log.Fields{"module": logModule, "err": err, "block_hash": bestHash.String()}).Errorf("trace updater get block") 33 break 34 } 35 36 if err := t.DetachBlock(block); err != nil { 37 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("trace updater detach block") 38 break 39 } 40 } else { 41 if err := t.ApplyBlock(block); err != nil { 42 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("trace updater attach block") 43 break 44 } 45 } 46 } 47 } 48 49 func (t *TraceUpdater) walletBlockWaiter() { 50 <-t.chain.BlockWaiter(t.bestHeight + 1) 51 }