2017-09-12 15:20:19 -04:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2017-12-14 10:13:31 +01:00
|
|
|
|
|
|
|
"github.com/tendermint/tmlibs/merkle"
|
2017-09-12 15:20:19 -04:00
|
|
|
)
|
|
|
|
|
2017-09-18 17:00:09 -04:00
|
|
|
const (
|
2017-10-20 00:08:39 -06:00
|
|
|
maxBlockSizeBytes = 104857600 // 100MB
|
2017-09-18 17:00:09 -04:00
|
|
|
)
|
|
|
|
|
2017-09-12 15:20:19 -04:00
|
|
|
// ConsensusParams contains consensus critical parameters
|
|
|
|
// that determine the validity of blocks.
|
|
|
|
type ConsensusParams struct {
|
2017-12-19 12:43:15 -06:00
|
|
|
BlockSize `json:"block_size_params"`
|
|
|
|
TxSize `json:"tx_size_params"`
|
|
|
|
BlockGossip `json:"block_gossip_params"`
|
2017-09-16 00:16:49 -04:00
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// BlockSize contain limits on the block size.
|
|
|
|
type BlockSize struct {
|
2017-10-20 00:08:39 -06:00
|
|
|
MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB
|
2017-09-16 00:16:49 -04:00
|
|
|
MaxTxs int `json:"max_txs"`
|
|
|
|
MaxGas int `json:"max_gas"`
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// TxSize contain limits on the tx size.
|
|
|
|
type TxSize struct {
|
2017-09-16 00:16:49 -04:00
|
|
|
MaxBytes int `json:"max_bytes"`
|
|
|
|
MaxGas int `json:"max_gas"`
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// BlockGossip determine consensus critical elements of how blocks are gossiped
|
|
|
|
type BlockGossip struct {
|
2017-09-16 00:16:49 -04:00
|
|
|
BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
|
2017-09-12 15:20:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConsensusParams returns a default ConsensusParams.
|
2017-09-21 15:22:58 -04:00
|
|
|
func DefaultConsensusParams() *ConsensusParams {
|
|
|
|
return &ConsensusParams{
|
2017-12-19 12:43:15 -06:00
|
|
|
DefaultBlockSize(),
|
|
|
|
DefaultTxSize(),
|
|
|
|
DefaultBlockGossip(),
|
2017-09-16 00:16:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// DefaultBlockSize returns a default BlockSize.
|
|
|
|
func DefaultBlockSize() BlockSize {
|
|
|
|
return BlockSize{
|
2017-09-16 00:16:49 -04:00
|
|
|
MaxBytes: 22020096, // 21MB
|
|
|
|
MaxTxs: 100000,
|
|
|
|
MaxGas: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// DefaultTxSize returns a default TxSize.
|
|
|
|
func DefaultTxSize() TxSize {
|
|
|
|
return TxSize{
|
2017-09-16 00:16:49 -04:00
|
|
|
MaxBytes: 10240, // 10kB
|
|
|
|
MaxGas: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:43:15 -06:00
|
|
|
// DefaultBlockGossip returns a default BlockGossip.
|
|
|
|
func DefaultBlockGossip() BlockGossip {
|
|
|
|
return BlockGossip{
|
2017-09-16 00:16:49 -04:00
|
|
|
BlockPartSizeBytes: 65536, // 64kB,
|
2017-09-12 15:20:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the ConsensusParams to ensure all values
|
|
|
|
// are within their allowed limits, and returns an error if they are not.
|
|
|
|
func (params *ConsensusParams) Validate() error {
|
2017-09-18 17:00:09 -04:00
|
|
|
// ensure some values are greater than 0
|
2017-12-19 12:43:15 -06:00
|
|
|
if params.BlockSize.MaxBytes <= 0 {
|
|
|
|
return errors.Errorf("BlockSize.MaxBytes must be greater than 0. Got %d", params.BlockSize.MaxBytes)
|
2017-09-12 15:20:19 -04:00
|
|
|
}
|
2017-12-19 12:43:15 -06:00
|
|
|
if params.BlockGossip.BlockPartSizeBytes <= 0 {
|
|
|
|
return errors.Errorf("BlockGossip.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossip.BlockPartSizeBytes)
|
2017-09-12 15:20:19 -04:00
|
|
|
}
|
2017-09-18 17:00:09 -04:00
|
|
|
|
|
|
|
// ensure blocks aren't too big
|
2017-12-19 12:43:15 -06:00
|
|
|
if params.BlockSize.MaxBytes > maxBlockSizeBytes {
|
|
|
|
return errors.Errorf("BlockSize.MaxBytes is too big. %d > %d",
|
|
|
|
params.BlockSize.MaxBytes, maxBlockSizeBytes)
|
2017-09-18 17:00:09 -04:00
|
|
|
}
|
2017-09-12 15:20:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-14 10:13:31 +01:00
|
|
|
|
|
|
|
// Hash returns a merkle hash of the parameters to store
|
|
|
|
// in the block header
|
|
|
|
func (params *ConsensusParams) Hash() []byte {
|
|
|
|
return merkle.SimpleHashFromMap(map[string]interface{}{
|
2017-12-19 12:43:15 -06:00
|
|
|
"block_gossip_part_size_bytes": params.BlockGossip.BlockPartSizeBytes,
|
|
|
|
"block_size_max_bytes": params.BlockSize.MaxBytes,
|
|
|
|
"block_size_max_gas": params.BlockSize.MaxGas,
|
|
|
|
"block_size_max_txs": params.BlockSize.MaxTxs,
|
|
|
|
"tx_size_max_bytes": params.TxSize.MaxBytes,
|
|
|
|
"tx_size_max_gas": params.TxSize.MaxGas,
|
2017-12-14 10:13:31 +01:00
|
|
|
})
|
|
|
|
}
|