Skip to content

Commit

Permalink
Various small non-consequential changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Sep 27, 2023
1 parent 2c6d5bd commit 953f2ae
Show file tree
Hide file tree
Showing 62 changed files with 404 additions and 273 deletions.
18 changes: 9 additions & 9 deletions src/apps/chifra/cmd/root_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ func VerifyMigrations() {
}
}

// We need at least this version...
requiredVersion := "v0.40.0-beta"
if !config.IsAtLeastVersion(requiredVersion) {
msg := strings.Replace(backVersion, "{0}", "{"+requiredVersion+"}", -1)
msg = strings.Replace(msg, "[{VERSION}]", versionText, -1)
msg = strings.Replace(msg, "{", colors.Green, -1)
msg = strings.Replace(msg, "}", colors.Off, -1)
logger.Fatal(msg)
}
// // We need at least this version...
// requiredVersion := "v0.40.0-beta"
// if !config.IsAtLeastVersion(requiredVersion) {
// msg := strings.Replace(backVersion, "{0}", "{"+requiredVersion+"}", -1)
// msg = strings.Replace(msg, "[{VERSION}]", versionText, -1)
// msg = strings.Replace(msg, "{", colors.Green, -1)
// msg = strings.Replace(msg, "}", colors.Off, -1)
// logger.Fatal(msg)
// }
}
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/monitors/handle_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (opts *MonitorsOptions) getMonitorList() []monitor.Monitor {

for result := range monitorChan {
switch result.Address {
case monitor.SentinalAddr:
case base.SentinalAddr:
close(monitorChan)
default:
if result.Count() > 500000 {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/scrape/handle_scrape_blaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (bm *BlazeManager) WriteTimestamps(endPoint uint64) error {
}

defer func() {
tslib.DeCache(chain)
tslib.ClearCache(chain)
fp.Close()
// sigintTrap.Disable(trapCh)
// writeMutex.Unlock()
Expand Down
10 changes: 10 additions & 0 deletions src/apps/chifra/internal/tokens/handle_parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/names"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/ethereum/go-ethereum"
)
Expand All @@ -20,6 +21,8 @@ func (opts *TokensOptions) HandleParts() error {
fetchData := func(modelChan chan types.Modeler[types.RawToken], errorChan chan error) {
for _, address := range opts.Addrs {
addr := base.HexToAddress(address)
currentBn := uint64(0)
currentTs := base.Timestamp(0)
for _, br := range opts.BlockIds {
blockNums, err := br.ResolveBlocks(chain)
if err != nil {
Expand All @@ -41,6 +44,13 @@ func (opts *TokensOptions) HandleParts() error {
TotalSupply: state.TotalSupply,
Decimals: uint64(state.Decimals),
}
if opts.Globals.Verbose {
if bn == 0 || bn != currentBn {
currentTs, _ = tslib.FromBnToTs(chain, bn)
}
s.Timestamp = currentTs
currentBn = bn
}
modelChan <- s
}
}
Expand Down
16 changes: 5 additions & 11 deletions src/apps/chifra/pkg/abi/load_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,9 @@ func readFunction(reader *bufio.Reader) (function *types.SimpleFunction, err err
return
}

// getCacheAndChainPath returns path to cache for given chain
func getCacheAndChainPath(chain string) string {
cacheDir := config.GetRootConfig().Settings.CachePath
return path.Join(cacheDir, chain)
}

// getAbis reads all ABIs stored in the cache
func getAbis(chain string) ([]types.SimpleFunction, error) {
fullPath := path.Join(getCacheAndChainPath(chain), walk.CacheTypeToFolder[walk.Cache_Abis], "known.bin")
fullPath := path.Join(config.PathToCache(chain), walk.CacheTypeToFolder[walk.Cache_Abis], "known.bin")
if f, err := os.Open(fullPath); err != nil {
return nil, err

Expand Down Expand Up @@ -609,7 +603,7 @@ func SetAbis(chain string, abis []types.SimpleFunction) (err error) {

// save writes contents of `content` Reader to a file
func save(chain string, filePath string, content io.Reader) (err error) {
cacheDir := getCacheAndChainPath(chain)
cacheDir := config.PathToCache(chain)
fullPath := path.Join(cacheDir, filePath)

var f *os.File
Expand Down Expand Up @@ -750,7 +744,7 @@ func LoadAbiFromAddress(chain string, address base.Address, destination *Functio
if err = fromJson(localFile, destination); err != nil {
return
}
// File is correct
// File is correct, cache it
if err = insertAbi(chain, address, localFile); err != nil {
return
}
Expand All @@ -760,7 +754,7 @@ func LoadAbiFromAddress(chain string, address base.Address, destination *Functio

// insertAbi copies file (e.g. opened local file) into cache
func insertAbi(chain string, address base.Address, inputReader io.Reader) error {
fullPath := path.Join(getCacheAndChainPath(chain), walk.CacheTypeToFolder[walk.Cache_Abis], address.Hex()+".json")
fullPath := path.Join(config.PathToCache(chain), walk.CacheTypeToFolder[walk.Cache_Abis], address.Hex()+".json")
if file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
return err
} else {
Expand All @@ -775,7 +769,7 @@ func insertAbi(chain string, address base.Address, inputReader io.Reader) error
// GetAbi returns single ABI per address. ABI-per-address are stored as JSON, not binary.
func GetAbi(chain string, address base.Address) (simpleAbis []types.SimpleFunction, err error) {
filePath := path.Join(walk.CacheTypeToFolder[walk.Cache_Abis], address.Hex()+".json")
f, err := os.Open(path.Join(getCacheAndChainPath(chain), filePath))
f, err := os.Open(path.Join(config.PathToCache(chain), filePath))
if err != nil {
return
}
Expand Down
11 changes: 11 additions & 0 deletions src/apps/chifra/pkg/base/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Address struct {
common.Address
}

// A few well known address. ZeroAddr, of course, is 0x0. SentinalAddr is a marker to signify the end of the monitor list produced by ListMonitors
var (
SentinalAddr = HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead")
ZeroAddr = HexToAddress("0x0")
)

// Hex returns string representation of an address
func (a *Address) Hex() string {
if a.IsZero() {
Expand Down Expand Up @@ -162,3 +168,8 @@ func IsValidAddressE(val string) (bool, error) {

// FAKE_ETH_ADDRESS is the address we use to represent ETH in the ledgers
var FAKE_ETH_ADDRESS = HexToAddress("0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")

// GetTestPublisher does not get customized per chain. We can only test against mainnet currently
func GetTestPublisher() Address {
return HexToAddress("0xf503017d7baf7fbc0fff7492b751025c6a78179b")
}
3 changes: 1 addition & 2 deletions src/apps/chifra/pkg/base/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func TestAddress_IsZero(t *testing.T) {
t.Fatal("wrong result for zero value")
}

zeroAddr := HexToAddress("0x0")
if result := zeroAddr.IsZero(); result != true {
if result := ZeroAddr.IsZero(); result != true {
t.Fatal("wrong result for zero address")
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/apps/chifra/pkg/base/fileRange.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,7 @@ func (r *FileRange) LaterThanB(blk uint64) bool {
func (r *FileRange) Equals(needle FileRange) bool {
return r.First == needle.First && r.Last == needle.Last
}

func (r *FileRange) Span() uint64 {
return r.Last - r.First + 1
}
9 changes: 9 additions & 0 deletions src/apps/chifra/pkg/base/known_blocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package base

// TODO: This is not correct per chain...
const (
ByzantiumBlock = Blknum(4370000)
ConstantinopleBlock = Blknum(7280000)
LondonBlock = Blknum(12965000)
ShanghaiBlock = Blknum(17034870)
)
6 changes: 1 addition & 5 deletions src/apps/chifra/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ func (e *ExampleBlock) MarshalCache(writer io.Writer) (err error) {
var minimalVersion version.Version

func init() {
var err error
minimalVersion, err = version.NewVersion("GHC-TrueBlocks//0.10.0-beta")
if err != nil {
panic(err)
}
minimalVersion = version.NewVersion("GHC-TrueBlocks//0.10.0-beta")
}

// Now we make ExampleBlock implement CacheUnmarshaler interface, which is like
Expand Down
5 changes: 1 addition & 4 deletions src/apps/chifra/pkg/cache/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func init() {
// Set currentHeader, so that we don't have to parse version
// and build a header each time we want to encode/decode cache
// item.
ver, err := version.NewVersion(version.LibraryVersion)
if err != nil {
panic(err)
}
ver := version.NewVersion(version.LibraryVersion)
currentHeader = &header{
Magic: Magic,
Version: ver.Uint64(),
Expand Down
19 changes: 19 additions & 0 deletions src/apps/chifra/pkg/colors/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

package colors

import "strings"

var None = ""
var Off = "\033[0m"
var Red = "\033[31m"
var Green = "\033[32m"
Expand Down Expand Up @@ -43,3 +46,19 @@ func ColorsOff() {
BrightWhite = ""
BrightBlack = ""
}

func Colored(s string) string {
s = strings.Replace(s, "{", Green, -1)
s = strings.Replace(s, "@", BrightYellow, -1)
s = strings.Replace(s, "}", Off, -1)
return s
}

func ColoredWith(s string, c string) string {
s = c + s
s = strings.Replace(s, "{", Green, -1)
s = strings.Replace(s, "@", BrightYellow, -1)
s = strings.Replace(s, "}", c, -1)
s += Off
return s
}
15 changes: 0 additions & 15 deletions src/apps/chifra/pkg/config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"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/usage"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/version"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -126,20 +125,6 @@ func GetRootConfig() *ConfigFile {
return &trueBlocksConfig
}

func IsAtLeastVersion(needle string) bool {
var current, desired version.Version
var err error
if current, err = version.NewVersion(GetRootConfig().Version.Current); err != nil {
return true
}

if desired, err = version.NewVersion(needle); err != nil {
return true
}

return !current.IsEarlierThan(desired)
}

// PathToRootConfig returns the path where to find configuration files
func PathToRootConfig() string {
configPath, err := PathFromXDG("XDG_CONFIG_HOME")
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/file/asciifiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func LinesToAsciiFile(filename string, value []string) error {
}
defer file.Close()

lines := strings.Join(value, "\n") + "\n"
lines := strings.Join(value[:], "\n") + "\n"
_, err = io.WriteString(file, lines)
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions src/apps/chifra/pkg/file/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ package file

import (
"os"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
)

func NFilesInFolder(path string) int {
files, err := os.ReadDir(path)
if err != nil {
logger.Fatal(err)
}
files, _ := os.ReadDir(path)
return len(files)
}
3 changes: 2 additions & 1 deletion src/apps/chifra/pkg/index/chunk_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"strings"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
Expand Down
10 changes: 10 additions & 0 deletions src/apps/chifra/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ func toLog(sev severity, a ...interface{}) {
}
fmt.Fprintln(os.Stderr, "")

} else if sev == warning {
defer fmt.Fprint(os.Stderr, colors.Off)
fmt.Fprint(os.Stderr, colors.Yellow)
fmt.Fprintln(os.Stderr, a...)

} else if sev == err {
defer fmt.Fprint(os.Stderr, colors.Off)
fmt.Fprint(os.Stderr, colors.Red)
fmt.Fprintln(os.Stderr, a...)

} else {
fmt.Fprintln(os.Stderr, a...)
}
Expand Down
7 changes: 2 additions & 5 deletions src/apps/chifra/pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,11 @@ func (mon *Monitor) Remove() (bool, error) {
return !file.FileExists(mon.Path()), nil
}

// SentinalAddr is a marker to signify the end of the monitor list produced by ListMonitors
var SentinalAddr = base.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead")

// ListMonitors puts a list of Monitors into the monitorChannel. The list of monitors is built from
// a file called addresses.tsv in the current folder or, if not present, from existing monitors
func ListMonitors(chain, watchList string, monitorChan chan<- Monitor) {
defer func() {
monitorChan <- Monitor{Address: SentinalAddr}
monitorChan <- Monitor{Address: base.SentinalAddr}
}()

if watchList != "existing" {
Expand Down Expand Up @@ -250,7 +247,7 @@ func GetMonitorMap(chain string) (map[base.Address]*Monitor, []*Monitor) {
for mon := range monitorChan {
mon := mon
switch mon.Address {
case SentinalAddr:
case base.SentinalAddr:
close(monitorChan)
default:
monMap[mon.Address] = &mon
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/pinning/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func FetchFromGateway(ctx context.Context, gateway, hash string) (*FetchResult,
}
if response.StatusCode != 200 {
// logger.Fatalln("DefaultClient.Do returned StatusCode not equal to 200 in FetFromGateway with", url)
return nil, fmt.Errorf("wrong status code: %d", response.StatusCode)
return nil, fmt.Errorf("fetch to %s returned status code: %d", url, response.StatusCode)
}
body := response.Body

Expand Down
8 changes: 2 additions & 6 deletions src/apps/chifra/pkg/pinning/pin_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func PinItem(chain string, dbName, path string, isRemote bool) (hash base.IpfsHa
}

if isLocal {
logger.Info("Pinning", dbName, "to local service")
localService, _ := NewPinningService(chain, Local)
if hash, err = localService.PinFile(path, true); err != nil {
err = fmt.Errorf("error pinning locally: %s", err)
Expand All @@ -45,10 +44,9 @@ func PinItem(chain string, dbName, path string, isRemote bool) (hash base.IpfsHa
}

if isRemote {
logger.Info("Pinning", dbName, "to remote service")
remoteService, _ := NewPinningService(chain, Pinata)
var remoteHash base.IpfsHash
if remoteHash, err = remoteService.PinFile(path, true); err != nil {
if remoteHash, err = remoteService.PinFile(path, false); err != nil {
err = fmt.Errorf("error pinning remotely: %s", err)
return
}
Expand All @@ -60,7 +58,7 @@ func PinItem(chain string, dbName, path string, isRemote bool) (hash base.IpfsHa
}
}

logger.Info("Pinned", dbName, "file", path, "to", hash)
logger.Info(colors.Magenta+"Pinned", dbName, "file", path, "to", hash, colors.Off)
return
}

Expand All @@ -77,7 +75,6 @@ func PinChunk(chain, bloomFile, indexFile string, isRemote bool) (PinResult, err

isLocal := LocalDaemonRunning()
if isLocal {
// logger.Info(colors.Magenta+"Pinning locally...", colors.Off)
if result.Local.BloomHash, result.err = localService.PinFile(bloomFile, true); result.err != nil {
return PinResult{}, result.err
}
Expand All @@ -90,7 +87,6 @@ func PinChunk(chain, bloomFile, indexFile string, isRemote bool) (PinResult, err
}

if isRemote {
logger.Info(colors.Magenta+"Pinning remotely...", colors.Off)
if result.Remote.BloomHash, result.err = remoteService.PinFile(bloomFile, false); result.err != nil {
return PinResult{}, result.err
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/prefunds/prefunds.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Allocation struct {
}

// emptyAllocs is a list of empty allocations. We use this to return at least one allocation
var emptyAllocs = []Allocation{{Address: base.HexToAddress("0x0"), Balance: *big.NewInt(0)}}
var emptyAllocs = []Allocation{{Address: base.ZeroAddr, Balance: *big.NewInt(0)}}

type allocCallback func(*Allocation, *any) (bool, error)

Expand Down

0 comments on commit 953f2ae

Please sign in to comment.