Browse Source

Convert logging to lambda-based to reduce CPU load if logging disabled, also bump to v1.3.6 as interface changed

master
catbref 4 years ago
parent
commit
facb9c213f
  1. 2
      Java/pom.xml
  2. 8
      Java/src/main/java/org/ciyam/at/AtLogger.java
  3. 2
      Java/src/main/java/org/ciyam/at/FunctionCode.java
  4. 23
      Java/src/main/java/org/ciyam/at/MachineState.java
  5. 17
      Java/src/test/java/org/ciyam/at/test/TestLogger.java

2
Java/pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.ciyam</groupId>
<artifactId>AT</artifactId>
<version>1.3.5</version>
<version>1.3.6</version>
<packaging>jar</packaging>
<properties>
<skipTests>true</skipTests>

8
Java/src/main/java/org/ciyam/at/AtLogger.java

@ -1,11 +1,19 @@
package org.ciyam.at;
import java.util.function.Supplier;
public interface AtLogger {
void error(final String message);
void error(final Supplier<String> messageSupplier);
void debug(final String message);
void debug(final Supplier<String> messageSupplier);
void echo(final String message);
void echo(final Supplier<String> messageSupplier);
}

2
Java/src/main/java/org/ciyam/at/FunctionCode.java

@ -1068,7 +1068,7 @@ public enum FunctionCode {
if (functionData.paramCount == 2 && functionData.value2 == null)
throw new IllegalFunctionCodeException("Passed value2 is null but function has paramCount of (" + this.paramCount + ")");
state.getLogger().debug("Function \"" + this.name() + "\"");
state.getLogger().debug(() -> String.format("Function \"%s\"", this.name()));
postCheckExecute(functionData, state, rawFunctionCode);
}

23
Java/src/main/java/org/ciyam/at/MachineState.java

@ -675,18 +675,17 @@ public class MachineState {
// Pre-execution checks
if (this.isFinished) {
logger.debug("Not executing as already finished!");
logger.debug(() -> "Not executing as already finished!");
return;
}
if (this.isFrozen && this.currentBalance <= this.frozenBalance) {
logger.debug("Not executing as current balance [" + this.currentBalance + "] hasn't increased since being frozen at [" + this.frozenBalance + "]");
logger.debug(() -> String.format("Not executing as current balance [%d] hasn't increased since being frozen at [%d]", this.currentBalance, this.frozenBalance));
return;
}
if (this.isSleeping && this.sleepUntilHeight != null && this.currentBlockHeight < this.sleepUntilHeight) {
logger.debug("Not executing as current block height [" + this.currentBlockHeight + "] hasn't reached sleep-until block height ["
+ this.sleepUntilHeight + "]");
logger.debug(() -> String.format("Not executing as current block height [%d] hasn't reached sleep-until block height [%d]", this.currentBlockHeight, this.sleepUntilHeight));
return;
}
@ -716,21 +715,21 @@ public class MachineState {
if (nextOpCode == null)
throw new IllegalOperationException("OpCode 0x" + String.format("%02x", rawOpCode) + " not recognised");
this.logger.debug("[PC: " + String.format("%04x", this.programCounter) + "] " + nextOpCode.name());
this.logger.debug(() -> String.format("[PC: %04x] %s", this.programCounter, nextOpCode.name()));
// Request opcode step-fee from API, apply fee to balance, etc.
int opcodeSteps = this.api.getOpCodeSteps(nextOpCode);
long opcodeFee = opcodeSteps * feePerStep;
if (this.steps + opcodeSteps > maxSteps) {
logger.debug("Enforced sleep due to exceeding maximum number of steps (" + maxSteps + ") per execution round");
logger.debug(() -> String.format("Enforced sleep due to exceeding maximum number of steps (%d) per execution round", maxSteps));
this.isSleeping = true;
break;
}
if (this.currentBalance < opcodeFee) {
// Not enough balance left to continue execution - freeze AT
logger.debug("Frozen due to lack of balance");
logger.debug(() -> "Frozen due to lack of balance");
this.isFrozen = true;
this.frozenBalance = this.currentBalance;
break;
@ -746,7 +745,7 @@ public class MachineState {
// Synchronize programCounter with codeByteBuffer in case of JMPs, branches, etc.
this.programCounter = codeByteBuffer.position();
} catch (ExecutionException e) {
this.logger.debug("Error at PC " + String.format("%04x", this.programCounter) + ": " + e.getMessage());
this.logger.error(() -> String.format("Error at PC %04x: %s", this.programCounter, e.getMessage()));
if (this.onErrorAddress == null) {
this.isFinished = true;
@ -767,18 +766,18 @@ public class MachineState {
if (this.isSleeping) {
if (this.sleepUntilHeight != null)
this.logger.debug("Sleeping until block " + this.sleepUntilHeight);
this.logger.debug(() -> String.format("Sleeping until block %d", this.sleepUntilHeight));
else
this.logger.debug("Sleeping until next block");
this.logger.debug(() -> "Sleeping until next block");
}
if (this.isStopped) {
this.logger.debug("Setting program counter to stop address: " + String.format("%04x", this.onStopAddress));
this.logger.debug(() -> String.format("Setting program counter to stop address: %04x", this.onStopAddress));
this.programCounter = this.onStopAddress;
}
if (this.isFinished) {
this.logger.debug("Finished - refunding remaining funds back to creator");
this.logger.debug(() -> "Finished - refunding remaining funds back to creator");
this.api.onFinished(this.currentBalance, this);
this.currentBalance = 0;
}

17
Java/src/test/java/org/ciyam/at/test/TestLogger.java

@ -1,5 +1,7 @@
package org.ciyam.at.test;
import java.util.function.Supplier;
import org.ciyam.at.AtLogger;
public class TestLogger implements AtLogger {
@ -9,14 +11,29 @@ public class TestLogger implements AtLogger {
System.err.println("ERROR: " + message);
}
@Override
public void error(Supplier<String> messageSupplier) {
System.err.println("ERROR: " + messageSupplier.get());
}
@Override
public void debug(String message) {
System.err.println("DEBUG: " + message);
}
@Override
public void debug(Supplier<String> messageSupplier) {
System.err.println("DEBUG: " + messageSupplier.get());
}
@Override
public void echo(String message) {
System.err.println("ECHO: " + message);
}
@Override
public void echo(Supplier<String> messageSupplier) {
System.err.println("ECHO: " + messageSupplier.get());
}
}

Loading…
Cancel
Save