Skip to content

Commit

Permalink
Smalling cleanings leading to new Reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Feb 17, 2025
1 parent 5b044b9 commit 628a749
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 169 deletions.
16 changes: 12 additions & 4 deletions src/apps/chifra/internal/export/handle_accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (opts *ExportOptions) HandleAccounting(rCtx *output.RenderCtx, monitorArray
)

fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
visitAppearance := func(app *types.Appearance) error {
visitAppearance := func(prev, next base.Blknum, app *types.Appearance) error {
if tx, err := opts.Conn.GetTransactionByAppearance(app, false); err != nil {
errorChan <- err
return nil
Expand All @@ -46,7 +46,7 @@ func (opts *ExportOptions) HandleAccounting(rCtx *output.RenderCtx, monitorArray
}
}

if statements, err := ledgers.GetStatements(filter, tx); err != nil {
if statements, err := ledgers.GetStatements(prev, next, filter, tx); err != nil {
errorChan <- err

} else {
Expand Down Expand Up @@ -79,8 +79,16 @@ func (opts *ExportOptions) HandleAccounting(rCtx *output.RenderCtx, monitorArray
&opts.Asset,
)

for _, app := range apps {
if err := visitAppearance(&app); err != nil {
for i, app := range apps {
prev := uint32(0)
if i > 0 {
prev = apps[i-1].BlockNumber
}
next := app.BlockNumber + 1
if i < len(apps)-1 {
next = apps[i+1].BlockNumber
}
if err := visitAppearance(base.Blknum(prev), base.Blknum(next), &app); err != nil {
errorChan <- err
return
}
Expand Down
13 changes: 11 additions & 2 deletions src/apps/chifra/internal/export/handle_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,17 @@ func (opts *ExportOptions) HandleStatements(rCtx *output.RenderCtx, monitorArray
)

items := make([]types.Statement, 0, len(thisMap))
for _, tx := range txArray {
if statements, err := ledgers.GetStatements(filter, tx); err != nil {
for i, tx := range txArray {
// Note: apps and txArray are the same list, so we can use the index from txArray
prev := uint32(0)
if i > 0 {
prev = apps[i-1].BlockNumber
}
next := apps[i].BlockNumber + 1
if i < len(apps)-1 {
next = apps[i+1].BlockNumber
}
if statements, err := ledgers.GetStatements(base.Blknum(prev), base.Blknum(next), filter, tx); err != nil {
errorChan <- err

} else if len(statements) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions src/apps/chifra/pkg/ledger/stmnt_from_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
)

// GetStatements returns a statement from a given transaction
func (l *Ledger) GetStatements(filter *filter.AppearanceFilter, trans *types.Transaction) ([]types.Statement, error) {
func (l *Ledger) GetStatements(prev, next base.Blknum, filter *filter.AppearanceFilter, trans *types.Transaction) ([]types.Statement, error) {
if os.Getenv("NEW_CODE") == "true" {
r := ledger2.NewReconciler(l.accountFor)
return r.GetStatements(filter, trans)
r := ledger2.NewReconciler(l.connection, l.accountFor, l.names, l.asEther)
return r.GetStatements(prev, next, filter, trans)

} else {
// We need this below...
Expand Down
6 changes: 3 additions & 3 deletions src/apps/chifra/pkg/ledger2/asset_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type AssetTransfer struct {
BlockNumber base.Blknum
TransactionIndex base.Txnum
AssetAddr base.Address
AssetAddress base.Address
AssetName string
Amount base.Wei
Index string
Expand Down Expand Up @@ -39,7 +39,7 @@ func NewAssetTransfer(
return AssetTransfer{
BlockNumber: blockNumber,
TransactionIndex: txIndex,
AssetAddr: assetAddress,
AssetAddress: assetAddress,
AssetName: assetName,
Amount: amount,
Index: index,
Expand All @@ -54,7 +54,7 @@ func (at *AssetTransfer) String() string {
"AssetTransfer(Block=%d Tx=%d Asset=%s Amount=%s Index=%s From=%s To=%s)",
at.BlockNumber,
at.TransactionIndex,
at.AssetAddr,
at.AssetAddress,
at.Amount.String(),
at.Index,
at.FromAddress.Hex(),
Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/pkg/ledger2/asset_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestNewAssetTransfer(t *testing.T) {
if at.TransactionIndex != txIndex {
t.Fatalf("TransactionIndex mismatch. got=%d want=%d", at.TransactionIndex, txIndex)
}
if at.AssetAddr != assetAddr {
t.Fatalf("AssetAddr mismatch. got=%s want=%s", at.AssetAddr, assetAddr)
if at.AssetAddress != assetAddr {
t.Fatalf("AssetAddress mismatch. got=%s want=%s", at.AssetAddress, assetAddr)
}
if at.AssetName != assetName {
t.Fatalf("AssetName mismatch. got=%s want=%s", at.AssetName, assetName)
Expand Down
22 changes: 11 additions & 11 deletions src/apps/chifra/pkg/ledger2/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
// Filter is used to select a subset of ledger data, such as a particular
// asset address, or a specific block range.
type Filter struct {
AssetAddr string
MinBlock base.Blknum
MaxBlock base.Blknum
AssetAddress string
MinBlock base.Blknum
MaxBlock base.Blknum
}

// NewFilter returns a Filter with the given asset address, minimum block, and maximum block.
func NewFilter(assetAddress string, minBlock, maxBlock base.Blknum) Filter {
return Filter{
AssetAddr: assetAddress,
MinBlock: minBlock,
MaxBlock: maxBlock,
AssetAddress: assetAddress,
MinBlock: minBlock,
MaxBlock: maxBlock,
}
}

// String returns a human-readable description of this Filter.
func (f *Filter) String() string {
return "Filter(Asset=" + f.AssetAddr +
return "Filter(Asset=" + f.AssetAddress +
" MinBlock=" + fmt.Sprintf("%d", f.MinBlock) +
" MaxBlock=" + fmt.Sprintf("%d", f.MaxBlock) + ")"
}
Expand All @@ -35,11 +35,11 @@ func (f *Filter) IsBlockInRange(block base.Blknum) bool {
return block >= f.MinBlock && (f.MaxBlock == 0 || block <= f.MaxBlock)
}

// MatchesAsset returns true if the given asset matches this filter's AssetAddr.
// If the filter's AssetAddr is empty, any asset matches.
// MatchesAsset returns true if the given asset matches this filter's AssetAddress.
// If the filter's AssetAddress is empty, any asset matches.
func (f *Filter) MatchesAsset(assetAddress string) bool {
if f.AssetAddr == "" {
if f.AssetAddress == "" {
return true
}
return assetAddress == f.AssetAddr
return assetAddress == f.AssetAddress
}
4 changes: 2 additions & 2 deletions src/apps/chifra/pkg/ledger2/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func TestNewFilter(t *testing.T) {
f := NewFilter("0xAsset", 100, 200)
if f.AssetAddr != "0xAsset" {
t.Fatalf("Expected AssetAddr=0xAsset, got=%s", f.AssetAddr)
if f.AssetAddress != "0xAsset" {
t.Fatalf("Expected AssetAddress=0xAsset, got=%s", f.AssetAddress)
}
if f.MinBlock != 100 {
t.Fatalf("Expected MinBlock=100, got=%d", f.MinBlock)
Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/pkg/ledger2/get_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

func (r *Reconciler) GetStatements(filter *filter.AppearanceFilter, trans *types.Transaction) ([]types.Statement, error) {
func (r *Reconciler) GetStatements(prev, next base.Blknum, filter *filter.AppearanceFilter, trans *types.Transaction) ([]types.Statement, error) {
apps := []types.Appearance{
{Address: r.LedgerBook.AccountedFor, BlockNumber: uint32(trans.BlockNumber), TransactionIndex: uint32(trans.TransactionIndex)},
}
Expand All @@ -27,7 +27,7 @@ func (lb *LedgerBook) Statements() ([]types.Statement, error) {
for _, posting := range entry.Postings {
s := types.Statement{
AccountedFor: base.Address(lb.AccountedFor),
AssetAddr: base.Address(l.AssetAddr),
AssetAddr: base.Address(l.AssetAddress),
AssetSymbol: l.AssetName,
BlockNumber: posting.BlockNumber,
TransactionIndex: posting.TransactionIndex,
Expand Down
16 changes: 8 additions & 8 deletions src/apps/chifra/pkg/ledger2/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (

// Ledger is a collection of LedgerEntry items that refer to a single asset.
type Ledger struct {
AssetAddr base.Address
AssetName string
Entries []LedgerEntry
AssetAddress base.Address
AssetName string
Entries []LedgerEntry
}

// NewLedger creates a Ledger with the given asset address and asset name.
func NewLedger(assetAddress base.Address, assetName string) Ledger {
return Ledger{
AssetAddr: assetAddress,
AssetName: assetName,
Entries: make([]LedgerEntry, 0),
AssetAddress: assetAddress,
AssetName: assetName,
Entries: make([]LedgerEntry, 0),
}
}

Expand All @@ -28,8 +28,8 @@ func (l *Ledger) String() string {
totOut := l.TotalOut()
net := l.NetValue()
return fmt.Sprintf(
"Ledger(AssetAddr=%s AssetName=%s Entries=%d In=%s Out=%s Net=%s)",
l.AssetAddr,
"Ledger(AssetAddress=%s AssetName=%s Entries=%d In=%s Out=%s Net=%s)",
l.AssetAddress,
l.AssetName,
len(l.Entries),
totIn.String(),
Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/pkg/ledger2/ledger_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (lb *LedgerBook) String() string {
)
}

// AddLedger inserts a new Ledger into this LedgerBook, keyed by its AssetAddr.
// AddLedger inserts a new Ledger into this LedgerBook, keyed by its AssetAddress.
func (lb *LedgerBook) AddLedger(l Ledger) {
lb.Ledgers[l.AssetAddr.Hex()] = l
lb.Ledgers[l.AssetAddress.Hex()] = l
}

// GetLedger retrieves the Ledger for a given asset address, if it exists.
Expand Down
8 changes: 4 additions & 4 deletions src/apps/chifra/pkg/ledger2/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func TestNewLedger(t *testing.T) {
l := NewLedger(base.HexToAddress("0xABC"), "TEST")
if l.AssetAddr != base.HexToAddress("0xABC") {
t.Fatalf("AssetAddr mismatch. got=%s want=%s", l.AssetAddr, "0xABC")
if l.AssetAddress != base.HexToAddress("0xABC") {
t.Fatalf("AssetAddress mismatch. got=%s want=%s", l.AssetAddress, "0xABC")
}
if l.AssetName != "TEST" {
t.Fatalf("AssetName mismatch. got=%s want=%s", l.AssetName, "TEST")
Expand Down Expand Up @@ -82,8 +82,8 @@ func TestLedgerAggregation(t *testing.T) {
// l.AppendEntry(e)

// got := l.String()
// // We expect: Ledger(AssetAddr=0xLED AssetName=LED Entries=1 In=100 Out=60 Net=40)
// want := "Ledger(AssetAddr=0xLED AssetName=LED Entries=1 In=100 Out=60 Net=40)"
// // We expect: Ledger(AssetAddress=0xLED AssetName=LED Entries=1 In=100 Out=60 Net=40)
// want := "Ledger(AssetAddress=0xLED AssetName=LED Entries=1 In=100 Out=60 Net=40)"
// if got != want {
// t.Fatalf("String mismatch.\ngot: %s\nwant: %s", got, want)
// }
Expand Down
22 changes: 14 additions & 8 deletions src/apps/chifra/pkg/ledger2/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ import (

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

// Reconciler is responsible for processing Appearances, constructing Postings
// and LedgerEntries, appending them to Ledgers, and maintaining a LedgerBook.
type Reconciler struct {
connection *rpc.Connection
names map[base.Address]types.Name
asEther bool

// Typically you'd store references to RPC clients or indexers here,
// but we'll keep it simple for demonstration.
LedgerBook LedgerBook
// mu sync.Mutex
}

// NewReconciler creates a Reconciler for the specified accountedForAddress.
func NewReconciler(accountedForAddress base.Address) Reconciler {
func NewReconciler(conn *rpc.Connection, accountedForAddress base.Address, names map[base.Address]types.Name, asEth bool) Reconciler {
return Reconciler{
LedgerBook: NewLedgerBook(accountedForAddress),
connection: conn,
names: names,
asEther: asEth,
}
}

Expand All @@ -30,8 +38,6 @@ func (r *Reconciler) String() string {
return fmt.Sprintf("Reconciler for %s => %s", r.LedgerBook.AccountedFor, r.LedgerBook.String())
}

// ProcessAppearances takes a list of Appearances and their related AssetTransfers,
// converts them to Postings, and appends them into the appropriate Ledger.
func (r *Reconciler) ProcessAppearances(appearances []types.Appearance, allTransfers []AssetTransfer) {
// We assume allTransfers includes every relevant AssetTransfer for the Appearances.
// In reality, you'd fetch them from an indexer or node calls.
Expand Down Expand Up @@ -64,7 +70,7 @@ func (r *Reconciler) ProcessAppearances(appearances []types.Appearance, allTrans
}

// Build a unique key per asset + app
key := fmt.Sprintf("%s|%s", at.AssetAddr, appID)
key := fmt.Sprintf("%s|%s", at.AssetAddress, appID)

// Create the LedgerEntry if needed
entry, found := entriesMap[key]
Expand Down Expand Up @@ -170,7 +176,7 @@ func DeriveAssetTransfers(accountFor base.Address, tx *types.Transaction) []Asse
TransactionHash: trans.Hash,
LogIndex: 0,
Timestamp: trans.Timestamp,
AssetAddr: base.FAKE_ETH_ADDRESS,
AssetAddress: base.FAKE_ETH_ADDRESS,
AssetSymbol: "WEI",
Decimals: 18,
SpotPrice: 0.0,
Expand Down Expand Up @@ -247,7 +253,7 @@ func DeriveAssetTransfers(accountFor base.Address, tx *types.Transaction) []Asse
results = append(results, AssetTransfer{
BlockNumber: tx.BlockNumber,
TransactionIndex: tx.TransactionIndex,
AssetAddr: base.FAKE_ETH_ADDRESS, // for native chain coin
AssetAddress: base.FAKE_ETH_ADDRESS, // for native chain coin
AssetName: "ETH", // or another name if not Ethereum
Amount: tx.Value,
Index: "nativeVal",
Expand Down Expand Up @@ -303,7 +309,7 @@ func DeriveAssetTransfers(accountFor base.Address, tx *types.Transaction) []Asse
at := AssetTransfer{
BlockNumber: tx.BlockNumber,
TransactionIndex: tx.TransactionIndex,
AssetAddr: lg.Address,
AssetAddress: lg.Address,
AssetName: "ERC20", // you might map the address to a known symbol
Amount: *amount,
Index: logIndexToString(i),
Expand All @@ -325,7 +331,7 @@ func DeriveAssetTransfers(accountFor base.Address, tx *types.Transaction) []Asse
// xf := AssetTransfer{
// BlockNumber: tx.BlockNumber,
// TransactionIndex: tx.TransactionIndex,
// AssetAddr: "0x0",
// AssetAddress: "0x0",
// AssetName: "ETH",
// Amount: *base.NewWei(tr.ValueWei),
// Index: traceIndexToString(i),
Expand Down
Loading

0 comments on commit 628a749

Please sign in to comment.