源代码已迁移至 https://GitHub.com/hyperledger-labs/cckit
We're pleased to announce that ССKit has officially joined https://github.com/hyperledger-labs.
CCKit development continues here https://github.com/hyperledger-labs/cckit
A smart contract is code, invoked by a client application external to the blockchain network – that manages access and modifications to a set of key-value pairs in the World State. In Hyperledger Fabric, smart contracts are referred to as chaincode.
CCKit is a programming toolkit for developing and testing Hyperledger Fabric golang chaincodes. It enhances the development experience while providing developers components for creating more readable and secure smart contracts.
There are several chaincode "official" examples available:
and others
Main problems with existing examples are:
Cars - car registration chaincode, simplest example
Commercial paper, service-oriented approach - recommended way to start new application. Code generation radically simplifies building on-chain and off-chain applications.
Commercial paper - faithful reimplementation of the official example
Commercial paper extended example - with protobuf chaincode state schema and other features
ERC-20 - tokens smart contract, implementing ERC-20 interface
Cars private - car registration chaincode with private data
Payment - a few examples of chaincodes with encrypted state
CCKit requires Go 1.11+ with modules support
git clone [email protected]:s7techlab/cckit.git
go mod vendor
go get -u github.com/s7techlab/cckit
Commercial paper scenario from official documentation describes a Hyperledger Fabric network, aimed to issue, buy and redeem commercial paper.
Chaincode is a domain specific program which relates to specific business process. The job of a smart contract developer is to take an existing business process and express it as a smart contract in a programming language. Steps of chaincode development:
With protocol buffers, you write a .proto description of the data structure you wish to store.
From that, the protocol buffer compiler creates a golang struct that implements automatic encoding
and parsing of the protocol buffer data with an efficient binary format (or json).
Code generation can be simplified with a short Makefile:
.: generate
generate:
@echo "schema"
@protoc -I=./ --go_out=./ ./*.protoThe following file shows how to define the world state schema using protobuf.
examples/cpaper_extended/schema/state.proto
…This file defines the data payload used in business logic methods. In this example transaction and event payloads are exactly the same for the sake of brevity, but you could create a different schema for each type of payload.
examples/cpaper_extended/schema/payload.proto
…In examples/cpaper_extended/chaincode.go file we will define the mappings, chaincode initialization method and business logic in the transaction methods. For brevity, we will only display snippets of the code here, please refer to the original file for full example.
Firstly we define mapping rules. These specify the struct used to hold a specific chaincode state, it's primary key, list mapping, unique keys, etc. Then we define the schemas used for emitting events.
…CCKit uses router to define rules about how to map chaincode invocation to a particular handler, as well as what kind of middleware needs to be used during a request, for example how to convert incoming argument from []byte to target type (string, struct, etc).
…init methodIn many cases during chaincode instantiation we need to define permissions for chaincode functions -
"who is allowed to do this thing", incredibly important in the world of smart contracts.
The most common and basic form of access control is the concept of ownership: there's one account (combination
of MSP and certificate identifiers) that is the owner and can do administrative tasks on contracts. This
approach is perfectly reasonable for contracts that only have a single administrative user.
CCKit provides owner extension for implementing ownership and access control in Hyperledger Fabric chaincodes.
In the previous snippet, as an init method, we used owner.InvokeSetFromCreator, storing information
which stores the information about who is the owner into the world state upon chaincode instantiation.
Now we have to define the actual business logic which will modify the world state when a transaction occurs.
In this example we will show only the buy method for brevity.
Please refer to examples/cpaper_extended/chaincode.go for full implementation.
…And finally we should write tests to ensure our business logic is behaving as it should. Again, for brevity, we omitted most of the code from examples/cpaper_extended/chaincode_test.go. CCKit support chaincode testing with MockStub.
…暂无开放 Issues,或尚未同步最近议题。