Skip to content

Commit

Permalink
feat: update ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed Feb 9, 2023
2 parents 8b17579 + c3ae55e commit da35972
Show file tree
Hide file tree
Showing 18 changed files with 1,180 additions and 1,026 deletions.
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ linters-settings:
linters:
disable-all: true
enable:
- deadcode #Default linter
- errcheck #Default linter
- gosimple #Default linter
- govet #Default linter
- ineffassign #Default linter
- staticcheck #Default linter
- structcheck #Default linter
- typecheck #Default linter
- unused #Default linter
- varcheck #Default linter
- gofmt
- gci
- goimports

run:
timeout: 5m
go: '1.18'
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ repos:
- id: check-added-large-files
- id: check-merge-conflict
- repo: https://github.com/golangci/golangci-lint
rev: v1.46.2
rev: v1.51.0
hooks:
- id: golangci-lint
28 changes: 6 additions & 22 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vars:
TIMEOUT: "10m"
RUN: ".*"
TAGS: "-tags json1,netgo"
BENCH_TIME: "30s"
BENCH_RESULTS_DIR: "/tmp/benchmarks"
BENCH_RESULTS_FILE: "/tmp/benchmarks/ledger.txt"
BENCH_CPU_PROFILE: "/tmp/benchmarks/ledger.cpu.prof"
Expand All @@ -26,7 +27,9 @@ tasks:

tests:
cmds:
- go test {{.TAGS}} {{if eq .VERBOSE "true"}}-v{{end}} -coverpkg {{.PKG}} -coverprofile coverage.out -covermode atomic {{.PKG}}
- >
go test {{.TAGS}} {{if eq .VERBOSE "true"}}-v{{end}} -coverpkg {{.PKG}}
-coverprofile coverage.out -covermode atomic {{.PKG}}
tests:local:
cmds:
Expand Down Expand Up @@ -73,22 +76,8 @@ tasks:
cmds:
- mkdir -p {{.BENCH_RESULTS_DIR}}
- >
go test {{.TAGS}} ./pkg/storage/sqlstorage
-run=XXX -bench={{.RUN}} -benchmem -timeout 1h
-cpuprofile {{.BENCH_CPU_PROFILE}} -memprofile {{.BENCH_MEM_PROFILE}}
| tee {{.BENCH_RESULTS_FILE}}
- benchstat {{.BENCH_RESULTS_FILE}}
env:
NUMARY_STORAGE_DRIVER: "postgres"
NUMARY_STORAGE_POSTGRES_CONN_STRING: "postgresql://ledger:ledger@127.0.0.1/ledger"

bench:ledger:
deps: [postgres]
cmds:
- mkdir -p {{.BENCH_RESULTS_DIR}}
- >
go test {{.TAGS}} ./pkg/ledger
-run=XXX -bench=BenchmarkLedger_{{.RUN}} -benchmem -benchtime=30s -timeout 1h
go test {{.TAGS}} {{if eq .VERBOSE "true"}}-v{{end}} {{.PKG}}
-run=XXX -bench={{.RUN}} -benchmem -benchtime={{.BENCH_TIME}} -timeout {{.TIMEOUT}}
-cpuprofile {{.BENCH_CPU_PROFILE}} -memprofile {{.BENCH_MEM_PROFILE}}
| tee {{.BENCH_RESULTS_FILE}}
- benchstat {{.BENCH_RESULTS_FILE}}
Expand All @@ -107,7 +96,6 @@ tasks:
install:
deps:
- install:golangci-lint
- install:cov-report
- install:perf

install:golangci-lint:
Expand All @@ -117,10 +105,6 @@ tasks:
sh -s -- -b $(go env GOPATH)/bin latest
- golangci-lint --version

install:cov-report:
cmds:
- go install github.com/go-phorce/cov-report/cmd/cov-report

install:perf:
- go install golang.org/x/perf/cmd/...@latest

