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

Include operation information in the ingest.Change struct #5536

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d158205
Include operation information in the Change struct
karthikiyer56 Nov 20, 2024
66abef5
Dont error out when operation index out of range
karthikiyer56 Nov 20, 2024
fb94fdf
Fix failing unit test
karthikiyer56 Nov 21, 2024
06139d3
Reafactor ledger_transaction.GetChanges() to use internal functions
karthikiyer56 Nov 21, 2024
070c552
reformat comments
karthikiyer56 Nov 21, 2024
26780fa
Add all types of change reasons to the Change struct. Simplify Ledger…
karthikiyer56 Nov 21, 2024
b168415
Add LCM and transaction info altogether in change entry
karthikiyer56 Nov 21, 2024
29bc9bc
Wrte help doc for Change Entry
karthikiyer56 Nov 21, 2024
73e4494
Add TxHash to LedgerTransaction struct
karthikiyer56 Nov 21, 2024
c06e241
reorg comments
karthikiyer56 Nov 21, 2024
fe7cef8
Comments cleanup
karthikiyer56 Nov 21, 2024
06c515c
Address PR review changes
karthikiyer56 Nov 25, 2024
01b86b0
rename fields
karthikiyer56 Nov 25, 2024
e363afc
uncommit half baked changes
karthikiyer56 Nov 25, 2024
e2f95d6
Add helpers in intergration tests for creating captive core config
karthikiyer56 Nov 25, 2024
564c3bb
fix updates to change struct
karthikiyer56 Nov 27, 2024
88af498
Updates to integration.go
karthikiyer56 Nov 27, 2024
343373b
Integration tests for change - part 1
karthikiyer56 Nov 27, 2024
c7f37fb
Undo all changes to parameters_test.go
karthikiyer56 Nov 28, 2024
5313aa6
Make updates to comments and rename variables
karthikiyer56 Nov 30, 2024
dccf1e2
- Several changes to integration.go to pull out common functions from…
karthikiyer56 Nov 30, 2024
6d97032
cosmetic doc style changes
karthikiyer56 Nov 30, 2024
adf96c1
check err before file close
karthikiyer56 Nov 30, 2024
fee6685
Add tx test and in change_test.go
karthikiyer56 Dec 1, 2024
157c12e
code cleanup
karthikiyer56 Dec 1, 2024
b82a9dd
Rework test fixtures
karthikiyer56 Dec 3, 2024
3bd80ec
reformat file
karthikiyer56 Dec 3, 2024
e4b3a67
Fix breaking test
karthikiyer56 Dec 3, 2024
c3fbe24
Address code review comments
karthikiyer56 Dec 3, 2024
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
31 changes: 28 additions & 3 deletions ingest/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,35 @@ import (
// If an entry is created: Pre is nil and Post is not nil.
// If an entry is updated: Pre is not nil and Post is not nil.
// If an entry is removed: Pre is not nil and Post is nil.
// If this change is caused by a operation in a transaction, include the operation information. Wont work when changes are compacted
type Change struct {
Type xdr.LedgerEntryType
Pre *xdr.LedgerEntry
Post *xdr.LedgerEntry
Type xdr.LedgerEntryType
Pre *xdr.LedgerEntry
Post *xdr.LedgerEntry
reason LedgerEntryChangeReason
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved
TransactionData *TransactionEnvelopeAndResult
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved
operationInfo *OperationInfo
}

type LedgerEntryChangeReason uint16
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved

const (
Unknown LedgerEntryChangeReason = iota
Operation
Transaction
FeeChange
ProtocolUpgrade
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved
Eviction
)

type TransactionEnvelopeAndResult struct {
Envelope *xdr.TransactionEnvelope
Result *xdr.TransactionResultPair
}

type OperationInfo struct {
operationIdx uint32
operation *xdr.Operation
}

// String returns a best effort string representation of the change.
Expand Down
10 changes: 7 additions & 3 deletions ingest/ledger_change_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ func (r *LedgerChangeReader) Read() (Change, error) {
entry := entries[i]
// when a ledger entry is evicted it is removed from the ledger
changes[i] = Change{
Type: entry.Data.Type,
Pre: &entry,
Post: nil,
Type: entry.Data.Type,
Pre: &entry,
Post: nil,
reason: Eviction,
}
}
sortChanges(changes)
Expand All @@ -200,6 +201,9 @@ func (r *LedgerChangeReader) Read() (Change, error) {
changes := GetChangesFromLedgerEntryChanges(
r.LedgerTransactionReader.lcm.UpgradesProcessing()[r.upgradeIndex].Changes,
)
for _, change := range changes {
change.reason = ProtocolUpgrade // Is there any other information that we can add here?
}
r.pending = append(r.pending, changes...)
r.upgradeIndex++
return r.Read()
Expand Down
96 changes: 67 additions & 29 deletions ingest/ledger_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,30 @@ func (t *LedgerTransaction) txInternalError() bool {
// GetFeeChanges returns a developer friendly representation of LedgerEntryChanges
// connected to fees.
func (t *LedgerTransaction) GetFeeChanges() []Change {
return GetChangesFromLedgerEntryChanges(t.FeeChanges)
changes := GetChangesFromLedgerEntryChanges(t.FeeChanges)
txData := &TransactionEnvelopeAndResult{Envelope: &t.Envelope, Result: &t.Result}
for _, change := range changes {
change.reason = FeeChange
change.TransactionData = txData
}
return changes
}

// GetChanges returns a developer friendly representation of LedgerEntryChanges.
// It contains transaction changes and operation changes in that order. If the
// transaction failed with TxInternalError, operations and txChangesAfter are
// omitted. It doesn't support legacy TransactionMeta.V=0.

func (t *LedgerTransaction) getTransactionChanges(ledgerEntryChanges xdr.LedgerEntryChanges) []Change {
changes := GetChangesFromLedgerEntryChanges(ledgerEntryChanges)
txData := &TransactionEnvelopeAndResult{Envelope: &t.Envelope, Result: &t.Result}
for _, change := range changes {
change.reason = Transaction
change.TransactionData = txData
}
return changes
}

func (t *LedgerTransaction) GetChanges() ([]Change, error) {
var changes []Change

Expand All @@ -42,42 +59,48 @@ func (t *LedgerTransaction) GetChanges() ([]Change, error) {
return changes, errors.New("TransactionMeta.V=0 not supported")
case 1:
v1Meta := t.UnsafeMeta.MustV1()
txChanges := GetChangesFromLedgerEntryChanges(v1Meta.TxChanges)
// The var `txChanges` reflect the ledgerEntryChanges that are changed because of the transaction as a whole
txChanges := t.getTransactionChanges(v1Meta.TxChanges)
changes = append(changes, txChanges...)

// Ignore operations meta if txInternalError https://github.com/stellar/go/issues/2111
if t.txInternalError() && t.LedgerVersion <= 12 {
return changes, nil
}

for _, operationMeta := range v1Meta.Operations {
opChanges := GetChangesFromLedgerEntryChanges(
operationMeta.Changes,
)
// These changes reflect the ledgerEntry changes that were caused by the operations in the transaction
// Populate the operationInfo for these changes in the `Change` struct

operationMeta := v1Meta.Operations
// operationMeta is a list of lists.
// Each element in operationMeta is a list of ledgerEntryChanges
// caused by the operation at that index of the element
for opIdx := range operationMeta {
opChanges := t.operationChanges(v1Meta.Operations, uint32(opIdx))
changes = append(changes, opChanges...)
}
case 2, 3:
var (
beforeChanges, afterChanges xdr.LedgerEntryChanges
operationMeta []xdr.OperationMeta
txBeforeChanges, txAfterChanges xdr.LedgerEntryChanges
operationMeta []xdr.OperationMeta
)

switch t.UnsafeMeta.V {
case 2:
v2Meta := t.UnsafeMeta.MustV2()
beforeChanges = v2Meta.TxChangesBefore
afterChanges = v2Meta.TxChangesAfter
txBeforeChanges = v2Meta.TxChangesBefore
txAfterChanges = v2Meta.TxChangesAfter
operationMeta = v2Meta.Operations
case 3:
v3Meta := t.UnsafeMeta.MustV3()
beforeChanges = v3Meta.TxChangesBefore
afterChanges = v3Meta.TxChangesAfter
txBeforeChanges = v3Meta.TxChangesBefore
txAfterChanges = v3Meta.TxChangesAfter
operationMeta = v3Meta.Operations
default:
panic("Invalid meta version, expected 2 or 3")
}

txChangesBefore := GetChangesFromLedgerEntryChanges(beforeChanges)
txChangesBefore := t.getTransactionChanges(txBeforeChanges)
changes = append(changes, txChangesBefore...)

// Ignore operations meta and txChangesAfter if txInternalError
Expand All @@ -86,14 +109,15 @@ func (t *LedgerTransaction) GetChanges() ([]Change, error) {
return changes, nil
}

for _, operationMeta := range operationMeta {
opChanges := GetChangesFromLedgerEntryChanges(
operationMeta.Changes,
)
// operationMeta is a list of lists.
// Each element in operationMeta is a list of ledgerEntryChanges
// caused by the operation at that index of the element
for opIdx := range operationMeta {
opChanges := t.operationChanges(operationMeta, uint32(opIdx))
changes = append(changes, opChanges...)
}

txChangesAfter := GetChangesFromLedgerEntryChanges(afterChanges)
txChangesAfter := t.getTransactionChanges(txAfterChanges)
changes = append(changes, txChangesAfter...)
default:
return changes, errors.New("Unsupported TransactionMeta version")
Expand All @@ -114,15 +138,13 @@ func (t *LedgerTransaction) GetOperation(index uint32) (xdr.Operation, bool) {
// GetOperationChanges returns a developer friendly representation of LedgerEntryChanges.
// It contains only operation changes.
func (t *LedgerTransaction) GetOperationChanges(operationIndex uint32) ([]Change, error) {
changes := []Change{}

if t.UnsafeMeta.V == 0 {
return changes, errors.New("TransactionMeta.V=0 not supported")
return []Change{}, errors.New("TransactionMeta.V=0 not supported")
}

// Ignore operations meta if txInternalError https://github.com/stellar/go/issues/2111
if t.txInternalError() && t.LedgerVersion <= 12 {
return changes, nil
return []Change{}, nil
}

var operationMeta []xdr.OperationMeta
Expand All @@ -134,21 +156,37 @@ func (t *LedgerTransaction) GetOperationChanges(operationIndex uint32) ([]Change
case 3:
operationMeta = t.UnsafeMeta.MustV3().Operations
default:
return changes, errors.New("Unsupported TransactionMeta version")
return []Change{}, errors.New("Unsupported TransactionMeta version")
}

return operationChanges(operationMeta, operationIndex), nil
return t.operationChanges(operationMeta, operationIndex), nil
}

func operationChanges(ops []xdr.OperationMeta, index uint32) []Change {
func (t *LedgerTransaction) operationChanges(ops []xdr.OperationMeta, index uint32) []Change {
if int(index) >= len(ops) {
return []Change{}
return []Change{} // TODO - operations_processor somehow seems to be failing without this
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved
}

operationMeta := ops[index]
return GetChangesFromLedgerEntryChanges(
operationMeta.Changes,
)
changes := GetChangesFromLedgerEntryChanges(operationMeta.Changes)
op, found := t.GetOperation(index)
operationInfo := &OperationInfo{
operationIdx: index,
operation: &op,
}
txData := &TransactionEnvelopeAndResult{Envelope: &t.Envelope, Result: &t.Result}

res := make([]Change, 0, len(changes))
for _, change := range changes {
if !found {
continue
}
change.operationInfo = operationInfo
change.reason = Operation
change.TransactionData = txData
res = append(res, change)
}
return res
}

// GetDiagnosticEvents returns all contract events emitted by a given operation.
Expand Down
4 changes: 4 additions & 0 deletions xdr/transaction_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func (e TransactionEnvelope) Preconditions() Preconditions {
// Note for fee bump transactions, Operations() returns the operations
// of the inner transaction
func (e TransactionEnvelope) Operations() []Operation {
// This is not expected to happen.
if (e == TransactionEnvelope{}) {
return []Operation{}
}
karthikiyer56 marked this conversation as resolved.
Show resolved Hide resolved
switch e.Type {
case EnvelopeTypeEnvelopeTypeTxFeeBump:
return e.FeeBump.Tx.InnerTx.V1.Tx.Operations
Expand Down
Loading