Skip to content

Commit

Permalink
updated tutorial (#350)
Browse files Browse the repository at this point in the history
code formatting

Signed-off-by: Matthias Sieber <manonthemat@users.noreply.github.com>

Signed-off-by: Matthias Sieber <manonthemat@users.noreply.github.com>
  • Loading branch information
manonthemat authored Oct 10, 2022
1 parent 78a02ad commit 37b4256
Showing 1 changed file with 65 additions and 76 deletions.
141 changes: 65 additions & 76 deletions TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The dependencies of `fabric-contract-api` and `fabric-shim` will be required.
"npm": "^6.4.1"
},
"scripts": {
"test":"mocha.....
"test":"mocha.....
},
"engine-strict": true,
"engineStrict": true,
Expand Down Expand Up @@ -74,7 +74,7 @@ In this example we have a single value that can be queried and updated. This has
const UpdateValues = require('./updatevalues')
const RemoveValues = require('./removevalues')
module.exports.contracts = [UpdateValues,RemoveValues];
module.exports.contracts = [UpdateValues, RemoveValues];
```

This exports two classes that together form the Contract. There can be other code that within the model that is used in a support role.
Expand All @@ -97,24 +97,23 @@ const util = require('util');
/**
* Support the Updating of values within the SmartContract
*/
class UpdateValues extends Contract{
class UpdateValues extends Contract {
constructor(){
super('UpdateValuesContract');
}
async instantiate(ctx){
// .....
}
constructor() {
super('UpdateValuesContract');
}
async setNewAssetValue(ctx, newValue) {
// .....
}
async instantiate(ctx) {
// .....
}
async doubleAssetValue(ctx) {
// .....
}
async setNewAssetValue(ctx, newValue) {
// .....
}
async doubleAssetValue(ctx) {
// .....
}
};
module.exports = UpdateValues;
Expand Down Expand Up @@ -171,40 +170,39 @@ For example


```
/**
* Sets a name so that the functions in this particular class can
* be separated from others.
*/
constructor() {
super('UpdateValuesContract');
}
/** The function to invoke if something unkown comes in.
*
*/
async unknownTransaction(ctx){
throw new Error('a custom error message')
}
async beforeTransaction(ctx){
console.info(`Transaction ID: ${ctx.stub.getTxID()}`);
}
async afterTransaction(ctx,result){
// log result to preferred log implementation
// emit events etc...
}
async aroundTransaction(ctx, fn, parameters) {
try {
// don't forget to call super, or your transaction function won't run!
super.aroundTransaction(ctx, fn, parameters)
} catch (error) {
// do something with the error, then rethrow
throw error
}
}
/**
* Sets a name so that the functions in this particular class can
* be separated from others.
*/
constructor() {
super('UpdateValuesContract');
}
/** The function to invoke if something unkown comes in.
*
*/
async unknownTransaction(ctx) {
throw new Error('a custom error message');
}
async beforeTransaction(ctx){
console.info(`Transaction ID: ${ctx.stub.getTxID()}`);
}
async afterTransaction(ctx,result){
// log result to preferred log implementation
// emit events etc...
}
async aroundTransaction(ctx, fn, parameters) {
try {
// don't forget to call super, or your transaction function won't run!
super.aroundTransaction(ctx, fn, parameters);
} catch (error) {
// do something with the error, then rethrow
throw error;
}
}
```

### Structure of the Transaction Context
Expand All @@ -222,29 +220,28 @@ The context object contains
You are at liberty to create a subclass of the Context to provide additional functions, or per-transaction context storage. For example

```
/**
* Custom context for use within this contract
*/
createContext(){
return new ScenarioContext();
}
/**
* Custom context for use within this contract
*/
createContext() {
return new ScenarioContext();
}
```

and the Context class itself is

```
const { Context } = require('fabric-contract-api');
class ScenarioContext extends Context{
class ScenarioContext extends Context {
constructor(){
super();
}
generateKey(){
return this.stub.createCompositeKey('type',['keyvalue']);
}
constructor() {
super();
}
generateKey() {
return this.stub.createCompositeKey('type', ['keyvalue']);
}
}
```
Expand Down Expand Up @@ -278,22 +275,14 @@ Hyperledger Fabric's consensus algorithm permits the ability to use general purp
may not be read back.

```
let v1 = getState("key")
v1=="hello" // is true
putState("key","world")
let v1 = getState("key");
v1 == "hello"; // is true
putState("key", "world");
let v2 = getState("key")
v2=="world" // is false, v2 is "hello"
let v2 = getState("key");
v2 == "world"; // is false, v2 is "hello"
```

In any subsequent invocation, the value would be seen to be updated.

Note that if you have used any Flux architecture implications such as Redux, the above restrictions will be familiar.








0 comments on commit 37b4256

Please sign in to comment.