github.com/amazechain/amc@v0.1.3/internal/sync/initial-sync/blocks_fetcher_utils.go (about)

     1  package initialsync
     2  
     3  import (
     4  	"context"
     5  	"github.com/amazechain/amc/api/protocol/types_pb"
     6  	"github.com/holiman/uint256"
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  	"github.com/pkg/errors"
     9  	"go.opencensus.io/trace"
    10  )
    11  
    12  // forkData represents alternative chain path supported by a given peer.
    13  // Blocks are stored in an ascending slot order. The first block is guaranteed to have parent
    14  // either in DB or initial sync cache.
    15  type forkData struct {
    16  	peer   peer.ID
    17  	blocks []*types_pb.Block
    18  }
    19  
    20  // nonSkippedSlotAfter checks slots after the given one in an attempt to find a non-empty future slot.
    21  // For efficiency only one random slot is checked per epoch, so returned slot might not be the first
    22  // non-skipped slot. This shouldn't be a problem, as in case of adversary peer, we might get incorrect
    23  // data anyway, so code that relies on this function must be robust enough to re-request, if no progress
    24  // is possible with a returned value.
    25  func (f *blocksFetcher) nonSkippedSlotAfter(ctx context.Context, blockNr *uint256.Int) (*uint256.Int, error) {
    26  	ctx, span := trace.StartSpan(ctx, "initialsync.nonSkippedSlotAfter")
    27  	defer span.End()
    28  	//todo
    29  	return nil, nil
    30  }
    31  
    32  // findFork queries all peers that have higher head slot, in an attempt to find
    33  // ones that feature blocks from alternative branches. Once found, peer is further queried
    34  // to find common ancestor slot. On success, all obtained blocks and peer is returned.
    35  func (f *blocksFetcher) findFork(ctx context.Context, blockNr *uint256.Int) (*forkData, error) {
    36  	ctx, span := trace.StartSpan(ctx, "initialsync.findFork")
    37  	defer span.End()
    38  
    39  	return nil, errNoPeersWithAltBlocks
    40  }
    41  
    42  // findForkWithPeer loads some blocks from a peer in an attempt to find alternative blocks.
    43  func (f *blocksFetcher) findForkWithPeer(ctx context.Context, pid peer.ID, blockNr *uint256.Int) (*forkData, error) {
    44  
    45  	//fork, err := f.findAncestor(ctx, pid, blockNr)
    46  	return nil, errors.New("no alternative blocks exist within scanned range")
    47  }
    48  
    49  // findAncestor tries to figure out common ancestor slot that connects a given root to known block.
    50  func (f *blocksFetcher) findAncestor(ctx context.Context, pid peer.ID, b *types_pb.Block) (*forkData, error) {
    51  
    52  	return nil, errors.New("no common ancestor found")
    53  }
    54  
    55  // bestFinalizedSlot returns the highest finalized slot of the majority of connected peers.
    56  func (f *blocksFetcher) bestFinalizedBlockNr() *uint256.Int {
    57  	finalizedBlockNr, _ := f.p2p.Peers().BestPeers(f.p2p.GetConfig().MinSyncPeers, f.chain.CurrentBlock().Number64())
    58  	return finalizedBlockNr
    59  }