** Forked from https://github.com/IoTone/substrate-enterprise-sample-v3 **
This sample project demonstrates how the Substrate framework for building blockchains and its standard FRAME library for runtime development can be used to quickly build an ergonomic, end-to-end, blockchain-based application. This sample includes a custom front-end that was created from the helpful Substrate Front-End Template, which itself makes use of the powerful Polkadot{JS} API. The chain in this sample is a fork of the official Substrate Node Template - a supported starting point that decreases the time it takes to implement a custom next-generation blockchain.
The use case that this sample demonstrates is a decentralized supply-chain consortium. In order to accomplish this, FRAME is used to implement custom business logic as well as existing standards, such as decentralized identifiers (DIDs).
The capabilities demonstrated by this project include:
- Fine-grained and performance-preserving role-based access control (RBAC).
- Set-up and coordinate a decentralized network (permissioned blockchain) among several organisations.
- Manage decentralized identities for member organisations and their delegates.
- Register products and associated metadata, such as the organisation that owns them.
- Create shipments and track their journey through the supply chain.
- Monitor a shipment's storage and transportation conditions.
The sample demonstrates many features and capabilities of the Substrate framework, including:
- Consortium network with a proof-of-authority consensus (Aura for block production, GRANDPA for block finalization). Substrate and FRAME ship with a number of well-research and battle-tested consensus mechanisms and also make it possible to design and implement custom consensus mechanisms.
- Dynamic set of authority nodes.
- Role-based access control (RBAC) built on signed extensions.
- Reliable real-world data integration with off-chain workers.
- Flexible blockchain runtime development that uses FRAME pallets to encapsulate domain-specific logic (e.g. separate pallets for product registry & tracking).
The provided cargo run command will launch a temporary node and its state will be discarded after you terminate the process. After the project has been built, there are other ways to launch the node.
Single-Node Development Chain This command will start the single-node development chain with non-persistent state:
./target/release/node-template --dev Purge the development chain's state:
./target/release/node-template purge-chain --dev Start the development chain with detailed logging:
RUST_BACKTRACE=1 ./target/release/node-template -ldebug --dev Development chain means that the state of our chain will be in a tmp folder while the nodes are running. Also, alice account will be authority and sudo account as declared in the genesis state. At the same time the following accounts will be prefunded:
Alice Bob Alice//stash Bob//stash In case of being interested in maintaining the chain' state between runs a base path must be added so the db can be stored in the provided folder instead of a temporal one. We could use this folder to store different chain databases, as a different folder will be created per different chain that is ran. The following commands shows how to use a newly created folder as our db base path.
// Create a folder to use as the db base path
$ mkdir my-chain-state
// Use of that folder to store the chain state
$ ./target/release/node-template --dev --base-path ./my-chain-state/
// Check the folder structure created inside the base path after running the chain
$ ls ./my-chain-state
chains
$ ls ./my-chain-state/chains/
dev
$ ls ./my-chain-state/chains/dev
db keystore network
Follow the [installation instructions](https://substrate.dev/docs/en/knowledgebase/getting-started
This demo also uses Node.js and Yarn, so ensure that those dependencies are installed before continuing.
-
Run the Substrate chain
cd chain cargo build --release # Launch the node in development mode and do not persist chain state ./target/release/enterprise-sample --dev --tmp
OR
cd chain
# Launch the node in development mode and do not persist chain state
cargo run -- --dev --tmp
(if you want to use the node from external mode use the command bash cargo run -- --dev --tmp --ws--external
or bash ./target/release/enterprise-sample --dev --tmp --ws--external
you can find more info here [https://github.com/substrate-developer-hub/substrate-node-template])
-
Launch the front-end
cd ui yarn install && yarn start
please read the official documentation of substrate (https://docs.substrate.io/tutorials/v3/private-network/)
This guided demo will walk through a simplified version of the steps performed by the initialization script. The demo makes use of a number of well-known development accounts. In order to understand the demo's steps, it's necessary to understand the runtime modules (pallets) that inform the supply chain application and how they relate to each other.
The supply chain consortium application is comprised of a number of a modules, many of which are
configured in the chain specification's development_config
function:
-
Role-Based Access Control (RBAC) pallet - This pallet maintains an on-chain registry of roles and the users to which those roles are assigned. A
Role
is a tuple that encapsulates the name of a pallet and aPermission
that qualifies the level of access granted by theRole
. APermission
is an enum with the following variants:Execute
andManage
. TheExecute
permission allows a user to invoke a pallet's dispatchable functions. TheManage
permission allows a user to assign and revoke roles for a pallet, and also implies theExecute
permission. Access control validation is done at the transaction pool validation layer by way of the RBAC pallet'sAuthorize
signed extension. Notice the permissions that are configured in the chain specification file. Alice is granted theExecute
permission on the RBAC pallet, which allows her to use the RBAC pallet to create roles. In order to enable her to bootstrap the consortium, Alice is also granted theManage
permission on a few other pallet. -
Registrar pallet - The Registrar pallet inherits decentralized identifier (DID) capabilities from the DID pallet and uses these capabilities to implement an organization registry. This pallet maintains a list of organizations and maps each organization to a list of members. Organizations are identified by the ID of the account that created and owns it, which means that an account may create and own at most one organization. Organizations are associated with a name, which is designated by the value of the
Org
attribute on the DID of the organization owner. Organization owners are the only accounts that may add members to their organizations. When an account is added to an organization as a member, the organization owner creates anOrgMember
delegate for the member's DID - this is a way for the organization owner to certify an account's membership in the organization. The registrar pallet exposes a custom origin,EnsureOrg
, that validates whether or not an account owns or is a member of at least one organization. TheEnsureOrg
origin is used to control access to many of the chain's capabilities, including the ability to create roles with the RBAC pallet. -
Raw materials pallet - This pallet maintains a registry of raw materials and maps each material to the organization to which it belongs. A raw material is defined by six required properties (an ID, an origin process, a time of creation, state, amount and remaining amount)
-
supply Chain pallet - The Supply chain pallet save and handle all the process from the beans to sale coffee products, going through Harvesting, Processing, Packaging, Transporting, Roasting, Grinding
-
Retail transaction pallet - saves and handles the retail packaging creating a sku for the products, amont of produtcs, etc. also handles the retail sales
-
coffee products pallet - saves and handles the coffee products, amount created, remaining amount, date of creation, lb, price, kind of product(grinded, roasted,etc )
-
Navigate to the locally deployed instance of the demo UI, which should be running at http://localhost:8000/demo. Notice that the UI has an account selector that default to the Alice account. This is important due to the special permissions that were configured for Alice in the chain specification file.
-
Use the Members tab to create the Execute permission for three pallets:
registrar
,productRegistry
, andproductTracking
. -
Assign the three newly created roles to Bob, whose address is 5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty. This will allow Bob to create an organization, products, shipments, and shipment events.
Note: use this link to navigate to the Polkadot{JS} Apps UI and configure it to connect to the local supply chain network: https://polkadot.js.org/apps/#/explorer?rpc=ws://127.0.0.1:9944. Use the block hash provided by the supply chain UI to inspect the block that contained a transaction.
-
Use the account selector to switch to Bob's account, and then go back to the Organizations tab to create an organization.
-
Navigate to the Raw material tab and create raw material.
-
Use the Process tab to create register a new process whit the raw material that were created in the previous step, the input amount is how much of the raw material you want to use, and the produced is the material created for in process (then you can use that produced amount to retail packaging or to create a new raw material for another process)
-
Navigate to the raw material tab and use the previous step created, this will create a new raw material for another step
-
Navigate to the Process tab and you will can continue with another process with the input of the previously raw material
-
Navigate to the retail Packaging tab when you want to create the products for sale, you will have to select the process where the material comes from, and the amount of product that you want to create, as well you will have to introduce a unique SKU and all the others requeriodes fields
-
And if you go to sale tab, you can create a new sale, where you will select the sku of the product to sale, the amount of product and the buyer
- Use rustup: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Configure your rust: source ~/.cargo/env
- rustup update
- rustup update nightly
- rustup target add wasm32-unknown-unknown --toolchain nightly
To use the software on other platforms, you will need to set up a cross compiler. See instructions below.
- install the cross compile of the target on your system, add the new target to the toolchain and compile with the target flag, all the explication here (https://rust-lang.github.io/rustup/cross-compilation.html) the target of arm for the rust toolchain are armv7-unknown-linux-gnueabihf or armv7-unknown-linux-musleabihf
From the Mac, install:
- brew tap messense/macos-cross-toolchains
- brew install aarch64-unknown-linux-gnu
- rustup target add aarch64-unknown-linux-gnu
- in the chain/.cargo/config file, uncomment both lines
From Windows, install:
- Install the toolchain somewhere on your windows system. You will need to add the path to your toolchain bin directory via the windows control panel system environment PATH. https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/gcc-arm-10.3-2021.07-mingw-w64-i686-aarch64-none-linux-gnu.tar.xz
- rustup target add aarch64-unknown-linux-gnu
- in the chain/.cargo/config file, uncomment both lines
- cargo build --target=aarch64-none-linux-gnu
TODO
See: https://chacin.dev/blog/cross-compiling-rust-for-the-raspberry-pi/
-
Some tests were done with 1 cpu to know the build time and the final space
-
a virtual machine was created with 1 cpu and two different configurations
-
The amount of ram usage when the node is runing is about 79 MB
This project is intended for demonstration purposes and is not audited or ready for production use and was based on (https://github.com/IoTone/substrate-enterprise-sample-v3)
also some of the documentation is based in the node template example' readme of substrate(https://github.com/substrate-developer-hub/substrate-node-template#readme)