Skip to content

Feat/through #57

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

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Lexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ALLOWING: 'allowing';
UNBOUNDED: 'unbounded';
OVERDRAFT: 'overdraft';
ONEOF: 'oneof';
THROUGH: 'through';
KEPT: 'kept';
SAVE: 'save';
LPARENS: '(';
Expand Down
11 changes: 6 additions & 5 deletions Numscript.g4
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ source:
address = valueExpr colorConstraint? ALLOWING UNBOUNDED OVERDRAFT # srcAccountUnboundedOverdraft
| address = valueExpr colorConstraint? ALLOWING OVERDRAFT UP TO maxOvedraft = valueExpr #
srcAccountBoundedOverdraft
| valueExpr colorConstraint? # srcAccount
| LBRACE allotmentClauseSrc+ RBRACE # srcAllotment
| LBRACE source* RBRACE # srcInorder
| ONEOF LBRACE source+ RBRACE # srcOneof
| MAX cap = valueExpr FROM source # srcCapped;
| valueExpr colorConstraint? # srcAccount
| LBRACE allotmentClauseSrc+ RBRACE # srcAllotment
| LBRACE source* RBRACE # srcInorder
| ONEOF LBRACE source+ RBRACE # srcOneof
| MAX cap = valueExpr FROM source # srcCapped
| src = source THROUGH proxy = source # srcThrough;
allotmentClauseSrc: allotment FROM source;

keptOrDestination:
Expand Down
4 changes: 4 additions & 0 deletions internal/interpreter/batch_balances_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (st *programState) findBalancesQueries(source parser.Source) InterpreterErr
}
return nil

case *parser.SourceThrough:
// TODO also check right side? create failing test with colors
return st.findBalancesQueries(source.Source)

default:
utils.NonExhaustiveMatchPanic[error](source)
return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/interpreter/evaluate_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func (s *programState) evaluateColor(colorExpr parser.ValueExpr) (*string, Inter
return nil, err
}
if color == nil {
return nil, nil
c := ""
return &c, nil
}

isValidColor := colorRe.Match([]byte(*color))
Expand Down
89 changes: 89 additions & 0 deletions internal/interpreter/funds_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package interpreter

import (
"math/big"
"slices"
)

type Sender struct {
Name string
Amount *big.Int
Color string
}

type fundsStack struct {
senders []Sender
}

func newFundsStack(senders []Sender) fundsStack {
senders = slices.Clone(senders)

// TODO do not modify arg
// TODO clone big ints so that we can manipulate them
slices.Reverse(senders)
return fundsStack{
senders: senders,
}
}

func (s *fundsStack) compactTop() {
for len(s.senders) >= 2 {
first := s.senders[len(s.senders)-1]
second := s.senders[len(s.senders)-2]

if second.Amount.Cmp(big.NewInt(0)) == 0 {
s.senders = append(s.senders[0:len(s.senders)-2], first)
continue
}

if first.Name != second.Name || first.Color != second.Color {
return
}

s.senders = append(s.senders[0:len(s.senders)-2], Sender{
Name: first.Name,
Color: first.Color,
Amount: new(big.Int).Add(first.Amount, second.Amount),
})
}
}

func (s *fundsStack) Pull(requiredAmount *big.Int) []Sender {
// clone so that we can manipulate this arg
requiredAmount = new(big.Int).Set(requiredAmount)

// TODO preallocate for perfs
var out []Sender

for requiredAmount.Cmp(big.NewInt(0)) != 0 && len(s.senders) != 0 {
s.compactTop()

available := s.senders[len(s.senders)-1]
s.senders = s.senders[:len(s.senders)-1]

switch available.Amount.Cmp(requiredAmount) {
case -1: // not enough:
out = append(out, available)
requiredAmount.Sub(requiredAmount, available.Amount)

case 1: // more than enough
s.senders = append(s.senders, Sender{
Name: available.Name,
Color: available.Color,
Amount: new(big.Int).Sub(available.Amount, requiredAmount),
})
fallthrough

case 0: // exactly the same
out = append(out, Sender{
Name: available.Name,
Color: available.Color,
Amount: new(big.Int).Set(requiredAmount),
})
return out
}

}

return out
}
159 changes: 159 additions & 0 deletions internal/interpreter/funds_stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package interpreter

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
)

func TestEnoughBalance(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(100)},
})

out := stack.Pull(big.NewInt(2))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(2)},
}, out)

}

func TestSimple(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s2", Amount: big.NewInt(10)},
})

out := stack.Pull(big.NewInt(5))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s2", Amount: big.NewInt(3)},
}, out)

out = stack.Pull(big.NewInt(7))
require.Equal(t, []Sender{
{Name: "s2", Amount: big.NewInt(7)},
}, out)
}

func TestPullZero(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s2", Amount: big.NewInt(10)},
})

out := stack.Pull(big.NewInt(0))
require.Equal(t, []Sender(nil), out)
}

func TestCompactFunds(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s1", Amount: big.NewInt(10)},
})

out := stack.Pull(big.NewInt(5))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(5)},
}, out)
}

func TestCompactFunds3Times(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s1", Amount: big.NewInt(3)},
{Name: "s1", Amount: big.NewInt(1)},
})

out := stack.Pull(big.NewInt(6))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(6)},
}, out)
}

func TestCompactFundsWithEmptySender(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
{Name: "s2", Amount: big.NewInt(0)},
{Name: "s1", Amount: big.NewInt(10)},
})

out := stack.Pull(big.NewInt(5))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(5)},
}, out)
}

func TestMissingFunds(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(2)},
})

out := stack.Pull(big.NewInt(300))
require.Equal(t, []Sender{
{Name: "s1", Amount: big.NewInt(2)},
}, out)
}

func TestNoZeroLeftovers(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "s1", Amount: big.NewInt(10)},
{Name: "s2", Amount: big.NewInt(15)},
})

stack.Pull(big.NewInt(10))

out := stack.Pull(big.NewInt(15))
require.Equal(t, []Sender{
{Name: "s2", Amount: big.NewInt(15)},
}, out)
}

func TestReconcileColoredAssetExactMatch(t *testing.T) {
stack := newFundsStack([]Sender{
{Name: "src", Amount: big.NewInt(10), Color: "X"},
{Name: "s2", Amount: big.NewInt(15)},
})

out := stack.Pull(big.NewInt(10))
require.Equal(t, []Sender{
{Name: "src", Amount: big.NewInt(10), Color: "X"},
}, out)

}

func TestReconcileColoredManyDestPerSender(t *testing.T) {

stack := newFundsStack([]Sender{
{"src", big.NewInt(10), "X"},
})

out := stack.Pull(big.NewInt(5))
require.Equal(t, []Sender{
{Name: "src", Amount: big.NewInt(5), Color: "X"},
}, out)

out = stack.Pull(big.NewInt(5))
require.Equal(t, []Sender{
{Name: "src", Amount: big.NewInt(5), Color: "X"},
}, out)

}

func TestReconcileColoredManySenderColors(t *testing.T) {
c1 := ("c1")
c2 := ("c2")

stack := newFundsStack([]Sender{
{"src", big.NewInt(1), c1},
{"src", big.NewInt(1), c2},
})

out := stack.Pull(big.NewInt(2))
require.Equal(t, []Sender{
{Name: "src", Amount: big.NewInt(1), Color: c1},
{Name: "src", Amount: big.NewInt(1), Color: c2},
}, out)

}
Loading