npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@blockchainlabs/fabric-state-manager

v0.0.15

Published

A reusable chaincode component for deleting data from the world state.

Downloads

5

Readme

Build Status - Master

fabric-state-manager-ts

This repository contains a reusable chaincode component, RemoverCC, for deleting the records found under a list of namespaces (i.e. composite keys). Thus, this chaincode component can be used to wipe out the world state. The RemoverCC chaincode component, written in TypeScript, exposes the following methods:

  • DeleteState - Deletes all records found under the namespaces that are passed in to the Init() method.

Though more than likely you won't be resetting the world state in a production environment, doing so in a development, test, or staging environment or as part of a PoC application is more than common.

Finally, please be aware that this code stores the namespace names (from which records should be deleted) in the blockchain under the fabric-state-manager-ts key. Therefore, please make sure you are not using this key to store any values on the blockchain.

Compiling and running test cases

Platform

It is strongly recommended to use macOS or a Linux flavor (such as Ubuntu) for building and testing the RemoverCC chaincode component.

Prerequisites

  1. Before you attempt to build and run the tests cases for RemoverCC, please make sure you have the necessary pre-requisites on your system:
  • Node.js (v8.x)

Steps

  1. Once you have cloned this repository to your local workstation, navigate to its root folder and execute the commands shown below to build and test the RemoverCC chaincode component:
$ pwd
/Users/olivieri/git/fabric-state-manager-ts
$ ls -la
total 368
drwxr-xr-x  14 olivieri  staff     448 Jun 17 17:09 .
drwxr-xr-x  60 olivieri  staff    1920 Jun 17 17:09 ..
drwxr-xr-x  13 olivieri  staff     416 Jun 17 17:09 .git
-rw-r--r--   1 olivieri  staff    1155 Jun 17 17:09 .gitignore
-rw-r--r--   1 olivieri  staff     264 Jun 17 17:09 .npmignore
-rw-r--r--   1 olivieri  staff     130 Jun 17 17:09 .travis.yml
-rw-r--r--   1 olivieri  staff   11357 Jun 17 17:09 LICENSE
-rw-r--r--   1 olivieri  staff    7581 Jun 17 17:09 README.md
-rw-r--r--   1 olivieri  staff     324 Jun 17 17:09 deploy_config.json
-rw-r--r--   1 olivieri  staff  138374 Jun 17 17:09 package-lock.json
-rw-r--r--   1 olivieri  staff    1884 Jun 17 17:09 package.json
drwxr-xr-x   5 olivieri  staff     160 Jun 17 17:09 src
-rw-r--r--   1 olivieri  staff     355 Jun 17 17:09 tsconfig.json
-rw-r--r--   1 olivieri  staff     513 Jun 17 17:09 tslint.json
$ npm install

...

added 403 packages from 1008 contributors and audited 1244 packages in 10.736s
found 0 vulnerabilities

$ npm run test

> [email protected] pretest /Users/olivieri/git/fabric-state-manager-ts
> npm run lint


> [email protected] lint /Users/olivieri/git/fabric-state-manager-ts
> tslint -c tslint.json 'src/**/*.ts'


> [email protected] test /Users/olivieri/git/fabric-state-manager-ts
> nyc mocha src/**/*.spec.ts



  RemoverContract
    #Ping
      ✓ should ping successfully
    #Unknown
      ✓ should fail
    #DeleteState
2019-06-17T20:54:34.365Z info [RemoverCC]                                         info: Deleting data for namespaces: namespace1,namespace2,namespace3 {"timestamp":"2019-06-17T20:54:34.365Z"}
2019-06-17T20:54:34.370Z info [RemoverCC]                                         info: Total number of records deleted: 30 {"timestamp":"2019-06-17T20:54:34.370Z"}
Number of records deleted: 30
Summary: Expected number of deleted records = 30, actual number of deleted records from chain = 30.
      ✓ should delete the records from namespaces


  3 passing (22ms)

---------------------|----------|----------|----------|----------|-------------------|
File                 |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------------|----------|----------|----------|----------|-------------------|
All files            |      100 |     87.5 |      100 |      100 |                   |
 src                 |      100 |      100 |      100 |      100 |                   |
  index.ts           |      100 |      100 |      100 |      100 |                   |
 src/remover-cc      |      100 |     87.5 |      100 |      100 |                   |
  remover-cc.spec.ts |      100 |      100 |      100 |      100 |                   |
  remover-cc.ts      |      100 |     87.5 |      100 |      100 |                55 |
 src/util            |      100 |      100 |      100 |      100 |                   |
  logger.ts          |      100 |      100 |      100 |      100 |                   |
---------------------|----------|----------|----------|----------|-------------------|

=============================== Coverage summary ===============================
Statements   : 100% ( 100/100 )
Branches     : 87.5% ( 7/8 )
Functions    : 100% ( 16/16 )
Lines        : 100% ( 97/97 )
================================================================================
$  

How to leverage this code from your chaincode component

Development

Please remember that Hyperledger Fabric takes into account chaincode components when partitioning the data stored in a channel. In other words, a chaincode component won't be able to read and/or delete any state that has been saved by another chaincode component on the same channel. Because of this, you cannot just deploy this code as a chaincode component and expect it to have access to the data written by another chaincode. Therefore, to leverage the code in this repository, you can follow the following steps:

  1. Configure your Node.js application to use this component; from the root folder of your application, execute the following command:

    npm i @blockchainlabs/fabric-state-manager --save
  2. Use composition (or inheritance)) to extend the capabilities of your code by referencing the RemoverCC class (which resides in the fabric-state-manager module you just imported) in your chaincode component. In this example, we'll use composition:

    ...
    
    import { RemoverCC } from "@blockchainlabs/fabric-state-manager";
    
    ...
    
    export class SampleChaincodeCC implements ChaincodeInterface {
    
        ...
    
        private removerCC = new RemoverCC();
    
        ...
           
    }
  3. From the Init() method in your chaincode component, invoke the Initialize(...) method. The invocation of the Initialize() method from your chaincode should pass an array of strings that contains the namespaces whose data should be deleted from the world state. Ex:

    // Init initializes chaincode
    public async Init(stub: ChaincodeStub): Promise<ChaincodeResponse> {
        logger.debug('entering >>> Init');
    
        ...
    
        const namespaces = ["namespace1", "namespace2", ... "namespaceN"];	
        await this.removerCC.Initialize(stub, namespaces);
    
        ...
    
        return Shim.success();
    }
  4. Add the DeleteState(...) method to the Invoke() method of your chaincode component. Ex:

    // Invoke - Entry point for Invocations
    public async Invoke(stub: ChaincodeStub): Promise<ChaincodeResponse> {
        logger.debug('entering >>> Invoke');
        const funcAndParams = stub.getFunctionAndParameters();
        switch (funcAndParams.fcn) {
            case 'Function1':
                return await this.Function1(stub);
            case 'Function2':
                return await this.Function2(stub);
               
            ...
               
            case 'DeleteState':
                return await this.removerCC.DeleteState(stub);
    
            ...
        }
    
        ...
          
    }
  5. Whenever there is the need to reset the world state, your Fabric client application should call the DeleteState() method which will then read the namespaces provided to the Initialize() method; the invocation of the DeleteState() method will result in the deletion of all the records found under those namespaces.