Skip to content

Commit 4efa999

Browse files
committed
fix: fixes after colors feature rebase
1 parent 80540b5 commit 4efa999

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

internal/interpreter/evaluate_expr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ func (s *programState) evaluateColor(colorExpr parser.ValueExpr) (*string, Inter
140140
return nil, err
141141
}
142142
if color == nil {
143-
return nil, nil
143+
c := ""
144+
return &c, nil
144145
}
145146

146147
isValidColor := colorRe.Match([]byte(*color))

internal/interpreter/funds_stack.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type Sender struct {
99
Name string
1010
Amount *big.Int
11+
Color string
1112
}
1213

1314
type fundsStack struct {
@@ -35,12 +36,13 @@ func (s *fundsStack) compactTop() {
3536
continue
3637
}
3738

38-
if first.Name != second.Name {
39+
if first.Name != second.Name || first.Color != second.Color {
3940
return
4041
}
4142

4243
s.senders = append(s.senders[0:len(s.senders)-2], Sender{
4344
Name: first.Name,
45+
Color: first.Color,
4446
Amount: new(big.Int).Add(first.Amount, second.Amount),
4547
})
4648
}
@@ -67,13 +69,15 @@ func (s *fundsStack) Pull(requiredAmount *big.Int) []Sender {
6769
case 1: // more than enough
6870
s.senders = append(s.senders, Sender{
6971
Name: available.Name,
72+
Color: available.Color,
7073
Amount: new(big.Int).Sub(available.Amount, requiredAmount),
7174
})
7275
fallthrough
7376

7477
case 0: // exactly the same
7578
out = append(out, Sender{
7679
Name: available.Name,
80+
Color: available.Color,
7781
Amount: new(big.Int).Set(requiredAmount),
7882
})
7983
return out

internal/interpreter/funds_stack_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,51 @@ func TestNoZeroLeftovers(t *testing.T) {
109109
{Name: "s2", Amount: big.NewInt(15)},
110110
}, out)
111111
}
112+
113+
func TestReconcileColoredAssetExactMatch(t *testing.T) {
114+
stack := newFundsStack([]Sender{
115+
{Name: "src", Amount: big.NewInt(10), Color: "X"},
116+
{Name: "s2", Amount: big.NewInt(15)},
117+
})
118+
119+
out := stack.Pull(big.NewInt(10))
120+
require.Equal(t, []Sender{
121+
{Name: "src", Amount: big.NewInt(10), Color: "X"},
122+
}, out)
123+
124+
}
125+
126+
func TestReconcileColoredManyDestPerSender(t *testing.T) {
127+
128+
stack := newFundsStack([]Sender{
129+
{"src", big.NewInt(10), "X"},
130+
})
131+
132+
out := stack.Pull(big.NewInt(5))
133+
require.Equal(t, []Sender{
134+
{Name: "src", Amount: big.NewInt(5), Color: "X"},
135+
}, out)
136+
137+
out = stack.Pull(big.NewInt(5))
138+
require.Equal(t, []Sender{
139+
{Name: "src", Amount: big.NewInt(5), Color: "X"},
140+
}, out)
141+
142+
}
143+
144+
func TestReconcileColoredManySenderColors(t *testing.T) {
145+
c1 := ("c1")
146+
c2 := ("c2")
147+
148+
stack := newFundsStack([]Sender{
149+
{"src", big.NewInt(1), c1},
150+
{"src", big.NewInt(1), c2},
151+
})
152+
153+
out := stack.Pull(big.NewInt(2))
154+
require.Equal(t, []Sender{
155+
{Name: "src", Amount: big.NewInt(1), Color: c1},
156+
{Name: "src", Amount: big.NewInt(1), Color: c2},
157+
}, out)
158+
159+
}

internal/interpreter/interpreter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ type programState struct {
310310
FeatureFlags map[string]struct{}
311311
}
312312

313-
func (st *programState) pushSender(name string, monetary *big.Int, color *string) {
313+
func (st *programState) pushSender(name string, monetary *big.Int, color string) {
314314
if monetary.Cmp(big.NewInt(0)) == 0 {
315315
return
316316
}
@@ -334,7 +334,7 @@ func (st *programState) pushReceiver(name string, monetary *big.Int) {
334334
postings := Posting{
335335
Source: sender.Name,
336336
Destination: name,
337-
Asset: st.CurrentAsset,
337+
Asset: coloredAsset(st.CurrentAsset, &sender.Color),
338338
Amount: sender.Amount,
339339
}
340340

@@ -499,7 +499,7 @@ func (s *programState) sendAllToAccount(accountLiteral parser.ValueExpr, ovedraf
499499

500500
// we sent balance+overdraft
501501
sentAmt := utils.MaxBigInt(new(big.Int).Add(balance, ovedraft), big.NewInt(0))
502-
s.pushSender(*account, sentAmt, color)
502+
s.pushSender(*account, sentAmt, *color)
503503
return sentAmt, nil
504504
}
505505

@@ -634,7 +634,7 @@ func (s *programState) trySendingToAccount(accountLiteral parser.ValueExpr, amou
634634
actuallySentAmt = utils.MinBigInt(safeSendAmt, amount)
635635
}
636636

637-
s.pushSender(*account, actuallySentAmt, color)
637+
s.pushSender(*account, actuallySentAmt, *color)
638638
return actuallySentAmt, nil
639639
}
640640

internal/parser/__snapshots__/parser_test.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,6 +3250,7 @@ parser.Program{
32503250
End: parser.Position{Character:23, Line:2},
32513251
},
32523252
Source: &parser.SourceAccount{
3253+
Color: nil,
32533254
ValueExpr: &parser.AccountInterpLiteral{
32543255
Range: parser.Range{
32553256
Start: parser.Position{Character:10, Line:2},
@@ -3261,6 +3262,7 @@ parser.Program{
32613262
},
32623263
},
32633264
Proxy: &parser.SourceAccount{
3265+
Color: nil,
32643266
ValueExpr: &parser.AccountInterpLiteral{
32653267
Range: parser.Range{
32663268
Start: parser.Position{Character:21, Line:2},

0 commit comments

Comments
 (0)