Skip to content

Commit

Permalink
Merge pull request #3452 from TrueBlocks/fix/accounting-1
Browse files Browse the repository at this point in the history
Fix/accounting 1
  • Loading branch information
tjayrush authored Dec 8, 2023
2 parents 4498bee + b577490 commit 51ca8e4
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/export/handle_accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (opts *ExportOptions) HandleAccounting(monitorArray []monitor.Monitor) erro
}

if opts.Accounting {
if statements, err := ledgers.GetStatementsFromAppearance(opts.Conn, filter, app); err != nil {
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err
} else {
tx.Statements = &statements
Expand Down
15 changes: 7 additions & 8 deletions src/apps/chifra/internal/export/handle_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (opts *ExportOptions) HandleStatements(monitorArray []monitor.Monitor) erro
return txArray[i].BlockNumber < txArray[j].BlockNumber
})

items := make([]*types.SimpleStatement, 0, len(thisMap))
items := make([]types.SimpleStatement, 0, len(thisMap))

chain := opts.Globals.Chain
testMode := opts.Globals.TestMode
Expand All @@ -125,12 +125,11 @@ func (opts *ExportOptions) HandleStatements(monitorArray []monitor.Monitor) erro

// we need them sorted for the following to work
for _, tx := range txArray {
ledgers.Tx = tx // we need this below
if stmts := ledgers.GetStatementsFromTransaction(opts.Conn, filter, tx); len(stmts) > 0 {
for _, statement := range stmts {
statement := statement
items = append(items, statement)
}
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err

} else if len(statements) > 0 {
items = append(items, statements...)
}
}

Expand All @@ -152,7 +151,7 @@ func (opts *ExportOptions) HandleStatements(monitorArray []monitor.Monitor) erro
var passes bool
passes, finished = filter.ApplyCountFilter()
if passes {
modelChan <- item
modelChan <- &item
}
if finished {
break
Expand Down
15 changes: 11 additions & 4 deletions src/apps/chifra/internal/transactions/handle_accountfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ func (opts *TransactionsOptions) HandleAccounting() (err error) {
BlockNumber: raw.BlockNumber,
TransactionIndex: raw.TransactionIndex,
}
if statements, err := ledgers.GetStatementsFromAppearance(opts.Conn, filter, &app); err != nil {

if tx, err := opts.Conn.GetTransactionByAppearance(&app, false); err != nil {
errorChan <- err
cancel()

} else {
for _, statement := range statements {
statement := statement
modelChan <- &statement
if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
errorChan <- err
} else {
for _, statement := range statements {
statement := statement
modelChan <- &statement
}
}
}
}
Expand Down
101 changes: 63 additions & 38 deletions src/apps/chifra/pkg/ledger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,89 @@ import (
"github.com/ethereum/go-ethereum"
)

// LedgerContext is a struct to hold the context of a reconciliation (i.e., its
type reconType int

const (
genesis reconType = iota
diffDiff
sameSame
diffSame
sameDiff
shouldNotHappen
)

func (r reconType) String() string {
switch r {
case genesis:
return "genesis"
case diffDiff:
return "diff-diff"
case sameSame:
return "same-same"
case diffSame:
return "diff-same"
case sameDiff:
return "same-diff"
case shouldNotHappen:
return "should-not-happen"
default:
return "unknown"
}
}

// 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 string
type ledgerContext struct {
PrevBlock base.Blknum
CurBlock base.Blknum
NextBlock base.Blknum
// IsPrevDiff bool
// IsNextDiff bool
ReconType reconType
}

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

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

func (c *LedgerContext) Next() base.Blknum {
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,
IsPrevDiff: prev != cur,
IsNextDiff: cur != next,
func newLedgerContext(prev, cur, next base.Blknum) *ledgerContext {
c := &ledgerContext{
PrevBlock: prev,
CurBlock: cur,
NextBlock: next,
}
c.ReconType = c.getReconType()
c.ReconType = c.getReconType(prev != cur, cur != next)
return c
}

func (c *LedgerContext) getReconType() (reconType string) {
func (c *ledgerContext) getReconType(prevDiff, nextDiff bool) reconType {
if c.CurBlock == 0 {
c.IsPrevDiff = true
return "genesis"
return genesis
} else {
if c.IsPrevDiff && c.IsNextDiff {
return "diff-diff"
} else if !c.IsPrevDiff && !c.IsNextDiff {
return "same-same"
} else if c.IsPrevDiff {
return "diff-same"
} else if c.IsNextDiff {
return "same-diff"
if prevDiff && nextDiff {
return diffDiff
} else if !prevDiff && !nextDiff {
return sameSame
} else if prevDiff {
return diffSame
} else if nextDiff {
return sameDiff
} else {
return "should-not-happen"
return shouldNotHappen
}
}
}

func (l *Ledger) ctxKey(bn, txid uint64) string {
// 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)
}
Expand All @@ -79,9 +107,6 @@ const maxTestingBlock = 17000000
func (l *Ledger) SetContexts(chain string, apps []types.SimpleAppearance, outerBounds base.BlockRange) error {
for i := 0; i < len(apps); i++ {
cur := apps[i].BlockNumber
// if cur > maxTestingBlock {
// continue
// }

prev := outerBounds.First
if i > 0 {
Expand All @@ -98,7 +123,7 @@ 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))
l.Contexts[key] = *newLedgerContext(base.Blknum(prev), base.Blknum(cur), base.Blknum(next))
}

if l.TestMode {
Expand All @@ -113,8 +138,8 @@ func (l *Ledger) SetContexts(chain string, apps []types.SimpleAppearance, outerB
continue
}
msg := ""
if !c.IsPrevDiff || !c.IsNextDiff {
msg = fmt.Sprintf(" %12.12s %t %t", c.ReconType, c.IsPrevDiff, c.IsNextDiff)
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))
}
Expand Down Expand Up @@ -142,7 +167,7 @@ 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))
}
}
return nil
Expand Down
22 changes: 22 additions & 0 deletions src/apps/chifra/pkg/ledger/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 The TrueBlocks Authors. All rights reserved.
// Use of this source code is governed by a license that can
// be found in the LICENSE file.

package ledger

import (
"testing"
)

func TestLedgerContext(t *testing.T) {
expected := ledgerContext{
PrevBlock: 12,
CurBlock: 13,
NextBlock: 14,
ReconType: diffDiff,
}
got := newLedgerContext(12, 13, 14)
if *got != expected {
t.Error("expected:", expected, "got:", got)
}
}
4 changes: 2 additions & 2 deletions src/apps/chifra/pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Ledger struct {
LastBlock base.Blknum
Names map[base.Address]types.SimpleName
TestMode bool
Contexts map[string]LedgerContext
Contexts map[string]ledgerContext
AsEther bool
NoZero bool
UseTraces bool
Expand All @@ -35,7 +35,7 @@ func NewLedger(conn *rpc.Connection, acctFor base.Address, fb, lb base.Blknum, a
AccountFor: acctFor,
FirstBlock: fb,
LastBlock: lb,
Contexts: make(map[string]LedgerContext),
Contexts: make(map[string]ledgerContext),
AsEther: asEther,
TestMode: testMode,
NoZero: noZero,
Expand Down
32 changes: 0 additions & 32 deletions src/apps/chifra/pkg/ledger/stmnt_from_app.go

This file was deleted.

Loading

0 comments on commit 51ca8e4

Please sign in to comment.