Skip to content

Commit f9cc882

Browse files
author
Luis Sanchez
committed
[FAB-2461] inputs are optional when calculating hash
Matched the logic used to compute hash when installing a golang chaincode. Added some behave tests that can be run using: $ make behave-peer-chaincode or simply: $ cd peer/chaincode && behave Change-Id: If9747101c48754b26ce9c463f2adb7caef3220ad Signed-off-by: Luis Sanchez <[email protected]>
1 parent 21ce6b2 commit f9cc882

File tree

5 files changed

+174
-9
lines changed

5 files changed

+174
-9
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ behave: behave-deps
133133
@echo "Running behave tests"
134134
@cd bddtests; behave $(BEHAVE_OPTS)
135135

136+
behave-peer-chaincode: build/bin/peer peer-docker orderer-docker
137+
@cd peer/chaincode && behave
138+
136139
linter: buildenv
137140
@echo "LINT: Running code checks.."
138141
@$(DRUN) hyperledger/fabric-buildenv:$(DOCKER_TAG) ./scripts/golinter.sh

core/chaincode/platforms/java/hash.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ import (
3232
"github.com/hyperledger/fabric/common/util"
3333
ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util"
3434
pb "github.com/hyperledger/fabric/protos/peer"
35+
logging "github.com/op/go-logging"
3536
)
3637

38+
var logger = logging.MustGetLogger("java/hash")
39+
3740
func getCodeFromHTTP(path string) (codegopath string, err error) {
3841

3942
codegopath, err = ioutil.TempDir("", "javachaincode")
@@ -70,11 +73,6 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
7073
return "", errors.New("Cannot collect chaincode files from empty chaincode path")
7174
}
7275

73-
input := spec.Input
74-
if input == nil || len(input.Args) == 0 {
75-
return "", errors.New("Cannot collect chaincode files from empty input")
76-
}
77-
7876
codepath := chaincodeID.Path
7977

8078
var ishttp bool
@@ -103,11 +101,18 @@ func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, erro
103101
return "", fmt.Errorf("code does not exist %s", err)
104102
}
105103

