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