예제 #1
0
파일: address.go 프로젝트: skycoin/skycoin
// Get return nil on not found.
func (au *addressUx) Get(address cipher.Address) ([]cipher.SHA256, error) {
	uxHashes := []cipher.SHA256{}
	bin := au.bkt.Get(address.Bytes())
	if bin == nil {
		return nil, nil
	}
	if err := encoder.DeserializeRaw(bin, &uxHashes); err != nil {
		return nil, err
	}
	return uxHashes, nil
}
예제 #2
0
파일: visor.go 프로젝트: JmAbuDabi/skycoin
// 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
}
예제 #3
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
}
예제 #4
0
파일: address.go 프로젝트: skycoin/skycoin
func (au *addressUx) Add(address cipher.Address, uxHash cipher.SHA256) error {
	hashes, err := au.Get(address)
	if err != nil {
		return err
	}

	if hashes == nil {
		bin := encoder.Serialize([]cipher.SHA256{uxHash})
		return au.bkt.Put(address.Bytes(), bin)
	}

	// check dup
	for _, u := range hashes {
		if u == uxHash {
			return nil
		}
	}

	hashes = append(hashes, uxHash)
	bin := encoder.Serialize(hashes)
	return au.bkt.Put(address.Bytes(), bin)
}