Пример #1
0
// Creates a spend transaction and broadcasts it to the network
func (self *Visor) Spend(walletID wallet.WalletID, amt wallet.Balance,
	fee uint64, dest cipher.Address, pool *Pool) (coin.Transaction, error) {
	if self.Config.Disabled {
		return coin.Transaction{}, errors.New("Visor disabled")
	}
	logger.Info("Attempting to send %d coins, %d hours to %s with %d fee",
		amt.Coins, amt.Hours, dest.String(), fee)
	txn, err := self.Visor.Spend(walletID, amt, fee, dest)
	if err != nil {
		return txn, err
	}
	err, _ = self.Visor.RecordTxn(txn)
	if err == nil {
		self.broadcastTransaction(txn, pool)
	}
	return txn, err
}
Пример #2
0
// Creates a genesis block and applies it against chain
// Takes in time as parameter
func (self *Blockchain) CreateGenesisBlock(genesisAddress cipher.Address,
	timestamp uint64, genesisCoins uint64) Block {
	logger.Info("Creating new genesis block with address %s",
		genesisAddress.String())
	if len(self.Blocks) > 0 {
		log.Panic("Genesis block already created")
	}
	// Why is there a transaction in the genesis block?
	// Limits the special casing around genesis blocks:
	//      -Allows assumption that all UxOuts have a SrcTransaction that
	//      -can be found in the blockchain, without checking for genesis UxOut
	txn := Transaction{}
	txn.PushOutput(genesisAddress, genesisCoins, genesisCoins)
	body := BlockBody{Transactions{txn}}
	prevHash := cipher.SHA256{}
	head := BlockHeader{
		Time:       timestamp,
		BodyHash:   body.Hash(),
		PrevHash:   prevHash,
		BkSeq:      0,
		Version:    0,
		Fee:        0,
		UxSnapshot: getSnapshotHash(self.Unspent, prevHash),
	}
	b := Block{
		Head: head,
		Body: body,
	}
	self.Blocks = append(self.Blocks, b)
	// Genesis output
	ux := UxOut{
		Head: UxHead{
			Time:  b.Head.Time,
			BkSeq: 0,
		},
		Body: UxBody{
			SrcTransaction: txn.Hash(),
			Address:        genesisAddress,
			Coins:          genesisCoins,
			Hours:          genesisCoins, // Allocate 1 coin hour per coin
		},
	}
	self.Unspent.Add(ux)
	return b
}