Skip to content

Commit

Permalink
feature(setState) New 'set state.Name = Expr; Stmt' support accordpro…
Browse files Browse the repository at this point in the history
…ject#143

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Oct 8, 2019
1 parent bf43a97 commit fe38933
Show file tree
Hide file tree
Showing 15 changed files with 3,584 additions and 3,439 deletions.
8 changes: 4 additions & 4 deletions backends/javascript/ergo-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function unwrap(doc) {
}
function concat(r1, r2) {
var result = { };
for (var key1 in r1)
result[key1] = r1[key1];
for (var key2 in r2)
if (!(key2 in r1))
result[key2] = r2[key2];
result[key2] = r2[key2];
for (var key1 in r1)
if (!(key1 in r2))
result[key1] = r1[key1];
return result;
}
function contains(v, b) {
Expand Down
6 changes: 4 additions & 2 deletions examples/helloworldstate/logic.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ contract HelloWorldState over TemplateModel state State {
// Simple Clause
clause helloworldstate(request : MyRequest) : MyResponse {
set state.counter = state.counter + 1.0;
set state.lastInput = request.input;
return MyResponse{
output: "Hello " ++ contract.name ++
" (" ++ request.input ++ ")" ++ " (" ++ toString(state.counter) ++ ")"
" (" ++ request.input ++ ")" ++ " (" ++ toString(state.counter) ++ ")"
}
}

clause init() : Unit {
set state State{
counter: 0.0
counter: 0.0,
lastInput: ""
};
return
}
Expand Down
37 changes: 37 additions & 0 deletions examples/helloworldstate/logicOld.ergo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.helloworldstate

contract HelloWorldState over TemplateModel state State {
// Simple Clause
clause helloworldstate(request : MyRequest) : MyResponse {
set state State{
counter: state.counter + 1.0,
lastInput: request.input
};
return MyResponse{
output: "Hello " ++ contract.name ++
" (" ++ request.input ++ ")" ++ " (" ++ toString(state.counter) ++ ")"
}
}

clause init() : Unit {
set state State{
counter: 0.0,
lastInput: ""
};
return
}
}
1 change: 1 addition & 0 deletions examples/helloworldstate/model.cto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ transaction MyResponse {

concept State {
o Double counter
o String lastInput
}

/**
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions examples/helloworldstate/request2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$class": "org.accordproject.helloworldstate.MyRequest",
"input": "Linux Foundation"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$class": "org.accordproject.helloworldstate.State",
"counter" : 0
"counter" : 0,
"lastInput" : ""
}
5 changes: 5 additions & 0 deletions examples/helloworldstate/state2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$class": "org.accordproject.helloworldstate.State",
"counter" : 1,
"lastInput" : "Accord Project"
}
14 changes: 7 additions & 7 deletions mechanization/ErgoC/Lang/ErgoCSugar.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ Section ErgoCSugar.
Definition thisThis (prov:provenance) : ergoc_expr :=
EVar prov this_this.

(* [[ set state.name = e1; e2 ]]
(* [[ set state.name = e1; s2 ]]
==
brand tname (([name : e1] ⊕ !state))
*)
Definition setStateDot (prov:provenance) name tname e1 e2 : ergoc_expr :=
ELet prov local_state None
(EUnaryBuiltin prov (OpBrand (tname::nil))
(EBinaryBuiltin prov OpRecConcat
(EUnaryBuiltin prov (OpRec name) e1)
(EUnaryBuiltin prov OpUnbrand (EVar prov local_state))))
e2.
setState prov
(EUnaryBuiltin prov (OpBrand (tname::nil))
(EBinaryBuiltin prov OpRecConcat
(EUnaryBuiltin prov OpUnbrand (EVar prov local_state))
(EUnaryBuiltin prov (OpRec name) e1)))
e2.

Definition thisContract (prov:provenance) : ergoc_expr :=
let prov := ProvThisContract (loc_of_provenance prov) in
Expand Down
2 changes: 1 addition & 1 deletion mechanization/Version.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Require Import String.

Section Version.
Definition ergo_version := "0.20.0-alpha.1"%string.
Definition ergo_version := "0.20.0-alpha.2"%string.

End Version.

2,344 changes: 1,170 additions & 1,174 deletions packages/ergo-cli/extracted/ergoccore.js

Large diffs are not rendered by default.

2,202 changes: 1,099 additions & 1,103 deletions packages/ergo-cli/extracted/ergotopcore.js

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions packages/ergo-cli/test/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ describe('ergo', () => {
const ergoPath = Path.join('test/examples/helloworldstate', 'logic.ergo');
const ctoPath = Path.join('test/examples/helloworldstate', 'model.cto');
const contractPath = { file: Path.join('test/examples/helloworldstate', 'contract.json') };
const requestPath = { file: Path.join('test/examples/helloworldstate', 'request.json') };
const statePath = { file: Path.join('test/examples/helloworldstate', 'state.json') };
const requestPath = { file: Path.join('test/examples/helloworldstate', 'request1.json') };
const statePath = { file: Path.join('test/examples/helloworldstate', 'state1.json') };
const result = await Commands.execute([ergoPath], [ctoPath], contractPath, statePath, '1970-01-01T00:00:00Z', [requestPath]);
result.response.output.should.equal('Hello Fred Blogs (Accord Project) (1.0)');
});
it('should execute a smart Ergo contract with state thrice', async function () {
const ergoPath = Path.join('test/examples/helloworldstate', 'logic.ergo');
const ctoPath = Path.join('test/examples/helloworldstate', 'model.cto');
const contractPath = { file: Path.join('test/examples/helloworldstate', 'contract.json') };
const requestPath = { file: Path.join('test/examples/helloworldstate', 'request.json') };
const statePath = { file: Path.join('test/examples/helloworldstate', 'state.json') };
const result = await Commands.execute([ergoPath], [ctoPath], contractPath, statePath, '1970-01-01T00:00:00Z', [requestPath,requestPath,requestPath]);
result.response.output.should.equal('Hello Fred Blogs (Accord Project) (3.0)');
const requestPath = { file: Path.join('test/examples/helloworldstate', 'request1.json') };
const requestPath2 = { file: Path.join('test/examples/helloworldstate', 'request2.json') };
const statePath = { file: Path.join('test/examples/helloworldstate', 'state1.json') };
const result = await Commands.execute([ergoPath], [ctoPath], contractPath, statePath, '1970-01-01T00:00:00Z', [requestPath,requestPath2,requestPath2]);
result.response.output.should.equal('Hello Fred Blogs (Linux Foundation) (3.0)');
});
});
describe('#executeinstallmentsale', function () {
Expand Down
2,272 changes: 1,134 additions & 1,138 deletions packages/ergo-compiler/extracted/compilercore.js

Large diffs are not rendered by default.

112 changes: 109 additions & 3 deletions packages/ergo-engine/test/workload.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,125 @@
}
},
{
"name": "helloworldstate",
"name": "helloworldstate (old style)",
"dir": "examples/helloworldstate",
"ergo": ["logicOld.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"state": null,
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": null,
"state": {
"counter": 0,
"lastInput": "",
"$class": "org.accordproject.helloworldstate.State"
}
}
},
{
"name": "helloworldstate 1 (old style)",
"dir": "examples/helloworldstate",
"ergo": ["logicOld.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"state": "state1.json",
"request": "request1.json",
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.helloworldstate.MyResponse",
"output": "Hello Fred Blogs (Accord Project) (1.0)"
},
"state": {
"counter": 1,
"lastInput": "Accord Project",
"$class": "org.accordproject.helloworldstate.State"
}
}
},
{
"name": "helloworldstate 2 (old style)",
"dir": "examples/helloworldstate",
"ergo": ["logicOld.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"state": "state2.json",
"request": "request2.json",
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.helloworldstate.MyResponse",
"output": "Hello Fred Blogs (Linux Foundation) (2.0)"
},
"state": {
"counter": 2,
"lastInput": "Linux Foundation",
"$class": "org.accordproject.helloworldstate.State"
}
}
},
{
"name": "helloworldstate init (new style)",
"dir": "examples/helloworldstate",
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"request": "request.json",
"state": null,
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": null,
"state": {
"counter": 0,
"lastInput": "",
"$class": "org.accordproject.helloworldstate.State"
}
}
},
{
"name": "helloworldstate 1 (new style)",
"dir": "examples/helloworldstate",
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"state": "state1.json",
"request": "request1.json",
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.helloworldstate.MyResponse",
"output": "Hello Fred Blogs (Accord Project) (1.0)"
},
"state": {
"counter": 1,
"lastInput": "Accord Project",
"$class": "org.accordproject.helloworldstate.State"
}
}
},
{
"name": "helloworldstate 2 (new style)",
"dir": "examples/helloworldstate",
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"state": "state2.json",
"request": "request2.json",
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.helloworldstate.MyResponse",
"output": "Hello Fred Blogs (Linux Foundation) (2.0)"
},
"state": {
"counter": 2,
"lastInput": "Linux Foundation",
"$class": "org.accordproject.helloworldstate.State"
}
}
Expand All @@ -89,14 +195,14 @@
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"request": "request.json",
"state": null,
"contractName": "org.accordproject.helloworldstate.HelloWorldState",
"currentTime": "2019-01-01T16:34:00-05:00",
"expected": {
"response": null,
"state": {
"counter": 0,
"lastInput": "",
"$class": "org.accordproject.helloworldstate.State"
}
}
Expand Down

0 comments on commit fe38933

Please sign in to comment.