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"
17 prvKey = testutil.MustDecodeHexString("60deaf67d0bbb4d7f2e70f91eb508d87918640f0811f3a5a5f2b20246a07184bf863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5")
18 pubKey = "a2c5cbbc128485dccf41b7baec85ec9c7bd9418bc0c5ea23f4eeb621725bf9f0f863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5"
20 checkpoints = []*state.Checkpoint{
23 Hash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
24 Status: state.Justified,
25 Votes: map[string]uint64{
26 "a2c5cbbc128485dccf41b7baec85ec9c7bd9418bc0c5ea23f4eeb621725bf9f0f863e6920e6ffc7a9a4ed8e43de568067ef8277fd45d3188c528a9f148f8d2f5": 1e14,
27 "a7ac9f585075d9f30313e290fc7a4e35e6af741cffe020e0ffd4f2e4527f0080746bd53f43d5559da7ba5b134e3f67bd851ca66d2c6955e3493abbb32c68d6fa": 1e14,
32 ParentHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
33 Hash: testutil.MustDecodeHash("104da9966d3ef81d20a67a0aa79d3979b2542adeb52666deff64f63ecbfe2535"),
34 Status: state.Unjustified,
35 SupLinks: []*types.SupLink{
38 SourceHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
39 Signatures: [consensus.MaxNumOfValidators][]byte{[]byte{0xaa}},
45 ParentHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"),
46 Hash: testutil.MustDecodeHash("ba043724330543c9b6699b62dbdc71573e666fe2caeca181e651bc96ec474b44"),
47 Status: state.Unjustified,
51 ParentHash: testutil.MustDecodeHash("ba043724330543c9b6699b62dbdc71573e666fe2caeca181e651bc96ec474b44"),
52 Hash: testutil.MustDecodeHash("51e2a85b8e3ec0b4aabe891fca1d55a9970cf1b7b7783975bfcc169db7cf2653"),
53 Status: state.Unjustified,
58 func TestRollback(t *testing.T) {
59 casper := NewCasper(&mockStore2{}, checkpoints, make(chan *rollbackMsg))
60 casper.prevCheckpointCache.Add(checkpoints[1].Hash, &checkpoints[0].Hash)
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())
66 rollbackMsg.reply <- nil
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())
73 xPrv := chainkd.XPrv{}
76 SourceHash: checkpoints[0].Hash,
77 TargetHash: checkpoints[1].Hash,
78 SourceHeight: checkpoints[0].Height,
79 TargetHeight: checkpoints[1].Height,
82 if err := v.Sign(xPrv); err != nil {
86 if err := casper.AuthVerification(v); err != nil {
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())
95 type mockStore2 struct{}
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) {
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 {
114 func (s *mockStore2) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error) {
115 return &types.BlockHeader{}, nil
117 func (s *mockStore2) GetCheckpoint(hash *bc.Hash) (*state.Checkpoint, error) {
118 for _, c := range checkpoints {
123 return nil, errors.New("fail to get checkpoint")