Skip to content

Instantly share code, notes, and snippets.

View felixklauke's full-sized avatar
:octocat:
Privatizing world peace.

Felix Klauke felixklauke

:octocat:
Privatizing world peace.
View GitHub Profile

Keybase proof

I hereby claim:

  • I am FelixKlauke on github.
  • I am felixklauke (https://keybase.io/felixklauke) on keybase.
  • I have a public key whose fingerprint is 9B6C 45FA 099B 076C 7BED 00A1 7181 F401 CE7C 0A8E

To claim this, I am signing this object:

Keybase proof

I hereby claim:

  • I am FelixKlauke on github.
  • I am felixklauke (https://keybase.io/felixklauke) on keybase.
  • I have a public key whose fingerprint is 9B6C 45FA 099B 076C 7BED 00A1 7181 F401 CE7C 0A8E

To claim this, I am signing this object:

@felixklauke
felixklauke / ProofOfWork.java
Last active June 21, 2018 06:57
Finding a new proof of work.
private static final String VALIDATION_PREFIX = "00";
public int proofOfWork() {
return proofOfWork(getLastBlock().getProofOfWork());
}
private int proofOfWork(int lastProof) {
int proof = 0;
while (!validate(lastProof, proof)) {
@felixklauke
felixklauke / Block.java
Created June 21, 2018 07:09
Creating the Hash of a block.
public String getHash() {
return DigestUtils.sha256Hex(timestamp + HASH_DELIMITER + transactions.toString() + HASH_DELIMITER + previousBlockHash + HASH_DELIMITER + proofOfWork);
}
@felixklauke
felixklauke / Block.java
Last active November 6, 2018 05:55
BlockChain example.
import org.apache.commons.codec.digest.DigestUtils;
import java.util.Collections;
import java.util.List;
/**
* Represents one block in the chain.
*
* @author Felix Klauke <info@felix-klauke.de>
*/
@felixklauke
felixklauke / BlockChain.java
Last active November 6, 2018 06:04
Block index aware block instantiation.
/**
* Start mining a new block.
*
* @return The block instance.
*/
public Block mineBlock() {
int proofOfWork = proofOfWork();
Block lastBlock = blocks.getLast();
Block block = new Block(lastBlock.getIndex() + 1, lastBlock.getHas(), transactions, proofOfWork);
@felixklauke
felixklauke / BlockChainService.java
Last active June 22, 2018 11:05
Simple block chains service layer implementation.
@Service
public class BlockChainService {
private BlockChain blockChain = new BlockChain();
public BlockChainService() {
blockChain.addBlock(new Block(0, "", new ArrayList<>(), 0));
}
public void addTransaction(Transaction transaction) {
@felixklauke
felixklauke / BlockChainController.java
Created June 22, 2018 10:11
Simple controller exposing rest endpoints that operate on a block chain service.
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BlockChainController {
private final BlockChainService blockChainService;
@RequestMapping(method = RequestMethod.GET, path = "/chain")
public List<Block> requestChainContent() {
return blockChainService.getBlockChainContent();
}
@felixklauke
felixklauke / SageNodeApplication.java
Created June 22, 2018 10:14
Node application bootstrap.
@SpringBootApplication
public class SageNodeApplication {
public static void main(String[] args) {
SpringApplication.run(SageNodeApplication.class, args);
}
}
/**
* Providing options for low level file access using an input and an output stream.
*/
class FileSystemAccessor {
public:
/**
* Create a new file system accessor by passing the file we want to access under the hood.
*