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