-
-
Save pointerpin/c08f0bd93a37bcf0da1088c55303bacb to your computer and use it in GitHub Desktop.
Make a genesis block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2019 cruzbit developers | |
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file. | |
package main | |
import ( | |
"encoding/base64" | |
"encoding/hex" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"log" | |
"math/rand" | |
"time" | |
. "github.com/cruzbit/cruzbit" | |
"golang.org/x/crypto/ed25519" | |
) | |
// Generate a genesis block | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
memoPtr := flag.String("memo", "", "A memo to include in the genesis block's coinbase memo field") | |
pubKeyPtr := flag.String("pubkey", "", "A public key to include in the genesis block's coinbase output") | |
flag.Parse() | |
if len(*memoPtr) == 0 { | |
log.Fatal("Memo required for genesis block") | |
} | |
if len(*pubKeyPtr) == 0 { | |
log.Fatal("Public key required for genesis block") | |
} | |
pubKeyBytes, err := base64.StdEncoding.DecodeString(*pubKeyPtr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pubKey := ed25519.PublicKey(pubKeyBytes) | |
// create the coinbase | |
tx := NewTransaction(nil, pubKey, INITIAL_COINBASE_REWARD, 0, 0, 0, 0, *memoPtr) | |
// create the block | |
targetBytes, err := hex.DecodeString(INITIAL_TARGET) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var target BlockID | |
copy(target[:], targetBytes) | |
block, err := NewBlock(BlockID{}, 0, target, BlockID{}, []*Transaction{tx}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// mine it | |
targetInt := block.Header.Target.GetBigInt() | |
ticker := time.NewTicker(30 * time.Second) | |
done: | |
for { | |
select { | |
case <-ticker.C: | |
block.Header.Time = time.Now().Unix() | |
default: | |
// keep hashing until proof-of-work is satisfied | |
idInt := block.Header.IDFast() | |
if idInt.Cmp(targetInt) <= 0 { | |
break done | |
} | |
block.Header.Nonce += 1 | |
if block.Header.Nonce > MAX_NUMBER { | |
block.Header.Nonce = 0 | |
} | |
} | |
} | |
blockJson, err := json.Marshal(block) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s\n", blockJson) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment