Compare commits
1 Commits
master
...
feature/po
Author | SHA1 | Date | |
---|---|---|---|
|
9f0d2c1b7d |
1
pom.xml
1
pom.xml
@ -63,6 +63,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
<module>power-xml</module>
|
||||
<module>power-reflection</module>
|
||||
<module>power-mail</module>
|
||||
<module>power-blockchain</module>
|
||||
|
||||
<module>power-security</module>
|
||||
<!-- TODO:
|
||||
|
122
power-blockchain/pom.xml
Normal file
122
power-blockchain/pom.xml
Normal file
@ -0,0 +1,122 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library 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;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library 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 library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-blockchain</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power Blockchain</name>
|
||||
<description>Blockchain library</description>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.nanoboot.powerframework.blockchain.orig.core.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id> <!-- this is used for inheritance merges -->
|
||||
<phase>package</phase> <!-- bind to the packaging phase -->
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.nanoboot.powerframework.blockchain.orig.core.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Power dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-core</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-time</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-json</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-random</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-security</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.10</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
34
power-blockchain/src/main/java/module-info.java
Normal file
34
power-blockchain/src/main/java/module-info.java
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
module powerframework.blockchain {
|
||||
requires lombok;
|
||||
requires powerframework.core;
|
||||
requires powerframework.time;
|
||||
requires powerframework.json;
|
||||
requires powerframework.random;
|
||||
requires powerframework.security;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockDeserializer {
|
||||
Block deserialize(String serializedBlock);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockSerializer {
|
||||
String serialize(Block block);
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.moment.UniversalDateTime;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Block implements JsonObjectSerializable {
|
||||
private static final String BLOCKCHAIN_PROTOCOL = "blockchainProtocol";
|
||||
|
||||
private static final String HEIGHT = "height";
|
||||
|
||||
private static final String PREVIOUS_HASH = "previousHash";
|
||||
private static final String TIMESTAMP = "timestamp";
|
||||
private static final String DIFFICULTY = "difficulty";
|
||||
private static final String DATA = "data";
|
||||
private static final String NONCE = "nonce";
|
||||
|
||||
@Getter
|
||||
private final BlockchainProtocol blockchainProtocol;
|
||||
|
||||
@Getter
|
||||
private final long height;
|
||||
@Getter
|
||||
private final String previousHash;
|
||||
@Getter
|
||||
private final String timeStamp;
|
||||
@Getter
|
||||
private final String difficultyTarget;
|
||||
@Getter
|
||||
private final byte[] data;
|
||||
|
||||
@Getter
|
||||
private long nonce;
|
||||
|
||||
//
|
||||
@Getter
|
||||
private String hash = null;
|
||||
|
||||
//Block Constructor.
|
||||
public Block(BlockchainProtocol blockchainProtocol,
|
||||
long height,
|
||||
String previousHash,
|
||||
String difficultyTarget,
|
||||
byte[] data) {
|
||||
this.blockchainProtocol=blockchainProtocol;
|
||||
this.height = height;
|
||||
this.previousHash = previousHash;
|
||||
this.timeStamp = UniversalDateTime.now().toString();
|
||||
this.difficultyTarget = difficultyTarget;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
//Calculate new hash based on blocks contents
|
||||
public String calculateHash(HashCalculator hashCalculator, BlockSerializer blockSerializer) {
|
||||
String serializedBlock = blockSerializer.serialize(this);
|
||||
String calculatedhash = hashCalculator.hash(serializedBlock);
|
||||
return calculatedhash;
|
||||
}
|
||||
|
||||
//Increases nonce value until hash target is reached.
|
||||
public void mineBlock(HashCalculator hashCalculator, BlockSerializer blockSerializer) {
|
||||
String target = this.difficultyTarget;
|
||||
|
||||
for (long i = 0; i <= Long.MAX_VALUE; i++) {
|
||||
this.nonce = i;
|
||||
if (nonce == Integer.MAX_VALUE) {
|
||||
throw new BlockChainException("Nonce was not found.");
|
||||
}
|
||||
if (nonce % 1000000 == 0) {
|
||||
System.out.println("Nonce # " + nonce);
|
||||
}
|
||||
|
||||
|
||||
|
||||
String hash = calculateHash(hashCalculator, blockSerializer);
|
||||
|
||||
if (hashCalculator.compareHexNumbers(hash, this.getDifficultyTarget()) <= 0) {
|
||||
System.out.println("Found nonce: " + nonce + " A new block was just mined: " + getHash() + "\n" /*+ block.toJsonObject().toPrettyString()*/);
|
||||
System.out.println("Block Mined!!! : " + hash);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
|
||||
JsonObject jo = new JsonObject();
|
||||
|
||||
jo.add(BLOCKCHAIN_PROTOCOL, blockchainProtocol.toJsonObject());
|
||||
|
||||
jo.add(HEIGHT, height);
|
||||
|
||||
jo.add(PREVIOUS_HASH, previousHash);
|
||||
jo.add(TIMESTAMP, timeStamp);
|
||||
jo.add(DIFFICULTY, difficultyTarget);
|
||||
jo.add(DATA, new String(data));
|
||||
jo.add(NONCE, nonce);
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerException;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockChainException extends PowerException {
|
||||
public BlockChainException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainEngine {
|
||||
@Getter
|
||||
private final String protocolName;
|
||||
private List<BlockchainEngineVersion> blockchainEngineVersionList = new ArrayList<>();
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainEngineVersion {
|
||||
@Getter
|
||||
private final String version;
|
||||
@Getter
|
||||
private final String hashCalculatorName;
|
||||
@Getter
|
||||
private final String targetDifficulty;
|
||||
@Getter
|
||||
private final BlockSerializer blockSerializer;
|
||||
@Getter
|
||||
private final long validFromBlock;
|
||||
@Getter
|
||||
private final long validUntilBlock;
|
||||
|
||||
public String getMayorVersion() {
|
||||
return version.split(("\\."))[0];
|
||||
}
|
||||
public String getMinorVersion() {
|
||||
return version.split(("\\."))[1];
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainProtocol implements JsonObjectSerializable {
|
||||
private static final String PROTOCOL_NAME = "protocolName";
|
||||
private static final String PROTOCOL_MAYOR_VERSION = "protocolMayorVersion";
|
||||
private static final String PROTOCOL_MINOR_VERSION = "protocolMinorVersion";
|
||||
@Getter
|
||||
private final String protocolName;
|
||||
@Getter
|
||||
private final int protocolMayorVersion;
|
||||
@Getter
|
||||
private final int protocolMinorVersion;
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
|
||||
JsonObject jo = new JsonObject();
|
||||
|
||||
jo.add(PROTOCOL_NAME, protocolName);
|
||||
jo.add(PROTOCOL_MAYOR_VERSION, protocolMayorVersion);
|
||||
jo.add(PROTOCOL_MINOR_VERSION, protocolMinorVersion);
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class JsonBlockSerializer implements BlockSerializer {
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
JsonObject jsonObject = block.toJsonObject();
|
||||
return jsonObject.toMinimalString();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class StringSerializer implements BlockSerializer {
|
||||
|
||||
public static final String SEPARATOR = "::";
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
return block.getBlockchainProtocol().toString() + SEPARATOR +
|
||||
block.getHeight() + SEPARATOR +
|
||||
block.getPreviousHash() + SEPARATOR +
|
||||
block.getTimeStamp() + SEPARATOR +
|
||||
block.getDifficultyTarget() + SEPARATOR +
|
||||
new String(block.getData()) + SEPARATOR +
|
||||
block.getNonce();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockData {
|
||||
String convertToString();
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockDeserializer {
|
||||
Block deserialize(String serializedBlock);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockFragment;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockMiner {
|
||||
Block mine(BlockFragment blockFragment, BlockType blockType);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockSerializer {
|
||||
String serialize(Block block);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockValidator {
|
||||
BlockType getBlockType();
|
||||
void validate(Block block);
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Block implements JsonObjectSerializable {
|
||||
public static final String BLOCK_TYPE_ID = "blockTypeId";
|
||||
public static final String BLOCK_HEADER = "blockHeader";
|
||||
public static final String BLOCK_DATA = "blockData";
|
||||
public static final String NONCE = "nonce";
|
||||
|
||||
@Getter
|
||||
private BlockHeader blockHeader;
|
||||
|
||||
@Getter
|
||||
private BlockData blockData;
|
||||
|
||||
private long nonce;
|
||||
|
||||
@Getter
|
||||
private String hash;
|
||||
@Getter
|
||||
private boolean locked = false;
|
||||
|
||||
@Getter
|
||||
private BlockType internalBlockType;
|
||||
|
||||
public Block(BlockHeader blockHeader, JsonObject additionBlockHeader, BlockData blockData) {
|
||||
this.blockHeader = blockHeader;
|
||||
|
||||
this.blockData = blockData;
|
||||
}
|
||||
|
||||
public void injectBlockType(BlockType blockType) {
|
||||
this.internalBlockType = blockType;
|
||||
}
|
||||
|
||||
public String calculateHashTest(long nonce) {
|
||||
this.nonce = nonce;
|
||||
String hashedSerializedBlock = internalCalculateHash();
|
||||
this.nonce = 0;//todo
|
||||
return hashedSerializedBlock;
|
||||
}
|
||||
|
||||
private String internalCalculateHash() {
|
||||
if (nonce == 0) {
|
||||
throw new BlockChainException("Nonce cannot be zero.");
|
||||
}
|
||||
if (locked) {
|
||||
throw new BlockChainException("This block is already locked. Cannot set nonce and calculate nonce.");
|
||||
}
|
||||
|
||||
if (internalBlockType == null) {
|
||||
throw new BlockChainException("internalBlockType was not injected.");
|
||||
}
|
||||
String serializedBlock = this.internalBlockType.getBlockSerializer().serialize(this);
|
||||
|
||||
String hashedSerializedBlock = this.internalBlockType.getHashCalculator().hash(serializedBlock);
|
||||
|
||||
//System.out.println("internalCalculateHash - hash for nonce " + nonce + " = " + hashedSerializedBlock);
|
||||
return hashedSerializedBlock;
|
||||
}
|
||||
|
||||
public void lock(long nonce) {
|
||||
this.nonce = nonce;
|
||||
String hashedSerializedBlock = internalCalculateHash();
|
||||
this.hash = hashedSerializedBlock;
|
||||
|
||||
this.locked = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
if (nonce == 0) {
|
||||
throw new BlockChainException("Nonce cannot be zero, if json object is created from this block instance.");
|
||||
}
|
||||
if (internalBlockType == null) {
|
||||
throw new BlockChainException("internalBlockType was not injected.");
|
||||
}
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.add(BLOCK_TYPE_ID, internalBlockType.getBlockTypeId());
|
||||
jo.add(BLOCK_HEADER, blockHeader.toJsonObject());
|
||||
|
||||
jo.add(BLOCK_DATA, blockData.convertToString());
|
||||
jo.add(NONCE, nonce);
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockChain {
|
||||
private String blockChainEngineName;
|
||||
private List<Block> blocks = new ArrayList<>();
|
||||
|
||||
public int getTotalHeight() {
|
||||
return blocks.size() - 1;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return blocks.isEmpty();
|
||||
}
|
||||
|
||||
public Block getGenesisBlock() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getBlock(0);
|
||||
}
|
||||
|
||||
public Block getLastBlock() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getBlock(blocks.size() - 1);
|
||||
}
|
||||
|
||||
public List<Block> getLastXBlocks(int xCount) {
|
||||
List<Block> result = new ArrayList<>();
|
||||
|
||||
if (isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
if (xCount <= blocks.size()) {
|
||||
for (Block b : blocks) {
|
||||
result.add(b);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
for (int i = blocks.size() - xCount; i <= (blocks.size() - 1); i++) {
|
||||
result.add(blocks.get(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void addBlock(Block block) {
|
||||
if (!block.isLocked()) {
|
||||
throw new BlockChainException("Block is not locked. It cannot be added");
|
||||
}
|
||||
if (blocks.isEmpty() && block.getBlockHeader().getHeight() != 0) {
|
||||
throw new BlockChainException("New block is a genesis block, but its height is not zero.");
|
||||
}
|
||||
Block lastBlock = getLastBlock();
|
||||
String previousBlockHash = lastBlock == null ? "0" : lastBlock.getHash();
|
||||
|
||||
if (!block.getBlockHeader().getPreviousHash().equals(previousBlockHash)) {
|
||||
throw new BlockChainException("Block previous hash is not equal to the hash of the previous block. It cannot be added to the blockchain.");
|
||||
}
|
||||
blocks.add(block);
|
||||
}
|
||||
|
||||
public Block getBlock(int blockHeight) {
|
||||
if (blockHeight >= blocks.size()) {
|
||||
throw new BlockChainException("There is no block with height " + blockHeight);
|
||||
}
|
||||
return blocks.get(blockHeight);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import lombok.AllArgsConstructor;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public abstract class BlockChainEngine {
|
||||
private String blockChainEngineName;
|
||||
protected BlockChain blockChain;
|
||||
protected BlockTypeVersionList blockTypeVersionList;
|
||||
|
||||
public void addData(String data) {
|
||||
BlockFragment blockFragment = createNewBlockFragment(data);
|
||||
BlockType blockType = blockTypeVersionList.findBlockType(blockFragment.getBlockHeader().getBlockTypeId());
|
||||
if (blockType == null) {
|
||||
throw new BlockChainException("blockType " + blockFragment.getBlockHeader().getBlockTypeId() + " was not found.");
|
||||
}
|
||||
|
||||
Block block = blockType.getBlockMiner().mine(blockFragment, blockType);
|
||||
addBlock(block);
|
||||
}
|
||||
|
||||
public abstract BlockFragment createNewBlockFragment(String data);
|
||||
|
||||
public void addBlock(Block newBlock) {
|
||||
blockChain.addBlock(newBlock);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockFragment {
|
||||
@Getter
|
||||
private BlockHeader blockHeader;
|
||||
|
||||
@Getter
|
||||
private BlockData blockData;
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockHeader implements JsonObjectSerializable {
|
||||
public static final String BLOCK_TYPE_ID = "blockTypeId";
|
||||
public static final String HEIGHT = "height";
|
||||
public static final String TIME_STAMP = "timeStamp";
|
||||
public static final String PREVIOUS_HASH = "previousHash";
|
||||
@Getter
|
||||
private String blockTypeId;
|
||||
@Getter
|
||||
private int height;
|
||||
@Getter
|
||||
private long timeStamp;
|
||||
@Getter
|
||||
private String previousHash;
|
||||
@Getter
|
||||
private String difficulty;
|
||||
|
||||
public JsonObject toJsonObject() {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.add(BLOCK_TYPE_ID, blockTypeId);
|
||||
jo.add(HEIGHT, height);
|
||||
jo.add(TIME_STAMP, timeStamp);
|
||||
jo.add(PREVIOUS_HASH, previousHash);
|
||||
return jo;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockDeserializer;
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockMiner;
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockType {
|
||||
@Getter
|
||||
private String type;
|
||||
@Getter
|
||||
private int mayorVersion;
|
||||
@Getter
|
||||
private int minorVersion;
|
||||
@Getter
|
||||
private HashCalculator hashCalculator;
|
||||
@Getter
|
||||
private BlockSerializer blockSerializer;
|
||||
@Getter
|
||||
private BlockDeserializer blockDeserializer;
|
||||
@Getter
|
||||
BlockMiner blockMiner;
|
||||
public String getBlockTypeId() {
|
||||
return type + ":" + mayorVersion + ":" + minorVersion;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockTypeVersionList {
|
||||
private Map<String, BlockType> blockTypes;
|
||||
|
||||
public BlockTypeVersionList(List<BlockType> blockTypesList) {
|
||||
blockTypes = new HashMap<>();
|
||||
for (BlockType bt : blockTypesList) {
|
||||
blockTypes.put(bt.getBlockTypeId(), bt);
|
||||
}
|
||||
}
|
||||
|
||||
public BlockType findBlockType(String blockTypeId) {
|
||||
return blockTypes.get(blockTypeId);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TransactionValidator {
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class Wallet {
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockChain;
|
||||
import org.nanoboot.powerframework.blockchain.orig.impl.TestCash_BlockChainEngine;
|
||||
import org.nanoboot.powerframework.random.generators.linearcongruent.combined.w5.W5RandomGenerator;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// HashCalculator.main(null);
|
||||
//
|
||||
// if(true) return;
|
||||
BlockChain blockChain = new BlockChain();
|
||||
TestCash_BlockChainEngine bce = new TestCash_BlockChainEngine(blockChain);
|
||||
bce.addData("ahoj");
|
||||
bce.addData("pavle");
|
||||
bce.addData("leo");
|
||||
for(int i =0;i<100000000;i++){
|
||||
bce.addData(W5RandomGenerator.getStaticInstance().nextText(8));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class JsonBlockSerializer implements BlockSerializer {
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
JsonObject jsonObject = block.toJsonObject();
|
||||
//System.out.println(jsonObject.toMinimalString());
|
||||
return jsonObject.toMinimalString();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class SimpleBlockData implements BlockData {
|
||||
@Getter
|
||||
private final String string;
|
||||
@Override
|
||||
public String convertToString() {
|
||||
return string;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockHeader;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_Block extends Block {
|
||||
public TestCash_1_0_Block(BlockHeader blockHeader, BlockData blockData) {
|
||||
super(blockHeader, null, blockData);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockMiner;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockFragment;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.duration.Duration;
|
||||
import org.nanoboot.powerframework.time.duration.StopWatch;
|
||||
import org.nanoboot.powerframework.time.utils.TimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_BlockMiner implements BlockMiner {
|
||||
public static List<Integer> lastFiveBlockMiningTimesInMilliseconds = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Block mine(BlockFragment blockFragment, BlockType blockType) {
|
||||
|
||||
TestCash_1_0_Block block = new TestCash_1_0_Block(blockFragment.getBlockHeader(), blockFragment.getBlockData());
|
||||
block.injectBlockType(blockType);
|
||||
|
||||
////System.out.println("Starting mining block # " + block.getBlockHeader().getHeight());
|
||||
//System.out.println("Note: One block should be mined in 1 minute, but the last time was " + countOfSecondsToMineLastBlock + " second(s).");
|
||||
//System.out.println("Starting mining block # " + block.getBlockHeader().getHeight());
|
||||
StopWatch sw = new StopWatch();
|
||||
|
||||
//System.out.println("start: " + UniversalDateTime.now().toString());
|
||||
sw.start();
|
||||
|
||||
////System.out.println("Searching hash equal or less than " + block.getBlockHeader().getDifficulty());
|
||||
for (long nonce = 1; nonce <= Long.MAX_VALUE; nonce++) {
|
||||
if (nonce == Integer.MAX_VALUE) {
|
||||
throw new BlockChainException("Nonce was not found.");
|
||||
}
|
||||
if (nonce % 1000000 == 0) {
|
||||
System.out.println("Nonce # " + nonce);
|
||||
}
|
||||
|
||||
|
||||
HashCalculator hashCalculator = block.getInternalBlockType().getHashCalculator();
|
||||
String hash = block.calculateHashTest(nonce);
|
||||
//System.err.println("hashCalculator.compareHexNumbers("+hash+"+, block.getBlockHeader().getDifficulty()="+block.getBlockHeader().getDifficulty()+")="+hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()));
|
||||
if (hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()) <= 0) {
|
||||
|
||||
//System.err.println("hashCalculator.compareHexNumbers("+hash+"+, block.getBlockHeader().getDifficulty()="+block.getBlockHeader().getDifficulty()+")="+hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()));
|
||||
block.lock(nonce);
|
||||
////System.out.println("Found nonce: " + nonce + " A new block was just mined: " + block.getHash() + "\n" /*+ block.toJsonObject().toPrettyString()*/);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//System.out.println("end: " + UniversalDateTime.now().toString());
|
||||
sw.stop();
|
||||
Duration lastDuration = sw.getCurrentDuration();
|
||||
if (lastFiveBlockMiningTimesInMilliseconds.size() == 1000) {
|
||||
lastFiveBlockMiningTimesInMilliseconds.remove(0);
|
||||
}
|
||||
lastFiveBlockMiningTimesInMilliseconds.add((int) lastDuration.toTotal(TimeUnit.MILLISECOND));
|
||||
//countOfSecondsToMineLastBlock = (int) lastDuration.toTotal(TimeUnit.SECOND);
|
||||
if (block.getBlockHeader().getHeight() % 100 == 0)
|
||||
System.out.println("Mining block " + block.getBlockHeader().getHeight() + " + took " + lastDuration.toString() + " .");
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
import org.nanoboot.powerframework.security.hash.locator.HashCalculatorLocator;
|
||||
import org.nanoboot.powerframework.security.hash.locator.HashImpl;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_BlockType extends BlockType {
|
||||
private static final String TYPE = "test_cash";
|
||||
private static final int MAYOR_VERSION = 1;
|
||||
private static final int MINOR_VERSION = 0;
|
||||
public TestCash_1_0_BlockType() {
|
||||
super(TYPE, MAYOR_VERSION, MINOR_VERSION, HashCalculatorLocator.locate(HashImpl.SHA_256), new JsonBlockSerializer(), null, new TestCash_1_0_BlockMiner());
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library 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;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library 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 library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.*;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.duration.Duration;
|
||||
import org.nanoboot.powerframework.time.moment.UniversalDateTime;
|
||||
import org.nanoboot.powerframework.time.utils.TimeUnit;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_BlockChainEngine extends BlockChainEngine {
|
||||
|
||||
public static final String TEST_CASH = "TestCash";
|
||||
public static final int COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK = 10;
|
||||
//public static final String DEFAULT_DIFFICULTY = "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
||||
public static final String DEFAULT_DIFFICULTY = "00184f329993e8d38cd0a31969b54367b4fc5af6ed86d827c1343dbf8b66a4a5";
|
||||
|
||||
public static float DIFFICULTY_PERCENT_CHANGE = (float) 1;
|
||||
|
||||
public TestCash_BlockChainEngine(BlockChain blockChain) {
|
||||
super(TEST_CASH, blockChain, new BlockTypeVersionList(List.of(new TestCash_1_0_BlockType())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockFragment createNewBlockFragment(String data) {
|
||||
Block lastBlock = blockChain.getLastBlock();
|
||||
int newBlockHeight = lastBlock == null ? 0 : lastBlock.getBlockHeader().getHeight() + 1;
|
||||
long newTimeStamp = UniversalDateTime.now().toLong();
|
||||
String previousBlockHash = lastBlock == null ? "0" : lastBlock.getHash();
|
||||
String oldDifficulty = lastBlock == null ? DEFAULT_DIFFICULTY : lastBlock.getBlockHeader().getDifficulty();
|
||||
String newDifficulty = oldDifficulty;
|
||||
|
||||
int newDifficultyPeriod = 1000;
|
||||
if (lastBlock != null && newBlockHeight % newDifficultyPeriod == 0) {
|
||||
|
||||
List<Block> lastBlocks = blockChain.getLastXBlocks(newDifficultyPeriod);
|
||||
int lastBlocksCount = lastBlocks.size();
|
||||
Block blockStart = lastBlocks.get(0);
|
||||
Block blockEnd = lastBlocks.get(lastBlocks.size() - 1);
|
||||
long moment1 = blockStart.getBlockHeader().getTimeStamp();
|
||||
long moment2 = blockEnd.getBlockHeader().getTimeStamp();
|
||||
UniversalDateTime udt1 = new UniversalDateTime(moment1);
|
||||
UniversalDateTime udt2 = new UniversalDateTime(moment2);
|
||||
Duration duration = new org.nanoboot.powerframework.time.duration.Period(udt1, udt2).getDuration();
|
||||
duration.toTotal(TimeUnit.SECOND);
|
||||
double averageCountOfMillisecondsToMineABlockForTenLastBlocks = (duration.toTotal(TimeUnit.SECOND) / (lastBlocksCount - 1));
|
||||
|
||||
int countX = 0;
|
||||
int sum = 0;
|
||||
for (Integer i : TestCash_1_0_BlockMiner.lastFiveBlockMiningTimesInMilliseconds) {
|
||||
sum = sum + i;
|
||||
countX = countX + 1;
|
||||
}
|
||||
System.out.println("sumInSeconds="+((double)sum));
|
||||
|
||||
averageCountOfMillisecondsToMineABlockForTenLastBlocks = ((((double) sum) / ((double) countX)) /*/ 1*/);
|
||||
System.out.println("averageCountOfMillisecondsToMineABlockForTenLastBlocks=" + averageCountOfMillisecondsToMineABlockForTenLastBlocks);
|
||||
boolean plus = false;
|
||||
if(((int)Math.round(averageCountOfMillisecondsToMineABlockForTenLastBlocks))==COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
DIFFICULTY_PERCENT_CHANGE=1f/16;
|
||||
} else {
|
||||
if(DIFFICULTY_PERCENT_CHANGE != (float) 1f/1f) {
|
||||
DIFFICULTY_PERCENT_CHANGE = (float) 1f/1f;
|
||||
}
|
||||
}
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks < COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
plus = false;
|
||||
System.out.println("Difficulty +++ increasing");
|
||||
|
||||
}
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks > COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
plus = true;
|
||||
System.out.println("Difficulty --- decreasing");
|
||||
}
|
||||
// if (DIFFICULTY_PERCENT_CHANGE >= 16) {
|
||||
// DIFFICULTY_PERCENT_CHANGE = 4;
|
||||
// }
|
||||
|
||||
System.out.println("averageCountOfSecondsToMineABlockForTenLastBlocks=" + averageCountOfMillisecondsToMineABlockForTenLastBlocks);
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks != COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
//DIFFICULTY_PERCENT_CHANGE = DIFFICULTY_PERCENT_CHANGE * 2;
|
||||
double realDifficultyPercentChange = DIFFICULTY_PERCENT_CHANGE;
|
||||
double more = averageCountOfMillisecondsToMineABlockForTenLastBlocks > COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK ? averageCountOfMillisecondsToMineABlockForTenLastBlocks : COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK;
|
||||
double less = averageCountOfMillisecondsToMineABlockForTenLastBlocks < COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK ? averageCountOfMillisecondsToMineABlockForTenLastBlocks : COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK;
|
||||
|
||||
if ((less / more) < 0.5) {
|
||||
realDifficultyPercentChange = 50;
|
||||
}
|
||||
double change = 1 + ((plus ? 1 : (-1)) * (realDifficultyPercentChange / 100));
|
||||
// if((double)(averageCountOfSecondsToMineABlockForTenLastBlocks) / (double)(COUNT_OF_SECONDS_TO_MINE_A_BLOCK)<0.9 || (double)(averageCountOfSecondsToMineABlockForTenLastBlocks) / (double)(COUNT_OF_SECONDS_TO_MINE_A_BLOCK)> 1.1){
|
||||
// change = 1 + ((plus ? 1 : (-1)) * (25f / 100));
|
||||
// }
|
||||
System.out.println(" Changing to " + change);
|
||||
BlockType blockType = blockTypeVersionList.findBlockType("test_cash:1:0");
|
||||
HashCalculator hashCalculator = blockType.getHashCalculator();
|
||||
String oldDifficultyAsDec = hashCalculator.convertHexStringToDecString(oldDifficulty);
|
||||
System.out.println("oldDifficultyAsDec=" + oldDifficultyAsDec);
|
||||
BigInteger bi = new BigDecimal(oldDifficultyAsDec).multiply(new BigDecimal(change)).toBigInteger();
|
||||
System.out.println("newDifficultyAsDec=" + bi.toString());
|
||||
newDifficulty = hashCalculator.convertDecStringToHexString(bi.toString());
|
||||
System.err.println("NEW_DIFFICULTY = " + newDifficulty);
|
||||
} else {
|
||||
//DIFFICULTY_PERCENT_CHANGE = DIFFICULTY_PERCENT_CHANGE / 2;
|
||||
}
|
||||
}
|
||||
BlockHeader blockHeader = new BlockHeader("test_cash:1:0", newBlockHeight, newTimeStamp, previousBlockHash, newDifficulty);
|
||||
return new BlockFragment(blockHeader, new SimpleBlockData(data));
|
||||
}
|
||||
}
|
0
power-blockchain/src/main/resources/.gitkeep
Normal file
0
power-blockchain/src/main/resources/.gitkeep
Normal file
Reference in New Issue
Block a user