Skip to content

Commit

Permalink
feat: consider dashes as alphanums in account names (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdupas22 authored Jan 25, 2024
1 parent 0a9b8d7 commit 3c9c314
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 186 deletions.
2 changes: 1 addition & 1 deletion components/ledger/internal/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (v ExpandedAccount) Copy() ExpandedAccount {
return v
}

const AccountSegmentRegex = "[a-zA-Z0-9_]+(?:-[a-zA-Z0-9_]+)*"
const AccountSegmentRegex = "[a-zA-Z0-9_-]+"
const AccountPattern = "^" + AccountSegmentRegex + "(:" + AccountSegmentRegex + ")*$"

var AccountRegexp = regexp.MustCompile(AccountPattern)
Expand Down
16 changes: 13 additions & 3 deletions components/ledger/internal/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func TestValidateAddress(t *testing.T) {
{
name: "using single dash",
address: "-",
shouldBeOk: false,
shouldBeOk: true,
},
{
name: "using dash without alphanum before",
address: "-toto",
shouldBeOk: false,
shouldBeOk: true,
},
{
name: "using dash without alphanum after",
address: "toto-",
shouldBeOk: false,
shouldBeOk: true,
},
{
name: "using dash",
Expand All @@ -71,6 +71,16 @@ func TestValidateAddress(t *testing.T) {
address: "toto-titi:tata-tutu",
shouldBeOk: true,
},
{
name: "using multiple dashes",
address: "toto----titi",
shouldBeOk: true,
},
{
name: "using multiple dashes 2",
address: "-toto----titi-",
shouldBeOk: true,
},
}

for _, testCase := range testsCases {
Expand Down
23 changes: 19 additions & 4 deletions components/ledger/internal/api/v1/controllers_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,25 @@ func TestPostAccountMetadata(t *testing.T) {
},
},
{
name: "invalid account address format",
account: "invalid--acc",
expectStatusCode: http.StatusBadRequest,
expectedErrorCode: v1.ErrValidation,
name: "nominal dash 1",
account: "-test",
body: metadata.Metadata{
"foo": "bar",
},
},
{
name: "nominal dash 2",
account: "-",
body: metadata.Metadata{
"foo": "bar",
},
},
{
name: "nominal dash 2",
account: "-tes--t--t--t-----",
body: metadata.Metadata{
"foo": "bar",
},
},
{
name: "invalid body",
Expand Down
16 changes: 12 additions & 4 deletions components/ledger/internal/api/v2/controllers_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,18 @@ func TestPostAccountMetadata(t *testing.T) {
},
},
{
name: "invalid account address format",
account: "invalid--acc",
expectStatusCode: http.StatusBadRequest,
expectedErrorCode: v2.ErrValidation,
name: "nominal",
account: "test-",
body: metadata.Metadata{
"foo": "bar",
},
},
{
name: "nominal",
account: "-t--e-st-",
body: metadata.Metadata{
"foo": "bar",
},
},
{
name: "invalid body",
Expand Down
3 changes: 2 additions & 1 deletion components/ledger/internal/machine/script/NumScript.g4
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ SAVE: 'save';
NUMBER: [0-9]+;
PERCENT: '%';
VARIABLE_NAME: '$' [a-z_]+ [a-z0-9_]*;
ACCOUNT : '@'[a-zA-Z0-9_]+(('-'|':')[a-zA-Z0-9_]+)*;
ACCOUNT: '@' [a-zA-Z0-9_-]+ (':' [a-zA-Z0-9_-]+)*;



ASSET: [A-Z/0-9]+;
Expand Down

Large diffs are not rendered by default.

307 changes: 153 additions & 154 deletions components/ledger/internal/machine/script/parser/numscript_lexer.go

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions components/ledger/internal/machine/vm/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,16 +1300,31 @@ func TestVariablesParsing(t *testing.T) {
"acc": "account:valid-acc",
}))

require.Error(t, m.SetVarsFromJSON(map[string]string{
"acc": "account:invalid--acc",
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "account:valid--acc",
}))

require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "valid:acc",
}))

require.Error(t, m.SetVarsFromJSON(map[string]string{
"acc": "invalid--acc",
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "valid--acc",
}))
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "-valid--acc",
}))
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "-valid--acc-",
}))
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "valid--acc-",
}))
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "-",
}))
require.NoError(t, m.SetVarsFromJSON(map[string]string{
"acc": "---------",
}))
})

Expand Down Expand Up @@ -1550,20 +1565,6 @@ func TestSetVarsFromJSON(t *testing.T) {
)`,
expectedError: fmt.Errorf("missing variable $dest"),
},
{
name: "invalid format for account",
script: `vars {
account $dest
}
send [COIN 99] (
source = @world
destination = $dest
)`,
vars: map[string]string{
"dest": "invalid--acc",
},
expectedError: fmt.Errorf("invalid JSON value for variable $dest of type account: value invalid--acc: accounts should respect pattern ^[a-zA-Z0-9_]+(?:-[a-zA-Z0-9_]+)*(:[a-zA-Z0-9_]+(?:-[a-zA-Z0-9_]+)*)*$"),
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand Down
45 changes: 45 additions & 0 deletions components/ledger/internal/machine/vm/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,51 @@ var runTestCases = []runTestCase{
AccountMetadata: map[string]metadata.Metadata{},
},
},
{
name: "send $42 dash",
script: `
send [USD/2 42] (
source = @world
destination = @user:001-toto
)`,
expectResult: Result{
Postings: []ledger.Posting{
ledger.NewPosting("world", "user:001-toto", "USD/2", big.NewInt(42)),
},
Metadata: metadata.Metadata{},
AccountMetadata: map[string]metadata.Metadata{},
},
},
{
name: "send $42 dash 2",
script: `
send [USD/2 42] (
source = @world
destination = @user:001-toto
)`,
expectResult: Result{
Postings: []ledger.Posting{
ledger.NewPosting("world", "user:001-toto", "USD/2", big.NewInt(42)),
},
Metadata: metadata.Metadata{},
AccountMetadata: map[string]metadata.Metadata{},
},
},
{
name: "send $42 dash 3",
script: `
send [USD/2 42] (
source = @world
destination = @--t-t--edd-st---
)`,
expectResult: Result{
Postings: []ledger.Posting{
ledger.NewPosting("world", "--t-t--edd-st---", "USD/2", big.NewInt(42)),
},
Metadata: metadata.Metadata{},
AccountMetadata: map[string]metadata.Metadata{},
},
},
{
name: "send all available",
script: `
Expand Down

0 comments on commit 3c9c314

Please sign in to comment.