forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
119 lines (96 loc) · 2.73 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"errors"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/contract/system"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/types"
)
const pubNetMaxBlockBodySize = 4000000
var (
CoinbaseAccount []byte
MaxAnchorCount int
VerifierCount int
// maxBlockBodySize is the upper limit of block size.
maxBlockBodySize uint32
maxBlockSize uint32
pubNet bool
consensusName string
)
var (
// ErrInvalidCoinbaseAccount is returned by Init when the coinbase account
// address is invalid.
ErrInvalidCoinbaseAccount = errors.New("invalid coinbase account in config")
ErrInvalidConsensus = errors.New("invalid consensus name from genesis")
)
// Init initializes the blockchain-related parameters.
func Init(maxBlkBodySize uint32, coinbaseAccountStr string, isBp bool, maxAnchorCount int, verifierCount int) error {
var err error
setBlockSizeLimit(maxBlkBodySize)
if isBp {
if len(coinbaseAccountStr) != 0 {
CoinbaseAccount, err = types.DecodeAddress(coinbaseAccountStr)
if err != nil {
return ErrInvalidCoinbaseAccount
}
logger.Info().Str("account", enc.ToString(CoinbaseAccount)).Str("str", coinbaseAccountStr).
Msg("set coinbase account")
} else {
logger.Info().Msg("Coinbase Account is nil, so BP reward will be discarded")
}
}
MaxAnchorCount = maxAnchorCount
VerifierCount = verifierCount
return nil
}
// IsPublic reports whether the block chain is public or not.
func IsPublic() bool {
return pubNet
}
func initChainParams(genesis *types.Genesis) {
pubNet = genesis.ID.PublicNet
if pubNet {
setBlockSizeLimit(pubNetMaxBlockBodySize)
}
if err := setConsensusName(genesis.ConsensusType()); err != nil {
logger.Panic().Err(err).Msg("invalid consensus type in genesis block")
}
system.InitDefaultBpCount(len(genesis.BPs))
if genesis.TotalBalance() != nil {
types.MaxAER = genesis.TotalBalance()
logger.Info().Str("TotalBalance", types.MaxAER.String()).Msg("set total from genesis")
}
}
// MaxBlockBodySize returns the max block body size.
func MaxBlockBodySize() uint32 {
return maxBlockBodySize
}
// MaxBlockSize returns the max block size.
func MaxBlockSize() uint32 {
return maxBlockSize
}
func setMaxBlockBodySize(size uint32) {
maxBlockBodySize = size
}
func setBlockSizeLimit(maxBlockBodySize uint32) {
setMaxBlockBodySize(maxBlockBodySize)
maxBlockSize = MaxBlockBodySize() + types.DefaultMaxHdrSize
}
func setConsensusName(val string) error {
for _, name := range consensus.ConsensusName {
if val == name {
consensusName = val
}
}
if consensusName == "" {
return ErrInvalidConsensus
}
return nil
}
func ConsensusName() string {
return consensusName
}