OSDN Git Service

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