github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/receipts/handle_show.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 receiptsPkg
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"sort"
    11  
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/articulate"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/identifiers"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    18  )
    19  
    20  func (opts *ReceiptsOptions) HandleShow(rCtx *output.RenderCtx) error {
    21  	chain := opts.Globals.Chain
    22  	testMode := opts.Globals.TestMode
    23  	nErrors := 0
    24  
    25  	abiCache := articulate.NewAbiCache(opts.Conn, opts.Articulate)
    26  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    27  		apps, _, err := identifiers.IdsToApps(chain, opts.TransactionIds)
    28  		if err != nil {
    29  			errorChan <- err
    30  			rCtx.Cancel()
    31  		}
    32  
    33  		if sliceOfMaps, cnt, err := types.AsSliceOfMaps[types.Transaction](apps, false); err != nil {
    34  			errorChan <- err
    35  			rCtx.Cancel()
    36  
    37  		} else if cnt == 0 {
    38  			errorChan <- fmt.Errorf("no receipts found")
    39  			rCtx.Cancel()
    40  
    41  		} else {
    42  			showProgress := opts.Globals.ShowProgress()
    43  			bar := logger.NewBar(logger.BarOptions{
    44  				Enabled: showProgress,
    45  				Total:   int64(cnt),
    46  			})
    47  
    48  			for _, thisMap := range sliceOfMaps {
    49  				if rCtx.WasCanceled() {
    50  					return
    51  				}
    52  
    53  				for app := range thisMap {
    54  					thisMap[app] = new(types.Transaction)
    55  				}
    56  
    57  				iterFunc := func(app types.Appearance, value *types.Transaction) error {
    58  					if tx, err := opts.Conn.GetTransactionByAppearance(&app, false /* needsTraces */); err != nil {
    59  						delete(thisMap, app)
    60  						return fmt.Errorf("transaction at %s returned an error: %w", app.Orig(), err)
    61  
    62  					} else if tx == nil || tx.Receipt == nil {
    63  						delete(thisMap, app)
    64  						return fmt.Errorf("transaction at %s has no receipts", app.Orig())
    65  
    66  					} else {
    67  						if opts.Articulate {
    68  							if err = abiCache.ArticulateReceipt(tx.Receipt); err != nil {
    69  								errorChan <- err // continue even with an error
    70  							}
    71  						}
    72  						*value = *tx
    73  						bar.Tick()
    74  						return nil
    75  					}
    76  				}
    77  
    78  				iterErrorChan := make(chan error)
    79  				iterCtx, iterCancel := context.WithCancel(context.Background())
    80  				defer iterCancel()
    81  				go utils.IterateOverMap(iterCtx, iterErrorChan, thisMap, iterFunc)
    82  				for err := range iterErrorChan {
    83  					if !testMode || nErrors == 0 {
    84  						errorChan <- err
    85  						nErrors++
    86  					}
    87  				}
    88  
    89  				items := make([]types.Receipt, 0, len(thisMap))
    90  				for _, tx := range thisMap {
    91  					if tx.Receipt != nil {
    92  						items = append(items, *tx.Receipt)
    93  					}
    94  				}
    95  				sort.Slice(items, func(i, j int) bool {
    96  					if items[i].BlockNumber == items[j].BlockNumber {
    97  						return items[i].TransactionIndex < items[j].TransactionIndex
    98  					}
    99  					return items[i].BlockNumber < items[j].BlockNumber
   100  				})
   101  
   102  				for _, item := range items {
   103  					modelChan <- &item
   104  				}
   105  			}
   106  			bar.Finish(true /* newLine */)
   107  		}
   108  	}
   109  
   110  	extraOpts := map[string]any{
   111  		"articulate": opts.Articulate,
   112  	}
   113  
   114  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
   115  }