github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/state/handle_decache.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 statePkg
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/cache"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/call"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/decache"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    17  )
    18  
    19  func (opts *StateOptions) HandleDecache(rCtx *output.RenderCtx) error {
    20  	itemsToRemove, err := opts.getItemsToRemove()
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    26  		showProgress := opts.Globals.ShowProgress()
    27  		monitorCacheTypes := []walk.CacheType{
    28  			walk.Cache_State,
    29  			walk.Cache_Results,
    30  		}
    31  		for _, cacheType := range monitorCacheTypes {
    32  			if msg, err := decache.Decache(opts.Conn, itemsToRemove, showProgress, cacheType); err != nil {
    33  				errorChan <- err
    34  			} else {
    35  				s := types.Message{
    36  					Msg: msg,
    37  				}
    38  				modelChan <- &s
    39  			}
    40  		}
    41  	}
    42  
    43  	opts.Globals.NoHeader = true
    44  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
    45  }
    46  
    47  func (opts *StateOptions) getItemsToRemove() ([]cache.Locator, error) {
    48  	allItems := make([]cache.Locator, 0)
    49  	for _, addr := range opts.Addrs {
    50  		address := base.HexToAddress(addr)
    51  		itemsToRemove, err := decache.LocationsFromState(opts.Conn, address, opts.BlockIds)
    52  		if err != nil {
    53  			return []cache.Locator{}, err
    54  		}
    55  		allItems = append(allItems, itemsToRemove...)
    56  
    57  		for _, c := range opts.Calls {
    58  			if len(c) > 0 {
    59  				callAddress := opts.GetCallAddress()
    60  				if contractCall, _, err := call.NewContractCall(opts.Conn, callAddress, c); err != nil {
    61  					wrapped := fmt.Errorf("the --call value provided (%s) was not found: %s", c, err)
    62  					return []cache.Locator{}, wrapped
    63  
    64  				} else {
    65  					itemsToRemove, err := decache.LocationsFromAddressAndEncodings(opts.Conn, address, contractCall.Method.Encoding, opts.BlockIds)
    66  					if err != nil {
    67  						return []cache.Locator{}, err
    68  					}
    69  					allItems = append(allItems, itemsToRemove...)
    70  				}
    71  			}
    72  		}
    73  	}
    74  	return allItems, nil
    75  }