Repository of Presentation:
https://github.com/ArturSkowronski/naivechain-java
Presented:
Bielsko-Biała Java User Group (Bielsko-Biała JUG), Kraków, 22.02.2018 (https://www.meetup.com/pl-PL/Bielsko-Biala-JUG/events/247764157/)
Biorąc pod uwagę szalone skoki (i spadki) wszelkich kryptowalut, Blockchain jest na językach wszystkich developerów. Jednocześnie ilość mitów, klechd i legend jakimi obrósł osiągnęła już rozmiary które warto nieco uporządkować. W ramach tej prezentacji postaram się rozwiać niektóre z nich, a dodatkowo pokazać Blockchain od strony stricte programistycznej - w ramach sesji Live Codingu zaimplementujemy uproszczony, ale kompletny wariant Blockchaina, zbliżony do tego znanego z Bitcoina. Wszystko to używając naszej ulubionej Javy. Dodatkowo, postaram się wskazać jej wady i zalety i wytłumaczyć, dlaczego jak grzyby po deszczu zewsząd wyrastają alternatywne implementacje mające być lekiem na całe zło (i dlaczego zwykle nie są). Mam nadzieje uczynić całość interaktywną - jeżeli ktoś ma jakieś pytania będę się starał na nie odpowiedzieć podczas prezentacji. Moim celem jest żeby każdy wyszedł z lepszą świadomością tego czym Blockchain jest, a co nawet najważniejsze - czym z pewnością Blockchain nie jest. ___
Artur to developer z serduchem rozdartym między JVM (całościowo) i nowoczesnym JavaScript - przy czym. z bardziej dojrzałymi planami matrymonialnymi związanymi z tą pierwszą platformą.
Nie może się powstrzymać przed testowaniem nowych technologii, nawet jeśli czasem wybuchają mu w twarz w wyniku nadużycia w projektach szpachli i taśmy klejącej.
Obecnie członek zespołu VirtusLab realizujący projekty dla Tesco Technology.
Część Dynamicznego Duetu którego lepszą połową jest pluszowa foka (ᵔᴥᵔ).
31. public class Block {
protected int index;
protected String hash;
protected String previousHash;
protected long timestamp;
protected Object data;
protected int nonce;
}
BLOCK PART
32. BLOCK PART
public class Block {
protected int index;
protected String hash;
protected String previousHash;
protected long timestamp;
protected Object data;
protected int nonce;
}
33. BLOCK PART
public class Block {
protected int index;
protected String hash;
protected String previousHash;
protected long timestamp;
protected Object data;
protected int nonce;
}
34. BLOCK PART
public class Block {
protected int index;
protected String hash;
protected String previousHash;
protected long timestamp;
protected Object data;
protected int nonce;
}
35. BLOCK PART
public class Block {
protected int index;
protected String hash;
protected String previousHash;
protected long timestamp;
protected Object data;
protected int nonce;
}
36. CHAIN PART
public class Blockchain extends LinkedList<Block> {
public Blockchain(Block block) {
this.add(GENESIS_BLOCK);
}
}
37. CHAIN PART
public class Blockchain extends LinkedList<Block> {
public Blockchain(Block block) {
this.add(GENESIS_BLOCK);
}
}
38. CHAIN PART
public class Blockchain extends LinkedList<Block> {
public Blockchain(Block block) {
this.add(GENESIS_BLOCK);
}
}
39. CHAIN PART
public class Blockchain extends LinkedList<Block> {
public Blockchain(Block block) {
this.add(GENESIS_BLOCK);
}
}
40. GENESIS BLOCK
public static final Block GENESIS_BLOCK = new Block(
0, //index
“e6337db8a921823784c14378abed4f7d7", //hash
null, //previousHash
1465154705, //timestamp
"Pozdro Bielsko-Biala!”, //data
0 //nonce
);
41. MINING BLOCK
public class BlockchainService {
Blockchain chain = new Blockchain();
public Block mineBlock(Object data) => {
Block newBlock = nextBlock(chain.getLast(), data);
chain.addBlock(newBlock);
return chain.getLast();
};
}
42. MINING BLOCK
public class BlockchainService {
Blockchain chain = new Blockchain();
public Block mineBlock(Object data) => {
Block newBlock = nextBlock(chain.getLast(), data);
chain.addBlock(newBlock);
return chain.getLast();
};
}
43. MINING BLOCK
public class BlockchainService {
Blockchain chain = new Blockchain();
public Block mineBlock(Object data) => {
Block newBlock = nextBlock(chain.getLast(), data);
chain.addBlock(newBlock);
return chain.getLast();
};
}
44. MINING BLOCK
public static Block nextBlock(Block prevBlock, Object data) {
int nextIndex = previousBlock.index + 1;
String prevHash = prevBlock.getHash();
long nextTime = new Date().getTime();
String nextHash = calculateHash(nextIndex, prevHash, nextTime, data);
return new Block(nextIndex, prevHash, nextHash, nextTime, data);
}
45. MINING BLOCK
public static Block nextBlock(Block prevBlock, Object data) {
int nextIndex = previousBlock.index + 1;
String prevHash = prevBlock.getHash();
long nextTime = new Date().getTime();
String nextHash = calculateHash(nextIndex, prevHash, nextTime, data);
return new Block(nextIndex, prevHash, nextHash, nextTime, data);
}
46. MINING BLOCK
public static Block nextBlock(Block prevBlock, Object data) {
int nextIndex = previousBlock.index + 1;
String prevHash = prevBlock.getHash();
long nextTime = new Date().getTime();
String nextHash = calculateHash(nextIndex, prevHash, nextTime, data);
return new Block(nextIndex, prevHash, nextHash, nextTime, data);
}
47. MINING BLOCK
public static Block nextBlock(Block prevBlock, Object data) {
int nextIndex = previousBlock.index + 1;
String prevHash = prevBlock.getHash();
long nextTime = new Date().getTime();
String nextHash = calculateHash(nextIndex, prevHash, nextTime, data);
return new Block(nextIndex, prevHash, nextHash, nextTime, data);
}
48. MINING BLOCK
public static Block nextBlock(Block prevBlock, Object data) {
int nextIndex = previousBlock.index + 1;
String prevHash = prevBlock.getHash();
long nextTime = new Date().getTime();
String nextHash = calculateHash(nextIndex, prevHash, nextTime, data);
return new Block(nextIndex, prevHash, nextHash, nextTime, data);
}
62. NEW BLOCK HANDLING
public Response handleBlockchainResponse(Blockchain newChain) {
if (newChain.getLast().getIndex() > chain.getLast().getIndex()) {
if (chain.getLast().getHash().equals(newChain.getLast().getPreviousHash())){
chain.addBlock(latestBlockReceived);
return new Response(RESPONSE_BLOCKCHAIN, new Blockchain(chain.getLast()));
}
return exchangeChains(newChain);
}
return new Response(DO_NOTHING);
}
63. NEW BLOCK HANDLING
public Response handleBlockchainResponse(Blockchain newChain) {
if (newChain.getLast().getIndex() > chain.getLast().getIndex()) {
if (chain.getLast().getHash().equals(newChain.getLast().getPreviousHash())){
chain.addBlock(latestBlockReceived);
return new Response(RESPONSE_BLOCKCHAIN, new Blockchain(chain.getLast()));
}
return exchangeChains(newChain);
}
return new Response(DO_NOTHING);
}
64. NEW BLOCK HANDLING
public Response handleBlockchainResponse(Blockchain newChain) {
if (newChain.getLast().getIndex() > chain.getLast().getIndex()) {
if (chain.getLast().getHash().equals(newChain.getLast().getPreviousHash())){
chain.addBlock(latestBlockReceived);
return new Response(RESPONSE_BLOCKCHAIN, new Blockchain(chain.getLast()));
}
return exchangeChains(newChain);
}
return new Response(DO_NOTHING);
}
65. NEW BLOCK HANDLING
public Response handleBlockchainResponse(Blockchain newChain) {
if (newChain.getLast().getIndex() > chain.getLast().getIndex()) {
if (chain.getLast().getHash().equals(newChain.getLast().getPreviousHash())){
chain.addBlock(latestBlockReceived);
return new BlockchainResponse(RESPONSE_BLOCKCHAIN, new Blockchain(chain.getLast()));
}
return exchangeChains(newChain);
}
return new BlockchainResponse(DO_NOTHING);
}