Last active
September 17, 2021 08:33
-
-
Save qkdxorjs1002/6961c08d82d1106a942e1001a6342c33 to your computer and use it in GitHub Desktop.
Hyperledger Fabric test chaincode
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
/* | |
Copyright IBM Corp. 2016 All Rights Reserved. | |
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. | |
*/ | |
package main | |
import ( | |
"errors" | |
"fmt" | |
"strconv" | |
"github.com/hyperledger/fabric-contract-api-go/contractapi" | |
) | |
// ABCstore Chaincode implementation | |
type ABCstore struct { | |
contractapi.Contract | |
} | |
func (t *ABCstore) Init(ctx contractapi.TransactionContextInterface, A string, Aval int, B string, Bval int, C string, Cval int) error { | |
fmt.Println("ABCstore Init") | |
var err error | |
// Initialize the chaincode | |
fmt.Printf("Aval = %d, Bval = %d, Cval = %d\n", Aval, Bval, Cval) | |
// Write the state to the ledger | |
err = ctx.GetStub().PutState(A, []byte(strconv.Itoa(Aval))) | |
if err != nil { | |
return err | |
} | |
err = ctx.GetStub().PutState(B, []byte(strconv.Itoa(Bval))) | |
if err != nil { | |
return err | |
} | |
err = ctx.GetStub().PutState(C, []byte(strconv.Itoa(Cval))) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
// Transaction makes payment of X units from A to B | |
func (t *ABCstore) Invoke(ctx contractapi.TransactionContextInterface, A string, B string, C string, X int) error { | |
var err error | |
var Aval int | |
var Bval int | |
var Cval int | |
var Fee int | |
// Get the state from the ledger | |
// TODO: will be nice to have a GetAllState call to ledger | |
Avalbytes, err := ctx.GetStub().GetState(A) | |
if err != nil { | |
return fmt.Errorf("Failed to get state") | |
} | |
if Avalbytes == nil { | |
return fmt.Errorf("Entity not found") | |
} | |
Aval, _ = strconv.Atoi(string(Avalbytes)) | |
Bvalbytes, err := ctx.GetStub().GetState(B) | |
if err != nil { | |
return fmt.Errorf("Failed to get state") | |
} | |
if Bvalbytes == nil { | |
return fmt.Errorf("Entity not found") | |
} | |
Bval, _ = strconv.Atoi(string(Bvalbytes)) | |
Cvalbytes, err := ctx.GetStub().GetState(C) | |
if err != nil { | |
return fmt.Errorf("Failed to get state") | |
} | |
if Cvalbytes == nil { | |
return fmt.Errorf("Entity not found") | |
} | |
Cval, _ = strconv.Atoi(string(Cvalbytes)) | |
// Perform the execution | |
Fee = (X / 10) | |
Aval = Aval - X | |
Cval = Cval + Fee | |
Bval = Bval + X - Fee | |
fmt.Printf("Aval = %d, Bval = %d, Cval = %d\n", Aval, Bval, Cval) | |
// Write the state back to the ledger | |
err = ctx.GetStub().PutState(A, []byte(strconv.Itoa(Aval))) | |
if err != nil { | |
return err | |
} | |
err = ctx.GetStub().PutState(B, []byte(strconv.Itoa(Bval))) | |
if err != nil { | |
return err | |
} | |
err = ctx.GetStub().PutState(C, []byte(strconv.Itoa(Cval))) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
// Delete an entity from state | |
func (t *ABCstore) Delete(ctx contractapi.TransactionContextInterface, A string) error { | |
// Delete the key from the state in ledger | |
err := ctx.GetStub().DelState(A) | |
if err != nil { | |
return fmt.Errorf("Failed to delete state") | |
} | |
return nil | |
} | |
// Query callback representing the query of a chaincode | |
func (t *ABCstore) Query(ctx contractapi.TransactionContextInterface, A string) (string, error) { | |
var err error | |
// Get the state from the ledger | |
Avalbytes, err := ctx.GetStub().GetState(A) | |
if err != nil { | |
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" | |
return "", errors.New(jsonResp) | |
} | |
if Avalbytes == nil { | |
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" | |
return "", errors.New(jsonResp) | |
} | |
jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" | |
fmt.Printf("Query Response:%s\n", jsonResp) | |
return string(Avalbytes), nil | |
} | |
func (t *ABCstore) GetAllQuery(ctx contractapi.TransactionContextInterface) ([]string, error) { | |
resultsIterator, err := ctx.GetStub().GetStateByRange("", "") | |
if err != nil { | |
return nil, err | |
} | |
defer resultsIterator.Close() | |
var wallet []string | |
for resultsIterator.HasNext() { | |
queryResponse, err := resultsIterator.Next() | |
if err != nil { | |
return nil, err | |
} | |
jsonResp := "{\"Name\":\"" + string(queryResponse.Key) + "\",\"Amount\":\"" + string(queryResponse.Value) + "\"}" | |
wallet = append(wallet, jsonResp) | |
} | |
return wallet, nil | |
} | |
func main() { | |
cc, err := contractapi.NewChaincode(new(ABCstore)) | |
if err != nil { | |
panic(err.Error()) | |
} | |
if err := cc.Start(); err != nil { | |
fmt.Printf("Error starting ABCstore chaincode: %s", err) | |
} | |
} |
Author
qkdxorjs1002
commented
Sep 17, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment