本文整理汇总了Golang中github.com/skycoin/skycoin/src/coin.Transaction类的典型用法代码示例。如果您正苦于以下问题:Golang Transaction类的具体用法?Golang Transaction怎么用?Golang Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: InjectTxn
// InjectTxn adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (utp *UnconfirmedTxnPool) InjectTxn(bc *Blockchain,
t coin.Transaction) (error, bool) {
if err := t.Verify(); err != nil {
return err, false
}
if err := VerifyTransactionFee(bc, &t); err != nil {
return err, false
}
if err := bc.VerifyTransaction(t); err != nil {
return err, false
}
// Update if we already have this txn
h := t.Hash()
ut, ok := utp.Txns[h]
if ok {
now := util.Now()
ut.Received = now
ut.Checked = now
utp.Txns[h] = ut
return nil, true
}
// Add txn to index
unspent := bc.GetUnspent()
utp.Txns[h] = utp.createUnconfirmedTxn(unspent, t)
// Add predicted unspents
utp.Unspent[h] = coin.CreateUnspents(bc.Head().Head, t)
return nil, false
}
示例2: InjectTransaction
func (fg fakeGateway) InjectTransaction(txn coin.Transaction) (coin.Transaction, error) {
if _, v := fg.injectRawTxMap[txn.Hash().Hex()]; v {
return txn, nil
}
return txn, errors.New("inject transaction failed")
}
示例3: NewReadableTransaction
func NewReadableTransaction(t *coin.Transaction) ReadableTransaction {
sigs := make([]string, len(t.Sigs))
for i, _ := range t.Sigs {
sigs[i] = t.Sigs[i].Hex()
}
in := make([]string, len(t.In))
for i, _ := range t.In {
in[i] = t.In[i].Hex()
}
out := make([]ReadableTransactionOutput, len(t.Out))
for i, _ := range t.Out {
out[i] = NewReadableTransactionOutput(&t.Out[i])
}
return ReadableTransaction{
Length: t.Length,
Type: t.Type,
Hash: t.Hash().Hex(),
InnerHash: t.InnerHash.Hex(),
Sigs: sigs,
In: in,
Out: out,
}
}
示例4: RecordTxn
// Adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (self *UnconfirmedTxnPool) RecordTxn(bc *coin.Blockchain,
t coin.Transaction, addrs map[coin.Address]byte, maxSize int,
burnFactor uint64) (error, bool) {
if err := VerifyTransaction(bc, &t, maxSize, burnFactor); err != nil {
return err, false
}
if err := bc.VerifyTransaction(t); err != nil {
return err, false
}
// Update if we already have this txn
ut, ok := self.Txns[t.Hash()]
if ok {
now := util.Now()
ut.Received = now
ut.Checked = now
self.Txns[ut.Txn.Hash()] = ut
return nil, true
}
// Add txn to index
self.Txns[t.Hash()] = self.createUnconfirmedTxn(&bc.Unspent, t, addrs)
// Add predicted unspents
uxs := coin.CreateExpectedUnspents(t)
for i, _ := range uxs {
self.Unspent.Add(uxs[i])
}
return nil, false
}
示例5: InitTransaction
func InitTransaction() coin.Transaction {
var tx coin.Transaction
output := cipher.MustSHA256FromHex("043836eb6f29aaeb8b9bfce847e07c159c72b25ae17d291f32125e7f1912e2a0")
tx.PushInput(output)
for i := 0; i < 100; i++ {
addr := cipher.MustDecodeBase58Address(AddrList[i])
tx.PushOutput(addr, 1e12, 1) // 10e6*10e6
}
/*
seckeys := make([]cipher.SecKey, 1)
seckey := ""
seckeys[0] = cipher.MustSecKeyFromHex(seckey)
tx.SignInputs(seckeys)
*/
txs := make([]cipher.Sig, 1)
sig := "ed9bd7a31fe30b9e2d53b35154233dfdf48aaaceb694a07142f84cdf4f5263d21b723f631817ae1c1f735bea13f0ff2a816e24a53ccb92afae685fdfc06724de01"
txs[0] = cipher.MustSigFromHex(sig)
tx.Sigs = txs
tx.UpdateHeader()
err := tx.Verify()
if err != nil {
log.Panic(err)
}
log.Printf("signature= %s", tx.Sigs[0].Hex())
return tx
}
示例6: RecordTxn
// Adds a coin.Transaction to the pool, or updates an existing one's timestamps
// Returns an error if txn is invalid, and whether the transaction already
// existed in the pool.
func (self *UnconfirmedTxnPool) RecordTxn(bc *coin.Blockchain,
t coin.Transaction, addrs map[cipher.Address]byte, maxSize int,
burnFactor uint64) (error, bool) {
if err := VerifyTransaction(bc, &t, maxSize, burnFactor); err != nil {
return err, false
}
if err := bc.VerifyTransaction(t); err != nil {
return err, false
}
// Update if we already have this txn
h := t.Hash()
ut, ok := self.Txns[h]
if ok {
now := util.Now()
ut.Received = now
ut.Checked = now
self.Txns[h] = ut
return nil, true
}
// Add txn to index
self.Txns[h] = self.createUnconfirmedTxn(&bc.Unspent, t, addrs)
// Add predicted unspents
self.Unspent[h] = coin.CreateUnspents(bc.Head().Head, t)
return nil, false
}
示例7: CreateGenesisBlock
func (fbc *fakeBlockchain) CreateGenesisBlock(genesisAddr cipher.Address, genesisCoins, timestamp uint64) coin.Block {
txn := coin.Transaction{}
txn.PushOutput(genesisAddr, genesisCoins, genesisCoins)
body := coin.BlockBody{coin.Transactions{txn}}
prevHash := cipher.SHA256{}
head := coin.BlockHeader{
Time: timestamp,
BodyHash: body.Hash(),
PrevHash: prevHash,
BkSeq: 0,
Version: 0,
Fee: 0,
UxHash: coin.NewUnspentPool().GetUxHash(),
}
b := coin.Block{
Head: head,
Body: body,
}
// b.Body.Transactions[0].UpdateHeader()
fbc.blocks = append(fbc.blocks, b)
ux := coin.UxOut{
Head: coin.UxHead{
Time: timestamp,
BkSeq: 0,
},
Body: coin.UxBody{
SrcTransaction: txn.InnerHash, //user inner hash
Address: genesisAddr,
Coins: genesisCoins,
Hours: genesisCoins, // Allocate 1 coin hour per coin
},
}
fbc.unspent.Add(ux)
return b
}
示例8: VerifyTransaction
// VerifyTransaction checks that the inputs to the transaction exist,
// that the transaction does not create or destroy coins and that the
// signatures on the transaction are valid
func (bc Blockchain) VerifyTransaction(tx coin.Transaction) error {
//CHECKLIST: DONE: check for duplicate ux inputs/double spending
//CHECKLIST: DONE: check that inputs of transaction have not been spent
//CHECKLIST: DONE: check there are no duplicate outputs
// Q: why are coin hours based on last block time and not
// current time?
// A: no two computers will agree on system time. Need system clock
// indepedent timing that everyone agrees on. fee values would depend on
// local clock
// Check transaction type and length
// Check for duplicate outputs
// Check for duplicate inputs
// Check for invalid hash
// Check for no inputs
// Check for no outputs
// Check for non 1e6 multiple coin outputs
// Check for zero coin outputs
// Check valid looking signatures
if err := tx.Verify(); err != nil {
return err
}
uxIn, err := bc.unspent.GetMultiple(tx.In)
if err != nil {
return err
}
// Checks whether ux inputs exist,
// Check that signatures are allowed to spend inputs
if err := tx.VerifyInput(uxIn); err != nil {
return err
}
// Get the UxOuts we expect to have when the block is created.
uxOut := coin.CreateUnspents(bc.Head().Head, tx)
// Check that there are any duplicates within this set
if uxOut.HasDupes() {
return errors.New("Duplicate unspent outputs in transaction")
}
if DebugLevel1 {
// Check that new unspents don't collide with existing. This should
// also be checked in verifyTransactions
for i := range uxOut {
if bc.unspent.Has(uxOut[i].Hash()) {
return errors.New("New unspent collides with existing unspent")
}
}
}
// Check that no coins are lost, and sufficient coins and hours are spent
err = coin.VerifyTransactionSpending(bc.Time(), uxIn, uxOut)
if err != nil {
return err
}
return nil
}
示例9: assertValidUnconfirmed
func assertValidUnconfirmed(t *testing.T, txns map[cipher.SHA256]UnconfirmedTxn,
txn coin.Transaction) {
ut, ok := txns[txn.Hash()]
assert.True(t, ok)
assert.Equal(t, ut.Txn, txn)
assert.True(t, ut.Announced.IsZero())
assert.False(t, ut.Received.IsZero())
assert.False(t, ut.Checked.IsZero())
}
示例10: TestBlockchainVerifyBlock
func TestBlockchainVerifyBlock(t *testing.T) {
ft := FakeTree{}
bc := NewBlockchain(&ft, nil)
gb := bc.CreateGenesisBlock(genAddress, _genCoins, _genTime)
// Genesis block not valid after the fact
assert.NotNil(t, bc.verifyBlock(gb))
assert.Equal(t, bc.Len(), uint64(1))
_, ux := addBlockToBlockchain(t, bc)
assert.Equal(t, bc.Len(), uint64(3))
// Valid block
tx := coin.Transaction{}
tx.PushInput(ux.Hash())
tx.PushOutput(genAddress, ux.Body.Coins, ux.CoinHours(bc.Time()))
tx.SignInputs([]cipher.SecKey{genSecret})
tx.UpdateHeader()
b, err := bc.NewBlockFromTransactions(coin.Transactions{tx}, bc.Time()+_incTime)
assert.Equal(t, len(b.Body.Transactions), 1)
assert.Equal(t, len(b.Body.Transactions[0].Out), 1)
assert.Nil(t, err)
assert.Nil(t, bc.verifyBlock(b))
// Invalid block header
b.Head.BkSeq = gb.Head.BkSeq
assert.Equal(t, len(b.Body.Transactions), 1)
assert.Equal(t, len(b.Body.Transactions[0].Out), 1)
assertError(t, bc.verifyBlock(b), "BkSeq invalid")
// Invalid transactions, makes duplicate outputs
b.Head.BkSeq = bc.Head().Head.BkSeq + 1
b.Body.Transactions = append(b.Body.Transactions, b.Body.Transactions[0])
b.Head.BodyHash = b.HashBody()
assertError(t, bc.verifyBlock(b),
"Duplicate unspent output across transactions")
}
示例11: splitUnspent
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
}
示例12: TestCreateUnspents
func TestCreateUnspents(t *testing.T) {
ft := FakeTree{}
bc := NewBlockchain(&ft, nil)
bc.CreateGenesisBlock(genAddress, _genCoins, _genTime)
// 1 out
tx := coin.Transaction{}
tx.PushOutput(genAddress, 11e6, 255)
bh := coin.BlockHeader{
Time: tNow(),
BkSeq: uint64(1),
}
uxout := coin.CreateUnspents(bh, tx)
assert.Equal(t, len(uxout), 1)
assertValidUnspents(t, bh, tx, uxout)
// Multiple outs. Should work regardless of validity
tx = coin.Transaction{}
ux := makeUxOut(t)
tx.PushInput(ux.Hash())
tx.PushOutput(genAddress, 100, 150)
tx.PushOutput(genAddress, 200, 77)
bh.BkSeq++
uxout = coin.CreateUnspents(bh, tx)
assert.Equal(t, len(uxout), 2)
assertValidUnspents(t, bh, tx, uxout)
// No outs
tx = coin.Transaction{}
uxout = coin.CreateUnspents(bh, tx)
assertValidUnspents(t, bh, tx, uxout)
}
示例13: InjectTransaction
//InjectTransaction makes the blockchain server aware of raw transactions
//InjectTransaction inserts the transaction into the unconfirmed set
// TODO: lock for thread safety
func (self *Blockchain) InjectTransaction(txn coin.Transaction) error {
//strict filter would disallow transactions that cant be executed from unspent output set
if txn.Size() > MaxTransactionSize { //16 KB/max size
return errors.New("transaction over size limit")
}
if err := self.blockchain.VerifyTransaction(txn); err != nil {
return err
}
self.Unconfirmed.RecordTxn(txn)
return nil
}
示例14: assertValidUnconfirmed
func assertValidUnconfirmed(t *testing.T, txns map[coin.SHA256]UnconfirmedTxn,
txn coin.Transaction, didAnnounce, isOurReceive, isOurSpend bool) {
ut, ok := txns[txn.Hash()]
assert.True(t, ok)
assert.Equal(t, ut.Txn, txn)
assert.Equal(t, ut.IsOurReceive, isOurReceive)
assert.Equal(t, ut.IsOurSpend, isOurSpend)
assert.Equal(t, ut.Announced.IsZero(), !didAnnounce)
assert.False(t, ut.Received.IsZero())
assert.False(t, ut.Checked.IsZero())
}
示例15: assertValidUnspents
func assertValidUnspents(t *testing.T, bh coin.BlockHeader, tx coin.Transaction,
uxo coin.UxArray) {
assert.Equal(t, len(tx.Out), len(uxo))
for i, ux := range uxo {
assert.Equal(t, bh.Time, ux.Head.Time)
assert.Equal(t, bh.BkSeq, ux.Head.BkSeq)
assert.Equal(t, tx.Hash(), ux.Body.SrcTransaction)
assert.Equal(t, tx.Out[i].Address, ux.Body.Address)
assert.Equal(t, tx.Out[i].Coins, ux.Body.Coins)
assert.Equal(t, tx.Out[i].Hours, ux.Body.Hours)
}
}