OSDN Git Service

dynamic_vote_pending_num (#2098)
[bytom/bytom.git] / consensus / general.go
1 package consensus
2
3 import (
4         "encoding/binary"
5         "fmt"
6         "math"
7         "strings"
8
9         log "github.com/sirupsen/logrus"
10
11         "github.com/bytom/bytom/crypto/ed25519/chainkd"
12         "github.com/bytom/bytom/protocol/bc"
13 )
14
15 // consensus variables
16 const (
17         // Max gas that one block contains
18         MaxBlockGas    = uint64(10000000)
19         VMGasRate      = int64(200)
20         StorageGasRate = int64(1)
21         MaxGasAmount   = int64(300000)
22
23         // These configs need add to casper config in elegant way
24         MaxNumOfValidators = int(10)
25         InitBTMSupply      = 169290721678579170 + 50000000000
26         RewardThreshold    = 0.5
27         BlockReward        = uint64(570776255)
28
29         // config parameter for coinbase reward
30         CoinbasePendingBlockNumber = uint64(10)
31         MinVoteOutputAmount        = uint64(100000000)
32
33         PayToWitnessPubKeyHashDataSize = 20
34         PayToWitnessScriptHashDataSize = 32
35         BCRPContractHashDataSize       = 32
36         CoinbaseArbitrarySizeLimit     = 128
37
38         BCRPRequiredBTMAmount = uint64(100000000)
39
40         BTMAlias = "BTM"
41         defaultVotePendingNum = 302400
42 )
43
44 type CasperConfig struct {
45         // BlockTimeInterval, milliseconds, the block time interval for producing a block
46         BlockTimeInterval uint64
47
48         // MaxTimeOffsetMs represent the max number of seconds a block time is allowed to be ahead of the current time
49         MaxTimeOffsetMs uint64
50
51         // BlocksOfEpoch represent the block num in one epoch
52         BlocksOfEpoch uint64
53
54         // MinValidatorVoteNum is the minimum vote number of become validator
55         MinValidatorVoteNum uint64
56
57         // VotePendingBlockNumber is the locked block number of vote utxo
58         VotePendingBlockNums []VotePendingBlockNum
59
60         FederationXpubs []chainkd.XPub
61 }
62
63 type VotePendingBlockNum struct {
64         BeginBlock uint64
65         EndBlock   uint64
66         Num        uint64
67 }
68
69 // BTMAssetID is BTM's asset id, the soul asset of Bytom
70 var BTMAssetID = &bc.AssetID{
71         V0: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
72         V1: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
73         V2: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
74         V3: binary.BigEndian.Uint64([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
75 }
76
77 // BTMDefinitionMap is the ....
78 var BTMDefinitionMap = map[string]interface{}{
79         "name":        BTMAlias,
80         "symbol":      BTMAlias,
81         "decimals":    8,
82         "description": `Bytom Official Issue`,
83 }
84
85 // IsBech32SegwitPrefix returns whether the prefix is a known prefix for segwit
86 // addresses on any default or registered network.  This is used when decoding
87 // an address string into a specific address type.
88 func IsBech32SegwitPrefix(prefix string, params *Params) bool {
89         prefix = strings.ToLower(prefix)
90         return prefix == params.Bech32HRPSegwit+"1"
91 }
92
93 // Params store the config for different network
94 type Params struct {
95         // Name defines a human-readable identifier for the network.
96         Name            string
97         Bech32HRPSegwit string
98         // DefaultPort defines the default peer-to-peer port for the network.
99         DefaultPort string
100
101         // DNSSeeds defines a list of DNS seeds for the network that are used
102         // as one method to discover peers.
103         DNSSeeds []string
104
105         // CasperConfig defines the casper consensus parameters
106         CasperConfig
107 }
108
109 // ActiveNetParams is ...
110 var ActiveNetParams = MainNetParams
111
112 // NetParams is the correspondence between chain_id and Params
113 var NetParams = map[string]Params{
114         "mainnet": MainNetParams,
115         "wisdom":  TestNetParams,
116         "solonet": SoloNetParams,
117 }
118
119 // MainNetParams is the config for production
120 var MainNetParams = Params{
121         Name:            "main",
122         Bech32HRPSegwit: "bn",
123         DefaultPort:     "46657",
124         DNSSeeds:        []string{},
125         CasperConfig: CasperConfig{
126                 BlockTimeInterval:      6000,
127                 MaxTimeOffsetMs:        3000,
128                 BlocksOfEpoch:          100,
129                 MinValidatorVoteNum:    1e14,
130                 VotePendingBlockNums:   []VotePendingBlockNum{
131                         {BeginBlock: 0, EndBlock: 432000, Num: 14400},
132                         {BeginBlock: 432000, EndBlock: math.MaxUint64, Num: defaultVotePendingNum},
133                 },
134                 FederationXpubs: []chainkd.XPub{
135                         xpub("f9003633ccbd8cc37e034f4dbe70d9fae980d437948d8cb908d0cab7909780d74a324b4decb5dfcd43fbc6b896ac066b7e02c733a1537360e933278a101a850c"),
136                         xpub("d301fee5d4ba7eb5b9d41ca13ec56c19daceb5f6b752d91d49777fd1fc7c45891e5773cafb3b6d6ab764ef2794e8ba953c8bdb9dc77a3af51e979f96885f96b2"),
137                         xpub("2ba14bdd29fd84c73f67d6025d2a98292dbdd46b90a2af29c8669dd88dacb1cec62a3e9448d8b731a448f0454b0aa367748659d6c01ad7125d395ffda972da54"),
138                         xpub("1313379b05c38ff2d171d512f23f199f0f068a67d77b9d5b6db040f2da1edc0c35c68a21b068956f448fed6441b9c27294f1ca6aaedc2c580de322f3f0260c1f"),
139                 },
140         },
141 }
142
143 // TestNetParams is the config for test-net
144 var TestNetParams = Params{
145         Name:            "test",
146         Bech32HRPSegwit: "tn",
147         DefaultPort:     "46656",
148         DNSSeeds:        []string{},
149         CasperConfig: CasperConfig{
150                 BlockTimeInterval:      6000,
151                 MaxTimeOffsetMs:        3000,
152                 BlocksOfEpoch:          100,
153                 MinValidatorVoteNum:    1e8,
154                 VotePendingBlockNums:   []VotePendingBlockNum{{BeginBlock: 0, EndBlock: math.MaxUint64, Num: 10}},
155                 FederationXpubs: []chainkd.XPub{
156                         xpub("7732fac62320799ff5e4eec1dc4ba7b07dc0e5a647850bf0bc34cb9aca195a05a1118b57d377947d7936156c831c87b700ed945a82cae63aff14905beb39d001"),
157                         xpub("08543fef8c3ca27483954f80eee6d461c307b6aa564aafaf235a4bd2740debbc71b14af78715c94cbc1d16fa84da97a3eabc5b21f003ab49882e4af7f9f00bbd"),
158                         xpub("0dd00fe3880c1cb5d5b0b5d03993c004e7fbe3697a47ff60c3bc12950bead964843dfe45b2bab5d01ae32fb23a4b0460049e822d7787a9a15b76d8bb9dfcec74"),
159                         xpub("b0584ecaefc02d3c367f280e128ec310c9f9198d44cd76b6726cd6c06c002770a1a7dc069ddd06f7a821a176931573d40e63b015ce88b6de01a61205d719567f"),
160                 },
161         },
162 }
163
164 // SoloNetParams is the config for test-net
165 var SoloNetParams = Params{
166         Name:            "solo",
167         Bech32HRPSegwit: "sn",
168         CasperConfig: CasperConfig{
169                 BlockTimeInterval:      6000,
170                 MaxTimeOffsetMs:        24000,
171                 BlocksOfEpoch:          100,
172                 MinValidatorVoteNum:    1e8,
173                 VotePendingBlockNums:   []VotePendingBlockNum{{BeginBlock: 0, EndBlock: math.MaxUint64, Num: 10}},
174                 FederationXpubs:        []chainkd.XPub{},
175         },
176 }
177
178 func VotePendingBlockNums(height uint64) uint64 {
179         for _, pendingNum := range ActiveNetParams.VotePendingBlockNums {
180                 if height >= pendingNum.BeginBlock && height < pendingNum.EndBlock {
181                         return pendingNum.Num
182                 }
183         }
184         return defaultVotePendingNum
185 }
186
187 // InitActiveNetParams load the config by chain ID
188 func InitActiveNetParams(chainID string) error {
189         var exist bool
190         if ActiveNetParams, exist = NetParams[chainID]; !exist {
191                 return fmt.Errorf("chain_id[%v] don't exist", chainID)
192         }
193         return nil
194 }
195
196 func xpub(str string) (xpub chainkd.XPub) {
197         if err := xpub.UnmarshalText([]byte(str)); err != nil {
198                 log.Panicf("Fail converts a string to xpub")
199         }
200         return xpub
201 }