Skip to content

Commit

Permalink
Begins forcing access to the RPC through an object (#3113)
Browse files Browse the repository at this point in the history
* Begins forcing access to the RPC through an object

* Fixing

* Moves more stuff to rpcOptions

* Further forcing of access to the RPC through the object

* Fix

* All functions in rpcClient are gated by the rpcOptions object

* Creates an RPC Connection while initializing the command

* Trying to get control of `opts.Globals.Chain` as `chain`

* Trying to get control of the rpcOptions

* More cleaning

* Trying to get control of rpc connection

* Adding debugging to rpcOptions

* We're done for now.

* Enable cache code but not implemented
  • Loading branch information
tjayrush authored Aug 1, 2023
1 parent 811d7d1 commit 8b5bdbd
Show file tree
Hide file tree
Showing 155 changed files with 1,244 additions and 568 deletions.
248 changes: 248 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/apps/chifra/internal/abis/handle_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
)

func (opts *AbisOptions) HandleAddresses() (err error) {
chain := opts.Globals.Chain

result := make(abi.AbiInterfaceMap)
if opts.Known {
if err = abi.PreloadKnownAbis(opts.Globals.Chain, result); err != nil {
if err = abi.PreloadKnownAbis(chain, result); err != nil {
return
}
}
Expand All @@ -27,14 +29,14 @@ func (opts *AbisOptions) HandleAddresses() (err error) {
// Note here, that known ABIs are not downloaded. They are only loaded from the local cache.
for _, addr := range opts.Addrs {
address := base.HexToAddress(addr)
if err = abi.LoadAbiFromAddress(opts.Globals.Chain, address, result); err != nil {
if err = abi.LoadAbiFromAddress(chain, address, result); err != nil {
if !os.IsNotExist(err) {
// The error was not due to a missing file...
errorChan <- err
cancel()
}
// Let's try to download the file from somewhere
if err := rpcClient.IsContractAt(opts.Globals.Chain, address, nil); err != nil {
if err := opts.Conn.IsContractAt(chain, address, nil); err != nil {
if !errors.Is(err, rpcClient.ErrNotAContract) {
errorChan <- err
cancel()
Expand All @@ -45,7 +47,7 @@ func (opts *AbisOptions) HandleAddresses() (err error) {
}
} else {
// It's okay to not find the ABI. We report an error, but do not stop processing
if err = abi.DownloadAbi(opts.Globals.Chain, address, result); err != nil {
if err = abi.DownloadAbi(chain, address, result); err != nil {
errorChan <- err
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/apps/chifra/internal/abis/handle_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

func (opts *AbisOptions) HandleClean() error {
chain := opts.Globals.Chain

if opts.Globals.IsApiMode() {
return fmt.Errorf("clean is not supported in API mode")
}
Expand All @@ -25,14 +27,14 @@ func (opts *AbisOptions) HandleClean() error {
// TODO: This code is not actually used
// filenameChan := make(chan cache.CacheFileInfo)
// var nRoutines int = 1
// go cache.WalkCacheFolder(context.Background(), opts.Globals.Chain, cache.Cache_Abis, nil, filenameChan)
// go cache.WalkCacheFolder(context.Background(), chain, cache.Cache_Abis, nil, filenameChan)
// for result := range filenameChan {
// switch result.Type {
// case cache.Cache_Abis:
// skip := !cache.IsCacheType(result.Path, cache.Cache_Abis, true /* checkExt */)
// if !skip {
// if file.FileSize(result.Path) == 0 {
// logger.Info("Removing empty abi: "+strings.Replace(result.Path, config.GetPathToCache(opts.Globals.Chain)+"abis/", "", -1), file.FileSize(result.Path))
// logger.Info("Removing empty abi: "+strings.Replace(result.Path, config.GetPathToCache(chain)+"abis/", "", -1), file.FileSize(result.Path))
// // if err := cleanOneAbi(result.Path, ""); err != nil {
// // return err
// // }
Expand All @@ -47,7 +49,7 @@ func (opts *AbisOptions) HandleClean() error {
// }
} else {
for _, addr := range opts.Addrs {
if err := cleanOneAbi(config.GetPathToCache(opts.Globals.Chain)+"abis/", addr); err != nil {
if err := cleanOneAbi(config.GetPathToCache(chain)+"abis/", addr); err != nil {
return err
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/apps/chifra/internal/abis/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type AbisOptions struct {
Clean bool `json:"clean,omitempty"` // Remove an abi file for an address or all zero-length files if no address is given
Sol bool `json:"sol,omitempty"` // Please use the `solc --abi` tool instead
Globals globals.GlobalOptions `json:"globals,omitempty"` // The global options
Conn *rpcClient.Options `json:"conn,omitempty"` // The connection to the RPC server
BadFlag error `json:"badFlag,omitempty"` // An error flag if needed
// EXISTING_CODE
// EXISTING_CODE
Expand All @@ -44,6 +45,7 @@ func (opts *AbisOptions) testLog() {
logger.TestLog(len(opts.Hint) > 0, "Hint: ", opts.Hint)
logger.TestLog(len(opts.Encode) > 0, "Encode: ", opts.Encode)
logger.TestLog(opts.Clean, "Clean: ", opts.Clean)
opts.Conn.TestLog()
opts.Globals.TestLog()
}

Expand Down Expand Up @@ -84,14 +86,19 @@ func abisFinishParseApi(w http.ResponseWriter, r *http.Request) *AbisOptions {
opts.Sol = true
default:
if !copy.Globals.Caps.HasKey(key) {
opts.Conn = &rpcClient.Options{}
opts.BadFlag = validate.Usage("Invalid key ({0}) in {1} route.", key, "abis")
return opts
}
}
}
opts.Globals = *globals.GlobalsFinishParseApi(w, r)
chain := opts.Globals.Chain
caches := []string{}
opts.Conn = rpcClient.NewConnection(chain, caches)

// EXISTING_CODE
opts.Addrs, _ = rpcClient.GetAddressesFromEns(opts.Globals.Chain, opts.Addrs)
opts.Addrs, _ = opts.Conn.GetAddressesFromEns(chain, opts.Addrs)
// EXISTING_CODE

return opts
Expand All @@ -102,15 +109,20 @@ func abisFinishParse(args []string) *AbisOptions {
opts := GetOptions()
opts.Globals.FinishParse(args)
defFmt := "txt"
chain := opts.Globals.Chain
caches := []string{}
opts.Conn = rpcClient.NewConnection(chain, caches)

// EXISTING_CODE
if opts.Globals.IsApiMode() {
defFmt = "json"
}
opts.Addrs, _ = rpcClient.GetAddressesFromEns(opts.Globals.Chain, args)
opts.Addrs, _ = opts.Conn.GetAddressesFromEns(chain, args)
// EXISTING_CODE
if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
opts.Globals.Format = defFmt
}

return opts
}

Expand Down
26 changes: 13 additions & 13 deletions src/apps/chifra/internal/blocks/handle_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import (
)

func (opts *BlocksOptions) HandleCounts() error {
rpcOptions := rpcClient.DefaultRpcOptions(&rpcClient.DefaultRpcOptionsSettings{
Chain: opts.Globals.Chain,
chain := opts.Globals.Chain
settings := rpcClient.DefaultRpcOptionsSettings{
Chain: chain,
Opts: opts,
})
}
opts.Conn = settings.DefaultRpcOptions()

// TODO: Why does this have to dirty the caller?
// If the cache is writeable, fetch the latest block timestamp so that we never
// cache pending blocks
if !rpcOptions.Store.ReadOnly() {
rpcOptions.LatestBlockTimestamp = rpcClient.GetBlockTimestamp(opts.Globals.Chain, nil)
if !opts.Conn.Store.ReadOnly() {
opts.Conn.LatestBlockTimestamp = opts.Conn.GetBlockTimestamp(chain, nil)
}

chain := opts.Globals.Chain

ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawModeler], errorChan chan error) {
for _, br := range opts.BlockIds {
Expand All @@ -45,7 +45,7 @@ func (opts *BlocksOptions) HandleCounts() error {

for _, bn := range blockNums {
var block types.SimpleBlock[string]
if block, err = rpcClient.GetBlockHeaderByNumber(chain, bn, rpcClient.NoOptions); err != nil {
if block, err = opts.Conn.GetBlockHeaderByNumber(chain, bn); err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
Expand All @@ -61,7 +61,7 @@ func (opts *BlocksOptions) HandleCounts() error {
}

if opts.Uncles {
if blockCount.UnclesCnt, err = rpcClient.GetCountUnclesInBlock(chain, bn); err != nil {
if blockCount.UnclesCnt, err = opts.Conn.GetCountUnclesInBlock(chain, bn); err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
Expand All @@ -72,7 +72,7 @@ func (opts *BlocksOptions) HandleCounts() error {
}

if opts.Traces {
if blockCount.TracesCnt, err = rpcClient.GetCountTracesInBlock(chain, bn); err != nil {
if blockCount.TracesCnt, err = opts.Conn.GetCountTracesInBlock(chain, bn); err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
Expand All @@ -83,7 +83,7 @@ func (opts *BlocksOptions) HandleCounts() error {
}

if opts.Logs {
if blockCount.LogsCnt, err = rpcClient.GetCountLogsInBlock(chain, bn); err != nil {
if blockCount.LogsCnt, err = opts.Conn.GetCountLogsInBlock(chain, bn); err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
Expand All @@ -100,8 +100,8 @@ func (opts *BlocksOptions) HandleCounts() error {
}

addrMap := make(index.AddressBooleanMap)
ts := rpcClient.GetBlockTimestamp(chain, &bn)
if err := opts.ProcessBlockUniqs(chain, countFunc, bn, addrMap, ts, rpcOptions); err != nil {
ts := opts.Conn.GetBlockTimestamp(chain, &bn)
if err := opts.ProcessBlockUniqs(chain, countFunc, bn, addrMap, ts); err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
Expand Down
19 changes: 9 additions & 10 deletions src/apps/chifra/internal/blocks/handle_decache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ import (
)

func (opts *BlocksOptions) HandleDecache() error {
rpcOptions := rpcClient.DefaultRpcOptions(&rpcClient.DefaultRpcOptionsSettings{
Chain: opts.Globals.Chain,
ReadonlyCache: true,
})
chain := opts.Globals.Chain
opts.Conn = rpcClient.NewReadOnlyConnection(chain)

toRemove := make([]cacheNew.Locator, 0)
for _, br := range opts.BlockIds {
blockNums, err := br.ResolveBlocks(opts.Globals.Chain)
blockNums, err := br.ResolveBlocks(chain)
if err != nil {
return err
}
for _, bn := range blockNums {
rawBlock, err := rpcClient.GetBlockBodyByNumber(opts.Globals.Chain, bn, rpcOptions)
rawBlock, err := opts.Conn.GetBlockBodyByNumber(chain, bn)
if err != nil {
return err
}
Expand Down Expand Up @@ -64,7 +63,7 @@ func (opts *BlocksOptions) HandleDecache() error {
return true
}

rpcOptions.Store.Decache(toRemove, processorFunc)
opts.Conn.Store.Decache(toRemove, processorFunc)

if itemsSeen == 0 {
logger.Info("No items matching the query were found in the cache.", strings.Repeat(" ", 60))
Expand All @@ -77,12 +76,12 @@ func (opts *BlocksOptions) HandleDecache() error {
// TODO: Review then remove
// pairs := []base.Pair[uint32,uint32]{}
// for _, br := range opts.BlockIds {
// blockNums, err := br.ResolveBlocks(opts.Globals.Chain)
// blockNums, err := br.ResolveBlocks(chain)
// if err != nil {
// return err
// }
// for _, bn := range blockNums {
// rawBlock, err := rpcClient.GetBlockBodyByNumber(opts.Globals.Chain, bn, nil)
// rawBlock, err := rpcOptions.GetBlockBodyByNumber(chain, bn)
// if err != nil {
// return err
// }
Expand Down Expand Up @@ -123,7 +122,7 @@ func (opts *BlocksOptions) HandleDecache() error {
// }

// caches := []string{"blocks", "txs", "traces"}
// if cont, err := cache.DecacheItems(opts.Globals.Chain, "", processorFunc, caches, pairs); err != nil || !cont {
// if cont, err := cache.DecacheItems(chain, "", processorFunc, caches, pairs); err != nil || !cont {
// return err
// }

Expand Down
7 changes: 3 additions & 4 deletions src/apps/chifra/internal/blocks/handle_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import (
"errors"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpcClient"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/ethereum/go-ethereum"
)

func (opts *BlocksOptions) HandleList() error {
var rpcOptions = rpcClient.NoOptions
chain := opts.Globals.Chain

// Don't do this in the loop
meta, err := rpcClient.GetMetaData(opts.Globals.Chain, opts.Globals.TestMode)
meta, err := opts.Conn.GetMetaData(chain, opts.Globals.TestMode)
if err != nil {
return err
}
Expand All @@ -34,7 +33,7 @@ func (opts *BlocksOptions) HandleList() error {
ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawBlock], errorChan chan error) {
for bn := start; bn > end; bn-- {
block, err := rpcClient.GetBlockHeaderByNumber(opts.Globals.Chain, bn, rpcOptions)
block, err := opts.Conn.GetBlockHeaderByNumber(chain, bn)
if err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
Expand Down
3 changes: 1 addition & 2 deletions src/apps/chifra/internal/blocks/handle_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/articulate"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpcClient"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/ethereum/go-ethereum"
)
Expand Down Expand Up @@ -54,7 +53,7 @@ func (opts *BlocksOptions) HandleLogs() error {
errorChan <- errors.New("TESTING_ONLY_filter" + fmt.Sprintf("%+v", logFilter))
}

logs, err := rpcClient.GetLogsByFilter(chain, logFilter)
logs, err := opts.Conn.GetLogsByFilter(chain, logFilter)
if err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
Expand Down
18 changes: 10 additions & 8 deletions src/apps/chifra/internal/blocks/handle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ import (
)

func (opts *BlocksOptions) HandleShowBlocks() error {
rpcOptions := rpcClient.DefaultRpcOptions(&rpcClient.DefaultRpcOptionsSettings{
Chain: opts.Globals.Chain,
chain := opts.Globals.Chain
settings := rpcClient.DefaultRpcOptionsSettings{
Chain: chain,
Opts: opts,
})
}
opts.Conn = settings.DefaultRpcOptions()

// TODO: Why does this have to dirty the caller?
// If the cache is writeable, fetch the latest block timestamp so that we never
// cache pending blocks
if !rpcOptions.Store.ReadOnly() {
rpcOptions.LatestBlockTimestamp = rpcClient.GetBlockTimestamp(opts.Globals.Chain, nil)
if !opts.Conn.Store.ReadOnly() {
opts.Conn.LatestBlockTimestamp = opts.Conn.GetBlockTimestamp(chain, nil)
}

ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawBlock], errorChan chan error) {
for _, br := range opts.BlockIds {
blockNums, err := br.ResolveBlocks(opts.Globals.Chain)
blockNums, err := br.ResolveBlocks(chain)
if err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
Expand All @@ -46,11 +48,11 @@ func (opts *BlocksOptions) HandleShowBlocks() error {
var err error
if !opts.Hashes {
var b types.SimpleBlock[types.SimpleTransaction]
b, err = rpcClient.GetBlockBodyByNumber(opts.Globals.Chain, bn, rpcOptions)
b, err = opts.Conn.GetBlockBodyByNumber(chain, bn)
block = &b
} else {
var b types.SimpleBlock[string]
b, err = rpcClient.GetBlockHeaderByNumber(opts.Globals.Chain, bn, rpcOptions)
b, err = opts.Conn.GetBlockHeaderByNumber(chain, bn)
block = &b
}

Expand Down
4 changes: 1 addition & 3 deletions src/apps/chifra/internal/blocks/handle_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
"errors"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpcClient"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/ethereum/go-ethereum"
)

func (opts *BlocksOptions) HandleTraces() error {
chain := opts.Globals.Chain

ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawTrace], errorChan chan error) {
for _, br := range opts.BlockIds {
Expand All @@ -32,7 +30,7 @@ func (opts *BlocksOptions) HandleTraces() error {

for _, bn := range blockNums {
var traces []types.SimpleTrace
traces, err = rpcClient.GetTracesByNumber(chain, bn)
traces, err = opts.Conn.GetTracesByNumber(chain, bn)
if err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
Expand Down
Loading

0 comments on commit 8b5bdbd

Please sign in to comment.