Exemplo n.º 1
0
func splitUnspent(t *testing.T, bc *Blockchain, ux coin.UxOut) coin.UxArray {
	tx := coin.Transaction{}
	hrs := ux.CoinHours(bc.Time())
	if hrs < 2 {
		log.Panic("Not enough hours, would generate duplicate output")
	}
	assert.Equal(t, ux.Body.Address, genAddress)
	tx.PushInput(ux.Hash())
	coinsA := ux.Body.Coins / 2
	coinsB := coinsA
	if (ux.Body.Coins/1e6)%2 == 1 {
		coinsA = (ux.Body.Coins - 1e6) / 2
		coinsB = coinsA + 1e6
	}
	tx.PushOutput(genAddress, coinsA, hrs/4)
	tx.PushOutput(genAddress, coinsB, hrs/2)
	tx.SignInputs([]cipher.SecKey{genSecret})
	tx.UpdateHeader()
	b, err := bc.NewBlockFromTransactions(coin.Transactions{tx}, bc.Time()+_incTime)
	assert.Nil(t, err)
	uxs, err := bc.ExecuteBlock(&b)
	assert.Nil(t, err)
	assert.Equal(t, len(uxs), 2)
	return uxs
}
Exemplo n.º 2
0
func NewReadableOutput(t coin.UxOut) ReadableOutput {
	return ReadableOutput{
		Hash:              t.Hash().Hex(),
		SourceTransaction: t.Body.SrcTransaction.Hex(),
		Address:           t.Body.Address.String(),
		Coins:             t.Body.Coins,
		Hours:             t.Body.Hours,
	}
}
Exemplo n.º 3
0
func makeTransactionForChainWithFee(t *testing.T, bc *Blockchain,
	fee uint64) coin.Transaction {
	ux := coin.UxOut{}
	hrs := uint64(100)
	for _, u := range bc.GetUnspent().Array() {
		if ux.CoinHours(bc.Time()) > hrs {
			ux = u
			break
		}
	}
	assert.Equal(t, ux.Body.Address, genAddress)
	tx, _ := makeTransactionForChainWithHoursFee(t, bc, ux, genSecret, hrs,
		fee)
	return tx
}
Exemplo n.º 4
0
func NewTransactionOutputJSON(ux coin.TransactionOutput, src_tx cipher.SHA256) TransactionOutputJSON {
	tmp := coin.UxOut{
		Body: coin.UxBody{
			SrcTransaction: src_tx,
			Address:        ux.Address,
			Coins:          ux.Coins,
			Hours:          ux.Hours,
		},
	}

	var o TransactionOutputJSON
	o.Hash = tmp.Hash().Hex()
	o.SourceTransaction = src_tx.Hex()

	o.Address = ux.Address.String()
	o.Coins = ux.Coins
	o.Hours = ux.Hours
	return o
}
Exemplo n.º 5
0
func makeTransactionForChainWithHoursFee(t *testing.T, bc *Blockchain,
	ux coin.UxOut, sec cipher.SecKey, hours, fee uint64) (coin.Transaction, cipher.SecKey) {
	chrs := ux.CoinHours(bc.Time())
	if chrs < hours+fee {
		log.Panicf("CoinHours underflow. Have %d, need at least %d", chrs,
			hours+fee)
	}
	assert.Equal(t, cipher.AddressFromPubKey(cipher.PubKeyFromSecKey(sec)), ux.Body.Address)
	knownUx, exists := bc.GetUnspent().Get(ux.Hash())
	assert.True(t, exists)
	assert.Equal(t, knownUx, ux)
	tx := coin.Transaction{}
	tx.PushInput(ux.Hash())
	p, newSec := cipher.GenerateKeyPair()
	addr := cipher.AddressFromPubKey(p)
	tx.PushOutput(addr, 1e6, hours)
	coinsOut := ux.Body.Coins - 1e6
	if coinsOut > 0 {
		tx.PushOutput(genAddress, coinsOut, chrs-hours-fee)
	}
	tx.SignInputs([]cipher.SecKey{sec})
	assert.Equal(t, len(tx.Sigs), 1)
	assert.Nil(t, cipher.ChkSig(ux.Body.Address, cipher.AddSHA256(tx.HashInner(), tx.In[0]), tx.Sigs[0]))
	tx.UpdateHeader()
	assert.Nil(t, tx.Verify())
	err := bc.VerifyTransaction(tx)
	assert.Nil(t, err)
	return tx, newSec
}
Exemplo n.º 6
0
func NewBalanceFromUxOut(headTime uint64, ux *coin.UxOut) Balance {
	return Balance{
		Coins: ux.Body.Coins,
		Hours: ux.CoinHours(headTime),
	}
}