-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
190 lines (158 loc) · 5.01 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package consensushashing
import (
"io"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/utils/serialization"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/model/externalapi"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/utils/hashes"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/utils/transactionhelper"
"github.com/Nirvana-Chain/nirvanad/util/binaryserializer"
"github.com/pkg/errors"
)
// txEncoding is a bitmask defining which transaction fields we
// want to encode and which to ignore.
type txEncoding uint8
const (
txEncodingFull txEncoding = 0
txEncodingExcludeSignatureScript = 1 << iota
)
// TransactionHash returns the transaction hash.
func TransactionHash(tx *externalapi.DomainTransaction) *externalapi.DomainHash {
// Encode the header and hash everything prior to the number of
// transactions.
writer := hashes.NewTransactionHashWriter()
err := serializeTransaction(writer, tx, txEncodingFull)
if err != nil {
// It seems like this could only happen if the writer returned an error.
// and this writer should never return an error (no allocations or possible failures)
// the only non-writer error path here is unknown types in `WriteElement`
panic(errors.Wrap(err, "TransactionHash() failed. this should never fail for structurally-valid transactions"))
}
return writer.Finalize()
}
// TransactionID generates the Hash for the transaction without the signature script and payload field.
func TransactionID(tx *externalapi.DomainTransaction) *externalapi.DomainTransactionID {
// If transaction ID is already cached, return it
if tx.ID != nil {
return tx.ID
}
// Encode the transaction, replace signature script with zeroes, cut off
// payload and hash the result.
var encodingFlags txEncoding
if !transactionhelper.IsCoinBase(tx) {
encodingFlags = txEncodingExcludeSignatureScript
}
writer := hashes.NewTransactionIDWriter()
err := serializeTransaction(writer, tx, encodingFlags)
if err != nil {
// this writer never return errors (no allocations or possible failures) so errors can only come from validity checks,
// and we assume we never construct malformed transactions.
panic(errors.Wrap(err, "TransactionID() failed. this should never fail for structurally-valid transactions"))
}
transactionID := externalapi.DomainTransactionID(*writer.Finalize())
tx.ID = &transactionID
return tx.ID
}
// TransactionIDs converts the provided slice of DomainTransactions to a corresponding slice of TransactionIDs
func TransactionIDs(txs []*externalapi.DomainTransaction) []*externalapi.DomainTransactionID {
txIDs := make([]*externalapi.DomainTransactionID, len(txs))
for i, tx := range txs {
txIDs[i] = TransactionID(tx)
}
return txIDs
}
func serializeTransaction(w io.Writer, tx *externalapi.DomainTransaction, encodingFlags txEncoding) error {
err := binaryserializer.PutUint16(w, tx.Version)
if err != nil {
return err
}
count := uint64(len(tx.Inputs))
err = serialization.WriteElement(w, count)
if err != nil {
return err
}
for _, ti := range tx.Inputs {
err = writeTransactionInput(w, ti, encodingFlags)
if err != nil {
return err
}
}
count = uint64(len(tx.Outputs))
err = serialization.WriteElement(w, count)
if err != nil {
return err
}
for _, output := range tx.Outputs {
err = writeTxOut(w, output)
if err != nil {
return err
}
}
err = binaryserializer.PutUint64(w, tx.LockTime)
if err != nil {
return err
}
_, err = w.Write(tx.SubnetworkID[:])
if err != nil {
return err
}
err = binaryserializer.PutUint64(w, tx.Gas)
if err != nil {
return err
}
err = writeVarBytes(w, tx.Payload)
if err != nil {
return err
}
return nil
}
// writeTransactionInput encodes ti to the nirvana protocol encoding for a transaction
// input to w.
func writeTransactionInput(w io.Writer, ti *externalapi.DomainTransactionInput, encodingFlags txEncoding) error {
err := writeOutpoint(w, &ti.PreviousOutpoint)
if err != nil {
return err
}
if encodingFlags&txEncodingExcludeSignatureScript != txEncodingExcludeSignatureScript {
err = writeVarBytes(w, ti.SignatureScript)
if err != nil {
return err
}
_, err = w.Write([]byte{ti.SigOpCount})
if err != nil {
return err
}
} else {
err = writeVarBytes(w, []byte{})
if err != nil {
return err
}
}
return binaryserializer.PutUint64(w, ti.Sequence)
}
func writeOutpoint(w io.Writer, outpoint *externalapi.DomainOutpoint) error {
_, err := w.Write(outpoint.TransactionID.ByteSlice())
if err != nil {
return err
}
return binaryserializer.PutUint32(w, outpoint.Index)
}
func writeVarBytes(w io.Writer, data []byte) error {
dataLength := uint64(len(data))
err := serialization.WriteElement(w, dataLength)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func writeTxOut(w io.Writer, to *externalapi.DomainTransactionOutput) error {
err := binaryserializer.PutUint64(w, to.Value)
if err != nil {
return err
}
err = binaryserializer.PutUint16(w, to.ScriptPublicKey.Version)
if err != nil {
return err
}
return writeVarBytes(w, to.ScriptPublicKey.Script)
}