Question: Here is the code! class BlockchainNode { // The basic BlockchainNode... String currHash; // To store the hash String contents; // To store the data
Here is the code!
class BlockchainNode { // The basic BlockchainNode...
String currHash; // To store the hash
String contents; // To store the data
int blockNumber; // The number of the node in the block after the Genesis Node
BlockchainNode next; // The next BlockchainNode
BlockchainNode prev; // The previous BlockchainNode
BlockchainNode (String s) {
// Creates a Genesis node
currHash= StringUtil.applySha256(Integer.toString(0)+"AllZeros:This is the Genesis String!"+s);
contents= s;
blockNumber= 0;
next= null;
prev= null;
}
BlockchainNode (String s, String pH, int bN) {
// Creates a new blocknumbered bN with contents s
currHash= StringUtil.applySha256(Integer.toString(bN)+pH+s);
contents= s;
blockNumber= bN;
next= null;
prev= null;
}
}
class SimpleBlockchain {
// Class invariant: all nodes in the Blockchain satisfy "Blockchain Validity"
// i.e. Blockchain Validity holds of all nodes (except the Genesis node):
// StringUtil.applySha256(this.blockNumber+ prev.currHash+this.contents == this.currHash)
// AND the Genesis node has been correctly initialised
BlockchainNode genesisNode; // Created by calling BlockchainNode (String s)
BlockchainNode lastNode; // The last node in the Blockchain
SimpleBlockchain(String s){
genesisNode= new BlockchainNode(s);
lastNode= genesisNode;
}
void addBlock(String s) { //TODO
// Post: Creates a new valid block with contents s, and adds it
// to the current SimpleBlockchain so that the result satisfies the blockchain condition.
}
boolean validate () { //TODO
// Post: Returns true if the SimpleBlockchain is valid, i.e. satisfies the blockChain condition
// null SimplBlockchains are valid
return true;
}
BlockchainNode findTamperedNode() { //TODO
// Post: If validate returns false, returns the address of the first node which fails to validate
// If validate returns true, then returns null
return null;
}
}
AND here is the test!
public void testAddBlock() {
SimpleBlockchain testBc= new SimpleBlockchain("hello");
BlockchainNode newBc= new BlockchainNode ("number two", testBc.genesisNode.currHash, 1);
testBc.addBlock("number two");
assertEquals("number two", testBc.lastNode.contents);
assertEquals(1, testBc.lastNode.blockNumber);
assertEquals(newBc.currHash, testBc.lastNode.currHash);
assertSame(testBc.genesisNode.next, testBc.lastNode);
}
@Test
public void testValidate() {
SimpleBlockchain testBc= new SimpleBlockchain("hello");
testBc.addBlock("number two"); // assumes addBlock is correctly implemented
assertTrue(testBc.validate());
}
@Test
public void findTamperedNode() {
SimpleBlockchain testBc= new SimpleBlockchain("hello");
testBc.addBlock("number two"); // assumes addBlock is correctly implemented
testBc.lastNode.contents= "something else"; // A simple attack: Change the block chain contents
assertFalse(testBc.validate()); // No longer valid
assertSame(testBc.lastNode, testBc.findTamperedNode()); // Last node has been altered
}
public ArrayList
ArrayList
if (items.length > 0){
list.add(new DNode(items[0],null,null));
for (int i = 1; i < items.length; i++){
DNode toAdd = new DNode(items[i],list.get(i-1),null);
list.add(toAdd);
list.get(i-1).next = toAdd;
}
}
return list;
}
}
Can anyone help plz !!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
