OSDN Git Service

b34f19d80eda04f550895a9685de132375ef2f50
[bytom/vapor.git] / consensus / general.go
1 package consensus
2
3 import (
4         "encoding/binary"
5         "fmt"
6
7         "github.com/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 // Params store the config for different network
79 type Params struct {
80         // Name defines a human-readable identifier for the network.
81         Name string
82
83         // Bech32HRPSegwit defines the prefix of address for the network
84         Bech32HRPSegwit string
85
86         // DefaultPort defines the default peer-to-peer port for the network.
87         DefaultPort string
88
89         // BasicConfig defines the gas and utxo relatived paramters.
90         BasicConfig
91
92         // DPOSConfig defines the dpos consensus paramters.
93         DPOSConfig
94
95         // DNSSeeds defines a list of DNS seeds for the network that are used
96         // as one method to discover peers.
97         DNSSeeds []string
98
99         // Checkpoints defines the checkpoint blocks
100         Checkpoints []Checkpoint
101
102         // ProducerSubsidys defines the producer subsidy by block height
103         ProducerSubsidys []ProducerSubsidy
104
105         SoftForkPoint map[uint64]uint64
106 }
107
108 // ActiveNetParams is the active NetParams
109 var ActiveNetParams = MainNetParams
110
111 // NetParams is the correspondence between chain_id and Params
112 var NetParams = map[string]Params{
113         "mainnet": MainNetParams,
114         "testnet": TestNetParams,
115         "solonet": SoloNetParams,
116 }
117
118 // MainNetParams is the config for vapor-mainnet
119 var MainNetParams = Params{
120         Name:            "main",
121         Bech32HRPSegwit: "vp",
122         DefaultPort:     "56656",
123         DNSSeeds:        []string{"www.mainnetseed.vapor.io"},
124         BasicConfig: BasicConfig{
125                 MaxBlockGas:                uint64(10000000),
126                 MaxGasAmount:               int64(640000),
127                 DefaultGasCredit:           int64(160000),
128                 StorageGasRate:             int64(1),
129                 VMGasRate:                  int64(200),
130                 VotePendingBlockNumber:     uint64(3456000),
131                 CoinbasePendingBlockNumber: uint64(7200),
132                 CoinbaseArbitrarySizeLimit: 128,
133         },
134         DPOSConfig: DPOSConfig{
135                 NumOfConsensusNode:      10,
136                 BlockNumEachNode:        12,
137                 MinConsensusNodeVoteNum: uint64(100000000000000),
138                 MinVoteOutputAmount:     uint64(100000000),
139                 BlockTimeInterval:       500,
140                 RoundVoteBlockNums:      1200,
141                 MaxTimeOffsetMs:         2000,
142         },
143         Checkpoints: []Checkpoint{},
144         ProducerSubsidys: []ProducerSubsidy{
145                 {BeginBlock: 1, EndBlock: 63072000, Subsidy: 9512938},
146         },
147         SoftForkPoint: map[uint64]uint64{SoftFork001: 10461600},
148 }
149
150 // TestNetParams is the config for vapor-testnet
151 var TestNetParams = Params{
152         Name:            "test",
153         Bech32HRPSegwit: "tp",
154         DefaultPort:     "56657",
155         DNSSeeds:        []string{"www.testnetseed.vapor.io"},
156         BasicConfig: BasicConfig{
157                 MaxBlockGas:                uint64(10000000),
158                 MaxGasAmount:               int64(200000),
159                 DefaultGasCredit:           int64(160000),
160                 StorageGasRate:             int64(1),
161                 VMGasRate:                  int64(200),
162                 VotePendingBlockNumber:     uint64(10000),
163                 CoinbasePendingBlockNumber: uint64(1200),
164                 CoinbaseArbitrarySizeLimit: 128,
165         },
166         DPOSConfig: DPOSConfig{
167                 NumOfConsensusNode:      10,
168                 BlockNumEachNode:        12,
169                 MinConsensusNodeVoteNum: uint64(100000000000000),
170                 MinVoteOutputAmount:     uint64(100000000),
171                 BlockTimeInterval:       500,
172                 RoundVoteBlockNums:      1200,
173                 MaxTimeOffsetMs:         2000,
174         },
175         Checkpoints: []Checkpoint{},
176         ProducerSubsidys: []ProducerSubsidy{
177                 {BeginBlock: 1, EndBlock: 63072000, Subsidy: 15000000},
178         },
179 }
180
181 // SoloNetParams is the config for vapor solonet
182 var SoloNetParams = Params{
183         Name:            "solo",
184         Bech32HRPSegwit: "sp",
185         DefaultPort:     "56658",
186         BasicConfig: BasicConfig{
187                 MaxBlockGas:                uint64(10000000),
188                 MaxGasAmount:               int64(200000),
189                 DefaultGasCredit:           int64(160000),
190                 StorageGasRate:             int64(1),
191                 VMGasRate:                  int64(200),
192                 VotePendingBlockNumber:     uint64(10000),
193                 CoinbasePendingBlockNumber: uint64(1200),
194                 CoinbaseArbitrarySizeLimit: 128,
195         },
196         DPOSConfig: DPOSConfig{
197                 NumOfConsensusNode:      10,
198                 BlockNumEachNode:        12,
199                 MinConsensusNodeVoteNum: uint64(100000000000000),
200                 MinVoteOutputAmount:     uint64(100000000),
201                 BlockTimeInterval:       500,
202                 RoundVoteBlockNums:      1200,
203                 MaxTimeOffsetMs:         2000,
204         },
205         Checkpoints: []Checkpoint{},
206         ProducerSubsidys: []ProducerSubsidy{
207                 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
208                 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
209                 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
210                 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
211         },
212 }
213
214 // BlockSubsidy calculate the coinbase rewards on given block height
215 func BlockSubsidy(height uint64) uint64 {
216         for _, subsidy := range ActiveNetParams.ProducerSubsidys {
217                 if height >= subsidy.BeginBlock && height <= subsidy.EndBlock {
218                         return subsidy.Subsidy
219                 }
220         }
221         return 0
222 }
223
224 // BytomMainNetParams is the config for bytom mainnet
225 func BytomMainNetParams(vaporParam *Params) *Params {
226         bech32HRPSegwit := "sm"
227         switch vaporParam.Name {
228         case "main":
229                 bech32HRPSegwit = "bm"
230         case "test":
231                 bech32HRPSegwit = "tm"
232         }
233         return &Params{Bech32HRPSegwit: bech32HRPSegwit}
234 }
235
236 func InitActiveNetParams(chainID string) error {
237         var exist bool
238         if ActiveNetParams, exist = NetParams[chainID]; !exist {
239                 return fmt.Errorf("chain_id[%v] don't exist", chainID)
240         }
241         return nil
242 }