Skip to content

Commit

Permalink
Merge branch 'master' of github.com:consensys/quorum into feat/gas-li…
Browse files Browse the repository at this point in the history
…mit-config
  • Loading branch information
antonydenyer committed Jul 19, 2022
2 parents db85b90 + 616f665 commit 46da6fc
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 53 deletions.
54 changes: 33 additions & 21 deletions consensus/istanbul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,46 +162,58 @@ func (c *Config) IsQBFTConsensusAt(blockNumber *big.Int) bool {
return true
}
}
for i := 0; c.Transitions != nil && i < len(c.Transitions) && c.Transitions[i].Block.Cmp(blockNumber) <= 0; i++ {
if c.Transitions[i].Algorithm == params.QBFT {
return true

result := false
c.getTransitionValue(blockNumber, func(transition params.Transition) {
if transition.Algorithm == params.QBFT {
result = true
}
}
return false
})

return result
}

func (c Config) GetConfig(blockNumber *big.Int) Config {
newConfig := c
for i := 0; c.Transitions != nil && i < len(c.Transitions) && c.Transitions[i].Block.Cmp(blockNumber) <= 0; i++ {
if c.Transitions[i].RequestTimeoutSeconds != 0 {
newConfig.RequestTimeout = c.Transitions[i].RequestTimeoutSeconds

c.getTransitionValue(blockNumber, func(transition params.Transition) {
if transition.RequestTimeoutSeconds != 0 {
newConfig.RequestTimeout = transition.RequestTimeoutSeconds
}
if c.Transitions[i].EpochLength != 0 {
newConfig.Epoch = c.Transitions[i].EpochLength
if transition.EpochLength != 0 {
newConfig.Epoch = transition.EpochLength
}
if c.Transitions[i].BlockPeriodSeconds != 0 {
newConfig.BlockPeriod = c.Transitions[i].BlockPeriodSeconds
if transition.BlockPeriodSeconds != 0 {
newConfig.BlockPeriod = transition.BlockPeriodSeconds
}
}
})
return newConfig
}

func (c Config) GetValidatorContractAddress(blockNumber *big.Int) common.Address {
validatorContractAddress := c.ValidatorContract
for i := 0; c.Transitions != nil && i < len(c.Transitions) && c.Transitions[i].Block.Cmp(blockNumber) <= 0; i++ {
if c.Transitions[i].ValidatorContractAddress != (common.Address{}) {
validatorContractAddress = c.Transitions[i].ValidatorContractAddress
c.getTransitionValue(blockNumber, func(transition params.Transition) {
if (transition.ValidatorContractAddress != common.Address{}) {
validatorContractAddress = transition.ValidatorContractAddress
}
}
})
return validatorContractAddress
}

func (c Config) GetValidatorSelectionMode(blockNumber *big.Int) string {
mode := params.BlockHeaderMode
for i := 0; c.Transitions != nil && i < len(c.Transitions) && c.Transitions[i].Block.Cmp(blockNumber) <= 0; i++ {
if c.Transitions[i].ValidatorSelectionMode != "" {
mode = c.Transitions[i].ValidatorSelectionMode
c.getTransitionValue(blockNumber, func(transition params.Transition) {
if transition.ValidatorSelectionMode != "" {
mode = transition.ValidatorSelectionMode
}
}
})
return mode
}

func (c *Config) getTransitionValue(num *big.Int, callback func(transition params.Transition)) {
if c != nil && num != nil && c.Transitions != nil {
for i := 0; i < len(c.Transitions) && c.Transitions[i].Block.Cmp(num) <= 0; i++ {
callback(c.Transitions[i])
}
}
}
2 changes: 1 addition & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func (b *EthAPIBackend) AccountExtraDataStateGetterByNumber(ctx context.Context,
}

func (b *EthAPIBackend) IsPrivacyMarkerTransactionCreationEnabled() bool {
return b.eth.config.QuorumChainConfig.PrivacyMarkerEnabled() && b.ChainConfig().IsPrivacyPrecompile(b.eth.blockchain.CurrentBlock().Number())
return b.eth.config.QuorumChainConfig.PrivacyMarkerEnabled() && b.ChainConfig().IsPrivacyPrecompileEnabled(b.eth.blockchain.CurrentBlock().Number())
}

// used by Quorum
Expand Down
73 changes: 47 additions & 26 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,18 @@ const (
)

type Transition struct {
Block *big.Int `json:"block"`
Algorithm string `json:"algorithm,omitempty"`
EpochLength uint64 `json:"epochlength,omitempty"` // Number of blocks that should pass before pending validator votes are reset
BlockPeriodSeconds uint64 `json:"blockperiodseconds,omitempty"` // Minimum time between two consecutive IBFT or QBFT blocks’ timestamps in seconds
RequestTimeoutSeconds uint64 `json:"requesttimeoutseconds,omitempty"` // Minimum request timeout for each IBFT or QBFT round in milliseconds
ContractSizeLimit uint64 `json:"contractsizelimit,omitempty"` // Maximum smart contract code size
ValidatorContractAddress common.Address `json:"validatorcontractaddress"` // Smart contract address for list of validators
ValidatorSelectionMode string `json:"validatorselectionmode"` // Validator selection mode to switch to
MinerGasLimit uint64 `json:"miner.gaslimit"` // Gas Limit
Block *big.Int `json:"block"`
Algorithm string `json:"algorithm,omitempty"`
EpochLength uint64 `json:"epochlength,omitempty"` // Number of blocks that should pass before pending validator votes are reset
BlockPeriodSeconds uint64 `json:"blockperiodseconds,omitempty"` // Minimum time between two consecutive IBFT or QBFT blocks’ timestamps in seconds
RequestTimeoutSeconds uint64 `json:"requesttimeoutseconds,omitempty"` // Minimum request timeout for each IBFT or QBFT round in milliseconds
ContractSizeLimit uint64 `json:"contractsizelimit,omitempty"` // Maximum smart contract code size
ValidatorContractAddress common.Address `json:"validatorcontractaddress"` // Smart contract address for list of validators
ValidatorSelectionMode string `json:"validatorselectionmode"` // Validator selection mode to switch to
EnhancedPermissioningEnabled bool `json:"enhancedPermissioningEnabled,omitempty"` // aka QIP714Block
PrivacyEnhancementsEnabled bool `json:"privacyEnhancementsEnabled,omitempty"` // privacy enhancements (mandatory party, private state validation)
PrivacyPrecompileEnalbed bool `json:"privacyPrecompileEnabled,omitempty"` // enable marker transactions support
MinerGasLimit uint64 `json:"miner.gaslimit"` // Gas Limit
}

// String implements the fmt.Stringer interface.
Expand Down Expand Up @@ -562,13 +565,11 @@ func (c *ChainConfig) IsEWASM(num *big.Int) bool {
//
// IsQIP714 returns whether num represents a block number where permissions is enabled
func (c *ChainConfig) IsQIP714(num *big.Int) bool {
return isForked(c.QIP714Block, num)
}

// IsMaxCodeSizeChangeBlock returns whether num represents a block number
// where maxCodeSize change was done
func (c *ChainConfig) IsMaxCodeSizeChangeBlock(num *big.Int) bool {
return isForked(c.MaxCodeSizeChangeBlock, num)
enableEnhancedPermissioning := false
c.getTransitionValue(num, func(transition Transition) {
enableEnhancedPermissioning = transition.EnhancedPermissioningEnabled
})
return isForked(c.QIP714Block, num) || enableEnhancedPermissioning
}

// Quorum
Expand All @@ -587,21 +588,31 @@ func (c *ChainConfig) GetMaxCodeSize(num *big.Int) int {
}
} else if c.MaxCodeSize > 0 {
if c.MaxCodeSizeChangeBlock != nil && c.MaxCodeSizeChangeBlock.Cmp(big.NewInt(0)) >= 0 {
if c.IsMaxCodeSizeChangeBlock(num) {
if isForked(c.MaxCodeSizeChangeBlock, num) {
maxCodeSize = int(c.MaxCodeSize) * 1024
}
} else {
maxCodeSize = int(c.MaxCodeSize) * 1024
}
}
if len(c.Transitions) > 0 {

c.getTransitionValue(num, func(transition Transition) {
if transition.ContractSizeLimit != 0 {
maxCodeSize = int(transition.ContractSizeLimit) * 1024
}
})

return maxCodeSize
}

// Quorum
// gets value at or after a transition
func (c *ChainConfig) getTransitionValue(num *big.Int, callback func(transition Transition)) {
if c != nil && num != nil && c.Transitions != nil {
for i := 0; i < len(c.Transitions) && c.Transitions[i].Block.Cmp(num) <= 0; i++ {
if c.Transitions[i].ContractSizeLimit != 0 {
maxCodeSize = int(c.Transitions[i].ContractSizeLimit) * 1024
}
callback(c.Transitions[i])
}
}
return maxCodeSize
}

// Quorum
Expand Down Expand Up @@ -823,14 +834,24 @@ func isTransitionsConfigCompatible(c1, c2 *ChainConfig, head *big.Int) (error, *
//
// IsPrivacyEnhancementsEnabled returns whether num represents a block number after the PrivacyEnhancementsEnabled fork
func (c *ChainConfig) IsPrivacyEnhancementsEnabled(num *big.Int) bool {
return isForked(c.PrivacyEnhancementsBlock, num)
isPrivacyEnhancementsEnabled := false
c.getTransitionValue(num, func(transition Transition) {
isPrivacyEnhancementsEnabled = transition.PrivacyEnhancementsEnabled
})

return isForked(c.PrivacyEnhancementsBlock, num) || isPrivacyEnhancementsEnabled
}

// Quorum
//
// Check whether num represents a block number after the PrivacyPrecompileBlock
func (c *ChainConfig) IsPrivacyPrecompile(num *big.Int) bool {
return isForked(c.PrivacyPrecompileBlock, num)
func (c *ChainConfig) IsPrivacyPrecompileEnabled(num *big.Int) bool {
isPrivacyPrecompileEnabled := false
c.getTransitionValue(num, func(transition Transition) {
isPrivacyPrecompileEnabled = transition.PrivacyPrecompileEnalbed
})

return isForked(c.PrivacyPrecompileBlock, num) || isPrivacyPrecompileEnabled
}

// CheckCompatible checks whether scheduled fork transitions have been imported
Expand Down Expand Up @@ -1073,6 +1094,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
IsBerlin: c.IsBerlin(num),
// Quorum
IsPrivacyEnhancementsEnabled: c.IsPrivacyEnhancementsEnabled(num),
IsPrivacyPrecompile: c.IsPrivacyPrecompile(num),
IsPrivacyPrecompile: c.IsPrivacyPrecompileEnabled(num),
}
}
106 changes: 101 additions & 5 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ func TestCheckTransitionsData(t *testing.T) {
wantErr error
}
var ibftTransitionsConfig, qbftTransitionsConfig, invalidTransition, invalidBlockOrder []Transition
tranI0 := Transition{big.NewInt(0), IBFT, 30000, 5, 10, 50, common.Address{}, "", 0}
tranQ5 := Transition{big.NewInt(5), QBFT, 30000, 5, 10, 50, common.Address{}, "", 1}
tranI10 := Transition{big.NewInt(10), IBFT, 30000, 5, 10, 50, common.Address{}, "", 0}
tranQ8 := Transition{big.NewInt(8), QBFT, 30000, 5, 10, 50, common.Address{}, "", 2}
tranI0 := Transition{big.NewInt(0), IBFT, 30000, 5, 10, 50, common.Address{}, "", false, false, false, 0}
tranQ5 := Transition{big.NewInt(5), QBFT, 30000, 5, 10, 50, common.Address{}, "", false, false, false, 1}
tranI10 := Transition{big.NewInt(10), IBFT, 30000, 5, 10, 50, common.Address{}, "", false, false, false, 0}
tranQ8 := Transition{big.NewInt(8), QBFT, 30000, 5, 10, 50, common.Address{}, "", false, false, false, 2}

ibftTransitionsConfig = append(ibftTransitionsConfig, tranI0, tranI10)
qbftTransitionsConfig = append(qbftTransitionsConfig, tranQ5, tranQ8)
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestCheckTransitionsData(t *testing.T) {
wantErr: ErrBlockOrder,
},
{
stored: &ChainConfig{Transitions: []Transition{{nil, IBFT, 30000, 5, 10, 50, common.Address{}, "", 0}}},
stored: &ChainConfig{Transitions: []Transition{{nil, IBFT, 30000, 5, 10, 50, common.Address{}, "", false, false, false, 0}}},
wantErr: ErrBlockNumberMissing,
},
{
Expand Down Expand Up @@ -488,3 +488,99 @@ func TestGetMaxCodeSize(t *testing.T) {
}
}
}

func TestIsQIP714(t *testing.T) {
type test struct {
config *ChainConfig
blockNumber int64
IsQIP714 bool
}

config1, config2 := *TestChainConfig, *TestChainConfig
config1.QIP714Block = big.NewInt(11)

config2.QIP714Block = nil
config2.Transitions = []Transition{
{Block: big.NewInt(21), EnhancedPermissioningEnabled: true},
}

tests := []test{
{MainnetChainConfig, 0, false},
{&config1, 10, false},
{&config1, 11, true},
{&config2, 20, false},
{&config2, 21, true},
{&config2, 22, true},
}

for _, test := range tests {
isQIP714 := test.config.IsQIP714(big.NewInt(test.blockNumber))
if !reflect.DeepEqual(isQIP714, test.IsQIP714) {
t.Errorf("error mismatch on %v:\nexpected: %v\nreceived: %v\n", test.blockNumber, test.IsQIP714, isQIP714)
}
}
}

func TestIsPrivacyEnhancementsEnabled(t *testing.T) {
type test struct {
config *ChainConfig
blockNumber int64
PrivacyEnhancementsEnabled bool
}

config1, config2 := *TestChainConfig, *TestChainConfig
config1.PrivacyEnhancementsBlock = big.NewInt(11)

config2.PrivacyEnhancementsBlock = nil
config2.Transitions = []Transition{
{Block: big.NewInt(21), PrivacyEnhancementsEnabled: true},
}

tests := []test{
{MainnetChainConfig, 0, false},
{&config1, 10, false},
{&config1, 11, true},
{&config2, 20, false},
{&config2, 21, true},
{&config2, 22, true},
}

for _, test := range tests {
isPrivacyEnhancementsEnabled := test.config.IsPrivacyEnhancementsEnabled(big.NewInt(test.blockNumber))
if !reflect.DeepEqual(isPrivacyEnhancementsEnabled, test.PrivacyEnhancementsEnabled) {
t.Errorf("error mismatch on %v:\nexpected: %v\nreceived: %v\n", test.blockNumber, test.PrivacyEnhancementsEnabled, isPrivacyEnhancementsEnabled)
}
}
}

func TestIsPrivacyPrecompileEnabled(t *testing.T) {
type test struct {
config *ChainConfig
blockNumber int64
PrivacyPrecompileEnabled bool
}

config1, config2 := *TestChainConfig, *TestChainConfig
config1.PrivacyPrecompileBlock = big.NewInt(11)

config2.PrivacyPrecompileBlock = nil
config2.Transitions = []Transition{
{Block: big.NewInt(21), PrivacyPrecompileEnalbed: true},
}

tests := []test{
{MainnetChainConfig, 0, false},
{&config1, 10, false},
{&config1, 11, true},
{&config2, 20, false},
{&config2, 21, true},
{&config2, 22, true},
}

for _, test := range tests {
isPrivacyPrecompileEnabled := test.config.IsPrivacyPrecompileEnabled(big.NewInt(test.blockNumber))
if !reflect.DeepEqual(isPrivacyPrecompileEnabled, test.PrivacyPrecompileEnabled) {
t.Errorf("error mismatch on %v:\nexpected: %v\nreceived: %v\n", test.blockNumber, test.PrivacyPrecompileEnabled, isPrivacyPrecompileEnabled)
}
}
}

0 comments on commit 46da6fc

Please sign in to comment.