6 "github.com/bytom/bytom/protocol/bc"
7 "github.com/bytom/bytom/protocol/state"
8 "github.com/bytom/bytom/testutil"
11 func TestBestChain(t *testing.T) {
18 desc: "only root node",
20 Checkpoint: &state.Checkpoint{
22 Hash: testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
23 Status: state.Justified,
26 wantBestHash: testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
29 desc: "best chain is not the longest chain",
31 Checkpoint: &state.Checkpoint{
33 Hash: testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
34 Status: state.Finalized,
36 children: []*treeNode{
38 Checkpoint: &state.Checkpoint{
40 Hash: testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
41 Status: state.Justified,
45 Checkpoint: &state.Checkpoint{
47 Hash: testutil.MustDecodeHash("049f012c12b94d34b13163eddbd31866f97b38c5b742e4c170a44d80ff503166"),
48 Status: state.Unjustified,
50 children: []*treeNode{
52 Checkpoint: &state.Checkpoint{
54 Hash: testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
55 Status: state.Growing,
62 wantBestHash: testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
65 desc: "two distinct chain has same justified height, the longest chain is the best chain",
67 Checkpoint: &state.Checkpoint{
69 Hash: testutil.MustDecodeHash("f5d687e6a5b60fb533c4296e55260803c54e70ad4898682541da15e894971769"),
70 Status: state.Justified,
72 children: []*treeNode{
74 Checkpoint: &state.Checkpoint{
76 Hash: testutil.MustDecodeHash("750408823bea9666f263870daded75d8f3d878606ec103bc9401b73714d77729"),
77 Status: state.Unjustified,
79 children: []*treeNode{
81 Checkpoint: &state.Checkpoint{
83 Hash: testutil.MustDecodeHash("0bf26d17ff2a578c1a733a1e969184d695e8f3ac6834150acc5c1e9edeb84de9"),
84 Status: state.Growing,
90 Checkpoint: &state.Checkpoint{
92 Hash: testutil.MustDecodeHash("049f012c12b94d34b13163eddbd31866f97b38c5b742e4c170a44d80ff503166"),
93 Status: state.Unjustified,
95 children: []*treeNode{
97 Checkpoint: &state.Checkpoint{
99 Hash: testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
100 Status: state.Growing,
107 wantBestHash: testutil.MustDecodeHash("8315f5337978076b3097230570484e36586ee27564ebcbf74c2093cd763e32e7"),
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())