Skip to content

Commit f210ffc

Browse files
committed
[blockchain]Some More review (data access)
1 parent a04e139 commit f210ffc

File tree

10 files changed

+149
-111
lines changed

10 files changed

+149
-111
lines changed

src/main/java/NoobChainTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ class NoobChainTests {
2828
void testPart01() {
2929
Block genesisBlock = new Block("Hi im the first block", "0");
3030
Assertions.assertNotNull( genesisBlock );
31-
System.out.println("Hash for block 1 : " + genesisBlock.hash);
32-
Assertions.assertNotNull( genesisBlock.hash );
31+
System.out.println("Hash for block 1 : " + genesisBlock.getHash() );
32+
Assertions.assertNotNull( genesisBlock.getHash() );
3333

34-
Block secondBlock = new Block("Yo im the second block", genesisBlock.hash);
34+
Block secondBlock = new Block("Yo im the second block", genesisBlock.getHash() );
3535
Assertions.assertNotNull( secondBlock );
36-
System.out.println("Hash for block 2 : " + secondBlock.hash);
37-
Assertions.assertNotNull( secondBlock.hash );
36+
System.out.println("Hash for block 2 : " + secondBlock.getHash() );
37+
Assertions.assertNotNull( secondBlock.getHash() );
3838

39-
Block thirdBlock = new Block("Hey im the third block", secondBlock.hash);
39+
Block thirdBlock = new Block("Hey im the third block", secondBlock.getHash() );
4040
Assertions.assertNotNull( thirdBlock );
41-
System.out.println("Hash for block 3 : " + thirdBlock.hash);
42-
Assertions.assertNotNull( thirdBlock.hash );
41+
System.out.println("Hash for block 3 : " + thirdBlock.getHash() );
42+
Assertions.assertNotNull( thirdBlock.getHash() );
4343
}
4444

4545
/**
@@ -50,8 +50,8 @@ void testPart02() {
5050
List<Block> blockchain = new ArrayList<Block>();
5151
// Add our blocks to the blockchain List:
5252
blockchain.add(new Block("Hi im the first block", "0"));
53-
blockchain.add(new Block("Yo im the second block", blockchain.get(blockchain.size()-1).hash));
54-
blockchain.add(new Block("Hey im the third block", blockchain.get(blockchain.size()-1).hash));
53+
blockchain.add(new Block("Yo im the second block", blockchain.get(blockchain.size()-1).getHash() ));
54+
blockchain.add(new Block("Hey im the third block", blockchain.get(blockchain.size()-1).getHash() ));
5555
Assertions.assertEquals(3, blockchain.size());
5656
String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
5757
Assertions.assertNotNull( blockchainJson );
@@ -67,11 +67,11 @@ void testPart03() {
6767
System.out.println("Trying to Mine block 1... ");
6868
blockchain.get(0).mineBlock(difficulty);
6969

70-
blockchain.add(new Block("Yo im the second block", blockchain.get(blockchain.size()-1).hash));
70+
blockchain.add(new Block("Yo im the second block", blockchain.get(blockchain.size()-1).getHash() ));
7171
System.out.println("Trying to Mine block 2... ");
7272
blockchain.get(1).mineBlock(difficulty);
7373

74-
blockchain.add(new Block("Hey im the third block", blockchain.get(blockchain.size()-1).hash));
74+
blockchain.add(new Block("Hey im the third block", blockchain.get(blockchain.size()-1).getHash() ));
7575
System.out.println("Trying to Mine block 3... ");
7676
blockchain.get(2).mineBlock(difficulty);
7777

src/main/java/gabywald/crypto/blockchain/Block.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
*/
1414
public class Block {
1515

16-
public String hash;
17-
public String previousHash;
16+
private String hash;
17+
private String previousHash;
1818
/** Our data will be a simple message. */
1919
// private String data;
2020
/** As number of milliseconds since 1/1/1970. */
2121
private long timeStamp;
2222
private int nonce;
2323

24-
public String merkleRoot;
25-
/** Our data will be a simple message. */
26-
public List<Transaction> transactions = new ArrayList<Transaction>();
24+
private String merkleRoot;
25+
26+
private List<Transaction> transactions = new ArrayList<Transaction>();
2727

2828
/**
2929
* Block Constructor.
@@ -106,4 +106,18 @@ public boolean addTransaction( final Transaction transaction,
106106
System.out.println("Transaction Successfully added to Block");
107107
return true;
108108
}
109+
110+
public String getHash()
111+
{ return this.hash; }
112+
113+
public String getPreviousHash()
114+
{ return this.previousHash; }
115+
116+
public String getMerkleRoot()
117+
{ return this.merkleRoot; }
118+
119+
public List<Transaction> getTransactions()
120+
{ return this.transactions; }
121+
122+
109123
}

src/main/java/gabywald/crypto/blockchain/BlockChain.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public static boolean isChainValidV1(List<Block> blockchain) {
3232
currentBlock = blockchain.get(i);
3333
previousBlock = blockchain.get(i-1);
3434
// Compare registered hash and calculated hash:
35-
if ( ! currentBlock.hash.equals(currentBlock.calculateHash()) ) {
35+
if ( ! currentBlock.getHash().equals(currentBlock.calculateHash()) ) {
3636
System.out.println("Current Hashes not equal");
3737
return false;
3838
}
3939
// Compare previous hash and registered previous hash
40-
if ( ! previousBlock.hash.equals(currentBlock.previousHash) ) {
40+
if ( ! previousBlock.getHash().equals(currentBlock.getPreviousHash() ) ) {
4141
System.out.println("Previous Hashes not equal");
4242
return false;
4343
}
@@ -59,7 +59,7 @@ public static boolean isChainValidV2(List<Block> blockchain, Transaction genesis
5959

6060
// A temporary working list of unspent transactions at a given block state.
6161
HashMap<String,TransactionOutput> tempUTXOs = new HashMap<String,TransactionOutput>();
62-
tempUTXOs.put(genesisTransaction.getOutputs().get(0).id, genesisTransaction.getOutputs().get(0));
62+
tempUTXOs.put(genesisTransaction.getOutputs().get(0).getId(), genesisTransaction.getOutputs().get(0));
6363

6464
// Loop through blockchain to check hashes:
6565
for (int i = 1 ; i < blockchain.size() ; i++) {
@@ -68,27 +68,27 @@ public static boolean isChainValidV2(List<Block> blockchain, Transaction genesis
6868
previousBlock = blockchain.get(i-1);
6969

7070
// Compare registered hash and calculated hash:
71-
if ( ! currentBlock.hash.equals(currentBlock.calculateHash()) ) {
71+
if ( ! currentBlock.getHash().equals(currentBlock.calculateHash()) ) {
7272
System.out.println("#Current Hashes not equal");
7373
return false;
7474
}
7575

7676
// Compare previous hash and registered previous hash
77-
if ( ! previousBlock.hash.equals(currentBlock.previousHash) ) {
77+
if ( ! previousBlock.getHash().equals(currentBlock.getPreviousHash() ) ) {
7878
System.out.println("#Previous Hashes not equal");
7979
return false;
8080
}
8181

8282
// Check if hash is solved
83-
if ( ! currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {
83+
if ( ! currentBlock.getHash().substring( 0, difficulty).equals(hashTarget)) {
8484
System.out.println("#This block hasn't been mined");
8585
return false;
8686
}
8787

8888
// Loop thru blockchains transactions:
8989
TransactionOutput tempOutput;
90-
for (int t = 0 ; t < currentBlock.transactions.size() ; t++) {
91-
Transaction currentTransaction = currentBlock.transactions.get(t);
90+
for (int t = 0 ; t < currentBlock.getTransactions().size() ; t++) {
91+
Transaction currentTransaction = currentBlock.getTransactions().get(t);
9292

9393
if ( ! currentTransaction.verifySignature()) {
9494
System.out.println("#Signature on Transaction(" + t + ") is Invalid");
@@ -101,31 +101,31 @@ public static boolean isChainValidV2(List<Block> blockchain, Transaction genesis
101101
}
102102

103103
for (TransactionInput input: currentTransaction.getInputs()) {
104-
tempOutput = tempUTXOs.get(input.transactionOutputId);
104+
tempOutput = tempUTXOs.get(input.getTransactionOutputId() );
105105

106106
if (tempOutput == null) {
107107
System.out.println("#Referenced input on Transaction(" + t + ") is Missing");
108108
return false;
109109
}
110110

111-
if (input.UTXO.value != tempOutput.value) {
111+
if (input.getTransactionOutput().getValue() != tempOutput.getValue()) {
112112
System.out.println("#Referenced input Transaction(" + t + ") value is Invalid");
113113
return false;
114114
}
115115

116-
tempUTXOs.remove(input.transactionOutputId);
116+
tempUTXOs.remove(input.getTransactionOutputId() );
117117
}
118118

119119
for (TransactionOutput output: currentTransaction.getOutputs()) {
120-
tempUTXOs.put(output.id, output);
120+
tempUTXOs.put(output.getId(), output);
121121
}
122122

123-
if (currentTransaction.getOutputs().get(0).reciepient != currentTransaction.getRecipient()) {
123+
if (currentTransaction.getOutputs().get(0).getRecipient() != currentTransaction.getRecipient()) {
124124
System.out.println("#Transaction(" + t + ") output reciepient is not who it should be");
125125
return false;
126126
}
127127

128-
if (currentTransaction.getOutputs().get(1).reciepient != currentTransaction.getSender()) {
128+
if (currentTransaction.getOutputs().get(1).getRecipient() != currentTransaction.getSender()) {
129129
System.out.println("#Transaction(" + t + ") output 'change' is not sender.");
130130
return false;
131131
}

src/main/java/gabywald/crypto/blockchain/NoobChainFinale.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ public static void main(String[] args) {
4646
NoobChainFinale.genesisTransaction.getTransactionId()));
4747

4848
// Its important to store our first transaction in the UTXOs list.
49-
NoobChainFinale.mapUTXOs.put(NoobChainFinale.genesisTransaction.getOutputs().get(0).id, NoobChainFinale.genesisTransaction.getOutputs().get(0));
49+
NoobChainFinale.mapUTXOs.put( NoobChainFinale.genesisTransaction.getOutputs().get(0).getId(),
50+
NoobChainFinale.genesisTransaction.getOutputs().get(0));
5051

5152
System.out.println("Creating and Mining Genesis block... ");
5253
Block genesis = new Block("0");
5354
genesis.addTransaction(NoobChainFinale.genesisTransaction, NoobChainFinale.mapUTXOs, NoobChainFinale.minimumTransaction);
5455
BlockChain.addBlock(NoobChainFinale.blockchain, genesis, NoobChainFinale.difficulty);
5556

5657
// Testing
57-
Block block1 = new Block(genesis.hash);
58+
Block block1 = new Block(genesis.getHash() );
5859
System.out.println("\nWalletA's balance is: " + NoobChainFinale.walletA.getBalance( NoobChainFinale.mapUTXOs ));
5960
System.out.println("\nWalletA is Attempting to send funds (40) to WalletB...");
6061
block1.addTransaction( NoobChainFinale.walletA.sendFunds(NoobChainFinale.walletB.publicKey, 40f, NoobChainFinale.mapUTXOs),
@@ -63,15 +64,15 @@ public static void main(String[] args) {
6364
System.out.println("\nWalletA's balance is: " + NoobChainFinale.walletA.getBalance( NoobChainFinale.mapUTXOs ));
6465
System.out.println("WalletB's balance is: " + NoobChainFinale.walletB.getBalance( NoobChainFinale.mapUTXOs ));
6566

66-
Block block2 = new Block(block1.hash);
67+
Block block2 = new Block(block1.getHash() );
6768
System.out.println("\nWalletA Attempting to send more funds (1000) than it has...");
6869
block2.addTransaction( NoobChainFinale.walletA.sendFunds(NoobChainFinale.walletB.publicKey, 1000f, NoobChainFinale.mapUTXOs),
6970
NoobChainFinale.mapUTXOs, NoobChainFinale.minimumTransaction);
7071
BlockChain.addBlock(NoobChainFinale.blockchain, block2, NoobChainFinale.difficulty);
7172
System.out.println("\nWalletA's balance is: " + NoobChainFinale.walletA.getBalance( NoobChainFinale.mapUTXOs ));
7273
System.out.println("WalletB's balance is: " + NoobChainFinale.walletB.getBalance( NoobChainFinale.mapUTXOs ));
7374

74-
Block block3 = new Block(block2.hash);
75+
Block block3 = new Block(block2.getHash() );
7576
System.out.println("\nWalletB is Attempting to send funds (20) to WalletA...");
7677
block3.addTransaction( NoobChainFinale.walletB.sendFunds( NoobChainFinale.walletA.publicKey, 20, NoobChainFinale.mapUTXOs),
7778
NoobChainFinale.mapUTXOs, NoobChainFinale.minimumTransaction);

src/main/java/gabywald/crypto/blockchain/NoobChain.java renamed to src/main/java/gabywald/crypto/blockchain/NoobChainIntegration.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* <br/><a href="https://github.com/CryptoKass/NoobChain-Tutorial-Part-2">https://github.com/CryptoKass/NoobChain-Tutorial-Part-2</a>
1616
* @author Gabriel Chandesris (2021)
1717
*/
18-
public class NoobChain {
18+
public class NoobChainIntegration {
1919

2020
private static List<Block> blockchain = new ArrayList<Block>();
2121
private static List<Block> blockchain2 = new ArrayList<Block>();
@@ -29,38 +29,38 @@ public static void main(String[] args) {
2929

3030
// ***** Part I
3131
Block genesisBlock = new Block("Hi im the first block", "0");
32-
System.out.println("Hash for block 1 : " + genesisBlock.hash);
33-
Block secondBlock = new Block("Yo im the second block", genesisBlock.hash);
34-
System.out.println("Hash for block 2 : " + secondBlock.hash);
35-
Block thirdBlock = new Block("Hey im the third block", secondBlock.hash);
36-
System.out.println("Hash for block 3 : " + thirdBlock.hash);
32+
System.out.println("Hash for block 1 : " + genesisBlock.getHash());
33+
Block secondBlock = new Block("Yo im the second block", genesisBlock.getHash());
34+
System.out.println("Hash for block 2 : " + secondBlock.getHash());
35+
Block thirdBlock = new Block("Hey im the third block", secondBlock.getHash());
36+
System.out.println("Hash for block 3 : " + thirdBlock.getHash());
3737

3838

3939
// ***** Part II
4040
// Add our blocks to the blockchain List:
41-
NoobChain.blockchain.add(new Block("Hi im the first block", "0"));
42-
NoobChain.blockchain.add(new Block("Yo im the second block", NoobChain.blockchain.get(NoobChain.blockchain.size()-1).hash));
43-
NoobChain.blockchain.add(new Block("Hey im the third block", NoobChain.blockchain.get(NoobChain.blockchain.size()-1).hash));
44-
String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(NoobChain.blockchain);
41+
NoobChainIntegration.blockchain.add(new Block("Hi im the first block", "0"));
42+
NoobChainIntegration.blockchain.add(new Block("Yo im the second block", NoobChainIntegration.blockchain.get(NoobChainIntegration.blockchain.size()-1).getHash() ));
43+
NoobChainIntegration.blockchain.add(new Block("Hey im the third block", NoobChainIntegration.blockchain.get(NoobChainIntegration.blockchain.size()-1).getHash() ));
44+
String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(NoobChainIntegration.blockchain);
4545
System.out.println(blockchainJson);
4646

4747
// ***** Part III
4848
// Add our blocks to the blockchain List:
49-
NoobChain.blockchain2.add(new Block("Hi im the first block", "0"));
49+
NoobChainIntegration.blockchain2.add(new Block("Hi im the first block", "0"));
5050
System.out.println("Trying to Mine block 1... ");
51-
NoobChain.blockchain2.get(0).mineBlock(NoobChain.difficulty);
51+
NoobChainIntegration.blockchain2.get(0).mineBlock(NoobChainIntegration.difficulty);
5252

53-
NoobChain.blockchain2.add(new Block("Yo im the second block", NoobChain.blockchain2.get(NoobChain.blockchain2.size()-1).hash));
53+
NoobChainIntegration.blockchain2.add(new Block("Yo im the second block", NoobChainIntegration.blockchain2.get(NoobChainIntegration.blockchain2.size()-1).getHash() ));
5454
System.out.println("Trying to Mine block 2... ");
55-
NoobChain.blockchain2.get(1).mineBlock(NoobChain.difficulty);
55+
NoobChainIntegration.blockchain2.get(1).mineBlock(NoobChainIntegration.difficulty);
5656

57-
NoobChain.blockchain2.add(new Block("Hey im the third block", NoobChain.blockchain2.get(NoobChain.blockchain2.size()-1).hash));
57+
NoobChainIntegration.blockchain2.add(new Block("Hey im the third block", NoobChainIntegration.blockchain2.get(NoobChainIntegration.blockchain2.size()-1).getHash() ));
5858
System.out.println("Trying to Mine block 3... ");
59-
NoobChain.blockchain2.get(2).mineBlock(NoobChain.difficulty);
59+
NoobChainIntegration.blockchain2.get(2).mineBlock(NoobChainIntegration.difficulty);
6060

61-
System.out.println("\nBlockchain is Valid: " + BlockChain.isChainValidV1( NoobChain.blockchain2 ));
61+
System.out.println("\nBlockchain is Valid: " + BlockChain.isChainValidV1( NoobChainIntegration.blockchain2 ));
6262

63-
String blockchain2Json = new GsonBuilder().setPrettyPrinting().create().toJson(NoobChain.blockchain2);
63+
String blockchain2Json = new GsonBuilder().setPrettyPrinting().create().toJson(NoobChainIntegration.blockchain2);
6464
System.out.println("\nThe block chain: ");
6565
System.out.println(blockchain2Json);
6666

@@ -70,21 +70,21 @@ public static void main(String[] args) {
7070
// Security.addProvider(new sun.security.provider.Sun());
7171
// XXX NOTE see https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html
7272
// Create the new wallets
73-
NoobChain.walletA = new Wallet();
74-
NoobChain.walletB = new Wallet();
73+
NoobChainIntegration.walletA = new Wallet();
74+
NoobChainIntegration.walletB = new Wallet();
7575
// Test public and private keys
7676
System.out.println("Private and public keys:");
77-
System.out.println(StringUtils.getStringFromKey(NoobChain.walletA.privateKey));
78-
System.out.println(StringUtils.getStringFromKey(NoobChain.walletA.publicKey));
77+
System.out.println(StringUtils.getStringFromKey(NoobChainIntegration.walletA.privateKey));
78+
System.out.println(StringUtils.getStringFromKey(NoobChainIntegration.walletA.publicKey));
7979
// Create a test transaction from WalletA to walletB
80-
Transaction transaction = new Transaction(NoobChain.walletA.publicKey, NoobChain.walletB.publicKey, 5, null);
81-
transaction.generateSignature(NoobChain.walletA.privateKey);
80+
Transaction transaction = new Transaction(NoobChainIntegration.walletA.publicKey, NoobChainIntegration.walletB.publicKey, 5, null);
81+
transaction.generateSignature(NoobChainIntegration.walletA.privateKey);
8282
// Verify the signature works and verify it from the public key
8383
System.out.println("Is signature verified");
8484
System.out.println(transaction.verifySignature());
8585

8686
// ***** Part V
87-
// TODO next step of tutorials !!
87+
// XXX NOTE see NoobChainFinale and NoobChainTests !!
8888
}
8989

9090
/*

0 commit comments

Comments
 (0)