2017-09-12 12:20:19 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-09-18 14:00:09 -07:00
|
|
|
const (
|
2017-10-15 14:29:31 -07:00
|
|
|
_100MiB = 104857600
|
2017-09-18 14:00:09 -07:00
|
|
|
)
|
|
|
|
|
2017-09-12 12:20:19 -07:00
|
|
|
// ConsensusParams contains consensus critical parameters
|
|
|
|
// that determine the validity of blocks.
|
|
|
|
type ConsensusParams struct {
|
2017-09-21 11:34:36 -07:00
|
|
|
BlockSizeParams `json:"block_size_params"`
|
|
|
|
TxSizeParams `json:"tx_size_params"`
|
|
|
|
BlockGossipParams `json:"block_gossip_params"`
|
2017-09-15 21:16:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockSizeParams contain limits on the block size.
|
|
|
|
type BlockSizeParams struct {
|
|
|
|
MaxBytes int `json:"max_bytes"` // NOTE: must not be 0
|
|
|
|
MaxTxs int `json:"max_txs"`
|
|
|
|
MaxGas int `json:"max_gas"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxSizeParams contain limits on the tx size.
|
|
|
|
type TxSizeParams struct {
|
|
|
|
MaxBytes int `json:"max_bytes"`
|
|
|
|
MaxGas int `json:"max_gas"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockGossipParams determine consensus critical elements of how blocks are gossiped
|
|
|
|
type BlockGossipParams struct {
|
|
|
|
BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
|
2017-09-12 12:20:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConsensusParams returns a default ConsensusParams.
|
2017-09-21 12:22:58 -07:00
|
|
|
func DefaultConsensusParams() *ConsensusParams {
|
|
|
|
return &ConsensusParams{
|
2017-09-15 21:16:49 -07:00
|
|
|
DefaultBlockSizeParams(),
|
|
|
|
DefaultTxSizeParams(),
|
|
|
|
DefaultBlockGossipParams(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultBlockSizeParams returns a default BlockSizeParams.
|
2017-09-21 11:34:36 -07:00
|
|
|
func DefaultBlockSizeParams() BlockSizeParams {
|
|
|
|
return BlockSizeParams{
|
2017-09-15 21:16:49 -07:00
|
|
|
MaxBytes: 22020096, // 21MB
|
|
|
|
MaxTxs: 100000,
|
|
|
|
MaxGas: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultTxSizeParams returns a default TxSizeParams.
|
2017-09-21 11:34:36 -07:00
|
|
|
func DefaultTxSizeParams() TxSizeParams {
|
|
|
|
return TxSizeParams{
|
2017-09-15 21:16:49 -07:00
|
|
|
MaxBytes: 10240, // 10kB
|
|
|
|
MaxGas: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultBlockGossipParams returns a default BlockGossipParams.
|
2017-09-21 11:34:36 -07:00
|
|
|
func DefaultBlockGossipParams() BlockGossipParams {
|
|
|
|
return BlockGossipParams{
|
2017-09-15 21:16:49 -07:00
|
|
|
BlockPartSizeBytes: 65536, // 64kB,
|
2017-09-12 12:20:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the ConsensusParams to ensure all values
|
|
|
|
// are within their allowed limits, and returns an error if they are not.
|
2017-10-15 14:29:31 -07:00
|
|
|
// Expecting:
|
|
|
|
// * BlockSizeParams.MaxBytes > 0
|
|
|
|
// * BlockGossipParams.BlockPartSizeBytes > 0
|
|
|
|
// * BlockSizeParams.MaxBytes <= 100MiB
|
2017-09-12 12:20:19 -07:00
|
|
|
func (params *ConsensusParams) Validate() error {
|
2017-09-18 14:00:09 -07:00
|
|
|
// ensure some values are greater than 0
|
2017-09-15 21:16:49 -07:00
|
|
|
if params.BlockSizeParams.MaxBytes <= 0 {
|
|
|
|
return errors.Errorf("BlockSizeParams.MaxBytes must be greater than 0. Got %d", params.BlockSizeParams.MaxBytes)
|
2017-09-12 12:20:19 -07:00
|
|
|
}
|
2017-09-15 21:16:49 -07:00
|
|
|
if params.BlockGossipParams.BlockPartSizeBytes <= 0 {
|
|
|
|
return errors.Errorf("BlockGossipParams.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossipParams.BlockPartSizeBytes)
|
2017-09-12 12:20:19 -07:00
|
|
|
}
|
2017-09-18 14:00:09 -07:00
|
|
|
|
|
|
|
// ensure blocks aren't too big
|
2017-10-15 14:29:31 -07:00
|
|
|
if params.BlockSizeParams.MaxBytes > _100MiB {
|
2017-09-18 14:00:09 -07:00
|
|
|
return errors.Errorf("BlockSizeParams.MaxBytes is too big. %d > %d",
|
2017-10-15 14:29:31 -07:00
|
|
|
params.BlockSizeParams.MaxBytes, _100MiB)
|
2017-09-18 14:00:09 -07:00
|
|
|
}
|
2017-09-12 12:20:19 -07:00
|
|
|
return nil
|
|
|
|
}
|