OSDN Git Service

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