Skip to content

Commit 65fc253

Browse files
committed
fix(ledger) tx reversion handles empty middle accounts
1 parent a929bff commit 65fc253

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

internal/controller/ledger/controller_default.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ func (ctrl *DefaultController) revertTransaction(ctx context.Context, store Stor
406406
balances[posting.Source][posting.Asset],
407407
big.NewInt(0).Neg(posting.Amount),
408408
)
409+
if _, ok := balances[posting.Destination]; ok {
410+
// if destination is also a source in some posting, since balances should only contain posting sources
411+
balances[posting.Destination][posting.Asset] = balances[posting.Destination][posting.Asset].Add(
412+
balances[posting.Destination][posting.Asset],
413+
posting.Amount,
414+
)
415+
}
409416
}
410417

411418
for account, forAccount := range balances {

test/e2e/api_transactions_revert_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,68 @@ var _ = Context("Ledger revert transactions API tests", func() {
219219
})
220220
})
221221
})
222+
When("creating a transaction through an empty passthrough account", func() {
223+
var (
224+
timestamp = time.Now().Round(time.Second).UTC()
225+
tx *components.V2Transaction
226+
events chan *nats.Msg
227+
err error
228+
)
229+
BeforeEach(func() {
230+
events = Subscribe(GinkgoT(), testServer.GetValue())
231+
tx, err = CreateTransaction(
232+
ctx,
233+
testServer.GetValue(),
234+
operations.V2CreateTransactionRequest{
235+
V2PostTransaction: components.V2PostTransaction{
236+
Metadata: map[string]string{},
237+
Postings: []components.V2Posting{
238+
{
239+
Amount: big.NewInt(100),
240+
Asset: "USD",
241+
Source: "walter",
242+
Destination: "wendy",
243+
},
244+
{
245+
Amount: big.NewInt(100),
246+
Asset: "USD",
247+
Source: "wendy",
248+
Destination: "world",
249+
},
250+
},
251+
Timestamp: &timestamp,
252+
},
253+
Ledger: "default",
254+
},
255+
)
256+
Expect(err).ToNot(HaveOccurred())
257+
})
258+
When("trying to revert the passthrough transaction", func() {
259+
BeforeEach(func() {
260+
_, err := RevertTransaction(
261+
ctx,
262+
testServer.GetValue(),
263+
operations.V2RevertTransactionRequest{
264+
Ledger: "default",
265+
ID: tx.ID,
266+
AtEffectiveDate: pointer.For(true),
267+
},
268+
)
269+
Expect(err).To(Succeed())
270+
})
271+
It("should revert the passthrough transaction at date of the original tx", func() {
272+
response, err := GetTransaction(
273+
ctx,
274+
testServer.GetValue(),
275+
operations.V2GetTransactionRequest{
276+
Ledger: "default",
277+
ID: tx.ID,
278+
},
279+
)
280+
Expect(err).NotTo(HaveOccurred())
281+
Expect(response.Reverted).To(BeTrue())
282+
Expect(response.Timestamp).To(Equal(tx.Timestamp))
283+
})
284+
})
285+
})
222286
})

0 commit comments

Comments
 (0)