Esempio n. 1
0
func TestVisorVerifySignedBlock(t *testing.T) {
	defer cleanupVisor()
	vc := newMasterVisorConfig(t)
	v := NewVisor(vc)
	w := v.Wallets[0]
	we := w.CreateEntry()

	// Master should verify its own blocks correctly
	txn, err := v.Spend(w.GetFilename(), wallet.Balance{1e6, 0}, 0, we.Address)
	assert.Nil(t, err)
	err, known := v.InjectTxn(txn)
	assert.Nil(t, err)
	assert.False(t, known)
	b, err := v.CreateBlock(uint64(util.UnixNow()))
	assert.Nil(t, err)
	assert.Nil(t, v.verifySignedBlock(&b))
	badb := b
	badb.Sig = cipher.Sig{}
	assert.NotNil(t, v.verifySignedBlock(&badb))

	// Non master should verify signed blocks generated by master
	mv := v
	v = setupVisorFromMaster(mv)
	assert.Nil(t, v.verifySignedBlock(&b))
	assert.NotNil(t, v.verifySignedBlock(&badb))
}
Esempio n. 2
0
// Creates a SignedBlock from pending transactions and executes it
func (self *Visor) CreateAndExecuteBlock() (SignedBlock, error) {
	sb, err := self.CreateBlock(uint64(util.UnixNow()))
	if err == nil {
		return sb, self.ExecuteSignedBlock(sb)
	} else {
		return sb, err
	}
}
Esempio n. 3
0
// CreateAndExecuteBlock creates a SignedBlock from pending transactions and executes it
func (vs *Visor) CreateAndExecuteBlock() (coin.SignedBlock, error) {
	sb, err := vs.CreateBlock(uint64(util.UnixNow()))
	if err == nil {
		return sb, vs.ExecuteSignedBlock(sb)
	}

	return sb, err
}
Esempio n. 4
0
func addSignedBlocks(t *testing.T, v *Visor, n int) []SignedBlock {
	sbs := make([]SignedBlock, n)
	now := uint64(util.UnixNow())
	for i := 0; i < n; i++ {
		sbs[i] = addSignedBlockAt(t, v, now+1+uint64(i))
	}
	return sbs
}
Esempio n. 5
0
func (self *Visor) CreateFreshGenesisBlock() (SignedBlock, error) {
	if len(self.blockchain.Blocks) != 0 || len(self.blockSigs.Sigs) != 0 {
		log.Panic("Blockchain already has genesis")
	}
	gb := self.blockchain.CreateGenesisBlock(self.Config.MasterKeys.Address,
		uint64(util.UnixNow()), self.Config.GenesisCoinVolume)
	sb := self.SignBlock(gb)
	if err := self.verifySignedBlock(&sb); err != nil {
		log.Panic("Signed a fresh genesis block, but its invalid: %v", err)
	}
	self.blockSigs.record(&sb)
	return sb, nil
}
Esempio n. 6
0
func transferCoinsAdvanced(mv *Visor, v *Visor, b Balance, fee uint64,
	addr coin.Address) error {
	tx, err := mv.Spend(b, fee, addr)
	if err != nil {
		return err
	}
	mv.RecordTxn(tx)
	now := uint64(util.UnixNow())
	if len(mv.blockchain.Blocks) > 0 {
		now = mv.blockchain.Time() + 1
	}
	sb, err := mv.CreateBlock(now)
	if err != nil {
		return err
	}
	err = mv.ExecuteSignedBlock(sb)
	if err != nil {
		return err
	}
	return v.ExecuteSignedBlock(sb)
}
Esempio n. 7
0
func transferCoinsAdvanced(mv *Visor, v *Visor, b wallet.Balance, fee uint64,
	addr cipher.Address) error {
	tx, err := mv.Spend(mv.Wallets[0].GetFilename(), b, fee, addr)
	if err != nil {
		return err
	}
	mv.InjectTxn(tx)
	now := uint64(util.UnixNow())
	if len(mv.blockchain.Blocks) > 0 {
		now = mv.blockchain.Time() + 1
	}
	sb, err := mv.CreateBlock(now)
	if err != nil {
		return err
	}
	err = mv.ExecuteSignedBlock(sb)
	if err != nil {
		return err
	}
	return v.ExecuteSignedBlock(sb)
}
Esempio n. 8
0
func makeBlocks(mv *visor.Visor, n int) ([]visor.SignedBlock, error) {
	return makeMoreBlocks(mv, n, uint64(util.UnixNow()))
}
Esempio n. 9
0
func makeBlocks(t *testing.T, mv *Visor, n int) []SignedBlock {
	return makeMoreBlocks(t, mv, n, uint64(util.UnixNow()))
}
Esempio n. 10
0
func TestExecuteSignedBlock(t *testing.T) {
	defer cleanupVisor()
	cleanupVisor()
	we := wallet.NewWalletEntry()
	vc := newMasterVisorConfig(t)
	v := NewVisor(vc)
	wid := v.Wallets[0].GetFilename()
	assert.Equal(t, len(v.Unconfirmed.Txns), 0)
	tx, err := v.Spend(wid, wallet.Balance{1e6, 0}, 0, we.Address)
	assert.Nil(t, err)
	err, known := v.InjectTxn(tx)
	assert.Nil(t, err)
	assert.False(t, known)
	assert.Equal(t, len(v.Unconfirmed.Txns), 1)
	assert.Equal(t, len(v.blockSigs.Sigs), 1)
	now := uint64(util.UnixNow())

	// Invalid signed block
	sb, err := v.CreateBlock(now)
	assert.Equal(t, len(v.blockSigs.Sigs), 1)
	assert.Nil(t, err)
	sb.Sig = cipher.Sig{}
	err = v.ExecuteSignedBlock(sb)
	assert.NotNil(t, err)
	assert.Equal(t, len(v.Unconfirmed.Txns), 1)
	assert.Equal(t, len(v.blockSigs.Sigs), 1)

	// Invalid block
	sb, err = v.CreateBlock(now)
	assert.Nil(t, err)
	// TODO -- empty BodyHash is being accepted, fix blockchain verification
	sb.Block.Head.BodyHash = cipher.SHA256{}
	sb.Block.Body.Transactions = make(coin.Transactions, 0)
	sb = v.SignBlock(sb.Block)
	err = v.ExecuteSignedBlock(sb)
	assert.NotNil(t, err)
	assert.Equal(t, len(v.Unconfirmed.Txns), 1)
	assert.Equal(t, len(v.blockSigs.Sigs), 1)

	// Valid block
	sb, err = v.CreateBlock(now)
	assert.Nil(t, err)
	err = v.ExecuteSignedBlock(sb)
	assert.Nil(t, err)
	assert.Equal(t, len(v.blockSigs.Sigs), 2)
	assert.Equal(t, v.blockSigs.Sigs[uint64(1)], sb.Sig)
	assert.Equal(t, v.blockchain.Blocks[1], sb.Block)
	assert.Equal(t, len(v.Unconfirmed.Txns), 0)

	// Test a valid block created by a master but executing in non master
	vc2, mv := setupVisorConfig()
	v2 := NewVisor(vc2)
	w := v2.Wallets[0]
	addr := w.GetAddresses()[0]
	tx, err = mv.Spend(mv.Wallets[0].GetFilename(), wallet.Balance{1e6, 0}, 0, addr)
	assert.Nil(t, err)
	err, known = mv.InjectTxn(tx)
	assert.Nil(t, err)
	assert.False(t, known)
	sb, err = mv.CreateAndExecuteBlock()
	assert.Nil(t, err)
	err = v2.ExecuteSignedBlock(sb)
	assert.Nil(t, err)
	assert.Equal(t, len(v2.blockSigs.Sigs), 2)
	assert.Equal(t, v2.blockSigs.Sigs[uint64(1)], sb.Sig)
	assert.Equal(t, v2.blockchain.Blocks[1], sb.Block)
	assert.Equal(t, len(v2.Unconfirmed.Txns), 0)
}
Esempio n. 11
0
func addSignedBlock(t *testing.T, v *Visor) SignedBlock {
	return addSignedBlockAt(t, v, uint64(util.UnixNow()))
}