minifabric icon indicating copy to clipboard operation
minifabric copied to clipboard

Initialize chaincode fails with "ccup" command

Open zuck opened this issue 4 years ago • 4 comments

I have a very basic chaincode generated using the IBM Blockchain Platform Extension for VSCode:

class ProductContract extends Contract {

    async productExists(ctx, productId) {
        const buffer = await ctx.stub.getState(productId);
        return (!!buffer && buffer.length > 0);
    }

    async createProduct(ctx, productId, value) {
        const exists = await this.productExists(ctx, productId);
        if (exists) {
            throw new Error(`The product ${productId} already exists`);
        }
        const asset = { value };
        const buffer = Buffer.from(JSON.stringify(asset));
        await ctx.stub.putState(productId, buffer);
    }

    async readProduct(ctx, productId) {
        const exists = await this.productExists(ctx, productId);
        if (!exists) {
            throw new Error(`The product ${productId} does not exist`);
        }
        const buffer = await ctx.stub.getState(productId);
        const asset = JSON.parse(buffer.toString());
        return asset;
    }

    async updateProduct(ctx, productId, newValue) {
        const exists = await this.productExists(ctx, productId);
        if (!exists) {
            throw new Error(`The product ${productId} does not exist`);
        }
        const asset = { value: newValue };
        const buffer = Buffer.from(JSON.stringify(asset));
        await ctx.stub.putState(productId, buffer);
    }

    async deleteProduct(ctx, productId) {
        const exists = await this.productExists(ctx, productId);
        if (!exists) {
            throw new Error(`The product ${productId} does not exist`);
        }
        await ctx.stub.deleteState(productId);
    }

}

Launching minifab ccup -n example1 -l node -v 1.0 on a test network based on Fabric v2.3 fails at the "initialize" step with the following error:

# Running operation: ******************************************
  cc initialize
.....
# Run the chaincode instantiate script on cli container *******
  non-zero return code
  Error: endorsement failure during invoke. response: status:500 message:"error in simulation: transaction returned with failure: Error: You've asked to invoke a function that does not exist: init"

# STATS *******************************************************
minifab: ok=24	failed=1

Instead, launching the following sequence of commands, passing the -n example1 option to the minifab initialize step, works as expected:

minifab install -n example1 -l node -v 1.0
minifab approve
minifab commit
minifab initialize -n example1
minifab discover
minifab channelquery

zuck avatar Oct 08 '21 12:10 zuck

If you are using your own chaincode, and that chaincode does not require initialization, then you should pass in a parameter to indicate that, or if your chaincode does require initialization, then you should have the method supported and pass in the right init method and it's parameters. This is completely up to you and you just need to use the right minifabric parameters to indicate all these, take a look at how to do all these by simply run minifab -h to see all supported operations and parameters.

litong01 avatar Oct 09 '21 03:10 litong01

try below:

minifab ccup -n example1 -l node -v 1.0 -d false -p ''

minifabric has following options and all these options can be used with 'minifab ccup' operation. '-d' to skip chaincode initialization '-p' to pass parameter for chaincode process (initialization or invoke)

check 'minifab -h' for all options.

itaru2622 avatar Oct 14 '21 00:10 itaru2622

Thank you, @litong01 and @itaru2622, for your clarifications!

However, the point I'm struggling with is not about how to invoke (or disable) initialization for the chaincode but the inconsistent behavior between the all-in-one call to ccup command and the associated command sequence, executed step-by-step: the first fails while the second succeeds.

I would expect to see both failing or succeeding (without specific option flags set) giving the same chaincode as input instead. Am I wrong?

Thanks!

zuck avatar Oct 20 '21 14:10 zuck

it has lots of communication for dowonload dependencies as well as between fabric instances and state of each instance is also changing after chaincode installation.

so it is not so simple like stateless processings, and failure reason may different in each time.

in my condition, sometimes it succeeds just one time but other cases it takes five times until success. so you are lucky if it succeeds in second time always.

Thats the reason I made ccup subcommand for less typing.

itaru2622 avatar Oct 20 '21 22:10 itaru2622