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

Bugfix/names clean reporting tests #2944

Merged
merged 3 commits into from
May 29, 2023
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
61 changes: 38 additions & 23 deletions src/apps/chifra/internal/names/handle_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
Expand All @@ -27,15 +26,21 @@ func (opts *NamesOptions) HandleClean() error {
label = "regular"
db = names.DatabaseRegular
}
logger.Info("Processing", label, "names file", "("+names.GetDatabasePath(opts.Globals.Chain, db)+")")
sourcePath := names.GetDatabasePath(opts.Globals.Chain, db)
logger.Info("Processing", label, "names file", "("+sourcePath+")")
destinationLabel := sourcePath
if opts.DryRun {
destinationLabel = "standard output"
}
logger.Info("Writing results to", destinationLabel)

var message string
err := opts.cleanNames()
modifiedCount, err := opts.cleanNames()
if err != nil {
message = fmt.Sprintf("The %s names database was not cleaned", label)
logger.Warn(message)
} else {
message = fmt.Sprintf("The %s names database was cleaned", label)
message = fmt.Sprintf("The %s names database was cleaned. %d name(s) has been modified", label, modifiedCount)
logger.Info(message)
}

Expand All @@ -49,7 +54,7 @@ func (opts *NamesOptions) HandleClean() error {
return err
}

func (opts *NamesOptions) cleanNames() error {
func (opts *NamesOptions) cleanNames() (int, error) {
parts := names.Custom
if opts.Regular {
parts = names.Regular
Expand All @@ -58,30 +63,40 @@ func (opts *NamesOptions) cleanNames() error {
// Load databases
allNames, err := names.LoadNamesMap(opts.Globals.Chain, parts, []string{})
if err != nil {
return err
return 0, err
}
prefundMap, err := preparePrefunds(opts.Globals.Chain)
if err != nil {
return err
return 0, err
}

// Prepare progress reporting. We will report percentage.
total := len(allNames)
var done atomic.Int32
progressChan := make(chan int)
modifiedCount := 0
type Progress struct {
ProgressDelta int32
Modified bool
}
progressChan := make(chan Progress)
defer close(progressChan)
// Listen on a channel and whenever it updates, call `reportProgress`
go func() {
for progress := range progressChan {
doneNow := done.Add(int32(progress))
doneNow := done.Add(progress.ProgressDelta)
if progress.Modified {
modifiedCount += int(progress.ProgressDelta)
}
logger.PctProgress(doneNow, total, 10)
}
}()

// If nothing gets modified we won't bother with saving the files
var anyNameModified bool
// We'll use once to set `anyNameModified` from goroutines
var onceMod sync.Once
defer func() {
// Clean line after progress report.
if done.Load() > 0 {
logger.CleanLine()
}
}()

// For --dry_run, we don't want to write to the real database
var overrideDatabase names.Database
Expand All @@ -102,16 +117,16 @@ func (opts *NamesOptions) cleanNames() error {
modified = true
}

progressChan <- 1
progressChan <- Progress{
ProgressDelta: 1,
Modified: modified,
}

if !modified {
return nil
}

// The name has been modified, so set the flag and...
onceMod.Do(func() { anyNameModified = true })

// ...update names in-memory cache
// update names in-memory cache
if opts.Regular {
if err = names.UpdateRegularName(&name); err != nil {
return wrapErrorWithAddr(&address, err)
Expand All @@ -127,20 +142,20 @@ func (opts *NamesOptions) cleanNames() error {
// Block until we get an error from any of the iterations or the iteration finishes
if stepErr := <-errorChannel; stepErr != nil {
cancel()
return stepErr
return 0, stepErr
}

// If nothing has been changed, we can exit here
if !anyNameModified {
return nil
if modifiedCount == 0 {
return 0, nil
}

// Write to disk
if opts.Regular {
return names.WriteRegularNames(opts.Globals.Chain, overrideDatabase)
return modifiedCount, names.WriteRegularNames(opts.Globals.Chain, overrideDatabase)
}

return names.WriteCustomNames(opts.Globals.Chain, overrideDatabase)
return modifiedCount, names.WriteCustomNames(opts.Globals.Chain, overrideDatabase)
}

// wrapErrorWithAddr prepends `err` with `address`, so that we can learn which name caused troubles
Expand Down
5 changes: 5 additions & 0 deletions src/apps/chifra/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func Progress(tick bool, v ...any) {
toLog(progress, v...)
}

func CleanLine() {
// \033[K is escape sequence meaning "erase to end of line"
fmt.Print("\r\033[K")
}

func PctProgress(done int32, total int, tick int32) {
if done%tick != 0 {
return
Expand Down
32 changes: 16 additions & 16 deletions src/apps/chifra/pkg/token/token_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestGetState_Erc20(t *testing.T) {
t.Fatal("wrong decimals:", token.Decimals)
}

// if token.TotalSupply != "9118918230822796234900723527" {
// t.Fatal("wrong total supply:", token.TotalSupply)
// }
if token.TotalSupply != "9118918230822796234900723527" {
t.Fatal("wrong total supply:", token.TotalSupply)
}
}

func TestGetState_Erc721(t *testing.T) {
Expand Down Expand Up @@ -87,13 +87,13 @@ func TestGetState_Erc721(t *testing.T) {
t.Fatal("NFT should not have decimals set:", token.Decimals)
}

// if token.TotalSupply != "10000" {
// t.Fatal("wrong total supply:", token.TotalSupply)
// }
if token.TotalSupply != "10000" {
t.Fatal("wrong total supply:", token.TotalSupply)
}
}

func TestGetState_NonStandard(t *testing.T) {
blockNumber := "latest"
blockNumber := "0x1036640" // 17000000
chain := utils.GetTestChain()

token, err := GetState(chain, nonStandard1, blockNumber)
Expand Down Expand Up @@ -121,9 +121,9 @@ func TestGetState_NonStandard(t *testing.T) {
t.Fatal("wrong decimals:", token.Decimals)
}

// if token.TotalSupply != "7069797008171168928213" {
// t.Fatal("wrong total supply:", token.TotalSupply)
// }
if token.TotalSupply != "7069797008171168928213" {
t.Fatal("wrong total supply:", token.TotalSupply)
}

// Non-standard 2

Expand Down Expand Up @@ -152,9 +152,9 @@ func TestGetState_NonStandard(t *testing.T) {
t.Fatal("wrong decimals:", token.Decimals)
}

// if token.TotalSupply != "210000000000000000000000000" {
// t.Fatal("wrong total supply:", token.TotalSupply)
// }
if token.TotalSupply != "210000000000000000000000000" {
t.Fatal("wrong total supply:", token.TotalSupply)
}

// 3
nonStandard3 := base.HexToAddress("0xc4e0f3ec24972c75df7c716922096f4270b7bb4e")
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestGetState_NonStandard(t *testing.T) {
t.Fatal("wrong decimals:", token.Decimals)
}

// if token.TotalSupply != "" {
// t.Fatal("wrong total supply:", token.TotalSupply)
// }
if token.TotalSupply != "" {
t.Fatal("wrong total supply:", token.TotalSupply)
}
}