kaspad/cmd/genesis/genesis.go
stasatdaglabs f46dec449d [NOD-510] Change all references to Bitcoin to Kaspa (#531)
* [NOD-510] Change coinbase flags to kaspad.

* [NOD-510] Removed superfluous spaces after periods in comments.

* [NOD-510] Rename btcd -> kaspad in the root folder.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Continue renaming btcd -> kaspad.

* [NOD-510] Rename btcjson -> kaspajson.

* [NOD-510] Rename file names inside kaspajson.

* [NOD-510] Rename kaspajson -> jsonrpc.

* [NOD-510] Finish renaming in addrmgr.

* [NOD-510] Rename package btcec to ecc.

* [NOD-510] Finish renaming stuff in blockdag.

* [NOD-510] Rename stuff in cmd.

* [NOD-510] Rename stuff in config.

* [NOD-510] Rename stuff in connmgr.

* [NOD-510] Rename stuff in dagconfig.

* [NOD-510] Rename stuff in database.

* [NOD-510] Rename stuff in docker.

* [NOD-510] Rename stuff in integration.

* [NOD-510] Rename jsonrpc to rpcmodel.

* [NOD-510] Rename stuff in limits.

* [NOD-510] Rename stuff in logger.

* [NOD-510] Rename stuff in mempool.

* [NOD-510] Rename stuff in mining.

* [NOD-510] Rename stuff in netsync.

* [NOD-510] Rename stuff in peer.

* [NOD-510] Rename stuff in release.

* [NOD-510] Rename stuff in rpcclient.

* [NOD-510] Rename stuff in server.

* [NOD-510] Rename stuff in signal.

* [NOD-510] Rename stuff in txscript.

* [NOD-510] Rename stuff in util.

* [NOD-510] Rename stuff in wire.

* [NOD-510] Fix failing tests.

* [NOD-510] Fix merge errors.

* [NOD-510] Fix go vet errors.

* [NOD-510] Remove merged file that's no longer relevant.

* [NOD-510] Add a comment above Op0.

* [NOD-510] Fix some comments referencing Bitcoin Core.

* [NOD-510] Fix some more comments referencing Bitcoin Core.

* [NOD-510] Fix bitcoin -> kaspa.

* [NOD-510] Fix more bitcoin -> kaspa.

* [NOD-510] Fix comments, remove DisconnectBlock in addrindex.

* [NOD-510] Rename KSPD to KASD.

* [NOD-510] Fix comments and user agent.
2019-12-12 15:21:41 +02:00

92 lines
3.0 KiB
Go

// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"fmt"
"math/big"
"time"
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/dagconfig"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/daghash"
"github.com/kaspanet/kaspad/wire"
)
// solveGenesisBlock attempts to find some combination of a nonce and
// current timestamp which makes the passed block hash to a value less than the
// target difficulty.
func solveGenesisBlock(block *wire.MsgBlock, powBits uint32, netName string) {
// Create some convenience variables.
header := &block.Header
targetDifficulty := util.CompactToBig(header.Bits)
header.Bits = powBits
// Search through the entire nonce range for a solution while
// periodically checking for early quit and stale block
// conditions along with updates to the speed monitor.
maxNonce := ^uint64(0) // 2^64 - 1
for {
header.Timestamp = time.Unix(time.Now().Unix(), 0)
for i := uint64(0); i <= maxNonce; i++ {
// Update the nonce and hash the block header. Each
// hash is actually a double sha256 (two hashes), so
// increment the number of hashes completed for each
// attempt accordingly.
header.Nonce = i
hash := header.BlockHash()
// The block is solved when the new block hash is less
// than the target difficulty. Yay!
if daghash.HashToBig(hash).Cmp(targetDifficulty) <= 0 {
fmt.Printf("\n\nGenesis block of %s is solved:\n", netName)
fmt.Printf("timestamp: 0x%x\n", header.Timestamp.Unix())
fmt.Printf("bits (difficulty): 0x%x\n", header.Bits)
fmt.Printf("nonce: 0x%x\n", header.Nonce)
fmt.Printf("hash: %v\n\n\n", hex.EncodeToString(hash[:]))
return
}
}
}
}
func validateGenesisBlock(genesisBlock *wire.MsgBlock, netName string) bool {
block := util.NewBlock(genesisBlock)
hashMerkleTree := blockdag.BuildHashMerkleTreeStore(block.Transactions())
calculatedHashMerkleRoot := hashMerkleTree.Root()
header := genesisBlock.Header
if !header.HashMerkleRoot.IsEqual(calculatedHashMerkleRoot) {
fmt.Printf("%s: genesis block hash merkle root is invalid - block "+
"header indicates %s, but calculated value is %s\n\n",
netName, hex.EncodeToString(header.HashMerkleRoot[:]),
hex.EncodeToString(calculatedHashMerkleRoot[:]))
return false
}
return true
}
func validateAndSolve(genesisBlock *wire.MsgBlock, powBits uint32, netName string) {
// Validate merkle root
if validateGenesisBlock(genesisBlock, netName) {
// Solve genesis block
solveGenesisBlock(genesisBlock, powBits, netName)
}
}
// main
func main() {
bigOne := big.NewInt(1)
validateAndSolve(dagconfig.MainNetParams.GenesisBlock,
util.BigToCompact(new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)),
"MainNet")
validateAndSolve(dagconfig.DevNetParams.GenesisBlock,
util.BigToCompact(new(big.Int).Sub(new(big.Int).Lsh(bigOne, 239), bigOne)),
"DevNet")
}