Skip to content

Commit a25a7d6

Browse files
mbwhitejt-nti
authored andcommitted
[FABCJ-276] Access localmspid
Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
1 parent b509805 commit a25a7d6

File tree

6 files changed

+189
-89
lines changed

6 files changed

+189
-89
lines changed

fabric-chaincode-shim/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
319319
import org.gradle.api.tasks.testing.logging.TestLogEvent
320320

321321
tasks.withType(Test) {
322+
323+
environment "CORE_PEER_LOCALMSPID", "mymsp"
324+
322325
testLogging {
323326
// set options for log level LIFECYCLE
324327
events TestLogEvent.FAILED,

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ public abstract class ChaincodeBase implements Chaincode {
8989
private String tlsClientRootCertPath;
9090

9191
private String id;
92+
private String localMspId = "";
9293

9394
private static final String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME";
9495
private static final String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS";
9596
private static final String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED";
9697
private static final String CORE_PEER_TLS_ROOTCERT_FILE = "CORE_PEER_TLS_ROOTCERT_FILE";
9798
private static final String ENV_TLS_CLIENT_KEY_PATH = "CORE_TLS_CLIENT_KEY_PATH";
9899
private static final String ENV_TLS_CLIENT_CERT_PATH = "CORE_TLS_CLIENT_CERT_PATH";
100+
private static final String CORE_PEER_LOCALMSPID = "CORE_PEER_LOCALMSPID";
99101
private Properties props;
100102
private Level logLevel;
101103

@@ -145,7 +147,8 @@ protected final void connectToPeer() throws IOException {
145147
}
146148

147149
protected final void initializeLogging() {
148-
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$-80.80s %5$s%6$s%n");
150+
System.setProperty("java.util.logging.SimpleFormatter.format",
151+
"%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$-80.80s %5$s%6$s%n");
149152
final Logger rootLogger = Logger.getLogger("");
150153

151154
for (final java.util.logging.Handler handler : rootLogger.getHandlers()) {
@@ -156,8 +159,10 @@ protected final void initializeLogging() {
156159
public synchronized String format(final LogRecord record) {
157160
return super.format(record).replaceFirst(".*SEVERE\\s*\\S*\\s*\\S*", "\u001B[1;31m$0\u001B[0m")
158161
.replaceFirst(".*WARNING\\s*\\S*\\s*\\S*", "\u001B[1;33m$0\u001B[0m")
159-
.replaceFirst(".*CONFIG\\s*\\S*\\s*\\S*", "\u001B[35m$0\u001B[0m").replaceFirst(".*FINE\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m")
160-
.replaceFirst(".*FINER\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m").replaceFirst(".*FINEST\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m");
162+
.replaceFirst(".*CONFIG\\s*\\S*\\s*\\S*", "\u001B[35m$0\u001B[0m")
163+
.replaceFirst(".*FINE\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m")
164+
.replaceFirst(".*FINER\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m")
165+
.replaceFirst(".*FINEST\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m");
161166
}
162167

163168
});
@@ -192,7 +197,8 @@ public String format(final LogRecord record) {
192197
pw.close();
193198
throwable = sw.toString();
194199
}
195-
return String.format(format, dat, source, record.getLoggerName(), record.getLevel(), message, throwable);
200+
return String.format(format, dat, source, record.getLoggerName(), record.getLevel(), message,
201+
throwable);
196202

197203
}
198204

@@ -220,7 +226,7 @@ public String format(final LogRecord record) {
220226
final List<?> loggers = Collections.list(LogManager.getLogManager().getLoggerNames());
221227
loggers.forEach(x -> {
222228
final Logger l = LogManager.getLogManager().getLogger((String) x);
223-
// err what is the code supposed to do?
229+
// err what is the code supposed to do?
224230
});
225231

226232
}
@@ -249,19 +255,22 @@ private Level mapLevel(final String level) {
249255

250256
protected final void validateOptions() {
251257
if (this.id == null) {
252-
throw new IllegalArgumentException(
253-
format("The chaincode id must be specified using either the -i or --i command line options or the %s environment variable.",
254-
CORE_CHAINCODE_ID_NAME));
258+
throw new IllegalArgumentException(format(
259+
"The chaincode id must be specified using either the -i or --i command line options or the %s environment variable.",
260+
CORE_CHAINCODE_ID_NAME));
255261
}
256262
if (this.tlsEnabled) {
257263
if (tlsClientCertPath == null) {
258-
throw new IllegalArgumentException(format("Client key certificate chain (%s) was not specified.", ENV_TLS_CLIENT_CERT_PATH));
264+
throw new IllegalArgumentException(
265+
format("Client key certificate chain (%s) was not specified.", ENV_TLS_CLIENT_CERT_PATH));
259266
}
260267
if (tlsClientKeyPath == null) {
261-
throw new IllegalArgumentException(format("Client key (%s) was not specified.", ENV_TLS_CLIENT_KEY_PATH));
268+
throw new IllegalArgumentException(
269+
format("Client key (%s) was not specified.", ENV_TLS_CLIENT_KEY_PATH));
262270
}
263271
if (tlsClientRootCertPath == null) {
264-
throw new IllegalArgumentException(format("Peer certificate trust store (%s) was not specified.", CORE_PEER_TLS_ROOTCERT_FILE));
272+
throw new IllegalArgumentException(
273+
format("Peer certificate trust store (%s) was not specified.", CORE_PEER_TLS_ROOTCERT_FILE));
265274
}
266275
}
267276
}
@@ -286,7 +295,8 @@ protected final void processCommandLineOptions(final String[] args) {
286295
port = Integer.valueOf(hostArr[1].trim());
287296
host = hostArr[0].trim();
288297
} else {
289-
final String msg = String.format("peer address argument should be in host:port format, current %s in wrong", hostAddrStr);
298+
final String msg = String.format(
299+
"peer address argument should be in host:port format, current %s in wrong", hostAddrStr);
290300
LOGGER.severe(msg);
291301
throw new IllegalArgumentException(msg);
292302
}
@@ -318,10 +328,17 @@ protected final void processEnvironmentOptions() {
318328
this.port = Integer.valueOf(hostArr[1].trim());
319329
this.host = hostArr[0].trim();
320330
} else {
321-
final String msg = String.format("peer address argument should be in host:port format, ignoring current %s", System.getenv(CORE_PEER_ADDRESS));
331+
final String msg = String.format(
332+
"peer address argument should be in host:port format, ignoring current %s",
333+
System.getenv(CORE_PEER_ADDRESS));
322334
LOGGER.severe(msg);
323335
}
324336
}
337+
338+
if (System.getenv().containsKey(CORE_PEER_LOCALMSPID)) {
339+
this.localMspId = System.getenv(CORE_PEER_LOCALMSPID);
340+
}
341+
325342
this.tlsEnabled = Boolean.parseBoolean(System.getenv(CORE_PEER_TLS_ENABLED));
326343
if (this.tlsEnabled) {
327344
this.tlsClientRootCertPath = System.getenv(CORE_PEER_TLS_ROOTCERT_FILE);
@@ -336,12 +353,13 @@ protected final void processEnvironmentOptions() {
336353
LOGGER.info("CORE_PEER_TLS_ROOTCERT_FILE: " + this.tlsClientRootCertPath);
337354
LOGGER.info("CORE_TLS_CLIENT_KEY_PATH: " + this.tlsClientKeyPath);
338355
LOGGER.info("CORE_TLS_CLIENT_CERT_PATH: " + this.tlsClientCertPath);
356+
LOGGER.info("CORE_PEER_LOCALMSPID: " + this.localMspId);
339357
LOGGER.info("LOGLEVEL: " + this.logLevel);
340358
}
341359

342360
/**
343-
* Obtains configuration specifically for running the chaincode and settable
344-
* on a per chaincode basis rather than taking properties from the Peers'
361+
* Obtains configuration specifically for running the chaincode and settable on
362+
* a per chaincode basis rather than taking properties from the Peers'
345363
* configuration.
346364
*
347365
* @return Configuration
@@ -400,7 +418,9 @@ final SslContext createSSLContext() throws IOException {
400418
final byte[] ccb = Files.readAllBytes(Paths.get(this.tlsClientCertPath));
401419

402420
return GrpcSslContexts.forClient().trustManager(new File(this.tlsClientRootCertPath))
403-
.keyManager(new ByteArrayInputStream(Base64.getDecoder().decode(ccb)), new ByteArrayInputStream(Base64.getDecoder().decode(ckb))).build();
421+
.keyManager(new ByteArrayInputStream(Base64.getDecoder().decode(ccb)),
422+
new ByteArrayInputStream(Base64.getDecoder().decode(ckb)))
423+
.build();
404424
}
405425

406426
@Deprecated

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,10 @@ default void putStringState(final String key, final String value) {
708708
*/
709709
byte[] getBinding();
710710

711+
/**
712+
* Get the MSPID of the peer that started this chaincode.
713+
*
714+
* @return string MSPID
715+
*/
716+
String getMspId();
711717
}

0 commit comments

Comments
 (0)