github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/core/request_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  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/kisexp/xdchain/common"
    27  	"github.com/kisexp/xdchain/consensus/istanbul"
    28  	istanbulcommon "github.com/kisexp/xdchain/consensus/istanbul/common"
    29  	ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types"
    30  	"github.com/kisexp/xdchain/event"
    31  	"github.com/kisexp/xdchain/log"
    32  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    33  )
    34  
    35  func TestCheckRequestMsg(t *testing.T) {
    36  	c := &core{
    37  		state: ibfttypes.StateAcceptRequest,
    38  		current: newRoundState(&istanbul.View{
    39  			Sequence: big.NewInt(1),
    40  			Round:    big.NewInt(0),
    41  		}, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
    42  	}
    43  
    44  	// invalid request
    45  	err := c.checkRequestMsg(nil)
    46  	if err != istanbulcommon.ErrInvalidMessage {
    47  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrInvalidMessage)
    48  	}
    49  	r := &istanbul.Request{
    50  		Proposal: nil,
    51  	}
    52  	err = c.checkRequestMsg(r)
    53  	if err != istanbulcommon.ErrInvalidMessage {
    54  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrInvalidMessage)
    55  	}
    56  
    57  	// old request
    58  	r = &istanbul.Request{
    59  		Proposal: makeBlock(0),
    60  	}
    61  	err = c.checkRequestMsg(r)
    62  	if err != istanbulcommon.ErrOldMessage {
    63  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrOldMessage)
    64  	}
    65  
    66  	// future request
    67  	r = &istanbul.Request{
    68  		Proposal: makeBlock(2),
    69  	}
    70  	err = c.checkRequestMsg(r)
    71  	if err != istanbulcommon.ErrFutureMessage {
    72  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
    73  	}
    74  
    75  	// current request
    76  	r = &istanbul.Request{
    77  		Proposal: makeBlock(1),
    78  	}
    79  	err = c.checkRequestMsg(r)
    80  	if err != nil {
    81  		t.Errorf("error mismatch: have %v, want nil", err)
    82  	}
    83  }
    84  
    85  func TestStoreRequestMsg(t *testing.T) {
    86  	backend := &testSystemBackend{
    87  		events: new(event.TypeMux),
    88  	}
    89  	c := &core{
    90  		logger:  log.New("backend", "test", "id", 0),
    91  		backend: backend,
    92  		state:   ibfttypes.StateAcceptRequest,
    93  		current: newRoundState(&istanbul.View{
    94  			Sequence: big.NewInt(0),
    95  			Round:    big.NewInt(0),
    96  		}, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
    97  		pendingRequests:   prque.New(),
    98  		pendingRequestsMu: new(sync.Mutex),
    99  	}
   100  	requests := []istanbul.Request{
   101  		{
   102  			Proposal: makeBlock(1),
   103  		},
   104  		{
   105  			Proposal: makeBlock(2),
   106  		},
   107  		{
   108  			Proposal: makeBlock(3),
   109  		},
   110  	}
   111  
   112  	c.storeRequestMsg(&requests[1])
   113  	c.storeRequestMsg(&requests[0])
   114  	c.storeRequestMsg(&requests[2])
   115  	if c.pendingRequests.Size() != len(requests) {
   116  		t.Errorf("the size of pending requests mismatch: have %v, want %v", c.pendingRequests.Size(), len(requests))
   117  	}
   118  
   119  	c.current.sequence = big.NewInt(3)
   120  
   121  	c.subscribeEvents()
   122  	defer c.unsubscribeEvents()
   123  
   124  	c.processPendingRequests()
   125  
   126  	const timeoutDura = 2 * time.Second
   127  	timeout := time.NewTimer(timeoutDura)
   128  	select {
   129  	case ev := <-c.events.Chan():
   130  		e, ok := ev.Data.(istanbul.RequestEvent)
   131  		if !ok {
   132  			t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
   133  		}
   134  		if e.Proposal.Number().Cmp(requests[2].Proposal.Number()) != 0 {
   135  			t.Errorf("the number of proposal mismatch: have %v, want %v", e.Proposal.Number(), requests[2].Proposal.Number())
   136  		}
   137  	case <-timeout.C:
   138  		t.Error("unexpected timeout occurs")
   139  	}
   140  }