Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create Hyperledger 0.6 versions of all contracts
Change stub interface Change UUID accessor Clean up lint issues in simple contract Add missing .gitignore to building, cashManager, carbon_trading
- Loading branch information
Showing
169 changed files
with
53,156 additions
and
5,601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
iot_sample_contract.0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#A Hyperledger Sample Contract with Trade Lane Schema | ||
|
||
> Updated for Hyperledger 0.6 | ||
> **This smart contract is described in a set of documents in the `/docs` folder. See the [README](./docs/README.md) 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
contracts/advanced/iot_sample_contract.0.6/assethistory.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright (c) 2016 IBM Corporation and other Contributors. | ||
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. | ||
Contributors: | ||
Howard McKinney- Initial Contribution | ||
Kim Letkeman - Initial Contribution | ||
*/ | ||
|
||
// v3.0 HM 25 Feb 2016 Moved the asset state history code into a separate package. | ||
// v3.0.1 HM 03 Mar 2016 Store the state history in descending order. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/hyperledger/fabric/core/chaincode/shim" | ||
) | ||
|
||
const STATEHISTORYKEY string = ".StateHistory" | ||
|
||
type AssetStateHistory struct { | ||
AssetHistory []string `json:"assetHistory"` | ||
} | ||
|
||
// Create a new history entry in the ledger for an asset.,\ | ||
func createStateHistory(stub shim.ChaincodeStubInterface, assetID string, stateJSON string) error { | ||
|
||
var ledgerKey = assetID + STATEHISTORYKEY | ||
var assetStateHistory = AssetStateHistory{make([]string, 1)} | ||
assetStateHistory.AssetHistory[0] = stateJSON | ||
|
||
assetState, err := json.Marshal(&assetStateHistory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debugf("CreateStateHistory: putting to state ==>%s<==", string(assetState)) | ||
|
||
return stub.PutState(ledgerKey, []byte(assetState)) | ||
|
||
} | ||
|
||
// Update the ledger with new state history for an asset. States are stored in the ledger in descending order by timestamp. | ||
func updateStateHistory(stub shim.ChaincodeStubInterface, assetID string, stateJSON string) error { | ||
|
||
var ledgerKey = assetID + STATEHISTORYKEY | ||
var historyBytes []byte | ||
var assetStateHistory AssetStateHistory | ||
|
||
historyBytes, err := stub.GetState(ledgerKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(historyBytes, &assetStateHistory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var newSlice []string = make([]string, 0) | ||
newSlice = append(newSlice, stateJSON) | ||
newSlice = append(newSlice, assetStateHistory.AssetHistory...) | ||
assetStateHistory.AssetHistory = newSlice | ||
|
||
assetState, err := json.Marshal(&assetStateHistory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debugf("UpdateStateHistory: putting to state ==>%s<==", string(assetState)) | ||
|
||
return stub.PutState(ledgerKey, []byte(assetState)) | ||
|
||
} | ||
|
||
// Delete an state history from the ledger. | ||
func deleteStateHistory(stub shim.ChaincodeStubInterface, assetID string) error { | ||
|
||
var ledgerKey = assetID + STATEHISTORYKEY | ||
return stub.DelState(ledgerKey) | ||
|
||
} | ||
|
||
// Get the state history for an asset. | ||
func readStateHistory(stub shim.ChaincodeStubInterface, assetID string) (AssetStateHistory, error) { | ||
|
||
var ledgerKey = assetID + STATEHISTORYKEY | ||
var assetStateHistory AssetStateHistory | ||
var historyBytes []byte | ||
|
||
historyBytes, err := stub.GetState(ledgerKey) | ||
if err != nil { | ||
return assetStateHistory, err | ||
} | ||
|
||
err = json.Unmarshal(historyBytes, &assetStateHistory) | ||
if err != nil { | ||
return assetStateHistory, err | ||
} | ||
|
||
return assetStateHistory, nil | ||
|
||
} |
File renamed without changes.
Oops, something went wrong.