github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/core/handler_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  	"testing"
    22  
    23  	"github.com/kisexp/xdchain/common"
    24  	"github.com/kisexp/xdchain/consensus/istanbul"
    25  	istanbulcommon "github.com/kisexp/xdchain/consensus/istanbul/common"
    26  	ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types"
    27  )
    28  
    29  // notice: the normal case have been tested in integration tests.
    30  func TestHandleMsg(t *testing.T) {
    31  	N := uint64(4)
    32  	F := uint64(1)
    33  	sys := NewTestSystemWithBackend(N, F)
    34  
    35  	closer := sys.Run(true)
    36  	defer closer()
    37  
    38  	v0 := sys.backends[0]
    39  	r0 := v0.engine
    40  
    41  	m, _ := ibfttypes.Encode(&istanbul.Subject{
    42  		View: &istanbul.View{
    43  			Sequence: big.NewInt(0),
    44  			Round:    big.NewInt(0),
    45  		},
    46  		Digest: common.StringToHash("1234567890"),
    47  	})
    48  	// with a matched payload. msgPreprepare should match with *istanbul.Preprepare in normal case.
    49  	msg := &ibfttypes.Message{
    50  		Code:          ibfttypes.MsgPreprepare,
    51  		Msg:           m,
    52  		Address:       v0.Address(),
    53  		Signature:     []byte{},
    54  		CommittedSeal: []byte{},
    55  	}
    56  
    57  	_, val := v0.Validators(nil).GetByAddress(v0.Address())
    58  	if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodePreprepare {
    59  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodePreprepare)
    60  	}
    61  
    62  	m, _ = ibfttypes.Encode(&istanbul.Preprepare{
    63  		View: &istanbul.View{
    64  			Sequence: big.NewInt(0),
    65  			Round:    big.NewInt(0),
    66  		},
    67  		Proposal: makeBlock(1),
    68  	})
    69  	// with a unmatched payload. msgPrepare should match with *istanbul.Subject in normal case.
    70  	msg = &ibfttypes.Message{
    71  		Code:          ibfttypes.MsgPrepare,
    72  		Msg:           m,
    73  		Address:       v0.Address(),
    74  		Signature:     []byte{},
    75  		CommittedSeal: []byte{},
    76  	}
    77  
    78  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
    79  	if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodePrepare {
    80  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodePreprepare)
    81  	}
    82  
    83  	m, _ = ibfttypes.Encode(&istanbul.Preprepare{
    84  		View: &istanbul.View{
    85  			Sequence: big.NewInt(0),
    86  			Round:    big.NewInt(0),
    87  		},
    88  		Proposal: makeBlock(2),
    89  	})
    90  	// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case.
    91  	msg = &ibfttypes.Message{
    92  		Code:          ibfttypes.MsgCommit,
    93  		Msg:           m,
    94  		Address:       v0.Address(),
    95  		Signature:     []byte{},
    96  		CommittedSeal: []byte{},
    97  	}
    98  
    99  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
   100  	if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodeCommit {
   101  		t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodeCommit)
   102  	}
   103  
   104  	m, _ = ibfttypes.Encode(&istanbul.Preprepare{
   105  		View: &istanbul.View{
   106  			Sequence: big.NewInt(0),
   107  			Round:    big.NewInt(0),
   108  		},
   109  		Proposal: makeBlock(3),
   110  	})
   111  	// invalid message code. message code is not exists in list
   112  	msg = &ibfttypes.Message{
   113  		Code:          uint64(99),
   114  		Msg:           m,
   115  		Address:       v0.Address(),
   116  		Signature:     []byte{},
   117  		CommittedSeal: []byte{},
   118  	}
   119  
   120  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
   121  	if err := r0.handleCheckedMsg(msg, val); err == nil {
   122  		t.Errorf("error mismatch: have %v, want nil", err)
   123  	}
   124  
   125  	// with malicious payload
   126  	if err := r0.handleMsg([]byte{1}); err == nil {
   127  		t.Errorf("error mismatch: have %v, want nil", err)
   128  	}
   129  }