Skip to content

Commit

Permalink
Merge pull request #3453 from TrueBlocks/fix/accounting-2
Browse files Browse the repository at this point in the history
Fix/accounting 2
  • Loading branch information
tjayrush authored Dec 10, 2023
2 parents 51ca8e4 + 3e2a3f3 commit f512777
Show file tree
Hide file tree
Showing 34 changed files with 319 additions and 266 deletions.
26 changes: 13 additions & 13 deletions src/apps/chifra/internal/export/handle_accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
)

func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) error {
if opts.Accounting {
// TODO: BOGUS - RECONSIDER THIS
opts.Articulate = true
}
// TODO: BOGUS - RECONSIDER THIS
opts.Articulate = true

ledgers := &ledger.Ledger{}
chain := opts.Globals.Chain
Expand All @@ -42,6 +40,7 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
if tx, err := opts.Conn.GetTransactionByAppearance(app, false); err != nil {
errorChan <- err
return nil

} else {
passes, _ := filter.ApplyTxFilters(tx)
if passes {
Expand All @@ -51,12 +50,11 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
}
}

if opts.Accounting {
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err
} else {
tx.Statements = &statements
}
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err

} else {
tx.Statements = &statements
}

modelChan <- tx
Expand All @@ -69,6 +67,7 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
if apps, cnt, err := mon.ReadAndFilterAppearances(filter, true /* withCount */); err != nil {
errorChan <- err
return

} else if !opts.NoZero || cnt > 0 {
ledgers = ledger.NewLedger(
opts.Conn,
Expand All @@ -79,11 +78,11 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
testMode,
opts.NoZero,
opts.Traces,
opts.Reversed,
&opts.Asset,
)
if opts.Accounting {
_ = ledgers.SetContexts(chain, apps, filter.GetOuterBounds())
}

_ = ledgers.SetContexts(chain, apps, filter.GetOuterBounds())

for _, app := range apps {
app := app
Expand All @@ -92,6 +91,7 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
return
}
}

} else {
errorChan <- fmt.Errorf("no appearances found for %s", mon.Address.Hex())
continue
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/export/handle_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (opts *ExportOptions) HandleBalances(monitorArray []monitor.Monitor) error

iterFunc := func(app types.SimpleAppearance, value *types.SimpleToken) error {
var balance *big.Int
if balance, err = opts.Conn.GetBalanceByAppearance(mon.Address, &app); err != nil {
if balance, err = opts.Conn.GetBalanceAt(mon.Address, uint64(app.BlockNumber)); err != nil {
return err
}
value.Address = base.FAKE_ETH_ADDRESS
Expand Down
20 changes: 9 additions & 11 deletions src/apps/chifra/internal/export/handle_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ func (opts *ExportOptions) HandleStatements(monitorArray []monitor.Monitor) erro
return txArray[i].BlockNumber < txArray[j].BlockNumber
})

items := make([]types.SimpleStatement, 0, len(thisMap))
apps := make([]types.SimpleAppearance, 0, len(thisMap))
for _, tx := range txArray {
apps = append(apps, types.SimpleAppearance{
BlockNumber: uint32(tx.BlockNumber),
TransactionIndex: uint32(tx.TransactionIndex),
})
}

chain := opts.Globals.Chain
testMode := opts.Globals.TestMode
ledgers := ledger.NewLedger(
opts.Conn,
mon.Address,
Expand All @@ -111,19 +115,13 @@ func (opts *ExportOptions) HandleStatements(monitorArray []monitor.Monitor) erro
testMode,
opts.NoZero,
opts.Traces,
opts.Reversed,
&opts.Asset,
)

apps := make([]types.SimpleAppearance, 0, len(thisMap))
for _, tx := range txArray {
apps = append(apps, types.SimpleAppearance{
BlockNumber: uint32(tx.BlockNumber),
TransactionIndex: uint32(tx.TransactionIndex),
})
}
_ = ledgers.SetContexts(chain, apps, filter.GetOuterBounds())

// we need them sorted for the following to work
items := make([]types.SimpleStatement, 0, len(thisMap))
for _, tx := range txArray {
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/tokens/handle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (opts *TokensOptions) HandleShow() error {
}

for _, bn := range blockNums {
if bal, err := opts.Conn.GetTokenBalanceAt(tokenAddr, addr, fmt.Sprintf("0x%x", bn)); bal == nil {
if bal, err := opts.Conn.GetBalanceAtToken(tokenAddr, addr, fmt.Sprintf("0x%x", bn)); bal == nil {
errorChan <- err
} else {
if opts.Globals.Verbose {
Expand Down
6 changes: 4 additions & 2 deletions src/apps/chifra/internal/transactions/handle_accountfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (opts *TransactionsOptions) HandleAccounting() (err error) {
chain := opts.Globals.Chain
testMode := opts.Globals.TestMode
ether := opts.Globals.Ether
noZero := false // opts.Globals.NoZero

filter := filter.NewFilter(
false,
false,
Expand All @@ -33,8 +33,9 @@ func (opts *TransactionsOptions) HandleAccounting() (err error) {
utils.NOPOS,
ether,
testMode,
noZero,
false, // opts.Globals.NoZero
opts.Traces,
false, /* reversed */
nil,
)
_ = ledgers.SetContextsFromIds(chain, opts.TransactionIds)
Expand All @@ -61,6 +62,7 @@ func (opts *TransactionsOptions) HandleAccounting() (err error) {
} else {
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err

} else {
for _, statement := range statements {
statement := statement
Expand Down
132 changes: 75 additions & 57 deletions src/apps/chifra/pkg/ledger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
type reconType int

const (
genesis reconType = iota
invalid reconType = iota
genesis
diffDiff
sameSame
diffSame
Expand All @@ -35,68 +36,74 @@ func (r reconType) String() string {
return "diff-same"
case sameDiff:
return "same-diff"
case shouldNotHappen:
return "should-not-happen"
default:
return "unknown"
return "invalid"
}
}

type ledgerContextKey string

// ledgerContext is a struct to hold the context of a reconciliation (i.e., its
// previous and next blocks and whether they are different)
type ledgerContext struct {
PrevBlock base.Blknum
CurBlock base.Blknum
NextBlock base.Blknum
// IsPrevDiff bool
// IsNextDiff bool
ReconType reconType
}

func (c *ledgerContext) Prev() base.Blknum {
return c.PrevBlock
}

func (c *ledgerContext) Cur() base.Blknum {
return c.CurBlock
}

func (c *ledgerContext) Next() base.Blknum {
return c.NextBlock
}

func newLedgerContext(prev, cur, next base.Blknum) *ledgerContext {
c := &ledgerContext{
PrevBlock: prev,
CurBlock: cur,
NextBlock: next,
func newLedgerContext(prev, cur, next base.Blknum, reversed bool) *ledgerContext {
if prev > cur || cur > next {
return &ledgerContext{
ReconType: invalid,
}
}
c.ReconType = c.getReconType(prev != cur, cur != next)
return c
}

func (c *ledgerContext) getReconType(prevDiff, nextDiff bool) reconType {
if c.CurBlock == 0 {
return genesis
reconType := invalid
if cur == 0 {
reconType = genesis
} else {
prevDiff := prev != cur
nextDiff := cur != next
if prevDiff && nextDiff {
return diffDiff
reconType = diffDiff
} else if !prevDiff && !nextDiff {
return sameSame
reconType = sameSame
} else if prevDiff {
return diffSame
reconType = diffSame
} else if nextDiff {
return sameDiff
reconType = sameDiff
} else {
return shouldNotHappen
reconType = invalid
logger.Panic("should not happen")
}
}

return &ledgerContext{
PrevBlock: prev,
CurBlock: cur,
NextBlock: next,
ReconType: reconType,
// Reversed: reversed,
}
}

func (c *ledgerContext) Prev() base.Blknum {
return c.PrevBlock
}

func (l *Ledger) ctxKey(bn, txid uint64) string {
func (c *ledgerContext) Cur() base.Blknum {
return c.CurBlock
}

func (c *ledgerContext) Next() base.Blknum {
return c.NextBlock
}

func (l *Ledger) ctxKey(bn, txid uint64) ledgerContextKey {
// TODO: Is having the context per asset necessary?
// return fmt.Sprintf("%s-%09d-%05d", l.AccountFor.Hex(), bn, txid)
return fmt.Sprintf("%09d-%05d", bn, txid)
return ledgerContextKey(fmt.Sprintf("%09d-%05d", bn, txid))
}

const maxTestingBlock = 17000000
Expand All @@ -123,28 +130,10 @@ func (l *Ledger) SetContexts(chain string, apps []types.SimpleAppearance, outerB
}

key := l.ctxKey(uint64(apps[i].BlockNumber), uint64(apps[i].TransactionIndex))
l.Contexts[key] = *newLedgerContext(base.Blknum(prev), base.Blknum(cur), base.Blknum(next))
}

if l.TestMode {
keys := []string{}
for key := range l.Contexts {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
c := l.Contexts[key]
if c.CurBlock > maxTestingBlock {
continue
}
msg := ""
if c.ReconType == sameSame {
msg = fmt.Sprintf(" %12.12s false false", c.ReconType)
}
logger.Info(fmt.Sprintf("%s: % 10d % 10d % 11d%s", key, c.PrevBlock, c.CurBlock, c.NextBlock, msg))
}
l.Contexts[key] = newLedgerContext(base.Blknum(prev), base.Blknum(cur), base.Blknum(next), l.Reversed)
}

l.DebugContext()
return nil
}

Expand All @@ -167,8 +156,37 @@ func (l *Ledger) SetContextsFromIds(chain string, txIds []identifiers.Identifier
next := apps[i].BlockNumber + 1

key := l.ctxKey(uint64(apps[i].BlockNumber), uint64(apps[i].TransactionIndex))
l.Contexts[key] = *newLedgerContext(base.Blknum(prev), base.Blknum(cur), base.Blknum(next))
l.Contexts[key] = newLedgerContext(base.Blknum(prev), base.Blknum(cur), base.Blknum(next), l.Reversed)
}
}

l.DebugContext()
return nil
}

func (l *Ledger) DebugContext() {
if !l.TestMode {
return
}

keys := make([]ledgerContextKey, 0, len(l.Contexts))
for key := range l.Contexts {
keys = append(keys, key)
}

sort.Slice(keys, func(i, j int) bool {
return string(keys[i]) < string(keys[j])
})

for _, key := range keys {
c := l.Contexts[key]
if c.CurBlock > maxTestingBlock {
continue
}
msg := ""
if c.ReconType == sameSame {
msg = fmt.Sprintf(" %12.12s false false", c.ReconType)
}
logger.Info(fmt.Sprintf("%s: % 10d % 10d % 11d%s", key, c.PrevBlock, c.CurBlock, c.NextBlock, msg))
}
}
33 changes: 25 additions & 8 deletions src/apps/chifra/pkg/ledger/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@ package ledger

import (
"testing"

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

func TestLedgerContext(t *testing.T) {
expected := ledgerContext{
PrevBlock: 12,
CurBlock: 13,
NextBlock: 14,
ReconType: diffDiff,
tests := []struct {
prev base.Blknum
cur base.Blknum
next base.Blknum
reversed bool
expected reconType
}{
{0, 0, 0, false, genesis},
{0, 0, 1, false, genesis},
{1, 0, 1, false, invalid},
{12, 13, 14, false, diffDiff},
{12, 12, 13, false, sameDiff},
{12, 13, 13, false, diffSame},
{12, 12, 12, false, sameSame},
{10, 9, 9, false, invalid},
{10, 10, 9, false, invalid},
{10, 9, 8, false, invalid},
}
got := newLedgerContext(12, 13, 14)
if *got != expected {
t.Error("expected:", expected, "got:", got)

for _, test := range tests {
got := newLedgerContext(test.prev, test.cur, test.next, test.reversed)
if got.ReconType != test.expected {
t.Error("expected:", test.expected, "got:", got.ReconType)
}
}
}
Loading

0 comments on commit f512777

Please sign in to comment.