diff options
author | Sonic <sonic@dexon.org> | 2018-11-26 18:24:28 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:54 +0800 |
commit | fdb682a05c9d45d6d8232994330094a051fc39cc (patch) | |
tree | 624fde6e0eb516386ac5f7361c3729829145753e /dex/downloader/testchain_test.go | |
parent | 2ffc207955af78698d5ce6b9bc2d1df56b97fbf5 (diff) | |
download | dexon-fdb682a05c9d45d6d8232994330094a051fc39cc.tar.gz dexon-fdb682a05c9d45d6d8232994330094a051fc39cc.tar.zst dexon-fdb682a05c9d45d6d8232994330094a051fc39cc.zip |
dex/downloader: fix tests
Diffstat (limited to 'dex/downloader/testchain_test.go')
-rw-r--r-- | dex/downloader/testchain_test.go | 87 |
1 files changed, 69 insertions, 18 deletions
diff --git a/dex/downloader/testchain_test.go b/dex/downloader/testchain_test.go index e73bed513..5dcc1f403 100644 --- a/dex/downloader/testchain_test.go +++ b/dex/downloader/testchain_test.go @@ -19,12 +19,13 @@ package downloader import ( "fmt" "math/big" - "sync" "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/consensus/ethash" "github.com/dexon-foundation/dexon/core" + "github.com/dexon-foundation/dexon/core/state" "github.com/dexon-foundation/dexon/core/types" + "github.com/dexon-foundation/dexon/core/vm" "github.com/dexon-foundation/dexon/crypto" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/params" @@ -35,23 +36,58 @@ var ( testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") testAddress = crypto.PubkeyToAddress(testKey.PublicKey) testDB = ethdb.NewMemDatabase() - testGenesis = core.GenesisBlockForTesting(testDB, testAddress, big.NewInt(1000000000)) + testGenesis = genesisBlockForTesting(testDB, testAddress, big.NewInt(1000000000)) ) // The common prefix of all test chains: var testChainBase = newTestChain(blockCacheItems+200, testGenesis) -// Different forks on top of the base chain: -var testChainForkLightA, testChainForkLightB, testChainForkHeavy *testChain +func genesisBlockForTesting(db ethdb.Database, + addr common.Address, balance *big.Int) *types.Block { + var ( + // genesis node set + nodekey1, _ = crypto.HexToECDSA("3cf5bdee098cc34536a7b0e80d85e07a380efca76fc12136299b9e5ba24193c8") + nodekey2, _ = crypto.HexToECDSA("96c9f1435d53577db18d45411326311529a0e8affb19218e27f65769a482c0fb") + nodekey3, _ = crypto.HexToECDSA("b25e955e30dd87cbaec83287beea6ec9c4c72498bc66905590756bf48da5d1fc") + nodekey4, _ = crypto.HexToECDSA("35577f65312f4a5e0b5391f5385043a6bc7b51fa4851a579e845b5fea33efded") + nodeaddr1 = crypto.PubkeyToAddress(nodekey1.PublicKey) + nodeaddr2 = crypto.PubkeyToAddress(nodekey2.PublicKey) + nodeaddr3 = crypto.PubkeyToAddress(nodekey3.PublicKey) + nodeaddr4 = crypto.PubkeyToAddress(nodekey4.PublicKey) + ) -func init() { - var forkLen = int(MaxForkAncestry + 50) - var wg sync.WaitGroup - wg.Add(3) - go func() { testChainForkLightA = testChainBase.makeFork(forkLen, false, 1); wg.Done() }() - go func() { testChainForkLightB = testChainBase.makeFork(forkLen, false, 2); wg.Done() }() - go func() { testChainForkHeavy = testChainBase.makeFork(forkLen, true, 3); wg.Done() }() - wg.Wait() + ether := big.NewInt(1e18) + gspec := core.Genesis{ + Config: params.TestnetChainConfig, + Alloc: core.GenesisAlloc{ + addr: { + Balance: balance, + Staked: new(big.Int), + }, + nodeaddr1: { + Balance: new(big.Int).Mul(big.NewInt(1000), ether), + Staked: new(big.Int).Mul(big.NewInt(500), ether), + PublicKey: crypto.FromECDSAPub(&nodekey1.PublicKey), + }, + nodeaddr2: { + Balance: new(big.Int).Mul(big.NewInt(1000), ether), + Staked: new(big.Int).Mul(big.NewInt(500), ether), + PublicKey: crypto.FromECDSAPub(&nodekey2.PublicKey), + }, + nodeaddr3: { + Balance: new(big.Int).Mul(big.NewInt(1000), ether), + Staked: new(big.Int).Mul(big.NewInt(500), ether), + PublicKey: crypto.FromECDSAPub(&nodekey3.PublicKey), + }, + nodeaddr4: { + Balance: new(big.Int).Mul(big.NewInt(1000), ether), + Staked: new(big.Int).Mul(big.NewInt(500), ether), + PublicKey: crypto.FromECDSAPub(&nodekey4.PublicKey), + }, + }, + } + genesis := gspec.MustCommit(db) + return genesis } type testChain struct { @@ -118,7 +154,7 @@ func (tc *testChain) generate(n int, seed byte, parent *types.Block, heavy bool) // start := time.Now() // defer func() { fmt.Printf("test chain generated in %v\n", time.Since(start)) }() - blocks, receipts := core.GenerateChain(params.TestChainConfig, parent, ethash.NewFaker(), testDB, n, func(i int, block *core.BlockGen) { + blocks, receipts := core.GenerateChain(params.TestnetChainConfig, parent, ethash.NewFaker(), testDB, n, func(i int, block *core.BlockGen) { block.SetCoinbase(common.Address{seed}) // If a heavy chain is requested, delay blocks to raise difficulty if heavy { @@ -126,7 +162,7 @@ func (tc *testChain) generate(n int, seed byte, parent *types.Block, heavy bool) } // Include transactions to the miner to make blocks more interesting. if parent == tc.genesis && i%22 == 0 { - signer := types.MakeSigner(params.TestChainConfig, block.Number()) + signer := types.MakeSigner(params.TestnetChainConfig, block.Number()) tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddress), common.Address{seed}, big.NewInt(1000), params.TxGas, nil, nil), signer, testKey) if err != nil { panic(err) @@ -171,22 +207,37 @@ func (tc *testChain) td(hash common.Hash) *big.Int { } // headersByHash returns headers in ascending order from the given hash. -func (tc *testChain) headersByHash(origin common.Hash, amount int, skip int) []*types.Header { +func (tc *testChain) headersByHash(origin common.Hash, amount int, skip int) []*types.HeaderWithGovState { num, _ := tc.hashToNumber(origin) return tc.headersByNumber(num, amount, skip) } // headersByNumber returns headers in ascending order from the given number. -func (tc *testChain) headersByNumber(origin uint64, amount int, skip int) []*types.Header { - result := make([]*types.Header, 0, amount) +func (tc *testChain) headersByNumber(origin uint64, amount int, skip int) []*types.HeaderWithGovState { + result := make([]*types.HeaderWithGovState, 0, amount) for num := origin; num < uint64(len(tc.chain)) && len(result) < amount; num += uint64(skip) + 1 { if header, ok := tc.headerm[tc.chain[int(num)]]; ok { - result = append(result, header) + result = append(result, &types.HeaderWithGovState{Header: header}) } } return result } +func (tc *testChain) govStateByHash(hash common.Hash) *types.GovState { + header := tc.headersByHash(hash, 1, 0)[0] + statedb, err := state.New(header.Root, state.NewDatabase(testDB)) + if err != nil { + panic(err) + } + + govState, err := state.GetGovState(statedb, header.Header, + vm.GovernanceContractAddress) + if err != nil { + panic(err) + } + return govState +} + // receipts returns the receipts of the given block hashes. func (tc *testChain) receipts(hashes []common.Hash) [][]*types.Receipt { results := make([][]*types.Receipt, 0, len(hashes)) |