106-
inputbytes, err := proto.Marshal(input)
107-
if err != nil {
108-
return "", fmt.Errorf("Error marshalling constructor: %s", err)
104+
var hash []byte
105+
106+
//install will not have inputs and we don't have to collect hash for it
107+
if spec.Input == nil || len(spec.Input.Args) == 0 {
108+
logger.Debugf("not using input for hash computation for %v ", chaincodeID)
109+
} else {
110+
inputbytes, err2 := proto.Marshal(spec.Input)
111+
if err2 != nil {
112+
return "", fmt.Errorf("Error marshalling constructor: %s", err)
113+
}
114+
hash = util.GenerateHashFromSignature(codepath, inputbytes)
109115
}
110-
hash := util.GenerateHashFromSignature(codepath, inputbytes)
111116

112117
hash, err = ccutil.HashFilesInDir("", codepath, hash, tw)
113118
if err != nil {
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from behave import *
2+
import os
3+
import subprocess
4+
5+
# set the default step matcher
6+
use_step_matcher("re")
7+
8+
def before_all(context):
9+
# set some handy values
10+
context.go_path = os.environ['GOPATH']
11+
context.fabric_dir = os.path.join(context.go_path, 'src/github.com/hyperledger/fabric')
12+
context.peer_exe = os.path.join(context.fabric_dir, 'build/bin/peer')
13+
context.sample_chaincode = {
14+
'golang':'github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02',
15+
'java':'examples/chaincode/java/SimpleSample'
16+
}
17+
18+
def after_scenario(context, scenario):
19+
if getattr(context, 'peer_container_id', None):
20+
subprocess.check_output(['docker', 'rm', '--force', '--volumes', context.peer_container_id], stderr=subprocess.STDOUT)
21+
if getattr(context, 'orderer_container_id', None):
22+
subprocess.check_output(['docker', 'rm', '--force', '--volumes', context.orderer_container_id], stderr=subprocess.STDOUT)
23+
if getattr(context, 'network_id', None):
24+
subprocess.check_output(['docker', 'network', 'rm', context.network_id], stderr=subprocess.STDOUT)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Feature: Install chaincode via CLI
2+
3+
Scenario Outline: Install first version chaincode via CLI
4+
5+
Given a fabric peer and orderer
6+
When a <lang> chaincode is installed via the CLI
7+
Then the chaincode is installed on the peer
8+
9+
Examples:
10+
| lang |
11+
| go |
12+
| java |
13+
14+
Scenario Outline: Install chaincode with new version
15+
16+
Only the first version of a chaincode (identified by chaincode name) can be
17+
installed. Subsequent versions must be upgraded instead.
18+
19+
Given a fabric peer and orderer
20+
When version one of a <lang> chaincode is installed via the CLI
21+
Then installing version two of the same chaincode via the CLI will fail
22+
23+
Examples:
24+
| lang |
25+
| go |
26+
| java |
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from behave import *
2+
import os
3+
import random
4+
import subprocess
5+
import tempfile
6+
import time
7+
import socket
8+
9+
@given(u'a fabric peer and orderer')
10+
def step_impl(context):
11+
12+
# create network
13+
context.network_name = 'behave_' + ''.join(random.choice('0123456789') for i in xrange(7))
14+
context.network_id = subprocess.check_output([
15+
'docker', 'network', 'create', context.network_name
16+
]).strip()
17+
18+
# start orderer
19+
context.orderer_container_id = subprocess.check_output([
20+
'docker', 'run', '-d', '-p', '7050',
21+
'--expose', '7050',
22+
'--network', context.network_name,
23+
'--network-alias', 'orderer',
24+
'--env', 'ORDERER_GENERAL_LISTENADDRESS=0.0.0.0',
25+
'hyperledger/fabric-orderer'
26+
]).strip()
27+
context.orderer_address = subprocess.check_output(['docker', 'port', context.orderer_container_id, '7050']).strip()
28+
29+
# start peer
30+
context.peer_container_id = subprocess.check_output([
31+
'docker', 'run', '-d', '-p', '7051',
32+
'--network', context.network_name,
33+
'--network-alias', 'vp0',
34+
'--env', 'CORE_PEER_ADDRESSAUTODETECT=true',
35+
'--env', 'CORE_PEER_ID=vp0',
36+
'--env', 'CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer:7050',
37+
'--volume', '/var/run/docker.sock:/var/run/docker.sock',
38+
'hyperledger/fabric-peer'
39+
]).strip()
40+
context.peer_address = subprocess.check_output(['docker', 'port', context.peer_container_id, '7051']).strip()
41+
time.sleep(1)
42+
43+
# setup env for peer cli commands
44+
context.peer_env = os.environ.copy()
45+
context.peer_env['CORE_PEER_ADDRESS'] = context.peer_address
46+
context.peer_env['CORE_PEER_COMMITTER_LEDGER_ORDERER'] = context.orderer_address
47+
48+
@when(r'a (?P<lang>java|go|golang|car) chaincode is installed via the CLI')
49+
def step_impl(context, lang):
50+
context.chaincode_lang = 'golang' if lang == 'go' else lang
51+
context.chaincode_id_name = lang + '_cc_' + ''.join(random.choice('0123456789') for i in xrange(7))
52+
context.chaincode_id_version = '1000'
53+
try:
54+
print(subprocess.check_output([
55+
context.peer_exe, 'chaincode', 'install',
56+
'--logging-level', 'debug',
57+
'--name', context.chaincode_id_name,
58+
'--path', context.sample_chaincode[context.chaincode_lang],
59+
'--version', context.chaincode_id_version,
60+
'--lang', context.chaincode_lang
61+
], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env))
62+
except subprocess.CalledProcessError as e:
63+
print(e.output)
64+
print('CORE_PEER_ADDRESS = ' + context.peer_env['CORE_PEER_ADDRESS'])
65+
print('CORE_PEER_COMMITTER_LEDGER_ORDERER = ' + context.peer_env['CORE_PEER_COMMITTER_LEDGER_ORDERER'])
66+
raise
67+
68+
@step(u'the chaincode is installed on the peer')
69+
def step_impl(context):
70+
print(subprocess.check_output([
71+
'docker', 'exec', context.peer_container_id, 'ls', '-l', '/var/hyperledger/production/chaincodes/' + context.chaincode_id_name + '.' + context.chaincode_id_version
72+
]))
73+
74+
@step(r'version (?P<version>\S+) of a (?P<lang>java|go|golang|car) chaincode is installed via the CLI')
75+
def step_impl(context, version, lang):
76+
context.chaincode_lang = 'golang' if lang == 'go' else lang
77+
context.chaincode_id_name = lang + '_cc_' + ''.join(random.choice('0123456789') for i in xrange(7))
78+
context.chaincode_id_version = version
79+
try:
80+
print(subprocess.check_output([
81+
context.peer_exe, 'chaincode', 'install',
82+
'--logging-level', 'debug',
83+
'--name', context.chaincode_id_name,
84+
'--path', context.sample_chaincode[context.chaincode_lang],
85+
'--version', context.chaincode_id_version,
86+
'--lang', context.chaincode_lang
87+
], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env))
88+
except subprocess.CalledProcessError as e:
89+
print(e.output)
90+
raise
91+
92+
@step(r'installing version (?P<version>\S+) of the same chaincode via the CLI will fail')
93+
def step_impl(context, version):
94+
assert getattr(context, 'chaincode_id_name', None), 'No chaincode previously installed.'
95+
context.chaincode_id_version = version
96+
try:
97+
print(subprocess.check_output([
98+
context.peer_exe, 'chaincode', 'install',
99+
'--logging-level', 'debug',
100+
'--name', context.chaincode_id_name,
101+
'--path', context.sample_chaincode[context.chaincode_lang],
102+
'--version', context.chaincode_id_version,
103+
'--lang', context.chaincode_lang
104+
], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env))
105+
except subprocess.CalledProcessError as e:
106+
print(e.output)
107+
raise

0 commit comments

Comments
 (0)