Expand Down
Empty file modified go.sum
100755 → 100644
Empty file.
6 changes: 2 additions & 4 deletions pkg/api/controllers/script_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (ctl *ScriptController) PostScript(c *gin.Context) {
preview := ok && (strings.ToUpper(value) == "YES" || strings.ToUpper(value) == "TRUE" || value == "1")

res := ScriptResponse{}
execRes, err := l.(*ledger.Ledger).Execute(c.Request.Context(), false, preview, script)
execRes, err := l.(*ledger.Ledger).ExecuteScript(c.Request.Context(), preview, script)
if err != nil {
var (
code = apierrors.ErrInternal
Expand All @@ -62,9 +62,7 @@ func (ctl *ScriptController) PostScript(c *gin.Context) {
res.Details = apierrors.EncodeLink(message)
}
}
if len(execRes) > 0 {
res.Transaction = &execRes[0]
}
res.Transaction = &execRes

c.JSON(http.StatusOK, res)
}
33 changes: 17 additions & 16 deletions pkg/api/controllers/transaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,6 @@ func (ctl *TransactionController) PostTransaction(c *gin.Context) {
return
}

var res []core.ExpandedTransaction
var err error

if len(payload.Postings) > 0 && payload.Script.Plain != "" ||
len(payload.Postings) == 0 && payload.Script.Plain == "" {
apierrors.ResponseError(c, ledger.NewValidationError(
Expand All @@ -276,24 +273,29 @@ func (ctl *TransactionController) PostTransaction(c *gin.Context) {
Reference: payload.Reference,
Metadata: payload.Metadata,
}
res, err = l.(*ledger.Ledger).Execute(c.Request.Context(),
true, preview, core.TxsToScriptsData(txData)...)
} else {
script := core.ScriptData{
Script: payload.Script,
Timestamp: payload.Timestamp,
Reference: payload.Reference,
Metadata: payload.Metadata,
res, err := l.(*ledger.Ledger).ExecuteTxsData(c.Request.Context(), preview, txData)
if err != nil {
apierrors.ResponseError(c, err)
return
}
res, err = l.(*ledger.Ledger).Execute(c.Request.Context(),
false, preview, script)

respondWithData[[]core.ExpandedTransaction](c, http.StatusOK, res)
return
}

script := core.ScriptData{
Script: payload.Script,
Timestamp: payload.Timestamp,
Reference: payload.Reference,
Metadata: payload.Metadata,
}
res, err := l.(*ledger.Ledger).ExecuteScript(c.Request.Context(), preview, script)
if err != nil {
apierrors.ResponseError(c, err)
return
}

respondWithData[[]core.ExpandedTransaction](c, http.StatusOK, res)
respondWithData[[]core.ExpandedTransaction](c, http.StatusOK, []core.ExpandedTransaction{res})
}

func (ctl *TransactionController) GetTransaction(c *gin.Context) {
Expand Down Expand Up @@ -389,8 +391,7 @@ func (ctl *TransactionController) PostTransactionsBatch(c *gin.Context) {
}
}

res, err := l.(*ledger.Ledger).Execute(c.Request.Context(), true, false,
core.TxsToScriptsData(txs.Transactions...)...)
res, err := l.(*ledger.Ledger).ExecuteTxsData(c.Request.Context(), false, txs.Transactions...)
if err != nil {
apierrors.ResponseError(c, err)
return
Expand Down
99 changes: 90 additions & 9 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,9 @@ func TestPostTransactions(t *testing.T) {
expectedStatusCode: http.StatusBadRequest,
expectedErr: sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.TOK",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.TOK",
},
},
{
Expand Down Expand Up @@ -593,11 +592,95 @@ func TestPostTransactions(t *testing.T) {
ErrorMessageDeprecated: "cannot pass a timestamp prior to the last transaction:",
},
},
{
name: "mapping with postings",
payload: []controllers.PostTransaction{
{
Postings: core.Postings{
{
Source: "negativebalances:bar",
Destination: "world",
Amount: core.NewMonetaryInt(1000),
Asset: "TOK",
},
},
Timestamp: timestamp3,
},
},
expectedStatusCode: http.StatusOK,
expectedRes: sharedapi.BaseResponse[[]core.ExpandedTransaction]{
Data: &[]core.ExpandedTransaction{{
Transaction: core.Transaction{
TransactionData: core.TransactionData{
Postings: core.Postings{
{
Source: "negativebalances:bar",
Destination: "world",
Amount: core.NewMonetaryInt(1000),
Asset: "TOK",
},
},
Timestamp: timestamp3,
},
},
}},
},
},
{
name: "short asset",
payload: []controllers.PostTransaction{
{
Postings: core.Postings{
{
Source: "world",
Destination: "bank",
Amount: core.NewMonetaryInt(1000),
Asset: "F/9",
},
},
Timestamp: timestamp3,
},
},
expectedStatusCode: http.StatusOK,
expectedRes: sharedapi.BaseResponse[[]core.ExpandedTransaction]{
Data: &[]core.ExpandedTransaction{{
Transaction: core.Transaction{
TransactionData: core.TransactionData{
Postings: core.Postings{
{
Source: "world",
Destination: "bank",
Amount: core.NewMonetaryInt(1000),
Asset: "F/9",
},
},
Timestamp: timestamp3,
},
},
}},
},
},
}

internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
internal.SaveMapping(t, api, core.Mapping{
Contracts: []core.Contract{{
Name: "negative balances",
Account: "negativebalances:*",
Expr: core.ExprOr{
&core.ExprGte{
Op1: core.VariableExpr{Name: "balance"},
Op2: core.ConstantExpr{Value: 0},
},
&core.ExprLte{
Op1: core.VariableExpr{Name: "balance"},
Op2: core.ConstantExpr{Value: 0},
},
},
}},
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i < len(tc.payload)-1; i++ {
Expand Down Expand Up @@ -2003,10 +2086,9 @@ func TestPostTransactionsBatch(t *testing.T) {
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.COIN",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.COIN",
}, err)
})

Expand Down Expand Up @@ -2053,10 +2135,9 @@ func TestPostTransactionsBatch(t *testing.T) {
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.GEM",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.GEM",
}, err)
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/core/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
)

var assetRegexp = regexp.MustCompile(`^[A-Z][A-Z0-9]{1,16}(\/\d{1,6})?$`)
var assetRegexp = regexp.MustCompile(`^[A-Z][A-Z0-9]{0,16}(\/\d{1,6})?$`)

func AssetIsValid(v string) bool {
return assetRegexp.Match([]byte(v))
Expand Down
Loading

0 comments on commit da35972

Please sign in to comment.