From 9f0d2c1b7d19adb09046c12f44d5b6a991ab7c5b Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sat, 1 Jul 2023 12:54:33 +0200 Subject: [PATCH] Revert "Removed module power-blockchain" This reverts commit ab3de2227520db03110762886e2a956ee5c7ccea. --- pom.xml | 1 + power-blockchain/pom.xml | 122 ++++++++++++++++ .../src/main/java/module-info.java | 34 +++++ .../powerframework/blockchain/.gitkeep | 0 .../blockchain/api/BlockDeserializer.java | 33 +++++ .../blockchain/api/BlockSerializer.java | 33 +++++ .../powerframework/blockchain/core/Block.java | 132 +++++++++++++++++ .../blockchain/core/BlockChainException.java | 35 +++++ .../blockchain/core/BlockchainEngine.java | 40 ++++++ .../core/BlockchainEngineVersion.java | 54 +++++++ .../blockchain/core/BlockchainProtocol.java | 56 ++++++++ .../blockchain/impl/JsonBlockSerializer.java | 40 ++++++ .../blockchain/impl/StringSerializer.java | 45 ++++++ .../blockchain/orig/api/BlockData.java | 31 ++++ .../orig/api/BlockDeserializer.java | 33 +++++ .../blockchain/orig/api/BlockMiner.java | 35 +++++ .../blockchain/orig/api/BlockSerializer.java | 33 +++++ .../blockchain/orig/api/BlockValidator.java | 35 +++++ .../blockchain/orig/base/Block.java | 117 +++++++++++++++ .../blockchain/orig/base/BlockChain.java | 100 +++++++++++++ .../orig/base/BlockChainEngine.java | 55 +++++++ .../blockchain/orig/base/BlockFragment.java | 42 ++++++ .../blockchain/orig/base/BlockHeader.java | 60 ++++++++ .../blockchain/orig/base/BlockType.java | 55 +++++++ .../orig/base/BlockTypeVersionList.java | 47 ++++++ .../blockchain/orig/cash/Transaction.java | 29 ++++ .../orig/cash/TransactionValidator.java | 29 ++++ .../blockchain/orig/cash/Wallet.java | 29 ++++ .../blockchain/orig/core/Main.java | 48 +++++++ .../orig/impl/JsonBlockSerializer.java | 41 ++++++ .../blockchain/orig/impl/SimpleBlockData.java | 40 ++++++ .../orig/impl/TestCash_1_0_Block.java | 36 +++++ .../orig/impl/TestCash_1_0_BlockMiner.java | 95 ++++++++++++ .../orig/impl/TestCash_1_0_BlockType.java | 39 +++++ .../orig/impl/TestCash_BlockChainEngine.java | 136 ++++++++++++++++++ power-blockchain/src/main/resources/.gitkeep | 0 .../powerframework/blockchain/.gitkeep | 0 37 files changed, 1790 insertions(+) create mode 100644 power-blockchain/pom.xml create mode 100644 power-blockchain/src/main/java/module-info.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/.gitkeep create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockDeserializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockSerializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/Block.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockChainException.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngine.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngineVersion.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainProtocol.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/JsonBlockSerializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/StringSerializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockData.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockDeserializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockMiner.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockSerializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockValidator.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/Block.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChain.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChainEngine.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockFragment.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockHeader.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockType.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockTypeVersionList.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Transaction.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/TransactionValidator.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Wallet.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/core/Main.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/JsonBlockSerializer.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/SimpleBlockData.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_Block.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockMiner.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockType.java create mode 100644 power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_BlockChainEngine.java create mode 100644 power-blockchain/src/main/resources/.gitkeep create mode 100644 power-blockchain/src/test/java/org/nanoboot/powerframework/blockchain/.gitkeep diff --git a/pom.xml b/pom.xml index ae3a3d8..70c0861 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA power-xml power-reflection power-mail + power-blockchain power-security + + 4.0.0 + + + org.nanoboot.powerframework + power-framework + 2.0.0-SNAPSHOT + + + org.nanoboot.powerframework + power-blockchain + jar + + Power Blockchain + Blockchain library + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + + org.nanoboot.powerframework.blockchain.orig.core.Main + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.nanoboot.powerframework.blockchain.orig.core.Main + + + + + + + + + + + org.nanoboot.powerframework + power-core + ${power.version} + + + org.nanoboot.powerframework + power-time + ${power.version} + + + org.nanoboot.powerframework + power-json + ${power.version} + + + org.nanoboot.powerframework + power-random + ${power.version} + + + org.nanoboot.powerframework + power-security + ${power.version} + + + + + junit + junit + 4.12 + test + + + org.projectlombok + lombok + 1.18.10 + compile + + + + diff --git a/power-blockchain/src/main/java/module-info.java b/power-blockchain/src/main/java/module-info.java new file mode 100644 index 0000000..0689fa1 --- /dev/null +++ b/power-blockchain/src/main/java/module-info.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +module powerframework.blockchain { + requires lombok; + requires powerframework.core; + requires powerframework.time; + requires powerframework.json; + requires powerframework.random; + requires powerframework.security; +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/.gitkeep b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockDeserializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockDeserializer.java new file mode 100644 index 0000000..97eee57 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockDeserializer.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockDeserializer { + Block deserialize(String serializedBlock); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockSerializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockSerializer.java new file mode 100644 index 0000000..8a7ba93 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/api/BlockSerializer.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockSerializer { + String serialize(Block block); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/Block.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/Block.java new file mode 100644 index 0000000..8115ecd --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/Block.java @@ -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 Robert Vokac + * @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; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockChainException.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockChainException.java new file mode 100644 index 0000000..0fc92fb --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockChainException.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public class BlockChainException extends PowerException { + public BlockChainException(String msg) { + super(msg); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngine.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngine.java new file mode 100644 index 0000000..ba4b606 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngine.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +@AllArgsConstructor +public class BlockchainEngine { + @Getter + private final String protocolName; + private List blockchainEngineVersionList = new ArrayList<>(); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngineVersion.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngineVersion.java new file mode 100644 index 0000000..05ab32d --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainEngineVersion.java @@ -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 Robert Vokac + * @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]; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainProtocol.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainProtocol.java new file mode 100644 index 0000000..48814be --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/core/BlockchainProtocol.java @@ -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 Robert Vokac + * @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; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/JsonBlockSerializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/JsonBlockSerializer.java new file mode 100644 index 0000000..23d0a42 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/JsonBlockSerializer.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public class JsonBlockSerializer implements BlockSerializer { + + @Override + public String serialize(Block block) { + JsonObject jsonObject = block.toJsonObject(); + return jsonObject.toMinimalString(); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/StringSerializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/StringSerializer.java new file mode 100644 index 0000000..5fe3ae6 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/impl/StringSerializer.java @@ -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 Robert Vokac + * @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(); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockData.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockData.java new file mode 100644 index 0000000..f619d87 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockData.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockData { + String convertToString(); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockDeserializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockDeserializer.java new file mode 100644 index 0000000..3cda061 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockDeserializer.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockDeserializer { + Block deserialize(String serializedBlock); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockMiner.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockMiner.java new file mode 100644 index 0000000..24a1371 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockMiner.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockMiner { + Block mine(BlockFragment blockFragment, BlockType blockType); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockSerializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockSerializer.java new file mode 100644 index 0000000..fb9c75f --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockSerializer.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockSerializer { + String serialize(Block block); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockValidator.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockValidator.java new file mode 100644 index 0000000..6eaad05 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/api/BlockValidator.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public interface BlockValidator { + BlockType getBlockType(); + void validate(Block block); +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/Block.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/Block.java new file mode 100644 index 0000000..0a076ca --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/Block.java @@ -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 Robert Vokac + * @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; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChain.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChain.java new file mode 100644 index 0000000..4c0323e --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChain.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public class BlockChain { + private String blockChainEngineName; + private List 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 getLastXBlocks(int xCount) { + List 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); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChainEngine.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChainEngine.java new file mode 100644 index 0000000..ffe7f3b --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockChainEngine.java @@ -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 Robert Vokac + * @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); + } + +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockFragment.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockFragment.java new file mode 100644 index 0000000..0cedbf0 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockFragment.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +@AllArgsConstructor +public class BlockFragment { + @Getter + private BlockHeader blockHeader; + + @Getter + private BlockData blockData; + +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockHeader.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockHeader.java new file mode 100644 index 0000000..6272d74 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockHeader.java @@ -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 Robert Vokac + * @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; + } + +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockType.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockType.java new file mode 100644 index 0000000..7280eb3 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockType.java @@ -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 Robert Vokac + * @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; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockTypeVersionList.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockTypeVersionList.java new file mode 100644 index 0000000..ec6b8d8 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/base/BlockTypeVersionList.java @@ -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 Robert Vokac + * @since 0.0.0 + */ + +public class BlockTypeVersionList { + private Map blockTypes; + + public BlockTypeVersionList(List blockTypesList) { + blockTypes = new HashMap<>(); + for (BlockType bt : blockTypesList) { + blockTypes.put(bt.getBlockTypeId(), bt); + } + } + + public BlockType findBlockType(String blockTypeId) { + return blockTypes.get(blockTypeId); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Transaction.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Transaction.java new file mode 100644 index 0000000..d794978 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Transaction.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +public class Transaction { +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/TransactionValidator.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/TransactionValidator.java new file mode 100644 index 0000000..70e6ccf --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/TransactionValidator.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +public class TransactionValidator { +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Wallet.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Wallet.java new file mode 100644 index 0000000..0f09071 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/cash/Wallet.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +public class Wallet { +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/core/Main.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/core/Main.java new file mode 100644 index 0000000..d14916c --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/core/Main.java @@ -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 Robert Vokac + * @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)); + } + + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/JsonBlockSerializer.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/JsonBlockSerializer.java new file mode 100644 index 0000000..5e6ae63 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/JsonBlockSerializer.java @@ -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 Robert Vokac + * @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(); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/SimpleBlockData.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/SimpleBlockData.java new file mode 100644 index 0000000..53b1351 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/SimpleBlockData.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +@AllArgsConstructor +public class SimpleBlockData implements BlockData { + @Getter + private final String string; + @Override + public String convertToString() { + return string; + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_Block.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_Block.java new file mode 100644 index 0000000..2090d02 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_Block.java @@ -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 Robert Vokac + * @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); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockMiner.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockMiner.java new file mode 100644 index 0000000..e0a132d --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockMiner.java @@ -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 Robert Vokac + * @since 0.0.0 + */ +public class TestCash_1_0_BlockMiner implements BlockMiner { + public static List 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; + } + + +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockType.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockType.java new file mode 100644 index 0000000..284f671 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_1_0_BlockType.java @@ -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 Robert Vokac + * @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()); + } +} diff --git a/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_BlockChainEngine.java b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_BlockChainEngine.java new file mode 100644 index 0000000..5314529 --- /dev/null +++ b/power-blockchain/src/main/java/org/nanoboot/powerframework/blockchain/orig/impl/TestCash_BlockChainEngine.java @@ -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 Robert Vokac + * @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 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)); + } +} diff --git a/power-blockchain/src/main/resources/.gitkeep b/power-blockchain/src/main/resources/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/power-blockchain/src/test/java/org/nanoboot/powerframework/blockchain/.gitkeep b/power-blockchain/src/test/java/org/nanoboot/powerframework/blockchain/.gitkeep new file mode 100644 index 0000000..e69de29