func TestProcessGenesisBlock(t *testing.T) { db, teardown, err := setup(t) if err != nil { t.Fatal(err) } defer teardown() // bc := coin.NewBlockchain(&ft, nil) bc := newBlockchain() gb := bc.CreateGenesisBlock(genAddress, _genCoins, _genTime) hisDB, err := historydb.New(db) if err != nil { t.Fatal(err) } if err := hisDB.ProcessBlock(&gb); err != nil { t.Fatal(err) } // check transactions bucket. var tx historydb.Transaction txHash := gb.Body.Transactions[0].Hash() if err := getBucketValue(db, transactionBkt, txHash[:], &tx); err != nil { t.Fatal(err) } assert.Equal(t, tx.Tx, gb.Body.Transactions[0]) // check address in outID := []cipher.SHA256{} if err := getBucketValue(db, addressInBkt, genAddress.Bytes(), &outID); err != nil { t.Fatal(err) } ux := bc.GetUnspent().Array()[0] assert.Equal(t, outID[0], ux.Hash()) // check outputs output := historydb.UxOut{} if err := getBucketValue(db, outputBkt, outID[0][:], &output); err != nil { t.Fatal(err) } assert.Equal(t, output.Out, ux) }
// 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 }
func TestProcessBlock(t *testing.T) { db, teardown, err := setup(t) if err != nil { t.Fatal(err) } defer teardown() bc := newBlockchain() gb := bc.CreateGenesisBlock(genAddress, _genCoins, _genTime) // create historydb hisDB, err := historydb.New(db) if err != nil { t.Fatal(err) } if err := hisDB.ProcessBlock(&gb); err != nil { t.Fatal(err) } /* |-2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS genesisAddr ==>| |-2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS |-222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm ==>| |-222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm */ testData := []testData{ { PreBlockHash: gb.HashHeader(), Vin: txIn{ SigKey: genSecret.Hex(), Addr: genAddress.String(), TxID: gb.Body.Transactions[0].Hash(), BlockSeq: 0, }, Vouts: []txOut{ { ToAddr: "2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS", Coins: 10e6, Hours: 100, }, { ToAddr: "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm", Coins: _genCoins - 10e6, Hours: 400, }, }, AddrInNum: map[string]int{ "2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS": 1, "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm": 1, }, AddrOutNum: map[string]int{ genAddress.String(): 1, }, }, { Vin: txIn{ Addr: "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm", SigKey: "62f4d675d991c41a2819d908a4fcf4ba44ff0c31564039e80508c9d68197f90c", BlockSeq: 1, }, Vouts: []txOut{ { ToAddr: "2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS", Coins: 10e6, Hours: 100, }, { ToAddr: "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm", Coins: 1000e6 - 20e6, Hours: 100, }, }, AddrInNum: map[string]int{ "2RxP5N26GhDqHrP6SK45ZzEMSmSpeUeWxsS": 2, "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm": 2, }, AddrOutNum: map[string]int{ "222uMeCeL1PbkJGZJDgAz5sib2uisv9hYUm": 1, }, }, } testEngine(t, testData, bc, hisDB, db) }