Fantom-foundation/go-lachesis

View on GitHub
vector/branches_info.go

Summary

Maintainability
A
0 mins
Test Coverage
package vector

import (
    "github.com/Fantom-foundation/go-lachesis/inter/idx"
    "github.com/Fantom-foundation/go-lachesis/inter/pos"
)

// branchesInfo contains information about global branches of each validator
type branchesInfo struct {
    BranchIDLastSeq     []idx.Event       // branchID -> highest e.Seq in the branch
    BranchIDCreatorIdxs []idx.Validator   // branchID -> validator idx
    BranchIDByCreators  [][]idx.Validator // validator idx -> list of branch IDs
}

// initBranchesInfo loads branchesInfo from store
func (vi *Index) initBranchesInfo() {
    if vi.bi == nil {
        // if not cached
        vi.bi = vi.getBranchesInfo()
        if vi.bi == nil {
            // first run
            vi.bi = newInitialBranchesInfo(vi.validators)
        }
    }
}

func newInitialBranchesInfo(validators *pos.Validators) *branchesInfo {
    branchIDCreators := validators.SortedIDs()
    branchIDCreatorIdxs := make([]idx.Validator, len(branchIDCreators))
    for i := range branchIDCreators {
        branchIDCreatorIdxs[i] = idx.Validator(i)
    }

    branchIDLastSeq := make([]idx.Event, len(branchIDCreatorIdxs))
    branchIDByCreators := make([][]idx.Validator, validators.Len())
    for i := range branchIDByCreators {
        branchIDByCreators[i] = make([]idx.Validator, 1, validators.Len()/2+1)
        branchIDByCreators[i][0] = idx.Validator(i)
    }
    return &branchesInfo{
        BranchIDLastSeq:     branchIDLastSeq,
        BranchIDCreatorIdxs: branchIDCreatorIdxs,
        BranchIDByCreators:  branchIDByCreators,
    }
}

func (vi *Index) atLeastOneFork() bool {
    return len(vi.bi.BranchIDCreatorIdxs) > vi.validators.Len()
}