Skip to content

Commit

Permalink
fix(numscript): fix insufficient fund error instead of conflict error…
Browse files Browse the repository at this point in the history
… with used reference (#371)

* fix(numscript): fix insufficient fund error instead of conflict error with used reference

* refactor: factorize code
  • Loading branch information
gfyrag authored and flemzord committed Nov 17, 2022
1 parent 664781d commit 7cad0f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
sdk
.git
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
-X github.com/numary/ledger/cmd.Commit=${APP_SHA} \
-X github.com/numary/ledger/cmd.DefaultSegmentWriteKey=${SEGMENT_WRITE_KEY}" ./

FROM ubuntu:jammy
FROM ubuntu:jammy as app
RUN apt update && apt install -y ca-certificates wget && rm -rf /var/lib/apt/lists/*
COPY --from=builder /go/src/github.com/numary/ledger/numary /usr/local/bin/numary
EXPOSE 3068
Expand Down
11 changes: 11 additions & 0 deletions pkg/ledger/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
)

func (l *Ledger) execute(ctx context.Context, script core.Script) (*core.TransactionData, error) {
if script.Reference != "" {
txs, err := l.GetTransactions(ctx, *NewTransactionsQuery().WithReferenceFilter(script.Reference))
if err != nil {
return nil, err
}
if len(txs.Data) > 0 {
return nil, NewConflictError()
}
}
if script.Plain == "" {
return nil, NewScriptError(ScriptErrorNoScript, "no script to execute")
}
Expand Down Expand Up @@ -123,6 +132,7 @@ func (l *Ledger) execute(ctx context.Context, script core.Script) (*core.Transac
}

func (l *Ledger) Execute(ctx context.Context, script core.Script) (*core.ExpandedTransaction, error) {

t, err := l.execute(ctx, script)
if err != nil {
return nil, err
Expand All @@ -137,6 +147,7 @@ func (l *Ledger) Execute(ctx context.Context, script core.Script) (*core.Expande
}

func (l *Ledger) ExecutePreview(ctx context.Context, script core.Script) (*core.ExpandedTransaction, error) {

t, err := l.execute(ctx, script)
if err != nil {
return nil, err
Expand Down
34 changes: 34 additions & 0 deletions pkg/ledger/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,37 @@ func TestScriptSetReference(t *testing.T) {
assert.Equal(t, script.Reference, last.Reference)
})
}
func TestScriptReferenceConflict(t *testing.T) {
runOnLedger(func(l *ledger.Ledger) {
defer func(l *ledger.Ledger, ctx context.Context) {
require.NoError(t, l.Close(ctx))
}(l, context.Background())

_, err := l.Execute(context.Background(), core.Script{
ScriptCore: core.ScriptCore{
Plain: `
send [USD/2 99] (
source=@world
destination=@user:001
)`,
Vars: map[string]json.RawMessage{},
},
Reference: "tx_ref",
})
require.NoError(t, err)

_, err = l.Execute(context.Background(), core.Script{
ScriptCore: core.ScriptCore{
Plain: `
send [USD/2 99] (
source=@unexists
destination=@user:001
)`,
Vars: map[string]json.RawMessage{},
},
Reference: "tx_ref",
})
require.Error(t, err)
require.True(t, ledger.IsConflictError(err))
})
}

0 comments on commit 7cad0f7

Please sign in to comment.