github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/types_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 istanbul
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  )
    28  
    29  func TestViewCompare(t *testing.T) {
    30  	// test equality
    31  	srvView := &View{
    32  		Sequence: big.NewInt(2),
    33  		Round:    big.NewInt(1),
    34  	}
    35  	tarView := &View{
    36  		Sequence: big.NewInt(2),
    37  		Round:    big.NewInt(1),
    38  	}
    39  	if r := srvView.Cmp(tarView); r != 0 {
    40  		t.Errorf("source(%v) should be equal to target(%v): have %v, want %v", srvView, tarView, r, 0)
    41  	}
    42  
    43  	// test larger Sequence
    44  	tarView = &View{
    45  		Sequence: big.NewInt(1),
    46  		Round:    big.NewInt(1),
    47  	}
    48  	if r := srvView.Cmp(tarView); r != 1 {
    49  		t.Errorf("source(%v) should be larger than target(%v): have %v, want %v", srvView, tarView, r, 1)
    50  	}
    51  
    52  	// test larger Round
    53  	tarView = &View{
    54  		Sequence: big.NewInt(2),
    55  		Round:    big.NewInt(0),
    56  	}
    57  	if r := srvView.Cmp(tarView); r != 1 {
    58  		t.Errorf("source(%v) should be larger than target(%v): have %v, want %v", srvView, tarView, r, 1)
    59  	}
    60  
    61  	// test smaller Sequence
    62  	tarView = &View{
    63  		Sequence: big.NewInt(3),
    64  		Round:    big.NewInt(1),
    65  	}
    66  	if r := srvView.Cmp(tarView); r != -1 {
    67  		t.Errorf("source(%v) should be smaller than target(%v): have %v, want %v", srvView, tarView, r, -1)
    68  	}
    69  	tarView = &View{
    70  		Sequence: big.NewInt(2),
    71  		Round:    big.NewInt(2),
    72  	}
    73  	if r := srvView.Cmp(tarView); r != -1 {
    74  		t.Errorf("source(%v) should be smaller than target(%v): have %v, want %v", srvView, tarView, r, -1)
    75  	}
    76  }
    77  
    78  func dummyView() *View {
    79  	return &View{
    80  		Round:    big.NewInt(15),
    81  		Sequence: big.NewInt(42),
    82  	}
    83  }
    84  func dummySubject() *Subject {
    85  	return &Subject{
    86  		View:   dummyView(),
    87  		Digest: common.HexToHash("1234567890"),
    88  	}
    89  }
    90  
    91  func dummyBlock(number int64) *types.Block {
    92  	header := &types.Header{
    93  		Difficulty: big.NewInt(5),
    94  		Number:     big.NewInt(number),
    95  		GasLimit:   1002121,
    96  		GasUsed:    123213,
    97  		Time:       big.NewInt(100),
    98  		Extra:      []byte{01, 02},
    99  	}
   100  	feeCurrencyAddr := common.HexToAddress("02")
   101  	gatewayFeeRecipientAddr := common.HexToAddress("03")
   102  	tx := types.NewTransaction(1, common.HexToAddress("01"), big.NewInt(1), 10000, big.NewInt(10), &feeCurrencyAddr, &gatewayFeeRecipientAddr, big.NewInt(34), []byte{04})
   103  	return types.NewBlock(header, []*types.Transaction{tx}, nil, nil, nil)
   104  
   105  }
   106  func dummyMessage(code uint64) *Message {
   107  	return &Message{
   108  		Code:      code,
   109  		Address:   common.HexToAddress("AABB"),
   110  		Msg:       []byte{10, 20, 42},
   111  		Signature: []byte{30, 40, 52},
   112  	}
   113  }
   114  func dummyRoundChangeCertificate() *RoundChangeCertificate {
   115  	return &RoundChangeCertificate{
   116  		RoundChangeMessages: []Message{*dummyMessage(42), *dummyMessage(32), *dummyMessage(15)},
   117  	}
   118  }
   119  
   120  func dummyPreparedCertificate() *PreparedCertificate {
   121  	return &PreparedCertificate{
   122  		PrepareOrCommitMessages: []Message{*dummyMessage(42), *dummyMessage(32), *dummyMessage(15)},
   123  		Proposal:                dummyBlock(1),
   124  	}
   125  }
   126  
   127  func assertEqual(t *testing.T, prefix string, o, r interface{}) {
   128  	if !reflect.DeepEqual(o, r) {
   129  		t.Errorf("%s:  Got %#v, expected %#v", prefix, r, o)
   130  	}
   131  }
   132  
   133  func TestViewRLPEncoding(t *testing.T) {
   134  	var result, original *View
   135  	original = dummyView()
   136  
   137  	rawVal, err := rlp.EncodeToBytes(original)
   138  	if err != nil {
   139  		t.Fatalf("Error %v", err)
   140  	}
   141  
   142  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   143  		t.Fatalf("Error %v", err)
   144  	}
   145  
   146  	if !reflect.DeepEqual(original, result) {
   147  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   148  	}
   149  }
   150  
   151  func TestMessageRLPEncoding(t *testing.T) {
   152  	var result, original *Message
   153  	original = dummyMessage(42)
   154  
   155  	rawVal, err := rlp.EncodeToBytes(original)
   156  	if err != nil {
   157  		t.Fatalf("Error %v", err)
   158  	}
   159  
   160  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   161  		t.Fatalf("Error %v", err)
   162  	}
   163  
   164  	if !reflect.DeepEqual(original, result) {
   165  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   166  	}
   167  }
   168  
   169  func TestRoundChangeCertificateRLPEncoding(t *testing.T) {
   170  	var result, original *RoundChangeCertificate
   171  	original = dummyRoundChangeCertificate()
   172  
   173  	rawVal, err := rlp.EncodeToBytes(original)
   174  	if err != nil {
   175  		t.Fatalf("Error %v", err)
   176  	}
   177  
   178  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   179  		t.Fatalf("Error %v", err)
   180  	}
   181  
   182  	if !reflect.DeepEqual(original, result) {
   183  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   184  	}
   185  }
   186  
   187  func TestPreprepareRLPEncoding(t *testing.T) {
   188  	var result, original *Preprepare
   189  	original = &Preprepare{
   190  		View:                   dummyView(),
   191  		RoundChangeCertificate: *dummyRoundChangeCertificate(),
   192  		Proposal:               dummyBlock(1),
   193  	}
   194  
   195  	rawVal, err := rlp.EncodeToBytes(original)
   196  	if err != nil {
   197  		t.Fatalf("Error %v", err)
   198  	}
   199  
   200  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   201  		t.Fatalf("Error %v", err)
   202  	}
   203  
   204  	// decoded Blocks don't equal Original ones so we need to check equality differently
   205  	assertEqual(t, "RLP Encode/Decode mismatch: View", result.View, original.View)
   206  	assertEqual(t, "RLP Encode/Decode mismatch: RoundChangeCertificate", result.RoundChangeCertificate, original.RoundChangeCertificate)
   207  	assertEqual(t, "RLP Encode/Decode mismatch: BlockHash", result.Proposal.Hash(), original.Proposal.Hash())
   208  }
   209  
   210  func TestPreparedCertificateRLPEncoding(t *testing.T) {
   211  	var result, original *PreparedCertificate
   212  	original = dummyPreparedCertificate()
   213  
   214  	rawVal, err := rlp.EncodeToBytes(original)
   215  	if err != nil {
   216  		t.Fatalf("Error %v", err)
   217  	}
   218  
   219  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   220  		t.Fatalf("Error %v", err)
   221  	}
   222  
   223  	// decoded Blocks don't equal Original ones so we need to check equality differently
   224  	assertEqual(t, "RLP Encode/Decode mismatch: PrepareOrCommitMessages", result.PrepareOrCommitMessages, original.PrepareOrCommitMessages)
   225  	assertEqual(t, "RLP Encode/Decode mismatch: BlockHash", result.Proposal.Hash(), original.Proposal.Hash())
   226  }
   227  
   228  func TestRoundChangeRLPEncoding(t *testing.T) {
   229  	var result, original *RoundChange
   230  	original = &RoundChange{
   231  		View:                dummyView(),
   232  		PreparedCertificate: *dummyPreparedCertificate(),
   233  	}
   234  
   235  	rawVal, err := rlp.EncodeToBytes(original)
   236  	if err != nil {
   237  		t.Fatalf("Error %v", err)
   238  	}
   239  
   240  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   241  		t.Fatalf("Error %v", err)
   242  	}
   243  
   244  	// decoded Blocks don't equal Original ones so we need to check equality differently
   245  	assertEqual(t, "RLP Encode/Decode mismatch: View", result.View, original.View)
   246  	assertEqual(t, "RLP Encode/Decode mismatch: PreparedCertificate.PrepareOrCommitMessages", result.PreparedCertificate.PrepareOrCommitMessages, original.PreparedCertificate.PrepareOrCommitMessages)
   247  	assertEqual(t, "RLP Encode/Decode mismatch: PreparedCertificate.BlockHash", result.PreparedCertificate.Proposal.Hash(), original.PreparedCertificate.Proposal.Hash())
   248  }
   249  
   250  func TestSubjectRLPEncoding(t *testing.T) {
   251  	var result, original *Subject
   252  	original = dummySubject()
   253  
   254  	rawVal, err := rlp.EncodeToBytes(original)
   255  	if err != nil {
   256  		t.Fatalf("Error %v", err)
   257  	}
   258  
   259  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   260  		t.Fatalf("Error %v", err)
   261  	}
   262  
   263  	if !reflect.DeepEqual(original, result) {
   264  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   265  	}
   266  }
   267  
   268  func TestCommittedSubjectRLPEncoding(t *testing.T) {
   269  	var result, original *CommittedSubject
   270  	original = &CommittedSubject{
   271  		Subject:               dummySubject(),
   272  		CommittedSeal:         []byte{12, 13, 23},
   273  		EpochValidatorSetSeal: []byte{1, 5, 50},
   274  	}
   275  
   276  	rawVal, err := rlp.EncodeToBytes(original)
   277  	if err != nil {
   278  		t.Fatalf("Error %v", err)
   279  	}
   280  
   281  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   282  		t.Fatalf("Error %v", err)
   283  	}
   284  
   285  	if !reflect.DeepEqual(original, result) {
   286  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   287  	}
   288  }
   289  
   290  func TestForwardMessageRLPEncoding(t *testing.T) {
   291  	var result, original *ForwardMessage
   292  	original = &ForwardMessage{
   293  		DestAddresses: []common.Address{common.HexToAddress("123123")},
   294  		Msg:           []byte{23, 23, 12, 3},
   295  	}
   296  
   297  	rawVal, err := rlp.EncodeToBytes(original)
   298  	if err != nil {
   299  		t.Fatalf("Error %v", err)
   300  	}
   301  
   302  	if err = rlp.DecodeBytes(rawVal, &result); err != nil {
   303  		t.Fatalf("Error %v", err)
   304  	}
   305  
   306  	if !reflect.DeepEqual(original, result) {
   307  		t.Fatalf("RLP Encode/Decode mismatch. Got %v, expected %v", result, original)
   308  	}
   309  }