akchtc

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2019 License: GPL-2.0 Imports: 5 Imported by: 0

README

Akachain High Throughput Chaincode Template

I. Overview

The Akachain High Throughput Chaincode (AKC HTC) is designed for applications handling hundreds or thousands transaction per second which all read or update the same asset (key) in the ledger.

This document provides the AKC HTC template interface and how to use.

II. AKC HTC Interface

The AKC HTC template is packaged into akc_htc package which provide the following interfaces

Insert: The insert function inserts the value into the temporary storage (the state db that may be deleted later) as a single row. The key is unique and created by combining the input and transaction id.
Insert(<name>, <key>, <value>, <operation>)
  • Name: The name, object or attribute that applied the high throughput chaincode. Example: merchant, user ...
  • Key: The key identify the object. Example: merchant address, user id ...
  • Operation: The operation that used for aggregation. Currently support OP_ADD (+) and OP_SUB (-)
  • Value: The value of key. Currently for aggregation purpose only, so it should be in numeric type
Get: Get the value from temporary storage
Get(<name>, [key])
  • Name: same as insert function
  • Key: [optional] same as insert function. If the key is null, all key:value associated with the name will be returned
Prune: Prune the temporary storage by aggregating the multiple row (value) into single row (value)
Prune(<name>, [key], [prunt_type])
  • Name: same as insert function
  • Key: same as insert function. If key is null, all key:value associated with the name will be pruned
  • Prunt_type: Currently, we support two type of prune:
    • PRUNE_FAST: perform the aggregation operation then delete the related row
    • PRUNE_SAFE: Same to PRUNE_FAST but the result is backup before delete all related row.
Delete: Delete the temporary storage
Delete(<name>, [key])
  • Name: same as insert function
  • Key: same as insert function. If key is null, all key:value associated with the name will be deleted

III. How to use

To use AKC HTC, the package akchtc must be imported to chaincode file. Ex:

import (
  "encoding/json"
  "errors"
  "fmt"
  "reflect"
  "github.com/hyperledger/fabric/core/chaincode/shim"
  akchtc "github.com/Akachain/akc-go-sdk/akc-htc"
)

type ResponseData struct {
	Key  string
	Data []string
}

// example code insert using Akachain High throughput
func insertHTC(stub shim.ChaincodeStubInterface, args []string) (string, error) {
  // Init Akachain High Throughput
  akc := akchtc.AkcHighThroughput{}
  res := akc.Insert(stub, []string{"variableName", "112233", "100", "OP_ADD"})

  if res != nil {
    return fmt.Sprintf("Failure"), res
  }
  return fmt.Sprintf("Success"), nil
}


// Example get data HTC with variableName
// This func response JSON data for name: "variableName"
func getHTC(stub shim.ChaincodeStubInterface, args []string) (string, error) {
  akc := akchtc.AkcHighThroughput{}
  res, err := akc.Get(stub, []string{"variableName"})

  var data map[string]ResponseData
	if err := json.Unmarshal(check, &data); err != nil {
		panic(err)
  }
  
  return fmt.Sprintf("%v", data), nil
}

// Example prune data HTC
// This func response JSON data for "variableName" after prune success.
func pruneHTC(stub shim.ChaincodeStubInterface, args []string) (string, error) {
	akc := AkcHighThroughput{}
	resp, err := akc.Prune(stub, []string{"variableName"})

  if err != nil {
    return nil, err
  }

	return fmt.Sprintf("%s", resp), err
}

// Example delete variable "variableName" in HTC
func deleteHTC(stub shim.ChaincodeStubInterface, args []string) (string, error) {
	akc := AkcHighThroughput{}
	resp, err := akc.Delete(stub, []string{"variableName"})

  if err != nil {
    return nil, err
  }

	return fmt.Sprintf("%s", resp), err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AkcHighThroughput

type AkcHighThroughput struct {
	Name      string
	Key       string
	Value     string
	Operation string
}

func (*AkcHighThroughput) Delete

func (akcStub *AkcHighThroughput) Delete(APIstub shim.ChaincodeStubInterface, args []string) (bool, error)

*

  • Deletes all rows associated with an aggregate variable from the ledger. The args array
  • contains the following argument:
  • - args[0] -> The name of the variable to delete
  • - args[1] -> The key of the variable to prune *
  • @param APIstub The chaincode shim
  • @param args The arguments array for the delete invocation *
  • @return A response structure indicating success or failure with a message

func (*AkcHighThroughput) Get

func (akcStub *AkcHighThroughput) Get(APIstub shim.ChaincodeStubInterface, args []string) ([]byte, error)

*

  • Retrieves the aggregate value of a variable in the ledger. Gets all delta rows for the variable
  • and computes the final value from all deltas. The args array for the invocation must contain the
  • following argument:
  • - args[0] -> The name of the variable to get the value of
  • - args[1] -> The key of the variable to get the value of *
  • @param APIstub The chaincode shim
  • @param args The arguments array for the get invocation *
  • @return A response structure indicating success or failure with a message

func (*AkcHighThroughput) Insert

func (akcStub *AkcHighThroughput) Insert(APIstub shim.ChaincodeStubInterface, args []string) error

*

  • Insert (updates) the ledger to include a new delta for a particular variable. If this is the first time
  • this variable is being added to the ledger, then its initial value is assumed to be 0. The arguments
  • to give in the args array are as follows:
  • - args[0] -> name of the variable
  • - args[1] -> key of the variable
  • - args[2] -> new delta (float)
  • - args[3] -> operation (currently supported are addition "OP_ADD" and subtraction "OP_SUB") *
  • @param APIstub The chaincode shim
  • @param args The arguments array for the update invocation *
  • @return A response structure indicating success or failure with a message

func (*AkcHighThroughput) Prune

func (akcStub *AkcHighThroughput) Prune(APIstub shim.ChaincodeStubInterface, args []string) ([]byte, error)

*

  • Prune a variable by deleting all of its delta rows while computing the final value. Once all rows
  • have been processed and deleted, a single new row is added which defines a delta containing the final
  • computed value of the variable. If type prune is PRUNE_FAST, this is NOT safe as any failures or errors during pruning
  • will result in an undefined final value for the variable and loss of data. Use type PRUNE_SAFE if data
  • integrity is important. The args array contains the following argument:
  • - args[0] -> The name of the variable to prune
  • - args[1] -> The key of the variable to prune
  • - args[2] -> Type of prune *
  • @param APIstub The chaincode shim
  • @param args The args array for the pruneFast invocation *
  • @return A response structure indicating success or failure with a message

type ResponseData

type ResponseData struct {
	Key  string
	Data []string
}

type SampleChaincode

type SampleChaincode struct {
}

func (*SampleChaincode) Init

func (*SampleChaincode) Invoke

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL