OSDN Git Service

edit dup sup link struct (#1988)
[bytom/bytom.git] / protocol / auth_verification_test.go
1 package protocol
2
3 import (
4         "testing"
5
6         "github.com/bytom/bytom/consensus"
7         "github.com/bytom/bytom/crypto/ed25519/chainkd"
8         "github.com/bytom/bytom/database/storage"
9         "github.com/bytom/bytom/errors"
10         "github.com/bytom/bytom/protocol/bc"
11         "github.com/bytom/bytom/protocol/bc/types"
12         "github.com/bytom/bytom/protocol/state"
13         "github.com/bytom/bytom/testutil"
14 )
15
16 var (
17         prvKey = testutil.MustDecodeHexString("60deaf67d0bbb4d7f2e70f91eb508d87918640f0811f3a5a5f2b20246a07184bf863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5")
18         pubKey = "a2c5cbbc128485dccf41b7baec85ec9c7bd9418bc0c5ea23f4eeb621725bf9f0f863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5"
19
20         checkpoints = []*state.Checkpoint{
21                 {
22                         Height: 0,
23                         Hash:   testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
24                         Status: state.Justified,
25                         Votes: map[string]uint64{
26                                 "a2c5cbbc128485dccf41b7baec85ec9c7bd9418bc0c5ea23f4eeb621725bf9f0f863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5": 1e14,
27                                 "a7ac9f585075d9f30313e290fc7a4e35e6af741cffe020e0ffd4f2e4527f0080746bd53f43d5559da7ba5b134e3f67bd851ca66d2c6955e3493abbb32c68d6fa": 1e14,
28                         },
29                 },
30                 {
31                         Height:     100,
32                         ParentHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
33                         Hash:       testutil.MustDecodeHash("104da9966d3ef81d20a67a0aa79d3979b2542adeb52666deff64f63ecbfe2535"),
34                         Status:     state.Unjustified,
35                         SupLinks: []*types.SupLink{
36                                 {
37                                         SourceHeight: 0,
38                                         SourceHash:   testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
39                                         Signatures:   [consensus.MaxNumOfValidators][]byte{[]byte{0xaa}},
40                                 },
41                         },
42                 },
43                 {
44                         Height:     100,
45                         ParentHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
46                         Hash:       testutil.MustDecodeHash("ba043724330543c9b6699b62dbdc71573e666fe2caeca181e651bc96ec474b44"),
47                         Status:     state.Unjustified,
48                 },
49                 {
50                         Height:     200,
51                         ParentHash: testutil.MustDecodeHash("ba043724330543c9b6699b62dbdc71573e666fe2caeca181e651bc96ec474b44"),
52                         Hash:       testutil.MustDecodeHash("51e2a85b8e3ec0b4aabe891fca1d55a9970cf1b7b7783975bfcc169db7cf2653"),
53                         Status:     state.Unjustified,
54                 },
55         }
56 )
57
58 func TestRollback(t *testing.T) {
59         casper := NewCasper(&mockStore2{}, checkpoints, make(chan *rollbackMsg))
60         casper.prevCheckpointCache.Add(checkpoints[1].Hash, &checkpoints[0].Hash)
61         go func() {
62                 rollbackMsg := <-casper.rollbackCh
63                 if rollbackMsg.bestHash != checkpoints[1].Hash {
64                         t.Fatalf("want best chain %s, got %s\n", checkpoints[1].Hash.String(), rollbackMsg.bestHash.String())
65                 }
66                 rollbackMsg.reply <- nil
67         }()
68
69         if bestHash := casper.bestChain(); bestHash != checkpoints[3].Hash {
70                 t.Fatalf("want best chain %s, got %s\n", checkpoints[3].Hash.String(), bestHash.String())
71         }
72
73         xPrv := chainkd.XPrv{}
74         copy(xPrv[:], prvKey)
75         v := &Verification{
76                 SourceHash:   checkpoints[0].Hash,
77                 TargetHash:   checkpoints[1].Hash,
78                 SourceHeight: checkpoints[0].Height,
79                 TargetHeight: checkpoints[1].Height,
80                 PubKey:       pubKey,
81         }
82         if err := v.Sign(xPrv); err != nil {
83                 t.Fatal(err)
84         }
85
86         if err := casper.AuthVerification(v); err != nil {
87                 t.Fatal(err)
88         }
89
90         if bestHash := casper.bestChain(); bestHash != checkpoints[1].Hash {
91                 t.Fatalf("want best chain %s, got %s\n", checkpoints[1].Hash.String(), bestHash.String())
92         }
93 }
94
95 type mockStore2 struct{}
96
97 func (s *mockStore2) GetCheckpointsByHeight(u uint64) ([]*state.Checkpoint, error) { return nil, nil }
98 func (s *mockStore2) SaveCheckpoints([]*state.Checkpoint) error                    { return nil }
99 func (s *mockStore2) CheckpointsFromNode(height uint64, hash *bc.Hash) ([]*state.Checkpoint, error) {
100         return nil, nil
101 }
102 func (s *mockStore2) BlockExist(hash *bc.Hash) bool                            { return false }
103 func (s *mockStore2) GetBlock(*bc.Hash) (*types.Block, error)                  { return nil, nil }
104 func (s *mockStore2) GetStoreStatus() *BlockStoreState                         { return nil }
105 func (s *mockStore2) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
106 func (s *mockStore2) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)             { return nil, nil }
107 func (s *mockStore2) GetMainChainHash(uint64) (*bc.Hash, error)                { return nil, nil }
108 func (s *mockStore2) GetContract([32]byte) ([]byte, error)                     { return nil, nil }
109 func (s *mockStore2) SaveBlock(*types.Block) error                             { return nil }
110 func (s *mockStore2) SaveBlockHeader(*types.BlockHeader) error                 { return nil }
111 func (s *mockStore2) SaveChainStatus(*types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, *state.ContractViewpoint, uint64, *bc.Hash) error {
112         return nil
113 }
114 func (s *mockStore2) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error) {
115         return &types.BlockHeader{}, nil
116 }
117 func (s *mockStore2) GetCheckpoint(hash *bc.Hash) (*state.Checkpoint, error) {
118         for _, c := range checkpoints {
119                 if c.Hash == *hash {
120                         return c, nil
121                 }
122         }
123         return nil, errors.New("fail to get checkpoint")
124 }