Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
opt trace service
Browse files Browse the repository at this point in the history
  • Loading branch information
shenao78 committed Oct 27, 2021
1 parent d5f4687 commit 62a9d7c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions api/block_retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions contract/trace_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -153,19 +153,17 @@ 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)

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) {
Expand Down
22 changes: 13 additions & 9 deletions contract/trace_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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 {
Expand Down

0 comments on commit 62a9d7c

Please sign in to comment.