Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 507b829

Browse files
authored
chaincode updates in prep for alpha2 (#954)
Signed-off-by: Dave Kelsey <[email protected]>
1 parent d2ef0ae commit 507b829

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

packages/composer-runtime-hlfv1/chaincode.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@ package main
1616

1717
import "github.com/hyperledger/fabric/core/chaincode/shim"
1818
import pb "github.com/hyperledger/fabric/protos/peer"
19+
import "os"
20+
import "strings"
21+
// enable logging based on either world state or env variable.
22+
// default to INFO if neither have a value.
23+
func EnableLogging(stub shim.ChaincodeStubInterface) {
24+
var levelStr string
25+
levelBytes, err := stub.GetState("ComposerLogLevel")
26+
if err != nil || levelBytes == nil {
27+
var isSet bool
28+
levelStr, isSet = os.LookupEnv("CORE_CHAINCODE_LOGLEVEL")
29+
if !isSet {
30+
levelStr = "INFO"
31+
}
32+
} else {
33+
levelStr = string(levelBytes)
34+
}
35+
36+
level, _ := shim.LogLevel(levelStr)
37+
logger.SetLevel(level)
38+
}
39+
40+
// explicitly set the logging to a specific level
41+
func SetLogging(stub shim.ChaincodeStubInterface, levelStr string) {
42+
//We could check that the levelStr is valid but
43+
//currently if it isn't then I think shim.LogLevel will return a default of loglevel of Error.
44+
newLevel := strings.ToUpper(levelStr)
45+
stub.PutState("ComposerLogLevel", []byte(newLevel))
46+
level, _ := shim.LogLevel(newLevel)
47+
logger.SetLevel(level)
48+
logger.Warning("Setting loglevel to", newLevel)
49+
}
1950

2051
// Chaincode is the chaincode class. It is an implementation of the
2152
// Chaincode interface.
@@ -36,10 +67,9 @@ func NewChaincode() (result *Chaincode) {
3667
// Init is called by the Hyperledger Fabric when the chaincode is deployed.
3768
// Init can read from and write to the world state.
3869
func (chaincode *Chaincode) Init(stub shim.ChaincodeStubInterface) (response pb.Response) {
39-
//TODO: Need to control this via env var and/or api call.
4070
//logging needs to be set here again as the fabric chaincode disables it
4171
//even though it was enabled in main.
42-
logger.SetLevel(shim.LogDebug)
72+
EnableLogging(stub)
4373
logger.Debug("Entering Chaincode.Init", &stub)
4474
defer func() {
4575
logger.Debug("Exiting Chaincode.Init", response.Status, response.Message, string(response.Payload))
@@ -51,6 +81,17 @@ func (chaincode *Chaincode) Init(stub shim.ChaincodeStubInterface) (response pb.
5181

5282
// Execute the init function.
5383
function, arguments := stub.GetFunctionAndParameters()
84+
85+
// look for -d loglevel and set log and remove
86+
// from arguments
87+
var loglevel string
88+
for i, value := range arguments {
89+
if value == "-d" && (i + 1) < len(arguments) {
90+
loglevel = arguments[i+1]
91+
arguments = append(arguments[:i], arguments[i+2:]...)
92+
}
93+
}
94+
SetLogging(stub, loglevel)
5495
payload, err := composer.Init(stub, function, arguments)
5596
if err != nil {
5697
return shim.Error(err.Error())
@@ -61,10 +102,9 @@ func (chaincode *Chaincode) Init(stub shim.ChaincodeStubInterface) (response pb.
61102
// Invoke is called by the Hyperledger Fabric when the chaincode is invoked.
62103
// Invoke can read from and write to the world state.
63104
func (chaincode *Chaincode) Invoke(stub shim.ChaincodeStubInterface) (response pb.Response) {
64-
//TODO: Need to control this via env var and/or api call.
65105
//logging needs to be set here again as the fabric chaincode disables it
66106
//even though it was enabled in main.
67-
logger.SetLevel(shim.LogDebug)
107+
EnableLogging(stub)
68108
logger.Debug("Entering Chaincode.Invoke", &stub)
69109
defer func() {
70110
logger.Debug("Exiting Chaincode.Invoke", response.Status, response.Message, string(response.Payload))
@@ -76,9 +116,14 @@ func (chaincode *Chaincode) Invoke(stub shim.ChaincodeStubInterface) (response p
76116

77117
// Execute the invoke function.
78118
function, arguments := stub.GetFunctionAndParameters()
79-
payload, err := composer.Invoke(stub, function, arguments)
80-
if err != nil {
81-
return shim.Error(err.Error())
119+
if strings.ToLower(function) == "logging" {
120+
SetLogging(stub, arguments[0])
121+
return shim.Success(nil)
122+
} else {
123+
payload, err := composer.Invoke(stub, function, arguments)
124+
if err != nil {
125+
return shim.Error(err.Error())
126+
}
127+
return shim.Success(payload)
82128
}
83-
return shim.Success(payload)
84129
}

packages/composer-runtime-hlfv1/identityservice.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"bytes"
19+
"strings"
1920
"crypto/x509"
2021
"encoding/pem"
2122

@@ -107,7 +108,7 @@ func (identityService *IdentityService) getCurrentUserID(vm *duktape.Context) (r
107108

108109
// TODO: temporary for V1 admin user returns null to give them
109110
// full authority
110-
if ucert.Subject.CommonName == "admin" {
111+
if strings.Contains(strings.ToLower(ucert.Subject.CommonName), "admin") {
111112
vm.PushNull()
112113
return 1
113114
}

packages/composer-runtime-hlfv1/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ var logger = shim.NewLogger("Composer")
2626
// main starts the shim, which establishes the connection to the Hyperledger
2727
// Fabric and registers the chaincode for deploys, queries, and invokes.
2828
func main() {
29-
//TODO: Need to control this via env var and/or api call.
30-
logger.SetLevel(shim.LogDebug)
3129
logger.Debug("Entering main")
3230
defer func() { logger.Debug("Exiting main") }()
3331
chaincode := NewChaincode()

0 commit comments

Comments
 (0)