github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/casper/casper_test.go (about)

     1  package casper
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bytom/bytom/protocol/bc"
     7  	"github.com/bytom/bytom/protocol/state"
     8  	"github.com/bytom/bytom/testutil"
     9  )
    10  
    11  func TestBestChain(t *testing.T) {
    12  	cases := []struct {
    13  		desc         string
    14  		tree         *treeNode
    15  		wantBestHash bc.Hash
    16  	}{
    17  		{
    18  			desc: "only root node",
    19  			tree: &treeNode{
    20  				Checkpoint: &state.Checkpoint{
    21  					Height: 0,
    22  					Hash:   testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
    23  					Status: state.Justified,
    24  				},
    25  			},
    26  			wantBestHash: testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
    27  		},
    28  		{
    29  			desc: "best chain is not the longest chain",
    30  			tree: &treeNode{
    31  				Checkpoint: &state.Checkpoint{
    32  					Height: 0,
    33  					Hash:   testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
    34  					Status: state.Finalized,
    35  				},
    36  				children: []*treeNode{
    37  					{
    38  						Checkpoint: &state.Checkpoint{
    39  							Height: 5,
    40  							Hash:   testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
    41  							Status: state.Justified,
    42  						},
    43  					},
    44  					{
    45  						Checkpoint: &state.Checkpoint{
    46  							Height: 5,
    47  							Hash:   testutil.MustDecodeHash("049f012c12b94d34b13163eddbd31866f97b38c5b742e4c170a44d80ff503166"),
    48  							Status: state.Unjustified,
    49  						},
    50  						children: []*treeNode{
    51  							{
    52  								Checkpoint: &state.Checkpoint{
    53  									Height: 8,
    54  									Hash:   testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
    55  									Status: state.Growing,
    56  								},
    57  							},
    58  						},
    59  					},
    60  				},
    61  			},
    62  			wantBestHash: testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
    63  		},
    64  		{
    65  			desc: "two distinct chain has same justified height, the longest chain is the best chain",
    66  			tree: &treeNode{
    67  				Checkpoint: &state.Checkpoint{
    68  					Height: 0,
    69  					Hash:   testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
    70  					Status: state.Justified,
    71  				},
    72  				children: []*treeNode{
    73  					{
    74  						Checkpoint: &state.Checkpoint{
    75  							Height: 5,
    76  							Hash:   testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
    77  							Status: state.Unjustified,
    78  						},
    79  						children: []*treeNode{
    80  							{
    81  								Checkpoint: &state.Checkpoint{
    82  									Height: 7,
    83  									Hash:   testutil.MustDecodeHash("0bf26d17ff2a578c1a733a1e969184d695e8f3ac6834150acc5c1e9edeb84de9"),
    84  									Status: state.Growing,
    85  								},
    86  							},
    87  						},
    88  					},
    89  					{
    90  						Checkpoint: &state.Checkpoint{
    91  							Height: 5,
    92  							Hash:   testutil.MustDecodeHash("049f012c12b94d34b13163eddbd31866f97b38c5b742e4c170a44d80ff503166"),
    93  							Status: state.Unjustified,
    94  						},
    95  						children: []*treeNode{
    96  							{
    97  								Checkpoint: &state.Checkpoint{
    98  									Height: 8,
    99  									Hash:   testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
   100  									Status: state.Growing,
   101  								},
   102  							},
   103  						},
   104  					},
   105  				},
   106  			},
   107  			wantBestHash: testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
   108  		},
   109  	}
   110  
   111  	for i, c := range cases {
   112  		if bestNode, _ := c.tree.bestNode(0); bestNode.Hash != c.wantBestHash {
   113  			t.Errorf("case #%d(%s) want best hash:%s, got best hash:%s\n", i, c.desc, c.wantBestHash.String(), bestNode.Hash.String())
   114  		}
   115  	}
   116  }