github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/masternode/masternode_backend_test.go (about)

     1  package eth
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"github.com/vantum/vantum/common"
     7  	"github.com/vantum/vantum/consensus/ethash"
     8  	"github.com/vantum/vantum/core"
     9  	"github.com/vantum/vantum/core/types/masternode"
    10  	"github.com/vantum/vantum/node"
    11  	"github.com/vantum/vantum/ethdb"
    12  	"github.com/vantum/vantum/params"
    13  	"github.com/vantum/vantum/core/vm"
    14  	"github.com/vantum/vantum/crypto"
    15  	"time"
    16  	"math/big"
    17  )
    18  
    19  const (
    20  	testInstance = "console-tester"
    21  	testAddress1 = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    22  )
    23  
    24  var (
    25  	key0, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    26  	key1, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
    27  	key2, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
    28  	addr0   = crypto.PubkeyToAddress(key0.PublicKey)
    29  	addr1   = crypto.PubkeyToAddress(key1.PublicKey)
    30  	addr2   = crypto.PubkeyToAddress(key2.PublicKey)
    31  )
    32  
    33  func newEtherrum() *Ethereum {
    34  	// Create a temporary storage for the node keys and initialize it
    35  	workspace, err := ioutil.TempDir("", "console-tester-")
    36  	if err != nil {
    37  		fmt.Printf("failed to create temporary keystore: %v", err)
    38  	}
    39  
    40  	// Create a networkless protocol stack and start an Ethereum service within
    41  	stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: testInstance})
    42  	if err != nil {
    43  		fmt.Printf("failed to create node: %v", err)
    44  	}
    45  	ethConf := &Config{
    46  		Genesis:   core.DeveloperGenesisBlock(15, common.Address{}),
    47  		Etherbase: common.HexToAddress(testAddress1),
    48  		Ethash: ethash.Config{
    49  			PowMode: ethash.ModeTest,
    50  		},
    51  	}
    52  
    53  	if err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return New(ctx, ethConf) }); err != nil {
    54  		fmt.Printf("failed to register Ethereum protocol: %v", err)
    55  	}
    56  	// Start the node and assemble the JavaScript console around it
    57  	if err = stack.Start(); err != nil {
    58  		fmt.Printf("failed to start test stack: %v", err)
    59  	}
    60  	_, err = stack.Attach()
    61  	if err != nil {
    62  		fmt.Printf("failed to attach to node: %v", err)
    63  	}
    64  
    65  	// Create the final tester and return
    66  	var ethereum *Ethereum
    67  	err = stack.Service(&ethereum)
    68  	if err != nil {
    69  		fmt.Printf("failed to as a service: %v", err)
    70  	}
    71  
    72  	ethereum.blockchain = newBlockChain()
    73  	return ethereum
    74  }
    75  func genNewgspec() core.Genesis {
    76  	return core.Genesis{
    77  		Config: params.TestChainConfig,
    78  		Alloc:  core.GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}},
    79  	}
    80  }
    81  
    82  func newBlockChain() *core.BlockChain {
    83  	db := ethdb.NewMemDatabase()
    84  	fmt.Println("etherzero", genNewgspec().Config)
    85  	//bc, _ := core.NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{})
    86  	cacheConfig := &core.CacheConfig{
    87  		Disabled:      true, // Whether to disable trie write caching (archive node)
    88  		TrieNodeLimit: 1,    // Memory limit (MB) at which to flush the current in-memory trie to disk
    89  		TrieTimeLimit: time.Duration(10),
    90  	}
    91  	vmConfig := vm.Config{
    92  		Debug:                   true,
    93  		NoRecursion:             true,
    94  		EnablePreimageRecording: true,
    95  	}
    96  	a := genNewgspec()
    97  	core.SetupGenesisBlock(db, &a)
    98  	chainman, _ := core.NewBlockChain(db, cacheConfig, a.Config, ethash.NewFaker(), vmConfig)
    99  	hash, number := common.Hash{0: 0xff}, uint64(314)
   100  	core.WriteCanonicalHash(db, hash, number)
   101  	return chainman
   102  }
   103  
   104  func returnMasternodeManager() *MasternodeManager {
   105  	//// initial the parameter may needed during this test function
   106  	//eth := newEtherrum()
   107  	return &MasternodeManager{
   108  		newPeerCh:   make(chan *peer),
   109  		masternodes: &masternode.MasternodeSet{},
   110  	}
   111  }
   112