Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Modernize ChaincodeBase class #357

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,16 @@ protected final void connectToPeer() throws IOException {

LOGGER.info("making the grpc call");
// for any error - shut everything down
bhaskar-allam marked this conversation as resolved.
Show resolved Hide resolved
// as this is long lived (well forever) then any completion means something
// has stopped in the peer or the network comms, so also shutdown
final StreamObserver<ChaincodeMessage> requestObserver = chaincodeSupportClient.getStub().register(

new StreamObserver<ChaincodeMessage>() {
@Override
public void onNext(final ChaincodeMessage chaincodeMessage) {
// message off to the ITM...
itm.onChaincodeMessage(chaincodeMessage);
}

@Override
public void onError(final Throwable t) {
LOGGER.severe(
() -> "An error occured on the chaincode stream. Shutting down the chaincode stream."
+ Logging.formatError(t));

LOGGER.severe(() -> "An error occurred on the chaincode stream. Shutting down the chaincode stream." + Logging.formatError(t));
chaincodeSupportClient.shutdown(itm);
}

Expand All @@ -226,7 +219,6 @@ public void onCompleted() {
chaincodeSupportClient.shutdown(itm);
}
}

);

chaincodeSupportClient.start(itm, requestObserver);
Expand Down Expand Up @@ -287,48 +279,40 @@ public void onCompleted() {
}

protected final void initializeLogging() {
// the VM wide formatting string.
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");
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");
final Logger rootLogger = Logger.getLogger("");

for (final java.util.logging.Handler handler : rootLogger.getHandlers()) {
for (Handler handler : rootLogger.getHandlers()) {
handler.setLevel(ALL);
handler.setFormatter(new SimpleFormatter() {

@Override
public synchronized String format(final LogRecord record) {
return Thread.currentThread() + " " + super.format(record);
}

});
}

rootLogger.info("Updated all handlers the format");
// set logging level of chaincode logger
final Level chaincodeLogLevel = mapLevel(System.getenv(CORE_CHAINCODE_LOGGING_LEVEL));

final Package chaincodePackage = this.getClass().getPackage();
if (chaincodePackage != null) {
Logger.getLogger(chaincodePackage.getName()).setLevel(chaincodeLogLevel);
Package pkg = this.getClass().getPackage();
if (pkg != null) {
Logger.getLogger(pkg.getName()).setLevel(chaincodeLogLevel);
} else {
// If chaincode declared without package, i.e. default package, lets set level
// to root logger
// Chaincode should never be declared without package
Logger.getLogger("").setLevel(chaincodeLogLevel);
}

// set logging level of shim logger
final Level shimLogLevel = mapLevel(System.getenv(CORE_CHAINCODE_LOGGING_SHIM));
Logger.getLogger(ChaincodeBase.class.getPackage().getName()).setLevel(shimLogLevel);
Logger.getLogger(ContractRouter.class.getPackage().getName()).setLevel(chaincodeLogLevel);

}

private Level mapLevel(final String level) {

if (level != null) {
switch (level.toUpperCase().trim()) {
if (level == null) {
return Level.INFO;
}

switch (level.toUpperCase().trim()) {
case "CRITICAL":
case "ERROR":
return Level.SEVERE;
Expand All @@ -342,10 +326,8 @@ private Level mapLevel(final String level) {
case "DEBUG":
return Level.FINEST;
default:
break;
}
return Level.INFO;
}
return Level.INFO;
}


Expand Down Expand Up @@ -442,53 +424,47 @@ protected final void processCommandLineOptions(final String[] args) {
* set fields from env.
*/
public final void processEnvironmentOptions() {

if (System.getenv().containsKey(CORE_CHAINCODE_ID_NAME)) {
this.id = System.getenv(CORE_CHAINCODE_ID_NAME);
}
if (System.getenv().containsKey(CORE_PEER_ADDRESS)) {
final String[] hostArr = System.getenv(CORE_PEER_ADDRESS).split(":");
this.id = System.getenv().getOrDefault(CORE_CHAINCODE_ID_NAME, this.id);

String address = System.getenv(CORE_PEER_ADDRESS);
if (address != null) {
String[] hostArr = address.split(":");
if (hostArr.length == 2) {
this.port = Integer.valueOf(hostArr[1].trim());
this.port = Integer.parseInt(hostArr[1].trim());
this.host = hostArr[0].trim();
} else {
final String msg = String.format(
"peer address argument should be in host:port format, ignoring current %s",
System.getenv(CORE_PEER_ADDRESS));
LOGGER.severe(msg);
LOGGER.severe(() -> String.format("peer address argument should be in host:port format, ignoring current %s", address));
}
}

if (System.getenv().containsKey(CHAINCODE_SERVER_ADDRESS)) {
this.chaincodeServerAddress = System.getenv(CHAINCODE_SERVER_ADDRESS);
}

if (System.getenv().containsKey(CORE_PEER_LOCALMSPID)) {
this.localMspId = System.getenv(CORE_PEER_LOCALMSPID);
}
this.chaincodeServerAddress = System.getenv().getOrDefault(CHAINCODE_SERVER_ADDRESS, this.chaincodeServerAddress);
this.localMspId = System.getenv().getOrDefault(CORE_PEER_LOCALMSPID, this.localMspId);

this.tlsEnabled = Boolean.parseBoolean(System.getenv(CORE_PEER_TLS_ENABLED));
if (this.tlsEnabled) {
this.tlsClientRootCertPath = System.getenv(CORE_PEER_TLS_ROOTCERT_FILE);
this.tlsClientKeyPath = System.getenv(ENV_TLS_CLIENT_KEY_PATH);
this.tlsClientCertPath = System.getenv(ENV_TLS_CLIENT_CERT_PATH);

this.tlsClientKeyFile = System.getenv(ENV_TLS_CLIENT_KEY_FILE);
this.tlsClientCertFile = System.getenv(ENV_TLS_CLIENT_CERT_FILE);
}

LOGGER.info("<<<<<<<<<<<<<Environment options>>>>>>>>>>>>");
LOGGER.info("CORE_CHAINCODE_ID_NAME: " + this.id);
LOGGER.info("CORE_PEER_ADDRESS: " + this.host);
LOGGER.info("CORE_PEER_TLS_ENABLED: " + this.tlsEnabled);
LOGGER.info("CORE_PEER_TLS_ROOTCERT_FILE: " + this.tlsClientRootCertPath);
LOGGER.info("CORE_TLS_CLIENT_KEY_PATH: " + this.tlsClientKeyPath);
LOGGER.info("CORE_TLS_CLIENT_CERT_PATH: " + this.tlsClientCertPath);
LOGGER.info("CORE_TLS_CLIENT_KEY_FILE: " + this.tlsClientKeyFile);
LOGGER.info("CORE_TLS_CLIENT_CERT_FILE: " + this.tlsClientCertFile);
LOGGER.info("CORE_PEER_LOCALMSPID: " + this.localMspId);
LOGGER.info("CHAINCODE_SERVER_ADDRESS: " + this.chaincodeServerAddress);
LOGGER.info("LOGLEVEL: " + this.logLevel);
LOGGER.info(() -> String.format(
"<<<<<<<<<<<<<Environment options>>>>>>>>>>>>\n" +
"CORE_CHAINCODE_ID_NAME: %s\n" +
"CORE_PEER_ADDRESS: %s\n" +
"CORE_PEER_TLS_ENABLED: %s\n" +
"CORE_PEER_TLS_ROOTCERT_FILE: %s\n" +
"CORE_TLS_CLIENT_KEY_PATH: %s\n" +
"CORE_TLS_CLIENT_CERT_PATH: %s\n" +
"CORE_TLS_CLIENT_KEY_FILE: %s\n" +
"CORE_TLS_CLIENT_CERT_FILE: %s\n" +
"CORE_PEER_LOCALMSPID: %s\n" +
"CHAINCODE_SERVER_ADDRESS: %s\n" +
"LOGLEVEL: %s",
this.id, this.host, this.tlsEnabled, this.tlsClientRootCertPath,
this.tlsClientKeyPath, this.tlsClientCertPath, this.tlsClientKeyFile,
this.tlsClientCertFile, this.localMspId, this.chaincodeServerAddress, this.logLevel));
}

/**
Expand All @@ -500,26 +476,20 @@ public final void processEnvironmentOptions() {
*/
public Properties getChaincodeConfig() {
if (this.props == null) {
this.props = new Properties();

final ClassLoader cl = this.getClass().getClassLoader();
// determine the location of the properties file to control the metrics etc.

props = new Properties();

try (InputStream inStream = cl.getResourceAsStream("config.props")) {
try (InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("config.props")) {
if (inStream != null) {
props.load(inStream);
}
} catch (final IOException e) {
LOGGER.warning(() -> "Can not open the properties file for input " + Logging.formatError(e));
LOGGER.warning(() -> "Cannot open the properties file for input " + Logging.formatError(e));
}

// will be useful
props.setProperty(CORE_CHAINCODE_ID_NAME, this.id);
props.setProperty(CORE_PEER_ADDRESS, this.host);

LOGGER.info("<<<<<<<<<<<<<Properties options>>>>>>>>>>>>");
LOGGER.info(() -> this.props.toString());
LOGGER.info(() -> "<<<<<<<<<<<<<Properties options>>>>>>>>>>>>\n" + this.props);
}

return this.props;
Expand Down
Loading