Esempio n. 1
0
func setup(t *testing.T) (*bolt.DB, func(), error) {
	dbName := fmt.Sprintf("%ddb", rand.Int31n(10000))
	teardown := func() {}
	tmpDir := filepath.Join(os.TempDir(), dbName)
	if err := os.MkdirAll(tmpDir, 0777); err != nil {
		return nil, teardown, err
	}

	util.DataDir = tmpDir
	db, err := historydb.NewDB()
	if err != nil {
		t.Fatal(err)
	}

	teardown = func() {
		db.Close()
		if err := os.RemoveAll(tmpDir); err != nil {
			panic(err)
		}
	}
	return db, teardown, nil
}
Esempio n. 2
0
// NewVisor Creates a normal Visor given a master's public key
func NewVisor(c VisorConfig) *Visor {
	logger.Debug("Creating new visor")
	// Make sure inputs are correct
	if c.IsMaster {
		logger.Debug("Visor is master")
		if c.BlockchainPubkey != cipher.PubKeyFromSecKey(c.BlockchainSeckey) {
			log.Panicf("Cannot run in master: invalid seckey for pubkey")
		}
	}

	db, err := historydb.NewDB()
	if err != nil {
		log.Panic(err)
	}

	history, err := historydb.New(db)
	if err != nil {
		log.Panic(err)
	}

	tree := blockdb.NewBlockTree()
	bc := NewBlockchain(tree, walker)

	bp := NewBlockchainParser(history, bc)
	bp.Start()

	bc.BindListener(bp.BlockListener)

	v := &Visor{
		Config:      c,
		Blockchain:  bc,
		blockSigs:   blockdb.NewBlockSigs(),
		Unconfirmed: NewUnconfirmedTxnPool(),
		history:     history,
		bcParser:    bp,
	}
	gb := bc.GetGenesisBlock()
	if gb == nil {
		v.GenesisPreconditions()
		b := v.Blockchain.CreateGenesisBlock(c.GenesisAddress, c.GenesisCoinVolume, c.GenesisTimestamp)
		gb = &b
		logger.Debug("create genesis block")

		// record the signature of genesis block
		if c.IsMaster {
			sb := v.SignBlock(*gb)
			v.blockSigs.Add(&sb)
		} else {
			v.blockSigs.Add(&coin.SignedBlock{
				Block: *gb,
				Sig:   c.GenesisSignature,
			})
		}
	}

	if err := v.Blockchain.VerifySigs(c.BlockchainPubkey, v.blockSigs); err != nil {
		log.Panicf("Invalid block signatures: %v", err)
	}

	// db, err := historydb.NewDB()
	// if err != nil {
	// 	log.Panic(err)
	// }

	// v.history, err = historydb.New(db)
	// if err != nil {
	// 	log.Panic(err)
	// }

	// init the blockchain parser instance
	// v.bcParser = NewBlockchainParser(v.history, v.Blockchain)
	// v.StartParser()
	return v
}