Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to clean up initialization #3706

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions src/apps/chifra/cmd/root_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
)

// Initialize makes sure everything is ready to run. These routines don't return if they aren't
Expand Down Expand Up @@ -43,7 +44,7 @@ func VerifyOs() {

// VerifyMigrations will panic if the installation is not properly migrated
func VerifyMigrations() {
if isPermitted() {
if utils.IsPermitted() {
// Allow certain status and config routes to pass so as to aide user in migrating...
return
}
Expand Down Expand Up @@ -111,35 +112,3 @@ func VerifyMigrations() {
}
}
}

func isPermitted() bool {
isStatus := false
isConfig := false
hasPaths := false
hasEdit := false
cnt := len(os.Args)
for _, arg := range os.Args {
if arg == "help" {
return false
} else if arg == "status" {
isStatus = true
} else if arg == "config" {
isConfig = true
} else if arg == "--paths" {
hasPaths = true
} else if arg == "edit" {
hasEdit = true
} else if arg != "--verbose" {
isStatus = false
isConfig = false
} else {
cnt-- // allow --verbose
}
}

if isStatus && cnt == 2 {
return true
}

return isConfig && (hasPaths || hasEdit) && cnt < 4
}
5 changes: 4 additions & 1 deletion src/apps/chifra/internal/abis/handle_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (opts *AbisOptions) HandleAbiFind() error {
if bytes.Equal(sigBytes[:len(str)], str) {
scanBar.Found++
logger.Progress(len(opts.Find) < 2, "Found", scanBar.Found, "of", scanBar.Wanted, arg, testSig)
found := types.Function{Encoding: arg, Signature: testSig.(string)}
found := types.Function{
Encoding: arg,
Signature: testSig.(string),
}
if testMode {
mutex.Lock()
results = append(results, found)
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/chunks/handle_truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (opts *ChunksOptions) HandleTruncate(blockNums []base.Blknum) error {
_ = file.CleanFolder(chain, config.PathToIndex(chain), []string{"ripe", "unripe", "maps", "staging"})

bar := logger.NewBar(logger.BarOptions{
Enabled: !testMode && !logger.IsTerminal(),
Enabled: !testMode, // && !logger.IsTerminal(),
Total: 128,
Type: logger.Expanding,
})
Expand Down
38 changes: 38 additions & 0 deletions src/apps/chifra/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"strings"
"sync"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/usage"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/version"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -132,6 +134,9 @@ func GetRootConfig() *ConfigFile {
ch.LocalExplorer = clean(ch.LocalExplorer)
ch.RemoteExplorer = clean(ch.RemoteExplorer)
ch.RpcProvider = strings.Trim(clean(ch.RpcProvider), "/") // Infura, for example, doesn't like the trailing slash
if err := validateRpcEndpoint(ch.Chain, ch.RpcProvider); err != nil {
logger.Fatal(err)
}
ch.IpfsGateway = clean(ch.IpfsGateway)
if ch.Scrape.AppsPerChunk == 0 {
settings := ScrapeSettings{
Expand Down Expand Up @@ -201,3 +206,36 @@ func pathFromXDG(envVar string) (string, error) {

return filepath.Join(xdg, "") + "/", nil
}

func validateRpcEndpoint(chain, provider string) error {
if utils.IsPermitted() {
return nil
}

if provider == "https:" {
problem := `No rpcProvider found.`
return usage.Usage(rpcWarning, chain, provider, problem)
}

if !strings.HasPrefix(provider, "http") {
problem := `Invalid rpcProvider found (must be a url).`
return usage.Usage(rpcWarning, chain, provider, problem)
}

return nil
}

var rpcWarning string = `
We found a problem with the rpcProvider for the {0} chain.

Provider: {1}
Chain: {0}
Problem: {2}

Confirm the value for the given provider. You may edit this value with
"chifra config edit".

Also, try the following curl command. If this command does not work, neither will chifra.

curl -X POST -H "Content-Type: application/json" --data '{ "jsonrpc": "2.0", "method": "web3_clientVersion", "id": 6 }' {1}
`
12 changes: 12 additions & 0 deletions src/apps/chifra/pkg/manifest/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func ReadUnchainedIndex(chain string, publisher base.Address, database string) (

unchainedChain := "mainnet" // the unchained index is on mainnet
conn := rpc.TempConnection(unchainedChain)
// if conn.LatestBlockTimestamp < 1_705_173_443 { // block 19_000_000
// provider := config.GetChain(unchainedChain).RpcProvider
// logger.Fatal(usage.Usage(unchainedWarning, provider))
// }

theCall := fmt.Sprintf("manifestHashMap(%s, \"%s\")", publisher, database)
if contractCall, _, err := call.NewContractCallWithAbi(conn, callAddress, theCall, abiMap); err != nil {
wrapped := fmt.Errorf("the --call value provided (%s) was not found: %s", theCall, err)
Expand Down Expand Up @@ -128,3 +133,10 @@ func getUnchainedAbi() (base.Address, *abi.SelectorSyncMap, error) {

return callAddress, abiMap, nil
}

// var unchainedWarning string = `
// The Unchained Index requires your mainnet RPC to be synced (at least to block 0x1304073 or 19000000).
// Check the progress with the following curl command and try again later.

// curl -X POST -H "Content-Type: application/json" --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' {0}
// `
4 changes: 4 additions & 0 deletions src/apps/chifra/pkg/rpc/get_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func (conn *Connection) GetTracesByBlockNumber(bn base.Blknum) ([]types.Trace, e

if traces, err := query.Query[[]types.Trace](conn.Chain, method, params); err != nil {
return []types.Trace{{
Action: &types.TraceAction{},
Result: &types.TraceResult{},
}}, err

} else if traces == nil || len(*traces) == 0 {
return []types.Trace{{
Action: &types.TraceAction{},
Result: &types.TraceResult{},
}}, err

Expand Down Expand Up @@ -89,11 +91,13 @@ func (conn *Connection) GetTracesByTransactionHash(txHash string, transaction *t

if traces, err := query.Query[[]types.Trace](conn.Chain, method, params); err != nil {
return []types.Trace{{
Action: &types.TraceAction{},
Result: &types.TraceResult{},
}}, err

} else if traces == nil || len(*traces) == 0 {
return []types.Trace{{
Action: &types.TraceAction{},
Result: &types.TraceResult{},
}}, err

Expand Down
1 change: 1 addition & 0 deletions src/apps/chifra/pkg/rpc/get_traces_parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (conn *Connection) GetTracesByTransactionId(bn base.Blknum, txid base.Txnum
tx, err := conn.GetTransactionByNumberAndId(bn, txid)
if err != nil {
return []types.Trace{{
Action: &types.TraceAction{},
Result: &types.TraceResult{},
}}, err
}
Expand Down
8 changes: 6 additions & 2 deletions src/apps/chifra/pkg/rpc/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ func (conn *Connection) getTransactionFromRpc(blkHash base.Hash, txHash base.Has
}

if trans, err := query.Query[types.Transaction](conn.Chain, method, params); err != nil {
return &types.Transaction{}, err
return &types.Transaction{
Receipt: &types.Receipt{},
}, err
} else if trans.Hash.IsZero() {
return &types.Transaction{}, ethereum.NotFound
return &types.Transaction{
Receipt: &types.Receipt{},
}, ethereum.NotFound
} else {
return trans, nil
}
Expand Down
35 changes: 35 additions & 0 deletions src/apps/chifra/pkg/utils/is_permitted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package utils

import "os"

func IsPermitted() bool {
isStatus := false
isConfig := false
hasPaths := false
hasEdit := false
cnt := len(os.Args)
for _, arg := range os.Args {
if arg == "help" {
return false
} else if arg == "status" {
isStatus = true
} else if arg == "config" {
isConfig = true
} else if arg == "--paths" {
hasPaths = true
} else if arg == "edit" {
hasEdit = true
} else if arg != "--verbose" {
isStatus = false
isConfig = false
} else {
cnt-- // allow --verbose
}
}

if isStatus && cnt == 2 {
return true
}

return isConfig && (hasPaths || hasEdit) && cnt < 4
}
Loading