OSDN Git Service

edht tx txoutput (#1966)
[bytom/bytom.git] / protocol / bc / tx.go
1 package bc
2
3 import (
4         "github.com/bytom/bytom/crypto/sha3pool"
5         "github.com/bytom/bytom/errors"
6 )
7
8 // Tx is a wrapper for the entries-based representation of a transaction.
9 type Tx struct {
10         *TxHeader
11         ID       Hash
12         Entries  map[Hash]Entry
13         InputIDs []Hash // 1:1 correspondence with TxData.Inputs
14
15         SpentOutputIDs []Hash
16 }
17
18 // SigHash ...
19 func (tx *Tx) SigHash(n uint32) (hash Hash) {
20         hasher := sha3pool.Get256()
21         defer sha3pool.Put256(hasher)
22
23         tx.InputIDs[n].WriteTo(hasher)
24         tx.ID.WriteTo(hasher)
25         hash.ReadFrom(hasher)
26         return hash
27 }
28
29 // Convenience routines for accessing entries of specific types by ID.
30 var (
31         ErrEntryType    = errors.New("invalid entry type")
32         ErrMissingEntry = errors.New("missing entry")
33 )
34
35 // OriginalOutput try to get the output entry by given hash
36 func (tx *Tx) OriginalOutput(id Hash) (*OriginalOutput, error) {
37         e, ok := tx.Entries[id]
38         if !ok || e == nil {
39                 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
40         }
41         o, ok := e.(*OriginalOutput)
42         if !ok {
43                 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
44         }
45         return o, nil
46 }
47
48 // Spend try to get the spend entry by given hash
49 func (tx *Tx) Spend(id Hash) (*Spend, error) {
50         e, ok := tx.Entries[id]
51         if !ok || e == nil {
52                 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
53         }
54         sp, ok := e.(*Spend)
55         if !ok {
56                 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
57         }
58         return sp, nil
59 }
60
61 // Issuance try to get the issuance entry by given hash
62 func (tx *Tx) Issuance(id Hash) (*Issuance, error) {
63         e, ok := tx.Entries[id]
64         if !ok || e == nil {
65                 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
66         }
67         iss, ok := e.(*Issuance)
68         if !ok {
69                 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
70         }
71         return iss, nil
72 }
73
74 // VetoInput try to get the veto entry by given hash
75 func (tx *Tx) VetoInput(id Hash) (*VetoInput, error) {
76         e, ok := tx.Entries[id]
77         if !ok || e == nil {
78                 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
79         }
80         sp, ok := e.(*VetoInput)
81         if !ok {
82                 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
83         }
84         return sp, nil
85 }
86
87 // VoteOutput try to get the vote output entry by given hash
88 func (tx *Tx) VoteOutput(id Hash) (*VoteOutput, error) {
89         e, ok := tx.Entries[id]
90         if !ok || e == nil {
91                 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
92         }
93         o, ok := e.(*VoteOutput)
94         if !ok {
95                 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
96         }
97         return o, nil
98 }