OSDN Git Service

opt trace service
authorshenao78 <shenao.78@163.com>
Wed, 27 Oct 2021 09:45:29 +0000 (17:45 +0800)
committershenao78 <shenao.78@163.com>
Wed, 27 Oct 2021 09:45:29 +0000 (17:45 +0800)
api/block_retrieve.go
contract/trace_scheduler.go
contract/trace_service.go

index be5b56f..d2cf579 100644 (file)
@@ -45,6 +45,7 @@ type GetBlockResp struct {
        Version                uint64     `json:"version"`
        Height                 uint64     `json:"height"`
        Validator              string     `json:"validator"`
+       SupLinks               types.SupLinks `json:"sup_links"`
        PreviousBlockHash      *bc.Hash   `json:"previous_block_hash"`
        Timestamp              uint64     `json:"timestamp"`
        TransactionsMerkleRoot *bc.Hash   `json:"transaction_merkle_root"`
@@ -79,6 +80,7 @@ func (a *API) getBlock(ins BlockReq) Response {
                Size:                   uint64(len(rawBlock)),
                Version:                block.Version,
                Height:                 block.Height,
+               SupLinks:               block.SupLinks,
                Validator:              validatorPubKey,
                PreviousBlockHash:      &block.PreviousBlockHash,
                Timestamp:              block.Timestamp,
index b1dfecf..70a6261 100644 (file)
@@ -58,11 +58,11 @@ func (t *traceScheduler) processLoop() {
                }
                t.tracer = newTracer(jobs[beginHash])
 
-               for t.currentHeight, t.currentHash = beginHeight, beginHash; len(jobs) != 0; {
+               for t.currentHeight, t.currentHash = beginHeight, beginHash;; {
                        if t.currentHeight == t.tracerService.BestHeight() {
-                               if ok, err := t.finishJobs(jobs); err != nil {
+                               if err := t.finishJobs(jobs); err != nil {
                                        log.WithFields(log.Fields{"module": logModule, "err": err}).Error("finish jobs")
-                               } else if ok {
+                               } else {
                                        break
                                }
                        }
@@ -135,7 +135,7 @@ func (t *traceScheduler) detach(jobs map[bc.Hash][]*Instance) error {
        return nil
 }
 
-func (t *traceScheduler) finishJobs(jobs map[bc.Hash][]*Instance) (bool, error) {
+func (t *traceScheduler) finishJobs(jobs map[bc.Hash][]*Instance) error {
        inSyncInstances := t.tracer.allInstances()
        inSyncMap := make(map[string]bool)
        for _, inst := range inSyncInstances {
@@ -153,7 +153,7 @@ func (t *traceScheduler) finishJobs(jobs map[bc.Hash][]*Instance) (bool, error)
        }
 
        if err := t.infra.Repository.SaveInstances(offChainInstances); err != nil {
-               return false, err
+               return err
        }
 
        t.releaseInstances(offChainInstances)
@@ -161,11 +161,9 @@ func (t *traceScheduler) finishJobs(jobs map[bc.Hash][]*Instance) (bool, error)
        if len(inSyncInstances) != 0 {
                if ok := t.tracerService.takeOverInstances(inSyncInstances, t.currentHash); ok {
                        t.releaseInstances(inSyncInstances)
-                       return true, nil
                }
-               return false, nil
        }
-       return true, nil
+       return nil
 }
 
 func (t *traceScheduler) releaseInstances(instances []*Instance) {
index a2d8313..1b22988 100644 (file)
@@ -35,15 +35,7 @@ func NewTraceService(infra *Infrastructure) *TraceService {
                log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("load instances from db")
        }
 
-       chainStatus := infra.Repository.GetChainStatus()
-       if chainStatus == nil {
-               bestHeight, bestHash := infra.Chain.BestChain()
-               chainStatus = &ChainStatus{BlockHeight: bestHeight, BlockHash: bestHash}
-               if err := infra.Repository.SaveChainStatus(chainStatus); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("init chain status for trace service")
-               }
-       }
-
+       chainStatus := initChainStatus(infra)
        scheduler := newTraceScheduler(infra)
        inSyncInstances := dispatchInstances(allInstances, scheduler, infra.Chain.FinalizedHeight())
 
@@ -60,6 +52,18 @@ func NewTraceService(infra *Infrastructure) *TraceService {
        return service
 }
 
+func initChainStatus(infra *Infrastructure) *ChainStatus {
+       chainStatus := infra.Repository.GetChainStatus()
+       if chainStatus == nil {
+               bestHeight, bestHash := infra.Chain.BestChain()
+               chainStatus = &ChainStatus{BlockHeight: bestHeight, BlockHash: bestHash}
+               if err := infra.Repository.SaveChainStatus(chainStatus); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("init chain status for trace service")
+               }
+       }
+       return chainStatus
+}
+
 func dispatchInstances(instances []*Instance, scheduler *traceScheduler, finalizedHeight uint64) []*Instance {
        var result []*Instance
        for _, inst := range instances {