|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package vscc |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/golang/protobuf/proto" |
| 24 | + "github.com/hyperledger/fabric/core/chaincode/shim" |
| 25 | + //"github.com/hyperledger/fabric/core/crypto" |
| 26 | + pb "github.com/hyperledger/fabric/protos" |
| 27 | +) |
| 28 | + |
| 29 | +// ValidatorOneValidSignature implements the default transaction validation policy, |
| 30 | +// which is to check the correctness of the read-write set and the endorsement |
| 31 | +// signatures |
| 32 | +type ValidatorOneValidSignature struct { |
| 33 | +} |
| 34 | + |
| 35 | +// Init is called once when the chaincode started the first time |
| 36 | +func (vscc *ValidatorOneValidSignature) Init(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 37 | + // best practice to do nothing (or very little) in Init |
| 38 | + return nil, nil |
| 39 | +} |
| 40 | + |
| 41 | +// Invoke is called to validate the specified block of transactions |
| 42 | +// This validation system chaincode will check the read-write set validity and at least 1 |
| 43 | +// correct endorsement. Later we can create more validation system |
| 44 | +// chaincodes to provide more sophisticated policy processing such as enabling |
| 45 | +// policy specification to be coded as a transaction of the chaincode and the client |
| 46 | +// selecting which policy to use for validation using parameter function |
| 47 | +// @return serialized Block of valid and invalid transactions indentified |
| 48 | +// Note that Peer calls this function with 2 arguments, where args[0] is the |
| 49 | +// function name and args[1] is the block |
| 50 | +func (vscc *ValidatorOneValidSignature) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 51 | + // args[0] - function name (not used now) |
| 52 | + // args[1] - serialized Block object, which contains orderred transactions |
| 53 | + args := stub.GetArgs() |
| 54 | + if len(args) < 2 { |
| 55 | + return nil, errors.New("Incorrect number of arguments") |
| 56 | + } |
| 57 | + |
| 58 | + if args[1] == nil { |
| 59 | + return nil, errors.New("No block to validate") |
| 60 | + } |
| 61 | + |
| 62 | + block := &pb.Block2{} |
| 63 | + if err := proto.Unmarshal(args[1], block); err != nil { |
| 64 | + return nil, fmt.Errorf("Could not unmarshal block: %s", err) |
| 65 | + } |
| 66 | + |
| 67 | + // block.messages is an array, so we can deterministically iterate and |
| 68 | + // validate each transaction in order |
| 69 | + for _, v := range block.Transactions { |
| 70 | + tx := &pb.Transaction{} |
| 71 | + |
| 72 | + // Note: for v1, we do not have encrypted blocks |
| 73 | + |
| 74 | + if err := proto.Unmarshal(v, tx); err != nil { |
| 75 | + vscc.invalidate(tx) |
| 76 | + } else { |
| 77 | + vscc.validate(tx) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // TODO: fill in after we get the end-to-end v1 skeleton working. Mocked returned value for now |
| 82 | + return args[1], nil |
| 83 | +} |
| 84 | + |
| 85 | +// Query is here to satisfy the Chaincode interface. We don't need it for this system chaincode |
| 86 | +func (vscc *ValidatorOneValidSignature) Query(stub shim.ChaincodeStubInterface) ([]byte, error) { |
| 87 | + return nil, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (vscc *ValidatorOneValidSignature) validate(tx *pb.Transaction) { |
| 91 | + // TODO: fill in after we get the end-to-end v1 skeleton working |
| 92 | +} |
| 93 | + |
| 94 | +func (vscc *ValidatorOneValidSignature) invalidate(tx *pb.Transaction) { |
| 95 | + // TODO: fill in after we get the end-to-end v1 skeleton working |
| 96 | +} |
0 commit comments