Skip to content

Commit

Permalink
Initial work on PoW
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoras committed Feb 1, 2019
1 parent 497b600 commit 3939fa8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions chainparams.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"consensus_type": "PoA",
"creator": "Ivan Voras <ivoras@gmail.com>",
"genesis_block_timestamp": "2018-08-16T12:49:32+02:00",
"bootstrap_peers": [ "cosmos.ivoras.net:2017" ]
Expand Down
9 changes: 9 additions & 0 deletions chainparams.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
37 changes: 37 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<<z) == 0 {
nBits++
} else {
break
}
}
}
}
return nBits
}

0 comments on commit 3939fa8

Please sign in to comment.