OSDN Git Service

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