OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / consensus / general.go
1 package consensus
2
3 import (
4         "encoding/binary"
5         "fmt"
6
7         "github.com/bytom/vapor/protocol/bc"
8 )
9
10 // basic constant
11 const (
12         BTMAlias = "BTM"
13
14         PayToWitnessPubKeyHashDataSize = 20
15         PayToWitnessScriptHashDataSize = 32
16
17         _ = iota
18         SoftFork001
19 )
20
21 // BTMAssetID is BTM's asset id, the soul asset of Bytom
22 var BTMAssetID = &bc.AssetID{
23         V0: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
24         V1: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
25         V2: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
26         V3: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
27 }
28
29 // BTMDefinitionMap is the ....
30 var BTMDefinitionMap = map[string]interface{}{
31         "name":        BTMAlias,
32         "symbol":      BTMAlias,
33         "decimals":    8,
34         "description": `Bytom Official Issue`,
35 }
36
37 // BasicConfig indicate the basic config
38 type BasicConfig struct {
39         // gas config
40         MaxBlockGas      uint64 // the max used gas for all transactions of a block
41         MaxGasAmount     int64  // the max gas for a transaction
42         DefaultGasCredit int64  // the max default credit gas for a transaction with non-BTM asset
43         VMGasRate        int64  // the gas rate for VM
44         StorageGasRate   int64  // the gas rate for storage
45
46         // utxo config
47         VotePendingBlockNumber     uint64 // the valid block interval for vote utxo after the vote transaction is confirmed
48         CoinbasePendingBlockNumber uint64 // the valid block interval for coinbase utxo after the coinbase transaction is confirmed
49         CoinbaseArbitrarySizeLimit int    // the max size for coinbase arbitrary
50 }
51
52 // DPOSConfig indicate the dpos consensus config
53 type DPOSConfig struct {
54         NumOfConsensusNode      int64  // the number of consensus node
55         BlockNumEachNode        uint64 // the number of generated continuous blocks for each node
56         RoundVoteBlockNums      uint64 // the block interval which count the vote result in a round
57         MinConsensusNodeVoteNum uint64 // the min BTM amount for becoming consensus node(the unit is neu)
58         MinVoteOutputAmount     uint64 // the min BTM amount for voting output in a transaction(the unit is neu)
59         BlockTimeInterval       uint64 // the block time interval for producting a block
60         MaxTimeOffsetMs         uint64 // the max number of seconds a block time is allowed to be ahead of the current time
61 }
62
63 // Checkpoint identifies a known good point in the block chain.  Using
64 // checkpoints allows a few optimizations for old blocks during initial download
65 // and also prevents forks from old blocks.
66 type Checkpoint struct {
67         Height uint64
68         Hash   bc.Hash
69 }
70
71 // ProducerSubsidy is a subsidy to the producer of the generated block
72 type ProducerSubsidy struct {
73         BeginBlock uint64
74         EndBlock   uint64
75         Subsidy    uint64
76 }
77
78 // MovRewardProgram is a reward address corresponding to the range of the specified block height when matching transactions
79 type MovRewardProgram struct {
80         BeginBlock uint64
81         EndBlock   uint64
82         Program    string
83 }
84
85 // Params store the config for different network
86 type Params struct {
87         // Name defines a human-readable identifier for the network.
88         Name string
89
90         // Bech32HRPSegwit defines the prefix of address for the network
91         Bech32HRPSegwit string
92
93         // DefaultPort defines the default peer-to-peer port for the network.
94         DefaultPort string
95
96         // BasicConfig defines the gas and utxo relatived paramters.
97         BasicConfig
98
99         // DPOSConfig defines the dpos consensus paramters.
100         DPOSConfig
101
102         // DNSSeeds defines a list of DNS seeds for the network that are used
103         // as one method to discover peers.
104         DNSSeeds []string
105
106         // Checkpoints defines the checkpoint blocks
107         Checkpoints []Checkpoint
108
109         // ProducerSubsidys defines the producer subsidy by block height
110         ProducerSubsidys []ProducerSubsidy
111
112         SoftForkPoint map[uint64]uint64
113
114         // Mov will only start when the block height is greater than this value
115         MovStartHeight uint64
116
117         // Used to receive rewards for matching transactions
118         MovRewardPrograms []MovRewardProgram
119 }
120
121 // ActiveNetParams is the active NetParams
122 var ActiveNetParams = MainNetParams
123
124 // NetParams is the correspondence between chain_id and Params
125 var NetParams = map[string]Params{
126         "mainnet": MainNetParams,
127         "testnet": TestNetParams,
128         "solonet": SoloNetParams,
129 }
130
131 // MainNetParams is the config for vapor-mainnet
132 var MainNetParams = Params{
133         Name:            "main",
134         Bech32HRPSegwit: "vp",
135         DefaultPort:     "56656",
136         DNSSeeds:        []string{"www.mainnetseed.vapor.io"},
137         BasicConfig: BasicConfig{
138                 MaxBlockGas:                uint64(10000000),
139                 MaxGasAmount:               int64(640000),
140                 DefaultGasCredit:           int64(160000),
141                 StorageGasRate:             int64(1),
142                 VMGasRate:                  int64(200),
143                 VotePendingBlockNumber:     uint64(3456000),
144                 CoinbasePendingBlockNumber: uint64(7200),
145                 CoinbaseArbitrarySizeLimit: 128,
146         },
147         DPOSConfig: DPOSConfig{
148                 NumOfConsensusNode:      10,
149                 BlockNumEachNode:        12,
150                 MinConsensusNodeVoteNum: uint64(100000000000000),
151                 MinVoteOutputAmount:     uint64(100000000),
152                 BlockTimeInterval:       500,
153                 RoundVoteBlockNums:      1200,
154                 MaxTimeOffsetMs:         2000,
155         },
156         Checkpoints: []Checkpoint{},
157         ProducerSubsidys: []ProducerSubsidy{
158                 {BeginBlock: 1, EndBlock: 63072000, Subsidy: 9512938},
159                 {BeginBlock: 63072001, EndBlock: 126144000, Subsidy: 9512938},
160         },
161         SoftForkPoint:  map[uint64]uint64{SoftFork001: 10461600},
162         MovStartHeight: 42884800,
163         MovRewardPrograms: []MovRewardProgram{
164                 {
165                         BeginBlock: 1,
166                         EndBlock:   126144000,
167                         Program:    "00141d00f85e220e35a23282cfc7f91fe7b34bf6dc18",
168                 },
169         },
170 }
171
172 // TestNetParams is the config for vapor-testnet
173 var TestNetParams = Params{
174         Name:            "test",
175         Bech32HRPSegwit: "tp",
176         DefaultPort:     "56657",
177         DNSSeeds:        []string{"www.testnetseed.vapor.io"},
178         BasicConfig: BasicConfig{
179                 MaxBlockGas:                uint64(10000000),
180                 MaxGasAmount:               int64(640000),
181                 DefaultGasCredit:           int64(160000),
182                 StorageGasRate:             int64(1),
183                 VMGasRate:                  int64(200),
184                 VotePendingBlockNumber:     uint64(3456000),
185                 CoinbasePendingBlockNumber: uint64(7200),
186                 CoinbaseArbitrarySizeLimit: 128,
187         },
188         DPOSConfig: DPOSConfig{
189                 NumOfConsensusNode:      10,
190                 BlockNumEachNode:        12,
191                 MinConsensusNodeVoteNum: uint64(100000000000000),
192                 MinVoteOutputAmount:     uint64(100000000),
193                 BlockTimeInterval:       500,
194                 RoundVoteBlockNums:      1200,
195                 MaxTimeOffsetMs:         2000,
196         },
197         Checkpoints: []Checkpoint{},
198         ProducerSubsidys: []ProducerSubsidy{
199                 {BeginBlock: 1, EndBlock: 63072000, Subsidy: 15000000},
200         },
201 }
202
203 // SoloNetParams is the config for vapor solonet
204 var SoloNetParams = Params{
205         Name:            "solo",
206         Bech32HRPSegwit: "sp",
207         DefaultPort:     "56658",
208         BasicConfig: BasicConfig{
209                 MaxBlockGas:                uint64(10000000),
210                 MaxGasAmount:               int64(200000),
211                 DefaultGasCredit:           int64(160000),
212                 StorageGasRate:             int64(1),
213                 VMGasRate:                  int64(200),
214                 VotePendingBlockNumber:     uint64(10000),
215                 CoinbasePendingBlockNumber: uint64(1200),
216                 CoinbaseArbitrarySizeLimit: 128,
217         },
218         DPOSConfig: DPOSConfig{
219                 NumOfConsensusNode:      10,
220                 BlockNumEachNode:        12,
221                 MinConsensusNodeVoteNum: uint64(100000000000000),
222                 MinVoteOutputAmount:     uint64(100000000),
223                 BlockTimeInterval:       500,
224                 RoundVoteBlockNums:      1200,
225                 MaxTimeOffsetMs:         2000,
226         },
227         Checkpoints: []Checkpoint{},
228         ProducerSubsidys: []ProducerSubsidy{
229                 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
230                 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
231                 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
232                 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
233         },
234 }
235
236 // BlockSubsidy calculate the coinbase rewards on given block height
237 func BlockSubsidy(height uint64) uint64 {
238         for _, subsidy := range ActiveNetParams.ProducerSubsidys {
239                 if height >= subsidy.BeginBlock && height <= subsidy.EndBlock {
240                         return subsidy.Subsidy
241                 }
242         }
243         return 0
244 }
245
246 // BytomMainNetParams is the config for bytom mainnet
247 func BytomMainNetParams(vaporParam *Params) *Params {
248         bech32HRPSegwit := "sm"
249         switch vaporParam.Name {
250         case "main":
251                 bech32HRPSegwit = "bm"
252         case "test":
253                 bech32HRPSegwit = "tm"
254         }
255         return &Params{Bech32HRPSegwit: bech32HRPSegwit}
256 }
257
258 // InitActiveNetParams load the config by chain ID
259 func InitActiveNetParams(chainID string) error {
260         var exist bool
261         if ActiveNetParams, exist = NetParams[chainID]; !exist {
262                 return fmt.Errorf("chain_id[%v] don't exist", chainID)
263         }
264         return nil
265 }
266