github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/is_node.go (about)

     1  package rpc
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/prefunds"
    11  )
    12  
    13  // TODO: Some of this code may be chain-specific - for example,
    14  // some chains have no pre-allocation.
    15  
    16  // IsNodeArchive tries to return true if the node is an archive
    17  // node with the following caveteat: we assume that the node has
    18  // been initialized with a pre-allocation. This is a reasonable
    19  // assumption for most chains, but not all. For example, if a
    20  // chain does not have a pre-allocation, this function will return
    21  // false, when in fact the node may be an archive node.
    22  func (conn *Connection) IsNodeArchive() bool {
    23  	thePath := filepath.Join(config.MustGetPathToChainConfig(conn.Chain), "allocs.csv")
    24  	largest, err := prefunds.GetLargestPrefund(conn.Chain, thePath)
    25  	if err != nil {
    26  		return false
    27  	}
    28  
    29  	bal, err := conn.GetBalanceAt(largest.Address, 0)
    30  	if err != nil {
    31  		return false
    32  	}
    33  
    34  	return bal.Cmp(&largest.Balance) == 0
    35  }
    36  
    37  // IsNodeTracing returns true if the node exposes the `block_trace` RPC endpoint.
    38  // It queries block 1 or a user supplied block (which we presume exists). The function
    39  // returns false if block_trace returns an error or doesn't exist.
    40  func (conn *Connection) IsNodeTracing() (error, bool) {
    41  	firstTrace := base.Max(1, base.KnownBlock(conn.Chain, base.FirstTrace))
    42  	varName := "TB_" + strings.ToUpper(conn.Chain) + "_FIRSTTRACE"
    43  	if len(os.Getenv(varName)) > 0 {
    44  		firstTrace = base.Max(firstTrace, base.MustParseValue(os.Getenv(varName)))
    45  	}
    46  	_, err := conn.GetTracesByBlockNumber(firstTrace)
    47  	return err, err == nil
    48  }