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

Spotless Integration with Google Java Format #250

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Add the just downloaded jdk to the jenv versions, run: `jenv add /Library/Java/J

If you are using another java version in other projects, run `jenv local 1.8.0.352`. Otherwise you can set it globally by running: `jenv global 1.8.0.352`

To check that it worked, open a new terminal instance and run `java -version` command. It should output something like
To check that it worked, open a new terminal instance and run `java -version` command. It should output something like

```
java version "1.8.0_351"
Expand Down Expand Up @@ -189,7 +189,7 @@ Create a `node.conf` file in powpeg-project directory (this exact file’s struc
```
federator{
enabled = true

signers {
BTC {
type = "keyFile"
Expand Down
32 changes: 32 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id 'application'
id "jacoco"
id "org.sonarqube" version "2.7.1"
id "com.diffplug.spotless" version "6.13.0"
}

def config = new ConfigSlurper().parse(new File("$projectDir/src/main/resources/version.properties").toURI().toURL())
Expand Down Expand Up @@ -170,6 +171,37 @@ artifacts {
archives fatJar
}

spotless {
// only format files which have changed since ARROWHEAD-6.1.0.0
ratchetFrom 'ARROWHEAD-6.1.0.0'

format 'misc', {
target '*.gradle', '*.sh', '*.md', 'Dockerfile', '.gitattributes', '.gitignore'

trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}

java {
// see https://google.github.io/styleguide/javaguide.html
googleJavaFormat()

trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()

// see https://github.com/diffplug/spotless/tree/main/plugin-gradle#license-header
licenseHeaderFile("$projectDir/gradle/spotless/licenseHeaderFile")
}
}

tasks.register('addPreCommitGitHooksOnBuild', Copy) {
from "$projectDir/gradle/spotless/pre-commit"
into "$projectDir/.git/hooks"
}
compileJava.dependsOn addPreCommitGitHooksOnBuild

static def gitCurrentBranch() {
def process = "git rev-parse --abbrev-ref HEAD".execute()
return process.text.trim()
Expand Down
17 changes: 17 additions & 0 deletions gradle/spotless/licenseHeaderFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* This file is part of RskJ
* Copyright (C) $YEAR RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
32 changes: 32 additions & 0 deletions gradle/spotless/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

echo "*********************************************************"
echo "Running git pre-commit hook. Running Spotless Apply... "
echo "*********************************************************"

# Gather the staged files - to make sure changes are saved only for these files.
staged_files=$(git diff --staged --name-only)

# Run spotless apply.
./gradlew spotlessApply -PspotlessSetLicenseHeaderYearsFromGitHistory=true

status=$?

if [ "$status" = 0 ] ; then
echo "Static analysis found no problems."
# Add staged file changes to git.
for file in $staged_files; do
if test -f "$file"; then
git add $file
fi
done
exit 0
else
echo "*********************************************************"
echo " ******************************************** "
echo 1>&2 "Spotless Apply found violations it could not fix."
echo "Run spotless apply in your terminal and fix the issues before trying to commit again."
echo " ******************************************** "
echo "*********************************************************"
exit 1
fi
181 changes: 181 additions & 0 deletions gradle/verification-metadata.xml

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions src/main/java/co/rsk/federate/log/BtcLogMonitor.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
/*
* This file is part of RskJ
* Copyright (C) 2020-2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.federate.log;

import co.rsk.federate.bitcoin.BitcoinWrapper;

public class BtcLogMonitor {

private final BitcoinWrapper bitcoinWrapper;
private final FederateLogger monitoringLogger;
private final BitcoinWrapper bitcoinWrapper;
private final FederateLogger monitoringLogger;

public BtcLogMonitor(BitcoinWrapper bitcoinWrapper, FederateLogger monitoringLogger) {
this.bitcoinWrapper = bitcoinWrapper;
this.monitoringLogger = monitoringLogger;
}
public BtcLogMonitor(BitcoinWrapper bitcoinWrapper, FederateLogger monitoringLogger) {
this.bitcoinWrapper = bitcoinWrapper;
this.monitoringLogger = monitoringLogger;
}

public void start() {
bitcoinWrapper.addNewBlockListener(monitoringLogger::setCurrentBtcBestBlock);
}
public void start() {
bitcoinWrapper.addNewBlockListener(monitoringLogger::setCurrentBtcBestBlock);
}
}
111 changes: 65 additions & 46 deletions src/main/java/co/rsk/federate/log/FederateLogger.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,81 @@
/*
* This file is part of RskJ
* Copyright (C) 2020-2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.federate.log;

import co.rsk.federate.FederatorSupport;
import co.rsk.federate.util.CurrentTimeProvider;
import co.rsk.federate.util.LoggerProvider;
import org.bitcoinj.core.StoredBlock;
import org.ethereum.core.Block;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListenerAdapter;
import org.slf4j.Logger;

import java.util.List;

public class FederateLogger {

private final FederatorSupport federatorSupport;
private final CurrentTimeProvider currentTimeProvider;
private final long minTimeBetweenLogs;
private final long minBlocksBetweenLogs;
private final Logger monitoringLogger;

private long lastLogTime = 0;
private long lastBlockNumberLog = 0;
private Block currentRskBestBlock;
private StoredBlock currentBtcBestBlock;

public FederateLogger(FederatorSupport federatorSupport, CurrentTimeProvider currentTimeProvider, LoggerProvider loggerProvider, long minTimeBetweenLogs, long minBlocksBetweenLogs) {
this.federatorSupport = federatorSupport;
this.currentTimeProvider = currentTimeProvider;
this.minTimeBetweenLogs = minTimeBetweenLogs;
this.minBlocksBetweenLogs = minBlocksBetweenLogs;
this.monitoringLogger = loggerProvider.getLogger();
}
private final FederatorSupport federatorSupport;
private final CurrentTimeProvider currentTimeProvider;
private final long minTimeBetweenLogs;
private final long minBlocksBetweenLogs;
private final Logger monitoringLogger;

public void log() {
long currentRskBestChainHeight = -1;
if (currentRskBestBlock != null) {
currentRskBestChainHeight = currentRskBestBlock.getNumber();
}
long currentBtcBestChainHeight = -1;
if (currentBtcBestBlock != null) {
currentBtcBestChainHeight = currentBtcBestBlock.getHeight();
}
long currentTimeMillis = currentTimeProvider.currentTimeMillis();
if (currentRskBestChainHeight - lastBlockNumberLog > minBlocksBetweenLogs && currentTimeMillis - lastLogTime > minTimeBetweenLogs) {
monitoringLogger.info("RSK height: {}", currentRskBestChainHeight);
monitoringLogger.info("BTC height: {}", currentBtcBestChainHeight);
monitoringLogger.info("Bridge BTC height: {}", federatorSupport.getBtcBlockchainBestChainHeight());
lastBlockNumberLog = currentRskBestChainHeight;
lastLogTime = currentTimeMillis;
}
}
private long lastLogTime = 0;
private long lastBlockNumberLog = 0;
private Block currentRskBestBlock;
private StoredBlock currentBtcBestBlock;

public void setCurrentRskBestBlock(Block currentRskBestBlock) {
this.currentRskBestBlock = currentRskBestBlock;
}
public FederateLogger(
FederatorSupport federatorSupport,
CurrentTimeProvider currentTimeProvider,
LoggerProvider loggerProvider,
long minTimeBetweenLogs,
long minBlocksBetweenLogs) {
this.federatorSupport = federatorSupport;
this.currentTimeProvider = currentTimeProvider;
this.minTimeBetweenLogs = minTimeBetweenLogs;
this.minBlocksBetweenLogs = minBlocksBetweenLogs;
this.monitoringLogger = loggerProvider.getLogger();
}

public void setCurrentBtcBestBlock(StoredBlock currentBtcBestBlock) {
this.currentBtcBestBlock = currentBtcBestBlock;
public void log() {
long currentRskBestChainHeight = -1;
if (currentRskBestBlock != null) {
currentRskBestChainHeight = currentRskBestBlock.getNumber();
}
long currentBtcBestChainHeight = -1;
if (currentBtcBestBlock != null) {
currentBtcBestChainHeight = currentBtcBestBlock.getHeight();
}
long currentTimeMillis = currentTimeProvider.currentTimeMillis();
if (currentRskBestChainHeight - lastBlockNumberLog > minBlocksBetweenLogs
&& currentTimeMillis - lastLogTime > minTimeBetweenLogs) {
monitoringLogger.info("RSK height: {}", currentRskBestChainHeight);
monitoringLogger.info("BTC height: {}", currentBtcBestChainHeight);
monitoringLogger.info(
"Bridge BTC height: {}", federatorSupport.getBtcBlockchainBestChainHeight());
lastBlockNumberLog = currentRskBestChainHeight;
lastLogTime = currentTimeMillis;
}
}

public void setCurrentRskBestBlock(Block currentRskBestBlock) {
this.currentRskBestBlock = currentRskBestBlock;
}

public void setCurrentBtcBestBlock(StoredBlock currentBtcBestBlock) {
this.currentBtcBestBlock = currentBtcBestBlock;
}
}
49 changes: 33 additions & 16 deletions src/main/java/co/rsk/federate/log/RskLogMonitor.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
/*
* This file is part of RskJ
* Copyright (C) 2020-2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.federate.log;

import java.util.List;
import org.ethereum.core.Block;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListenerAdapter;

import java.util.List;

public class RskLogMonitor {

private final Ethereum ethereum;
private final FederateLogger federateLogger;
private final Ethereum ethereum;
private final FederateLogger federateLogger;

public RskLogMonitor(Ethereum ethereum, FederateLogger federateLogger) {
this.ethereum = ethereum;
this.federateLogger = federateLogger;
}
public RskLogMonitor(Ethereum ethereum, FederateLogger federateLogger) {
this.ethereum = ethereum;
this.federateLogger = federateLogger;
}

public void start() {
ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void onBestBlock(Block block, List<TransactionReceipt> receipts) {
federateLogger.setCurrentRskBestBlock(block);
federateLogger.log();
}
public void start() {
ethereum.addListener(
new EthereumListenerAdapter() {
@Override
public void onBestBlock(Block block, List<TransactionReceipt> receipts) {
federateLogger.setCurrentRskBestBlock(block);
federateLogger.log();
}
});
}
}
}
Loading