github.com/ConsenSys/Quorum@v20.10.0+incompatible/consensus/istanbul/core/core_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	elog "github.com/ethereum/go-ethereum/log"
    29  )
    30  
    31  func makeBlock(number int64) *types.Block {
    32  	header := &types.Header{
    33  		Difficulty: big.NewInt(0),
    34  		Number:     big.NewInt(number),
    35  		GasLimit:   0,
    36  		GasUsed:    0,
    37  		Time:       0,
    38  	}
    39  	block := &types.Block{}
    40  	return block.WithSeal(header)
    41  }
    42  
    43  func newTestProposal() istanbul.Proposal {
    44  	return makeBlock(1)
    45  }
    46  
    47  func TestNewRequest(t *testing.T) {
    48  	testLogger.SetHandler(elog.StdoutHandler)
    49  
    50  	N := uint64(4)
    51  	F := uint64(1)
    52  
    53  	sys := NewTestSystemWithBackend(N, F)
    54  
    55  	close := sys.Run(true)
    56  	defer close()
    57  
    58  	request1 := makeBlock(1)
    59  	sys.backends[0].NewRequest(request1)
    60  
    61  	<-time.After(1 * time.Second)
    62  
    63  	request2 := makeBlock(2)
    64  	sys.backends[0].NewRequest(request2)
    65  
    66  	<-time.After(1 * time.Second)
    67  
    68  	for _, backend := range sys.backends {
    69  		if len(backend.committedMsgs) != 2 {
    70  			t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs))
    71  		}
    72  		if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) {
    73  			t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number())
    74  		}
    75  		if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) {
    76  			t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number())
    77  		}
    78  	}
    79  }
    80  
    81  func TestQuorumSize(t *testing.T) {
    82  	N := uint64(4)
    83  	F := uint64(1)
    84  
    85  	sys := NewTestSystemWithBackend(N, F)
    86  	backend := sys.backends[0]
    87  	c := backend.engine.(*core)
    88  
    89  	valSet := c.valSet
    90  	for i := 1; i <= 1000; i++ {
    91  		valSet.AddValidator(common.StringToAddress(string(i)))
    92  		if 2*c.QuorumSize() <= (valSet.Size()+valSet.F()) || 2*c.QuorumSize() > (valSet.Size()+valSet.F()+2) {
    93  			t.Errorf("quorumSize constraint failed, expected value (2*QuorumSize > Size+F && 2*QuorumSize <= Size+F+2) to be:%v, got: %v, for size: %v", true, false, valSet.Size())
    94  		}
    95  	}
    96  }