github.com/aergoio/aergo@v1.3.1/chain/common.go (about)

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