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

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