Skip to content

Commit

Permalink
Improves AddressFromPath
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Feb 11, 2025
1 parent ec20f7e commit 56f7ee1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
11 changes: 4 additions & 7 deletions src/apps/chifra/internal/chunks/handle_truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,12 @@ func (opts *ChunksOptions) HandleTruncate(rCtx *output.RenderCtx, blockNums []ba
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".mon.bin") {
addr, _ := base.AddressFromPath(path, ".mon.bin")
bar.Prefix = fmt.Sprintf("Truncating monitor for %s", addr.Hex())
if !addr.IsZero() {
if addr, err := base.AddressFromPath(path, ".mon.bin"); err == nil && !addr.IsZero() {
bar.Prefix = fmt.Sprintf("Truncating monitor for %s", addr.Hex())
mon, _ := monitor.NewMonitor(chain, addr, false /* create */)
var removed bool
if removed, err = mon.TruncateTo(chain, uint32(latestChunk)); err != nil {
if removed, err := mon.TruncateTo(chain, uint32(latestChunk)); err != nil {
return err
}
if removed {
} else if removed {
nMonitorsTruncated++
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/apps/chifra/internal/monitors/handle_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@ func (opts *MonitorsOptions) HandleList(rCtx *output.RenderCtx) error {
isMonitor := strings.HasSuffix(name, ".mon.bin")
include := isMonitor && (incStaged || !isStaging)
if include {
address, _ := base.AddressFromPath(fn, ".mon.bin")
s := types.Monitor{
Address: address,
NRecords: (file.FileSize(fn) / 8) - 1, // two 32 bit integers and a 32 bit header
FileSize: file.FileSize(fn),
IsStaged: isStaging,
if addr, err := base.AddressFromPath(fn, ".mon.bin"); err == nil && !addr.IsZero() {
s := types.Monitor{
Address: addr,
NRecords: (file.FileSize(fn) / 8) - 1, // two 32 bit integers and a 32 bit header
FileSize: file.FileSize(fn),
IsStaged: isStaging,
}
s.IsEmpty = s.NRecords == 0
if opts.Globals.Verbose {
var mon monitor.Monitor
mon.Address = addr
mon.Staged = isStaging
_ = mon.ReadMonitorHeader()
mon.Close()
s.LastScanned = mon.LastScanned
s.Deleted = mon.Deleted
}
modelChan <- &s
}
s.IsEmpty = s.NRecords == 0
if opts.Globals.Verbose {
var mon monitor.Monitor
mon.Address = address
mon.Staged = isStaging
_ = mon.ReadMonitorHeader()
mon.Close()
s.LastScanned = mon.LastScanned
s.Deleted = mon.Deleted
}
modelChan <- &s
}
return true, nil
}
Expand Down
25 changes: 17 additions & 8 deletions src/apps/chifra/pkg/base/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"log"
"path/filepath"
"strings"

Expand Down Expand Up @@ -64,8 +63,14 @@ func (a *Address) IsZero() bool {
return v == "0x0000000000000000000000000000000000000000"
}

// UnmarshalJSON implements the json.Unmarshaler interface for Address.
// It supports multiple representations of a "zero" or uninitialized address. If the JSON input is
// "0x0", an empty string, or the literal null, the Address is set to ZeroAddr. Otherwise, it delegates
// to the embedded Address type's UnmarshalJSON method to handle a full-length hexadecimal address.
func (e *Address) UnmarshalJSON(data []byte) error {
if string(data) == "\"0x0\"" || string(data) == "\"\"" || string(data) == "null" {
s := string(data)
if s == "\"0x0\"" || s == "\"\"" || s == "null" {
*e = ZeroAddr
return nil
}
return e.Address.UnmarshalJSON(data)
Expand All @@ -79,8 +84,7 @@ func (a *Address) SetCommon(c *common.Address) Address {
return BytesToAddress(c.Bytes())
}

// HexToAddress returns new address with the given string
// as value.
// HexToAddress returns new address with the given string as value.
func HexToAddress(hex string) (addr Address) {
addr.SetHex(hex)
return
Expand Down Expand Up @@ -142,15 +146,17 @@ func AddressFromPath(path, fileType string) (Address, error) {
_, fileName := filepath.Split(path)

if !strings.HasSuffix(fileName, fileType) {
log.Fatal("should not happen ==> path should contain fileType: " + path + " " + fileType)
return ZeroAddr, fmt.Errorf("file name %q does not have the expected file type suffix %q", fileName, fileType)
}

if !strings.HasPrefix(fileType, ".") {
log.Fatal("should not happen ==> fileType should have a leading dot: " + path + " " + fileType)
return ZeroAddr, fmt.Errorf("file type %q should have a leading dot", fileType)
}

// Check that the fileName is long enough to contain a valid address and the fileType.
// A valid address is 42 characters (including "0x"). We require a '.' to separate the address from the file type.
if len(fileName) < (42+len(fileType)) || !strings.HasPrefix(fileName, "0x") || !strings.Contains(fileName, ".") {
return ZeroAddr, errors.New("path does not appear to contain an address")
return ZeroAddr, fmt.Errorf("path %q does not appear to contain a valid address", path)
}

parts := strings.Split(fileName, ".")
Expand Down Expand Up @@ -187,7 +193,10 @@ func IsValidAddress(val string) bool {

func IsValidAddressE(val string) (bool, error) {
if strings.HasSuffix(val, ".eth") {
return strings.Replace(val, "\t\n\r", "", -1) == val, nil
if strings.ContainsAny(val, " \t\n\r") {
return false, nil
}
return true, nil
}
return isValidHex(val, 20)
}
Expand Down
3 changes: 1 addition & 2 deletions src/apps/chifra/pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ func ListExistingMonitors(chain string, monitorChan chan<- Monitor) {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".mon.bin") {
addr, _ := base.AddressFromPath(path, ".mon.bin")
if !addr.IsZero() {
if addr, err := base.AddressFromPath(path, ".mon.bin"); err == nil && !addr.IsZero() {
mon, _ := NewMonitor(chain, addr, true /* create */)
if strings.Contains(path, "staging") {
mon.Staged = true
Expand Down

0 comments on commit 56f7ee1

Please sign in to comment.