github.com/annchain/OG@v0.0.9/og/downloader/downloader_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package downloader
    15  
    16  import (
    17  	types2 "github.com/annchain/OG/arefactor/og/types"
    18  	"github.com/annchain/OG/arefactor/ogcrypto"
    19  	"github.com/annchain/OG/common"
    20  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
    21  	"github.com/annchain/OG/og/protocol/dagmessage"
    22  	"github.com/annchain/OG/og/types"
    23  	core2 "github.com/annchain/OG/ogcore/ledger"
    24  	"github.com/annchain/OG/ogdb"
    25  	"github.com/annchain/OG/types"
    26  	"math/big"
    27  	"sync"
    28  	"testing"
    29  	"time"
    30  )
    31  
    32  var (
    33  	testKey, _  = ogcrypto2.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    34  	testAddress = ogcrypto.PubkeyToAddress(testKey.PublicKey)
    35  )
    36  
    37  // Reduce some of the parameters to make the tester faster.
    38  func init() {
    39  	MaxForkAncestry = uint64(10000)
    40  	blockCacheItems = 1024
    41  	fsHeaderContCheck = 500 * time.Millisecond
    42  }
    43  
    44  // downloadTester is a test simulator for mocking out local block chain.
    45  type downloadTester struct {
    46  	downloader *Downloader
    47  
    48  	genesis *types.Sequencer // Genesis blocks used by the tester and peers
    49  	peerDb  ogdb.Database    // Database of the peers containing all data
    50  
    51  	ownHashes  types2.Hashes                               // Hash chain belonging to the tester
    52  	ownHeaders map[types2.Hash]*dagmessage.SequencerHeader // Headers belonging to the tester
    53  	ownBlocks  map[types2.Hash]*types.Sequencer            // Blocks belonging to the tester
    54  	ownChainTd map[types2.Hash]uint64                      // id
    55  
    56  	peerHashes   map[string]types2.Hashes                               // Hash chain belonging to different test peers
    57  	peerHeaders  map[string]map[types2.Hash]*dagmessage.SequencerHeader // Headers belonging to different test peers
    58  	peerBlocks   map[string]map[types2.Hash]*types.Sequencer            // Blocks belonging to different test peers
    59  	peerChainTds map[string]map[types2.Hash]*big.Int                    // Total difficulties of the blocks in the peer chains
    60  
    61  	peerMissingStates map[string]map[types2.Hash]bool // State entries that fast sync should not return
    62  
    63  	lock sync.RWMutex
    64  }
    65  
    66  // newTester creates a new downloader test mocker.
    67  func newTester() *downloadTester {
    68  	testdb := ogdb.NewMemDatabase()
    69  	genesis, _ := core2.DefaultGenesis(0)
    70  	tester := &downloadTester{
    71  		genesis:           genesis,
    72  		peerDb:            testdb,
    73  		ownHashes:         types2.Hashes{genesis.GetHash()},
    74  		ownHeaders:        map[types2.Hash]*dagmessage.SequencerHeader{genesis.GetHash(): types.NewSequencerHead(genesis.GetHash(), genesis.Number())},
    75  		ownBlocks:         map[types2.Hash]*types.Sequencer{genesis.GetHash(): genesis},
    76  		ownChainTd:        map[types2.Hash]uint64{genesis.GetHash(): genesis.Number()},
    77  		peerHashes:        make(map[string]types2.Hashes),
    78  		peerHeaders:       make(map[string]map[types2.Hash]*dagmessage.SequencerHeader),
    79  		peerBlocks:        make(map[string]map[types2.Hash]*types.Sequencer),
    80  		peerChainTds:      make(map[string]map[types2.Hash]*big.Int),
    81  		peerMissingStates: make(map[string]map[types2.Hash]bool),
    82  	}
    83  
    84  	tester.downloader = New(FullSync, nil, nil, nil)
    85  
    86  	return tester
    87  }
    88  
    89  func TestHeaderEuqual(t *testing.T) {
    90  	testHash, _ := types2.HexToHash("0xe6a07ee5c2fb20b07ec81f0b124b9b4428b8a96e99de01a440b5e0c4c25e22e3")
    91  	head := types.NewSequencerHead(testHash, 1447)
    92  	seq := &types.Sequencer{}
    93  	seq.Height = 1447
    94  	seq.Hash = testHash
    95  	seq.Issuer = &common.Address{}
    96  	seqHead := seq.GetHead()
    97  	if head == seqHead {
    98  		t.Fatal("head", head.StringFull(), " seqHead", seqHead.StringFull(), "struct  shoud not be  equal")
    99  	}
   100  	if !head.Equal(seqHead) {
   101  		t.Fatal("head", head.StringFull(), " seqHead", seqHead.StringFull(), "content  shoud  be  equal")
   102  	}
   103  	t.Log("head", head, " seqHead", seqHead, "equal")
   104  }