github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/core/types/istanbul_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 types
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/hexutil"
    26  )
    27  
    28  func TestHeaderHash(t *testing.T) {
    29  	// 0xcefefd3ade63a5955bca4562ed840b67f39e74df217f7e5f7241a6e9552cca70
    30  	expectedExtra := common.FromHex("0x0000000000000000000000000000000000000000000000000000000000000000f89af8549444add0ec310f115a0e603b2d7db9f067778eaf8a94294fc7e8f22b3bcdcf955dd7ff3ba2ed833f8212946beaaed781d2d2ab6350f5c4566a2c6eaac407a6948be76812f765c24641ec63dc2852b378aba2b440b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0")
    31  	expectedHash := common.HexToHash("0xcefefd3ade63a5955bca4562ed840b67f39e74df217f7e5f7241a6e9552cca70")
    32  
    33  	// for istanbul consensus
    34  	header := &Header{MixDigest: IstanbulDigest, Extra: expectedExtra}
    35  	if !reflect.DeepEqual(header.Hash(), expectedHash) {
    36  		t.Errorf("expected: %v, but got: %v", expectedHash.Hex(), header.Hash().Hex())
    37  	}
    38  
    39  	// append useless information to extra-data
    40  	unexpectedExtra := append(expectedExtra, []byte{1, 2, 3}...)
    41  	header.Extra = unexpectedExtra
    42  	if !reflect.DeepEqual(header.Hash(), rlpHash(header)) {
    43  		t.Errorf("expected: %v, but got: %v", rlpHash(header).Hex(), header.Hash().Hex())
    44  	}
    45  }
    46  
    47  func TestExtractToQBFTExtra(t *testing.T) {
    48  	testCases := []struct {
    49  		istRawData     []byte
    50  		expectedResult *QBFTExtra
    51  		expectedErr    error
    52  	}{
    53  		{
    54  			// normal case
    55  			hexutil.MustDecode("0xf85a80f8549444add0ec310f115a0e603b2d7db9f067778eaf8a94294fc7e8f22b3bcdcf955dd7ff3ba2ed833f8212946beaaed781d2d2ab6350f5c4566a2c6eaac407a6948be76812f765c24641ec63dc2852b378aba2b440c080c0"),
    56  			&QBFTExtra{
    57  				VanityData: []byte{},
    58  				Validators: []common.Address{
    59  					common.BytesToAddress(hexutil.MustDecode("0x44add0ec310f115a0e603b2d7db9f067778eaf8a")),
    60  					common.BytesToAddress(hexutil.MustDecode("0x294fc7e8f22b3bcdcf955dd7ff3ba2ed833f8212")),
    61  					common.BytesToAddress(hexutil.MustDecode("0x6beaaed781d2d2ab6350f5c4566a2c6eaac407a6")),
    62  					common.BytesToAddress(hexutil.MustDecode("0x8be76812f765c24641ec63dc2852b378aba2b440")),
    63  				},
    64  				CommittedSeal: [][]byte{},
    65  				Round:         0,
    66  				Vote:          nil,
    67  			},
    68  			nil,
    69  		},
    70  	}
    71  	for _, test := range testCases {
    72  		h := &Header{Extra: test.istRawData}
    73  		istanbulExtra, err := ExtractQBFTExtra(h)
    74  		if err != test.expectedErr {
    75  			t.Errorf("expected: %v, but got: %v", test.expectedErr, err)
    76  		}
    77  		if !reflect.DeepEqual(istanbulExtra, test.expectedResult) {
    78  			t.Errorf("expected: %v, but got: %v", test.expectedResult, istanbulExtra)
    79  		}
    80  	}
    81  }
    82  
    83  func TestExtractToIstanbul(t *testing.T) {
    84  	testCases := []struct {
    85  		vanity         []byte
    86  		istRawData     []byte
    87  		expectedResult *IstanbulExtra
    88  		expectedErr    error
    89  	}{
    90  		{
    91  			// normal case
    92  			bytes.Repeat([]byte{0x00}, IstanbulExtraVanity),
    93  			hexutil.MustDecode("0xf858f8549444add0ec310f115a0e603b2d7db9f067778eaf8a94294fc7e8f22b3bcdcf955dd7ff3ba2ed833f8212946beaaed781d2d2ab6350f5c4566a2c6eaac407a6948be76812f765c24641ec63dc2852b378aba2b44080c0"),
    94  			&IstanbulExtra{
    95  				Validators: []common.Address{
    96  					common.BytesToAddress(hexutil.MustDecode("0x44add0ec310f115a0e603b2d7db9f067778eaf8a")),
    97  					common.BytesToAddress(hexutil.MustDecode("0x294fc7e8f22b3bcdcf955dd7ff3ba2ed833f8212")),
    98  					common.BytesToAddress(hexutil.MustDecode("0x6beaaed781d2d2ab6350f5c4566a2c6eaac407a6")),
    99  					common.BytesToAddress(hexutil.MustDecode("0x8be76812f765c24641ec63dc2852b378aba2b440")),
   100  				},
   101  				Seal:          []byte{},
   102  				CommittedSeal: [][]byte{},
   103  			},
   104  			nil,
   105  		},
   106  		{
   107  			// insufficient vanity
   108  			bytes.Repeat([]byte{0x00}, IstanbulExtraVanity-1),
   109  			nil,
   110  			nil,
   111  			ErrInvalidIstanbulHeaderExtra,
   112  		},
   113  	}
   114  	for _, test := range testCases {
   115  		h := &Header{Extra: append(test.vanity, test.istRawData...)}
   116  		istanbulExtra, err := ExtractIstanbulExtra(h)
   117  		if err != test.expectedErr {
   118  			t.Errorf("expected: %v, but got: %v", test.expectedErr, err)
   119  		}
   120  		if !reflect.DeepEqual(istanbulExtra, test.expectedResult) {
   121  			t.Errorf("expected: %v, but got: %v", test.expectedResult, istanbulExtra)
   122  		}
   123  	}
   124  }