forked from YuraFjrosr/xfsgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenesis.go
More file actions
140 lines (131 loc) · 4.44 KB
/
Copy pathgenesis.go
File metadata and controls
140 lines (131 loc) · 4.44 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2018 The xfsgo Authors
// This file is part of the xfsgo library.
//
// The xfsgo library is free software: you can redistribute it and/or modify
// it under the terms of the MIT Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The xfsgo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// MIT Lesser General Public License for more details.
//
// You should have received a copy of the MIT Lesser General Public License
// along with the xfsgo library. If not, see <https://mit-license.org/>.
package xfsgo
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"strings"
"xfsgo/common"
"xfsgo/storage/badger"
"github.com/sirupsen/logrus"
)
var (
maxTarget = new(big.Int).Lsh(big0xff, ((32-4)*8)+6)
)
// WriteGenesisBlock constructs the genesis blcok for the blockchain and stores it in the hd.
func WriteGenesisBlock(stateDB, chainDB *badger.Storage, reader io.Reader) (*Block, error) {
contents, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
// Genesis specifies the header fields, state of a genesis block. It also defines accounts
var genesis struct {
Version int32 `json:"version"`
HashPrevBlock string `json:"hash_prev_block"`
Timestamp string `json:"timestamp"`
Coinbase string `json:"coinbase"`
Bits uint32 `json:"bits"`
Nonce uint64 `json:"nonce"`
Accounts map[string]struct {
Balance string `json:"balance"`
} `json:"accounts"`
}
if err = json.Unmarshal(contents, &genesis); err != nil {
return nil, err
}
stateTree := NewStateTree(stateDB, nil)
// logrus.Debugf("initialize genesis account count: %d", len(genesis.Accounts))
for addr, a := range genesis.Accounts {
address := common.B58ToAddress([]byte(addr))
balance := common.ParseString2BigInt(a.Balance)
stateTree.AddBalance(address, balance)
// logrus.Debugf("initialize genesis account: %s, balance: %d", address, balance)
}
stateTree.UpdateAll()
timestamp := common.ParseString2BigInt(genesis.Timestamp)
var coinbase common.Address
if genesis.Coinbase != "" {
coinbase = common.B58ToAddress([]byte(genesis.Coinbase))
}
rootHash := common.Bytes2Hash(stateTree.Root())
HashPrevBlock := common.Hex2Hash(genesis.HashPrevBlock)
block := NewBlock(&BlockHeader{
Nonce: genesis.Nonce,
HashPrevBlock: HashPrevBlock,
Timestamp: timestamp.Uint64(),
Coinbase: coinbase,
Bits: genesis.Bits,
StateRoot: rootHash,
}, nil, nil)
chain := newChainDB(chainDB)
if old := chain.GetBlockByHash(block.Hash()); old != nil {
logrus.Infof("get genesis block hash: %s", old.HashHex())
return old, nil
}
logrus.Infof("write genesis block hash: %s", block.HashHex())
if err = stateTree.Commit(); err != nil {
return nil, err
}
if err = chain.WriteBlock(block); err != nil {
return nil, err
}
if err = chain.WriteHead(block); err != nil {
return nil, err
}
return block, nil
}
// WriteMainNetGenesisBlock constructs and stores a genesis blcok with default for xfs blockchain in mainnet model.
//
func WriteMainNetGenesisBlock(stateDB, blockDB *badger.Storage) (*Block, error) {
bits := BigByZip(maxTarget)
jsonStr := fmt.Sprintf(`{
"nonce": 0,
"bits": %d,
"coinbase": "1Eux8FG5RuaEiqRKEZgbSqHZwhskDjmS2p",
"accounts": {
"1Eux8FG5RuaEiqRKEZgbSqHZwhskDjmS2p": {"balance": "10000"}
}
}`, bits)
return WriteGenesisBlock(stateDB, blockDB, strings.NewReader(jsonStr))
}
// WriteMainNetGenesisBlock constructs and stores a genesis blcok with default for xfs blockchain in testnet model.
func WriteTestNetGenesisBlock(stateDB, blockDB *badger.Storage) (*Block, error) {
bits := BigByZip(maxTarget)
jsonStr := fmt.Sprintf(`{
"nonce": 0,
"bits": %d,
"coinbase": "18eAREmwhthQZeW3LASBXWi6ibPjjpYC6j",
"accounts": {
"18eAREmwhthQZeW3LASBXWi6ibPjjpYC6j": {"balance": "10000"}
}
}`, bits)
return WriteGenesisBlock(stateDB, blockDB, strings.NewReader(jsonStr))
}
func WriteTestGenesisBlock(stateDB, blockDB *badger.Storage) (*Block, error) {
bits := BigByZip(maxTarget)
jsonStr := fmt.Sprintf(`{
"nonce": 0,
"bits": %d,
"coinbase": "1A2QiH4FYc9c4nsNjCMxygg9HKTK9EJWX5",
"accounts": {
"1A2QiH4FYc9c4nsNjCMxygg9HKTK9EJWX5": {"balance": "10000000"}
}
}`, bits)
return WriteGenesisBlock(stateDB, blockDB, strings.NewReader(jsonStr))
}