Skip to content
Open
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
16 changes: 16 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ jobs:
- name: Test with Gradle
run: xvfb-run ./gradlew test --info

code_quality:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 11
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: run style check on main source sets
run: ./gradlew checkstyleMain --info
- name: run style check on test source sets
run: ./gradlew checkstyleTest --info

7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ subprojects {
apply plugin: 'java'
apply plugin: 'org.javamodularity.moduleplugin'

apply plugin: 'checkstyle'

checkstyle {
toolVersion = '8.29'
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
}

repositories {
jcenter()
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sigblockchain.projectred.client;


import com.google.gson.Gson;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
import com.google.gson.Gson;

public class AurumClient {

Expand Down
65 changes: 43 additions & 22 deletions client/src/main/java/sigblockchain/projectred/client/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,34 @@ public class Block {
private final byte[][] data; // [][]byte

/**
* Stores data (typically contracts). The entire block is either added to the blockchain or the entirety
* of it is rejected.
*
* @param version Version of the blockChain the block was created on
* @param height A hex encoded 2's complement 64 bit integer string (without the prefix '0x'). Note, the height cannot be negative Represents the height of the block in the blockChain.
* @param height A hex encoded 2's complement 64 bit integer string (without the prefix '0x'). Note,
* the height cannot be negative Represents the height of the block in the blockChain.
* @param timestamp Unix time, representing the number of nanoseconds elapsed since January 1, 1970 UTC
* @param previousHash Hash of the previous block
* @param merkleRootHash Hash of the merkle-root of all inputs
* @param dataLen Number of objects in the Data section
* @param data Actual contents of block
*/
public Block(int version, String height, long timestamp, byte[] previousHash, byte[] merkleRootHash, int dataLen, byte[][] data) {

public Block(int version, String height, long timestamp, byte[] previousHash,
byte[] merkleRootHash, int dataLen, byte[][] data) {
//Individual values Checking
if (checkUnsigned16(version)) {
throw new IllegalArgumentException("Invalid Input version");
}
if (!checkUnsigned64(new BigInteger(height, 16))) {
throw new IllegalArgumentException("Invalid Input height");
}
if (!checkSigned64(timestamp)) {
throw new IllegalArgumentException("Invalid Input timeStamp");
}
if (checkUnsigned16(dataLen)) {
throw new IllegalArgumentException("Invalid Input dataLen");
}
if (checkUnsigned16(version)) {
throw new IllegalArgumentException("Invalid Input version");
}
if (!checkUnsigned64(new BigInteger(height, 16))) {
throw new IllegalArgumentException("Invalid Input height");
}
if (!checkSigned64(timestamp)) {
throw new IllegalArgumentException("Invalid Input timeStamp");
}
if (checkUnsigned16(dataLen)) {
throw new IllegalArgumentException("Invalid Input dataLen");
}

this.height = new BigInteger(height, 16);
this.version = version;
Expand All @@ -49,57 +55,72 @@ private boolean checkUnsigned16(int value) {
}

private boolean checkUnsigned64(BigInteger value) {
return (value.compareTo(BigInteger.ZERO) >= 0) && (value.compareTo(new BigInteger("ffffffffffffffff", 16)) <= 0);
return (value.compareTo(BigInteger.ZERO) >= 0)
&& (value.compareTo(new BigInteger("ffffffffffffffff", 16)) <= 0);
}

private boolean checkSigned64(long value) {
return (value >= -(Math.pow(2, 63))) && value < (Math.pow(2, 63));
}

/**
* @return This function returns the value of version from the data
* Returns the version of Aurum in which the block was added/created.
*
* @return int value of version. Will be guaranteed to be unsigned 16bit value
*/
public int getVersion() {
return this.version;
}

/**
* @return This function returns the value of height from the data
* Returns height of the block.
*
* @return int value of the height of the block. Will be positive value less than 2^64
*/
public BigInteger getHeight() {
return this.height;
}

/**
* @return This function returns the value of timestamp from the data
* Returns the unix timestamp of block.
*
* @return long timestamp.
*/
public long getTimestamp() {
return this.timestamp;
}

/**
* @return This function returns the value of previousHash from the data
* The hash of the previous block.
*
* @return byte array representing the hash
*/
public byte[] getPreviousHash() {
return this.previousHash;
}

/**
* @return This function returns the value of merkleRootHash from the data
* Returns the merkle root hash of the block.
*
* @return byte array representing the hash
*/
public byte[] getMerkleRootHash() {
return this.merkleRootHash;
}

/**
* @return This function returns the value of dataLen from the data
* Returns the length of data present in the block.
*
* @return int value, garuanteed to be unsigned 16 bit value
*/
public int getDataLen() {
return this.dataLen;
}

/**
* @return This function returns the data from the data
* Returns the data actually present in the block.
*
* @return An array of byte arrays.
*/
public byte[][] getData() {
return this.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;


import java.math.BigInteger;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -39,11 +38,14 @@ public void testAccountTest3() {
var addr = "81aaf16d7e4b626dc1c34c47bf9973496a3698a6e7ab0255af867169b43529fb";
var balance = new BigInteger("-2", 16);
var nonce = new BigInteger("-2", 16);

try {
Account account = new Account(addr, balance, nonce);
fail("Account object was allowed to be made with invalid arguments");
} catch (Exception ignored) {
} catch (Exception e) {
return;
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;


import io.specto.hoverfly.junit.core.Hoverfly;
import io.specto.hoverfly.junit.rule.HoverflyRule;
import io.specto.hoverfly.junit5.HoverflyExtension;
Expand All @@ -30,6 +29,9 @@ public class AurumClientTest {

private static HoverflyRule hoverflyRule;

/**
* Gets a JSON resources from the resources directory.
*/
@BeforeAll
public static void setUpJsonResponse() {
var inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(jsonFile);
Expand All @@ -42,12 +44,11 @@ public static void setUpJsonResponse() {
}



@Test
public void testGetAccount(Hoverfly hoverfly) {
hoverfly.simulate(dsl(
service(host).
get(startsWith("/accountinfo"))
service(host)
.get(startsWith("/accountinfo"))
.queryParam("w", validAddress)
.willReturn(
success(validJson, "application/json"))
Expand Down
112 changes: 54 additions & 58 deletions client/src/test/java/sigblockchain/projectred/client/BlockTest.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
package sigblockchain.projectred.client;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.math.BigInteger;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class BlockTest {

private byte[] previousHash = "Previous Hash".getBytes();
private byte[] hash = "Block hash".getBytes();
private byte[][] data =
{
{1, 2, 3, 4},
{'H', 'E', 'L', 'L', 'O'},
{5, 6, 7, 8},
{'W', 'O', 'R', 'L', 'D'}
};
private byte[] previousHash = "Previous Hash".getBytes();
private byte[] hash = "Block hash".getBytes();
private byte[][] data = {{1, 2, 3, 4}, {'H', 'E', 'L', 'L', 'O'}, {5, 6, 7, 8}, {'W', 'O', 'R', 'L', 'D'}};


@Test
void testConstructor() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
assertNotNull(block);
}
@Test
void testConstructor() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
assertNotNull(block);
}

@Test
void testGetVersion() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
int v = block.getVersion();
assertEquals(v, 10);
}
@Test
void testGetVersion() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
int v = block.getVersion();
assertEquals(v, 10);
}

@Test
void testGetHeight() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
var v = block.getHeight();
assertEquals(v, new BigInteger("2B", 16));
}
@Test
void testGetHeight() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
var v = block.getHeight();
assertEquals(v, new BigInteger("2B", 16));
}

@Test
void testGetTimestamp() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
long v = block.getTimestamp();
assertEquals(v, -3556L);
}
@Test
void testGetTimestamp() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
long v = block.getTimestamp();
assertEquals(v, -3556L);
}

@Test
void testGetPreviousHash() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[] v = block.getPreviousHash();
assertEquals(v, previousHash);
}
@Test
void testGetPreviousHash() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[] v = block.getPreviousHash();
assertEquals(v, previousHash);
}

@Test
void testGetMerkleRootHash() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[] v = block.getMerkleRootHash();
assertEquals(v, hash);
}
@Test
void testGetMerkleRootHash() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[] v = block.getMerkleRootHash();
assertEquals(v, hash);
}

@Test
void testGetDataLen() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
int v = block.getDataLen();
assertEquals(v, 10);
}
@Test
void testGetDataLen() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
int v = block.getDataLen();
assertEquals(v, 10);
}

@Test
void testGetData() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[][] v = block.getData();
assertEquals(v, data);
}
@Test
void testGetData() {
Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data);
byte[][] v = block.getData();
assertEquals(v, data);
}
}
Loading