github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/when/handle_ts_repair.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package whenPkg
     6  
     7  import (
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/identifiers"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib"
    12  )
    13  
    14  // HandleTimestampsRepair handles chifra when --timestamps --reset <bn> to reset a single block's timestamps (call repeatedly if needed)
    15  func (opts *WhenOptions) HandleTimestampsRepair(rCtx *output.RenderCtx) error {
    16  	chain := opts.Globals.Chain
    17  
    18  	cnt, err := tslib.NTimestamps(chain)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	// skip := uint64(500)
    24  	// scanBar := progress.NewScanBar(cnt /* wanted */, (cnt / skip) /* freq */, cnt /* max */, (2. / 3.))
    25  
    26  	blockNums, err := identifiers.GetBlockNumbers(chain, opts.BlockIds)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	for _, bn := range blockNums {
    32  		if bn < cnt { // ranges may include blocks after last block
    33  			if err := tslib.Repair(chain, bn); err != nil {
    34  				return err
    35  			}
    36  			if bn == 1 { // weird special case because because I don't know how to get Cobra to handle non-zero defaults
    37  				if err := tslib.Repair(chain, 0); err != nil {
    38  					return err
    39  				}
    40  			}
    41  
    42  			ts, _ := tslib.FromBnToTs(chain, bn)
    43  			logger.Info("The timestamp at block", bn, "was reset to", ts, "from on chain.")
    44  		}
    45  	}
    46  
    47  	return nil
    48  }