github.com/aergoio/aergo@v1.3.1/chain/chainanchor.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package chain
     7  
     8  import (
     9  	"github.com/aergoio/aergo/internal/enc"
    10  	"github.com/aergoio/aergo/types"
    11  )
    12  
    13  type ChainAnchor []([]byte)
    14  
    15  const (
    16  	MaxAnchors = 32
    17  	//MaxAnchors = 1000000
    18  	Skip = 16
    19  )
    20  
    21  // returns anchor blocks of chain
    22  // use config
    23  func (cs *ChainService) getAnchorsNew() (ChainAnchor, types.BlockNo, error) {
    24  	//from top : 8 * 32 = 256
    25  	anchors := make(ChainAnchor, 0)
    26  	cnt := MaxAnchors
    27  	logger.Debug().Msg("get anchors")
    28  
    29  	blkNo := cs.getBestBlockNo()
    30  	var lastNo types.BlockNo
    31  LOOP:
    32  	for i := 0; i < cnt; i++ {
    33  		blockHash, err := cs.getHashByNo(blkNo)
    34  		if err != nil {
    35  			logger.Info().Msg("assertion - hash get failed")
    36  			// assertion!
    37  			return nil, 0, err
    38  		}
    39  
    40  		anchors = append(anchors, blockHash)
    41  		lastNo = blkNo
    42  
    43  		logger.Debug().Uint64("no", blkNo).Msg("anchor added")
    44  
    45  		switch {
    46  		case blkNo == 0:
    47  			break LOOP
    48  		case blkNo < Skip:
    49  			blkNo = 0
    50  		default:
    51  			blkNo -= Skip
    52  		}
    53  	}
    54  
    55  	return anchors, lastNo, nil
    56  }
    57  
    58  // returns anchor blocks of chain
    59  // use config
    60  func (cs *ChainService) getAnchorsFromHash(blockHash []byte) ChainAnchor {
    61  	/* TODO: use config */
    62  	anchors := make(ChainAnchor, 0, 1000)
    63  	anchors = append(anchors, blockHash)
    64  
    65  	// collect 10 latest hashes
    66  	latestNo := cs.getBestBlockNo()
    67  	for i := 0; i < 10; i++ {
    68  		blockHash, err := cs.getHashByNo(latestNo)
    69  		if err != nil {
    70  			logger.Info().Msg("assertion - hash get failed")
    71  			// assertion!
    72  			return nil
    73  		}
    74  
    75  		logger.Debug().Uint64("no", latestNo).Str("hash", enc.ToString(blockHash)).Msg("anchor")
    76  
    77  		anchors = append(anchors, blockHash)
    78  		if latestNo == 0 {
    79  			return anchors
    80  		}
    81  		latestNo--
    82  	}
    83  
    84  	count := MaxAnchorCount
    85  	// collect exponential
    86  	var dec types.BlockNo = 1
    87  	for i := 0; i < count; i++ {
    88  		blockHash, err := cs.getHashByNo(latestNo)
    89  		if err != nil {
    90  			// assertion!
    91  			return nil
    92  		}
    93  
    94  		logger.Debug().Uint64("no", latestNo).Str("hash", enc.ToString(blockHash)).Msg("anchor")
    95  
    96  		anchors = append(anchors, blockHash)
    97  		if latestNo <= dec {
    98  			if latestNo == 0 {
    99  				break
   100  			}
   101  			latestNo = 0
   102  		} else {
   103  			latestNo -= dec
   104  			dec *= 2
   105  		}
   106  	}
   107  
   108  	return anchors
   109  }