forked from hacash/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
executable file
·73 lines (68 loc) · 2.13 KB
/
block.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 genesis
import (
"bytes"
"encoding/hex"
"github.com/hacash/core/blocks"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
"github.com/hacash/core/transactions"
"sync"
"time"
)
var (
genesisBlock *blocks.Block_v1 = nil
)
var genesisblockLock sync.RWMutex
/**
* 创世区块
*/
func GetGenesisBlock() *blocks.Block_v1 {
genesisblockLock.Lock()
defer genesisblockLock.Unlock()
if genesisBlock != nil {
return genesisBlock
}
genesis := blocks.NewEmptyBlockVersion1(nil)
//loc, _ := time.LoadLocation("Asia/Chongqing")
secondsEastOfUTC := int((8 * time.Hour).Seconds())
loc_chongqing := time.FixedZone("Asia/Chongqing", secondsEastOfUTC)
//fmt.Println(time.Now().In(loc))
ttt := time.Date(2019, time.February, 4, 11, 25, 0, 0, loc_chongqing).Unix()
//fmt.Println( ttt )
genesis.Timestamp = fields.BlockTxTimestamp(ttt)
genesis.Nonce = fields.VarUint4(160117829)
genesis.Difficulty = fields.VarUint4(0)
// coinbase
addrreadble := "1271438866CSDpJUqrnchoJAiGGBFSQhjd"
addr, e0 := fields.CheckReadableAddress(addrreadble)
if e0 != nil {
panic(e0)
}
reward := fields.NewAmountNumSmallCoin(1)
coinbase := transactions.NewTransaction_0_CoinbaseV0()
coinbase.Address = *addr
coinbase.Reward = *reward
coinbase.Message = "hardertodobetter"
genesis.TransactionCount = 1
genesis.Transactions = make([]interfaces.Transaction, 1)
genesis.Transactions[0] = coinbase
root := blocks.CalculateMrklRoot(genesis.GetTrsList())
genesis.SetMrklRoot(root)
////////////////////
//fmt.Println( hex.EncodeToString(root) )
//bbbt1, _ := coinbase.Serialize();
//fmt.Println( hex.EncodeToString(bbbt1) )
//bbbt2, _ := genesis.Serialize();
//fmt.Println( hex.EncodeToString(bbbt2) )
////////////////////
hash := genesis.HashFresh()
check_hash := "000000077790ba2fcdeaef4a4299d9b667135bac577ce204dee8388f1b97f7e6"
check, _ := hex.DecodeString(check_hash)
if 0 != bytes.Compare(hash, check) {
panic("Genesis Block HashWithFee Error: need " + check_hash + ", but give " + hex.EncodeToString(hash))
}
genesisBlock = genesis
//bbb, _ := genesisBlock.Serialize()
//fmt.Println( hex.EncodeToString(bbb) )
return genesisBlock
}