github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/pbft/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  	"crypto/ecdsa"
    21  	"github.com/bigzoro/my_simplechain/crypto"
    22  	"math/big"
    23  	"reflect"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/bigzoro/my_simplechain/common"
    28  	"github.com/bigzoro/my_simplechain/consensus/pbft"
    29  	"github.com/bigzoro/my_simplechain/core/types"
    30  	elog "github.com/bigzoro/my_simplechain/log"
    31  )
    32  
    33  func makeBlock(number int64, txs ...*types.Transaction) *types.Block {
    34  	header := &types.Header{
    35  		Difficulty: big.NewInt(0),
    36  		Number:     big.NewInt(number),
    37  		GasLimit:   0,
    38  		GasUsed:    0,
    39  		Time:       0,
    40  	}
    41  	return types.NewBlock(header, txs, nil, nil)
    42  }
    43  
    44  func newTestProposal() pbft.Proposal {
    45  	return makeBlock(1)
    46  }
    47  
    48  func newTestConclusion() pbft.Conclusion {
    49  	return makeBlock(1)
    50  }
    51  
    52  func newTestLightProposal(txs ...*types.Transaction) pbft.LightProposal {
    53  	return types.NewLightBlock(makeBlock(1, txs...))
    54  }
    55  
    56  func newTransactions(n int, signer types.Signer, key *ecdsa.PrivateKey) types.Transactions {
    57  	var txs types.Transactions
    58  
    59  	for i := 0; i < n; i++ {
    60  		tx := types.NewTransaction(uint64(i), common.Address{}, common.Big1, 21000, common.Big1, make([]byte, 64))
    61  
    62  		signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    63  		if err != nil {
    64  			continue
    65  		}
    66  		signed, err := tx.WithSignature(signer, signature)
    67  		if err != nil {
    68  			continue
    69  		}
    70  		txs = append(txs, signed)
    71  	}
    72  
    73  	return txs
    74  }
    75  
    76  func TestNewRequest(t *testing.T) {
    77  	testLogger.SetHandler(elog.StdoutHandler)
    78  
    79  	N := uint64(4)
    80  	F := uint64(1)
    81  
    82  	sys := NewTestSystemWithBackend(N, F)
    83  
    84  	close := sys.Run(true)
    85  	defer close()
    86  
    87  	request1 := makeBlock(1)
    88  	sys.backends[0].NewRequest(request1)
    89  
    90  	<-time.After(1 * time.Second)
    91  
    92  	request2 := makeBlock(2)
    93  	sys.backends[0].NewRequest(request2)
    94  
    95  	<-time.After(1 * time.Second)
    96  
    97  	for _, backend := range sys.backends {
    98  		if len(backend.committedMsgs) != 2 {
    99  			t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs))
   100  		}
   101  		if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) {
   102  			t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number())
   103  		}
   104  		if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) {
   105  			t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number())
   106  		}
   107  	}
   108  }
   109  
   110  func TestQuorumSize(t *testing.T) {
   111  	N := uint64(4)
   112  	F := uint64(1)
   113  
   114  	sys := NewTestSystemWithBackend(N, F)
   115  	backend := sys.backends[0]
   116  	c := backend.engine.(*core)
   117  
   118  	valSet := c.valSet
   119  	for i := 1; i <= 1000; i++ {
   120  		valSet.AddValidator(common.BytesToAddress([]byte(string(rune(i)))))
   121  		if 2*c.Confirmations() <= (valSet.Size()+valSet.F()) || 2*c.Confirmations() > (valSet.Size()+valSet.F()+2) {
   122  			t.Errorf("quorumSize constraint failed, expected value (2*Confirmations > Size+F && 2*Confirmations <= Size+F+2) to be:%v, got: %v, for size: %v", true, false, valSet.Size())
   123  		}
   124  	}
   125  }