package
0.0.0-20190726131402-fc9645e03f95
Repository: https://github.com/ibm-watson-iot/blockchain-samples.git
Documentation: pkg.go.dev

# README

#A Hyperledger Sample Contract with Trade Lane Schema

This smart contract is described in a set of documents in the /docs folder. See the README for more informarmation. And please read the introduction and customization documents therein before proceeding through the code base.

This contract is originally derived from the openblockchain contract in the folder trade_lane_contract to adapt it to the hyperledger project. The intent is for the iot_ variant to evolve as a generic template for Hyperledger chaincode development with IoTP device event mapping.

The iot_ variant of the contract will be advanced along with Hyperledger itself while the trade_lane_ variant will be advanced in lock step with Hyperledger Bluemix.


This sample contract implements a simple Trade Lane scenario, moving assets from one place to another. It consists of several modules in package main that are all small enough to avoid creating separate packages in this first version. These add features that can be used without changing the code.

This sample is used to explore features and patterns that are of interest to smart contract writers in the IoT domain. These are:

  • A single contract instance that manages multiple assets
  • A CRUD-like API for the assets
  • An event that is also a partial state
  • A deep merge of events to state
  • a JSON Schema 4 compatible schema and a script written in Go that generates object samples and object schemas when go generate commands are issued
  • A mechanism for storing asset history (note, this mechanism is early work and will change for better scaling)
  • A mechanism for storing the most recent updates to any asset, most recent first. An asset can appear only once in the list and jumps to the top each time it is updated.
  • An alerts mechanism that tracks active alerts and marks the threshold events as raised or cleared.
  • A rules engine that performs threshold tests (e.g. temperature too high) and raises or clears alerts as necessary (and note that the rules need not be limited to alerts testing etc, they can in fact generate read-only properties directly if need be)
  • A set of map utilities that enable deep merging of incoming JSON events into the state that is stored in the ledger. This is necessary to implement a pattern where a partial state is used as an event.
  • Optional case-insensitivity for JSON tags for the convenience of clients that do not want to be held to the strictness of the JSON-RPC standard (note: insensitivity is not recommended, but can be explored with this sample)
  • A logging facility that can be adjusted in real time in order to debug a deployed contract without disrupting it in any way

Generic UIs exist in other folders in this project, driven from the schema in this and other contracts. Plugins for React are used to generate forms from the schema, and of course processing the schema directly would enable a host of other schema-driven features.

To enable application access to the schema, contract APIs getAssetSamples and getAssetSchemas return generated samples and schemas in JSON object form. This is used by the generic UIs and by the Watson IoT Platform for automated integration.

# Packages

No description provided by the author

# Functions

GETContractStateFromLedger retrieves state from ledger and returns to caller.
GETRecentStatesFromLedger returns the unmarshaled recent states.
NewContractLogger creates a logger for the contract to use.
PUTContractStateToLedger writes a contract state into the ledger.
PUTcreateOnUpdate marshals the new setting and writes it to the ledger.
PUTRecentStatesToLedger marshals and writes the recent states.

# Constants

AlertsOVERTEMP the over temperature alert .
AlertsSIZE is to be maintained always as 1 greater than the last alert, giving a size .
ASSETID is the JSON tag for the assetID.
CONTRACTSTATEKEY is used to store contract state, including version, nickname and activeAssets.
CRITICAL means cannot function.
DEBUG allows for a peek into the guts of the app for debugging.
DEFAULTLOGGINGLEVEL is normally INFO in test and WARNING in production.
DEFAULTNICKNAME is used when a contract is initialized without giving it a nickname.
ERROR means something is wrong.
INFO means this happened and might be of interest.
MaxRecentStates is an arbitrary limit on how many asset states we track across the entire contract.
MYVERSION Update for every change, use VX.X.X (Major, Minor, Fix).
NOTICE means take note, this should be investigated.
RECENTSTATESKEY is used as key for recent states bucket.
No description provided by the author
TIMESTAMP is the JSON tag for timestamps, devices must use this tag to be compatible!.
TXNTIMESTAMP is the JSON tag for transaction timestamps, which map directly onto the transaction in the blockchain.
TXNUUID is the JSON tag for transaction UUIDs, which map directly onto the transaction in the blockchain.
WARNING means something might be wrong.

# Variables

AlertsName is a map of ID to name.
AlertsValue is a map of name to ID.
CASESENSITIVEMODE defines whether property names in the EVENT have to strictly follow JSON RPC conventions of case matching.
NOALERTSACTIVE is the zero value of an external alerts array (string names).
NOALERTSACTIVEINTERNAL is the zero value of an internal alerts array (bools).

# Structs

No description provided by the author
AlertStatusInternal contains the three possible statuses for alerts.
AssetArr provides a way to gather all assets and sort them.
AssetIDT is assetID as type, used for simple unmarshaling.
No description provided by the author
ContractLogger is our version of goLogger.
ContractState struct defines contract state.
CreateOnUpdate is a shared parameter structure for the use of the createonupdate feature.
RecentStates is JSON encoded string slice .
SimpleChaincode is the receiver for all shim API.

# Interfaces

ILogger the goLogger interface to which we are 100% compatible.

# Type aliases

AlertArrayInternal is used to store the list of active, raised or cleared alerts for internal processing.
AlertNameArray is used for external alerts in JSON.
Alerts exists so that strict type checking can be applied.
ArgsMap is a generic map[string]interface{} to be used as a receiver.
ByAssetID is an array of tagged assets for sorting.
No description provided by the author