From 3939fa832f5131b8fbdae7f13f2df81476bbf253 Mon Sep 17 00:00:00 2001 From: Ivan Voras Date: Fri, 1 Feb 2019 23:35:18 +0100 Subject: [PATCH] Initial work on PoW --- blockchain.go | 2 +- chainparams.example.json | 1 + chainparams.go | 9 +++++++++ util.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/blockchain.go b/blockchain.go index f4c45b5..a793573 100644 --- a/blockchain.go +++ b/blockchain.go @@ -22,10 +22,10 @@ const GenesisBlockPreviousBlockHash = "10000000000000000000000000000000000000000 // ChainParams describe the genesis block and other blockchain properties var defaultChainParams = ChainParams{ + ConsensusType: ChainConsensusPoA, GenesisBlockHash: "9a0ff19183d1525a36de803047de4b73eb72506be8c81296eb463476a5c2d9e2", GenesisBlockHashSignature: "30460221008b8b3b3cfee2493ef58f2f6a1f1768b564f4c9e9a341ad42912cbbcf5c3ec82f022100fbcdfd0258fa1a5b073d18f688c2fb3d8f9a7c59204c6777f2bbf1faeb1eb1ed", GenesisBlockTimestamp: "2017-05-06T10:38:50Z02:00", - //GenesisBlockTimestamp: "Sat, 06 May 2017 10:38:50 +0200", } var chainParams = defaultChainParams diff --git a/chainparams.example.json b/chainparams.example.json index 7ebb507..e653225 100644 --- a/chainparams.example.json +++ b/chainparams.example.json @@ -1,4 +1,5 @@ { + "consensus_type": "PoA", "creator": "Ivan Voras ", "genesis_block_timestamp": "2018-08-16T12:49:32+02:00", "bootstrap_peers": [ "cosmos.ivoras.net:2017" ] diff --git a/chainparams.go b/chainparams.go index 0199ba0..c616c27 100644 --- a/chainparams.go +++ b/chainparams.go @@ -1,5 +1,10 @@ package main +const ( + ChainConsensusPoA = 0 + ChainConsensusPoW = 1 +) + // ChainParams holds blockchain configuration type ChainParams struct { // GenesisBlockHash is the SHA256 hash of the genesis block payload @@ -16,4 +21,8 @@ type ChainParams struct { // List of host:port string specifying default peers for this blockchain. If empty, the defaults are used. BootstrapPeers []string `json:"bootstrap_peers"` + + // Consensus algorithm used: "PoA", "PoW" + ConsensusTypeString string `json:"consensus_type"` + ConsensusType int } diff --git a/util.go b/util.go index 04d6b4e..641c784 100644 --- a/util.go +++ b/util.go @@ -72,6 +72,25 @@ func hashFileToHexString(fileName string) (string, error) { return hex.EncodeToString(hash.Sum(nil)), nil } +func hashFileToBytes(fileName string) ([]byte, error) { + file, err := os.Open(fileName) + if err != nil { + return nil, err + } + defer func() { + err = file.Close() + if err != nil { + log.Printf("hashFileToHexString file.Close: %v", err) + } + }() + hash := sha256.New() + _, err = io.Copy(hash, file) + if err != nil { + return nil, err + } + return hash.Sum(nil), nil +} + func mustDecodeHex(hexs string) []byte { b, err := hex.DecodeString(hexs) if err != nil { @@ -356,3 +375,21 @@ func copyFile(src, dst string) error { } return out.Close() } + +func countStartZeroBits(b []byte) int { + nBits := 0 + for i := 0; i < len(b); i++ { + if b[i] == 0 { + nBits += 8 + } else { + for z := uint(7); z >= 0; z-- { + if b[i]&(1<