Playground is an environment that lets you quickly build and test blockchain business networks. It doesn't need a running blockchain network, and so it reduces the complexity of getting a business network defined, validated, and tested.
Interactive Mode
This starts Docker interactively. I like to run Playground like this so I can see what gets logged to STDOUT.
In WSL Ubuntu terminal:
docker run --name composer-playground --publish 8080:8080 hyperledger/composer-playground
where
-
docker run
runs the docker image -
--name composer-playground
assigns the name composer-playground to the container -
--publish 8080:8080
publishes the port 8080 in the container, and port 8080 in the host machine -
hyperledger/composer-playground
is the name of the docker image to run
Exit Playground
When you're finished running Playground, ctrl+c to kill the container in interactive mode.
Detached Mode
If you want to run Playground detached, just add --detach:
docker run --name composer-playground --publish 8080:8080 --detach hyperledger/composer-playground
Exit Playground
To exit Playground:
$ docker stop composer-playground
Connect Playground in Browser
http://localhost:8080
Alternatively, replace localhost with the machine's IP address.
Start Playground After Existing (Interactive or Detached Modes)
$ docker start composer-playground
Issue the above command for the following error:
$ docker run --name composer-playground --publish 8080:8080 --detach hyperledger/composer-playground
docker: Error response from daemon: Conflict. The container name "/composer-playground" is already in use by container "78a527bb1d8bf0fcc74755f59d4de64b730b5158562c00c1b3ad58376af235cf". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
Remove Playground from Docker
When we are done with Playground and wants to remove its Docker's image:
$ docker rm --force composer-playground
Error: failed to start containers: composer-playground
Error response from daemon: driver failed programming external connectivity on endpoint composer-playground (030cc8e8b89df5e79fd0206ee55f5f4afeb4cd2260246c4ae9b16a2d50946606): Error starting userland proxy: mkdir /port/tcp:0.0.0.0:8080:tcp:172.17.0.2:8080: input/output error
Error: failed to start containers: composer-playground
This is a known issue for Docker version 18.06.1-ce running in Windows 10. To fix, try:
- Restart docker using the whale icon in the taskbar.
- Wait a couple of minutes for it to be started, then in Ubuntu terminal:
kiatng@win10:~$ docker start composer-playground
Testing Playground with Hello World
Steps in browser (http://localhost:8080):
- Let's Blockchain
- Deploy a New Business Network
- Give your new Business Network a name: test-network
- Model Network Starter Template: empty-business-network
- Click Deploy
- Click Connect now
- Under Define tab
- Edit model.cto
- Add script file script.js
- Click Deploy changes
- Under Test tab
- Create New Asset:
- testId: 1234
- message: 'my first playground'
- Click Create New
- Click Submit Transaction
- Edit test: ...Test#1234
- Click Submit
- Create New Asset:
- Press F12 to bring up the console, the last entry is Hello: my first playground
Summary of what we do in Playground:
- Model Asset and Transaction
- Instantiate Asset and pass it to the Asset Registry
- Submit transaction and test the Business Network
To start over playground:
- Press F12 to bring up the developer panel
- Under Storage Tab
- Delete cookies, indexed db, local storage
- Press F5 to refresh
model.cto
/*
* 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.example.myfirstmodel
asset Test identified by testId {
o String testId
o String message
}
transaction Hello {
--> Test test
}
script.js
/**
* New script file
*/
/**
* @param {org.example.myfirstmodel.Hello} hello
* @transaction
*/
function hello(hello) {
console.log("Hello: " + hello.test.message);
}
Online Playground at IBM Cloud
There is no need to install Playground locally, we can use Playground in the cloud:
https://composer-playground.mybluemix.net/