This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
genesis.go
73 lines (63 loc) · 1.68 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package config
import (
log "github.com/sirupsen/logrus"
"github.com/bytom/consensus"
"github.com/bytom/consensus/difficulty"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/legacy"
)
// GenerateGenesisTx will return genesis transaction
func GenerateGenesisTx() *legacy.Tx {
txData := legacy.TxData{
Version: 1,
SerializedSize: 63,
Inputs: []*legacy.TxInput{},
Outputs: []*legacy.TxOutput{
&legacy.TxOutput{
AssetVersion: 1,
OutputCommitment: legacy.OutputCommitment{
AssetAmount: bc.AssetAmount{
AssetId: consensus.BTMAssetID,
Amount: consensus.InitialBlockSubsidy,
},
VMVersion: 1,
ControlProgram: []byte{81},
},
},
},
}
return legacy.NewTx(txData)
}
// GenerateGenesisBlock will return genesis block
func GenerateGenesisBlock() *legacy.Block {
genesisCoinbaseTx := GenerateGenesisTx()
merkleRoot, err := bc.MerkleRoot([]*bc.Tx{genesisCoinbaseTx.Tx})
if err != nil {
log.Panicf("Fatal create merkelRoot")
}
var seed [32]byte
sha3pool.Sum256(seed[:], make([]byte, 32))
genesisBlock := &legacy.Block{
BlockHeader: legacy.BlockHeader{
Version: 1,
Height: 0,
Seed: bc.NewHash(seed),
TimestampMS: 1511318565142,
BlockCommitment: legacy.BlockCommitment{
TransactionsMerkleRoot: merkleRoot,
},
Bits: 2161727821138738707,
},
Transactions: []*legacy.Tx{genesisCoinbaseTx},
}
for i := uint64(0); i <= 10000000000000; i++ {
genesisBlock.Nonce = i
hash := genesisBlock.Hash()
if difficulty.CheckProofOfWork(&hash, genesisBlock.Bits) {
break
}
}
log.Infof("genesisBlock:%v", genesisBlock)
return genesisBlock
}