本文整理匯總了Golang中github.com/NebulousLabs/Sia/types.Transaction類的典型用法代碼示例。如果您正苦於以下問題:Golang Transaction類的具體用法?Golang Transaction怎麽用?Golang Transaction使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Transaction類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestApplySiacoinOutputs
// TestApplySiacoinOutputs probes the applySiacoinOutput method of the
// consensus set.
func TestApplySiacoinOutputs(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
cst, err := createConsensusSetTester("TestApplySiacoinOutputs")
if err != nil {
t.Fatal(err)
}
// Create a block node to use with application.
bn := new(blockNode)
// Apply a transaction with a single siacoin output.
txn := types.Transaction{
SiacoinOutputs: []types.SiacoinOutput{{}},
}
cst.cs.applySiacoinOutputs(bn, txn)
scoid := txn.SiacoinOutputID(0)
_, exists := cst.cs.siacoinOutputs[scoid]
if !exists {
t.Error("Failed to create siacoin output")
}
if len(cst.cs.siacoinOutputs) != 3 { // 3 because createConsensusSetTester has 2 initially.
t.Error("siacoin outputs not correctly updated")
}
if len(bn.siacoinOutputDiffs) != 1 {
t.Error("block node was not updated for single element transaction")
}
if bn.siacoinOutputDiffs[0].Direction != modules.DiffApply {
t.Error("wrong diff direction applied when creating a siacoin output")
}
if bn.siacoinOutputDiffs[0].ID != scoid {
t.Error("wrong id used when creating a siacoin output")
}
// Apply a transaction with 2 siacoin outputs.
txn = types.Transaction{
SiacoinOutputs: []types.SiacoinOutput{
{Value: types.NewCurrency64(1)},
{Value: types.NewCurrency64(2)},
},
}
cst.cs.applySiacoinOutputs(bn, txn)
scoid0 := txn.SiacoinOutputID(0)
scoid1 := txn.SiacoinOutputID(1)
_, exists = cst.cs.siacoinOutputs[scoid0]
if !exists {
t.Error("Failed to create siacoin output")
}
_, exists = cst.cs.siacoinOutputs[scoid1]
if !exists {
t.Error("Failed to create siacoin output")
}
if len(cst.cs.siacoinOutputs) != 5 { // 5 because createConsensusSetTester has 2 initially.
t.Error("siacoin outputs not correctly updated")
}
if len(bn.siacoinOutputDiffs) != 3 {
t.Error("block node was not updated correctly")
}
}
示例2: applyFileContracts
// applyFileContracts iterates through all of the file contracts in a
// transaction and applies them to the state, updating the diffs in the block
// node.
func (cs *State) applyFileContracts(bn *blockNode, t types.Transaction) {
for i, fc := range t.FileContracts {
// Sanity check - the file contract should not exists within the state.
fcid := t.FileContractID(i)
if build.DEBUG {
_, exists := cs.fileContracts[fcid]
if exists {
panic(ErrMisuseApplyFileContracts)
}
}
fcd := modules.FileContractDiff{
Direction: modules.DiffApply,
ID: fcid,
FileContract: fc,
}
bn.fileContractDiffs = append(bn.fileContractDiffs, fcd)
cs.commitFileContractDiff(fcd, modules.DiffApply)
// Get the portion of the contract that goes into the siafund pool and
// add it to the siafund pool.
sfpd := modules.SiafundPoolDiff{
Previous: cs.siafundPool,
Adjusted: cs.siafundPool.Add(fc.Tax()),
}
bn.siafundPoolDiffs = append(bn.siafundPoolDiffs, sfpd)
cs.commitSiafundPoolDiff(sfpd, modules.DiffApply)
}
return
}
示例3: validTransaction
// validTransaction checks that all fields are valid within the current
// consensus state. If not an error is returned.
func validTransaction(tx *bolt.Tx, t types.Transaction) error {
// StandaloneValid will check things like signatures and properties that
// should be inherent to the transaction. (storage proof rules, etc.)
err := t.StandaloneValid(blockHeight(tx))
if err != nil {
return err
}
// Check that each portion of the transaction is legal given the current
// consensus set.
err = validSiacoins(tx, t)
if err != nil {
return err
}
err = validStorageProofs(tx, t)
if err != nil {
return err
}
err = validFileContractRevisions(tx, t)
if err != nil {
return err
}
err = validSiafunds(tx, t)
if err != nil {
return err
}
return nil
}
示例4: validUnconfirmedTransaction
// validUnconfirmedTransaction checks that the transaction would be valid in a
// block that contained all of the other unconfirmed transactions.
func (tp *TransactionPool) validUnconfirmedTransaction(t types.Transaction) (err error) {
// Check that the transaction follows 'Standard.md' guidelines.
err = tp.IsStandardTransaction(t)
if err != nil {
return
}
// StandaloneValid will check things like signatures and properties that
// should be inherent to the transaction. (storage proof rules, etc.)
err = t.StandaloneValid(tp.consensusSetHeight)
if err != nil {
return
}
// Check the validity of the componenets in the context of the confirmed
// and unconfirmed set.
err = tp.validUnconfirmedSiacoins(t)
if err != nil {
return
}
err = tp.validUnconfirmedStorageProofs(t)
if err != nil {
return
}
err = tp.validUnconfirmedFileContractRevisions(t)
if err != nil {
return
}
err = tp.validUnconfirmedSiafunds(t)
if err != nil {
return
}
return
}
示例5: validSiacoins
// validSiacoins checks that the siacoin inputs and outputs are valid in the
// context of the current consensus set.
func validSiacoins(tx *bolt.Tx, t types.Transaction) error {
scoBucket := tx.Bucket(SiacoinOutputs)
var inputSum types.Currency
for _, sci := range t.SiacoinInputs {
// Check that the input spends an existing output.
scoBytes := scoBucket.Get(sci.ParentID[:])
if scoBytes == nil {
return errMissingSiacoinOutput
}
// Check that the unlock conditions match the required unlock hash.
var sco types.SiacoinOutput
err := encoding.Unmarshal(scoBytes, &sco)
if build.DEBUG && err != nil {
panic(err)
}
if sci.UnlockConditions.UnlockHash() != sco.UnlockHash {
return errWrongUnlockConditions
}
inputSum = inputSum.Add(sco.Value)
}
if inputSum.Cmp(t.SiacoinOutputSum()) != 0 {
return errSiacoinInputOutputMismatch
}
return nil
}
示例6: applySiacoinOutputs
// applySiacoinOutputs takes all of the siacoin outputs in a transaction and
// applies them to the state, updating the diffs in the processed block.
func applySiacoinOutputs(tx *bolt.Tx, pb *processedBlock, t types.Transaction) {
// Add all siacoin outputs to the unspent siacoin outputs list.
for i, sco := range t.SiacoinOutputs {
scoid := t.SiacoinOutputID(uint64(i))
scod := modules.SiacoinOutputDiff{
Direction: modules.DiffApply,
ID: scoid,
SiacoinOutput: sco,
}
pb.SiacoinOutputDiffs = append(pb.SiacoinOutputDiffs, scod)
commitSiacoinOutputDiff(tx, scod, modules.DiffApply)
}
}
示例7: applySiafundOutputs
// applySiafundOutput applies a siafund output to the consensus set.
func applySiafundOutputs(tx *bolt.Tx, pb *processedBlock, t types.Transaction) {
for i, sfo := range t.SiafundOutputs {
sfoid := t.SiafundOutputID(uint64(i))
sfo.ClaimStart = getSiafundPool(tx)
sfod := modules.SiafundOutputDiff{
Direction: modules.DiffApply,
ID: sfoid,
SiafundOutput: sfo,
}
pb.SiafundOutputDiffs = append(pb.SiafundOutputDiffs, sfod)
commitSiafundOutputDiff(tx, sfod, modules.DiffApply)
}
}
示例8: removeSiafundOutputs
// removeSiafundOutputs removes all of the siafund outputs of a transaction
// from the unconfirmed consensus set.
func (tp *TransactionPool) removeSiafundOutputs(t types.Transaction) {
for i, _ := range t.SiafundOutputs {
// Sanity check - the output should exist in the unconfirmed set as
// there is no dependent transaction which could have spent the output.
sfoid := t.SiafundOutputID(i)
if build.DEBUG {
_, exists := tp.siafundOutputs[sfoid]
if !exists {
panic("trying to remove nonexisting siafund output from unconfirmed set")
}
}
delete(tp.siafundOutputs, sfoid)
}
}
示例9: removeSiacoinOutputs
// removeSiacoinOutputs removes all of the siacoin outputs of a transaction
// from the unconfirmed consensus set.
func (tp *TransactionPool) removeSiacoinOutputs(t types.Transaction) {
for i, _ := range t.SiacoinOutputs {
scoid := t.SiacoinOutputID(i)
// Sanity check - the output should exist in the unconfirmed set as
// there should be no transaction dependents who have spent the output.
if build.DEBUG {
_, exists := tp.siacoinOutputs[scoid]
if !exists {
panic("trying to delete missing siacoin output")
}
}
delete(tp.siacoinOutputs, scoid)
}
}
示例10: removeFileContracts
// removeFileContracts removes all of the file contracts of a transaction from
// the unconfirmed consensus set.
func (tp *TransactionPool) removeFileContracts(t types.Transaction) {
for i, _ := range t.FileContracts {
fcid := t.FileContractID(i)
// Sanity check - file contract should be in the unconfirmed set as
// there should be no dependent transactions who have terminated the
// contract.
if build.DEBUG {
_, exists := tp.fileContracts[fcid]
if !exists {
panic("trying to remove missing file contract")
}
}
delete(tp.fileContracts, fcid)
}
}
示例11: applySiafundOutputs
// applySiafundOutput applies a siafund output to the consensus set.
func (cs *ConsensusSet) applySiafundOutputs(tx *bolt.Tx, pb *processedBlock, t types.Transaction) error {
for i, sfo := range t.SiafundOutputs {
sfoid := t.SiafundOutputID(i)
sfo.ClaimStart = getSiafundPool(tx)
sfod := modules.SiafundOutputDiff{
Direction: modules.DiffApply,
ID: sfoid,
SiafundOutput: sfo,
}
pb.SiafundOutputDiffs = append(pb.SiafundOutputDiffs, sfod)
err := cs.commitTxSiafundOutputDiff(tx, sfod, modules.DiffApply)
if err != nil {
return err
}
}
return nil
}
示例12: applySiacoinOutputs
// applySiacoinOutputs takes all of the siacoin outputs in a transaction and
// applies them to the state, updating the diffs in the processed block.
func (cs *ConsensusSet) applySiacoinOutputs(tx *bolt.Tx, pb *processedBlock, t types.Transaction) error {
// Add all siacoin outputs to the unspent siacoin outputs list.
scoBucket := tx.Bucket(SiacoinOutputs)
for i, sco := range t.SiacoinOutputs {
scoid := t.SiacoinOutputID(i)
scod := modules.SiacoinOutputDiff{
Direction: modules.DiffApply,
ID: scoid,
SiacoinOutput: sco,
}
pb.SiacoinOutputDiffs = append(pb.SiacoinOutputDiffs, scod)
err := cs.commitBucketSiacoinOutputDiff(scoBucket, scod, modules.DiffApply)
if err != nil {
return err
}
}
return nil
}
示例13: reviseObligation
// reviseObligation takes a file contract revision + transaction and applies it
// to an existing obligation.
func (h *Host) reviseObligation(revisionTransaction types.Transaction) {
// Sanity checks - there should be exactly one revision in the transaction,
// and that revision should correspond to a known obligation.
fcrlen := len(revisionTransaction.FileContractRevisions)
if fcrlen != 1 {
h.log.Critical("reviseObligation: revisionTransaction has the wrong number of revisions:", fcrlen)
return
}
obligation, exists := h.obligationsByID[revisionTransaction.FileContractRevisions[0].ParentID]
if !exists {
h.log.Critical("reviseObligation: revisionTransaction has no corresponding obligation")
return
}
// Expensive Sanity check - obligation being added should have a valid tranasction.
if build.DEBUG {
err := revisionTransaction.StandaloneValid(h.blockHeight)
if err != nil {
h.log.Critical("invalid transaction is being added in an obligation")
}
}
// Update the host's statistics.
h.spaceRemaining += int64(obligation.fileSize())
h.spaceRemaining -= int64(revisionTransaction.FileContractRevisions[0].NewFileSize)
h.anticipatedRevenue = h.anticipatedRevenue.Sub(obligation.value())
h.anticipatedRevenue = h.anticipatedRevenue.Add(revisionTransaction.FileContractRevisions[0].NewValidProofOutputs[1].Value)
// The host needs to verify that the revision transaction made it into the
// blockchain.
h.addActionItem(h.blockHeight+resubmissionTimeout, obligation)
// Add the revision to the obligation
obligation.RevisionTransaction = revisionTransaction
obligation.RevisionConfirmed = false
err := obligation.isSane()
if err != nil {
h.log.Critical("reviseObligation: obligation is not sane: " + err.Error())
}
err = h.save()
if err != nil {
h.log.Println("WARN: failed to save host:", err)
}
}
示例14: TestDuplicateStorageProof
// TestDuplicateStorageProof applies a storage proof which has already been
// applied.
func TestDuplicateStorageProof(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
cst, err := createConsensusSetTester("TestDuplicateStorageProof")
if err != nil {
t.Fatal(err)
}
// Create a block node.
bn := new(blockNode)
bn.height = cst.cs.height()
// Create a file contract for the storage proof to prove.
txn0 := types.Transaction{
FileContracts: []types.FileContract{
{
Payout: types.NewCurrency64(300e3),
ValidProofOutputs: []types.SiacoinOutput{
{Value: types.NewCurrency64(290e3)},
},
},
},
}
cst.cs.applyFileContracts(bn, txn0)
fcid := txn0.FileContractID(0)
// Apply a single storage proof.
txn1 := types.Transaction{
StorageProofs: []types.StorageProof{{ParentID: fcid}},
}
cst.cs.applyStorageProofs(bn, txn1)
// Trigger a panic by applying the storage proof again.
defer func() {
r := recover()
if r != ErrDuplicateValidProofOutput {
t.Error("failed to trigger ErrDuplicateValidProofOutput:", r)
}
}()
cst.cs.applyFileContracts(bn, txn0) // File contract was consumed by the first proof.
cst.cs.applyStorageProofs(bn, txn1)
}
示例15: addSignatures
// addSignatures will sign a transaction using a spendable key, with support
// for multisig spendable keys. Because of the restricted input, the function
// is compatible with both siacoin inputs and siafund inputs.
func addSignatures(txn *types.Transaction, cf types.CoveredFields, uc types.UnlockConditions, parentID crypto.Hash, spendKey spendableKey) (newSigIndices []int, err error) {
// Try to find the matching secret key for each public key - some public
// keys may not have a match. Some secret keys may be used multiple times,
// which is why public keys are used as the outer loop.
totalSignatures := uint64(0)
for i, siaPubKey := range uc.PublicKeys {
// Search for the matching secret key to the public key.
for j := range spendKey.SecretKeys {
pubKey := spendKey.SecretKeys[j].PublicKey()
if bytes.Compare(siaPubKey.Key, pubKey[:]) != 0 {
continue
}
// Found the right secret key, add a signature.
sig := types.TransactionSignature{
ParentID: parentID,
CoveredFields: cf,
PublicKeyIndex: uint64(i),
}
newSigIndices = append(newSigIndices, len(txn.TransactionSignatures))
txn.TransactionSignatures = append(txn.TransactionSignatures, sig)
sigIndex := len(txn.TransactionSignatures) - 1
sigHash := txn.SigHash(sigIndex)
encodedSig, err := crypto.SignHash(sigHash, spendKey.SecretKeys[j])
if err != nil {
return nil, err
}
txn.TransactionSignatures[sigIndex].Signature = encodedSig[:]
// Count that the signature has been added, and break out of the
// secret key loop.
totalSignatures++
break
}
// If there are enough signatures to satisfy the unlock conditions,
// break out of the outer loop.
if totalSignatures == uc.SignaturesRequired {
break
}
}
return newSigIndices, nil
}