Skip to content

Commit

Permalink
fix(ledger) tx reversion handles empty middle accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
CrosleyZack committed Feb 26, 2025
1 parent a929bff commit 674744e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ func (ctrl *DefaultController) revertTransaction(ctx context.Context, store Stor
balances[posting.Source][posting.Asset],
big.NewInt(0).Neg(posting.Amount),
)
if _, ok := balances[posting.Destination]; ok {
// if destination is also a source in some posting, since balances should only contain posting sources
balances[posting.Destination][posting.Asset] = balances[posting.Destination][posting.Asset].Add(
balances[posting.Destination][posting.Asset],
posting.Amount,
)
}
}

for account, forAccount := range balances {
Expand Down
62 changes: 62 additions & 0 deletions test/e2e/api_transactions_revert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,66 @@ var _ = Context("Ledger revert transactions API tests", func() {
})
})
})
When("creating a transaction through an empty passthrough account", func() {
var (
timestamp = time.Now().Round(time.Second).UTC()
tx *components.V2Transaction
events chan *nats.Msg
err error
)
BeforeEach(func() {
events = Subscribe(GinkgoT(), testServer.GetValue())
tx, err = CreateTransaction(
ctx,
testServer.GetValue(),
operations.V2CreateTransactionRequest{
V2PostTransaction: components.V2PostTransaction{
Metadata: map[string]string{},
Postings: []components.V2Posting{
{
Amount: big.NewInt(100),
Asset: "USD",
Source: "walter",
Destination: "wendy",
},
{
Amount: big.NewInt(100),
Asset: "USD",
Source: "wendy",
Destination: "world",
}
},
Timestamp: &timestamp,
},
Ledger: "default",
},
)
Expect(err).ToNot(HaveOccurred())
})
When("trying to revert the passthrough transaction", func() {
_, err := RevertTransaction(
ctx,
testServer.GetValue(),
operations.V2RevertTransactionRequest{
Ledger: "default",
ID: tx.ID,
AtEffectiveDate: pointer.For(true),
},
)
Expect(err).To(Succeed())
})
It("should revert the passthrough transaction at date of the original tx", func() {
response, err := GetTransaction(
ctx,
testServer.GetValue(),
operations.V2GetTransactionRequest{
Ledger: "default",
ID: tx.ID,
},
)
Expect(err).NotTo(HaveOccurred())
Expect(response.Reverted).To(BeTrue())
Expect(response.Timestamp).To(Equal(tx.Timestamp))
})
})
})

0 comments on commit 674744e

Please sign in to comment.