-
Notifications
You must be signed in to change notification settings - Fork 211
/
transaction.go
155 lines (127 loc) · 4.05 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
package types
import (
"bytes"
"time"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/hash"
"github.com/spacemeshos/go-spacemesh/log"
)
//go:generate scalegen -types Transaction,Reward,RawTx
// TransactionID is a 32-byte blake3 sum of the transaction, used as an identifier.
type TransactionID Hash32
const (
// TransactionIDSize in bytes.
TransactionIDSize = Hash32Length
)
// Hash32 returns the TransactionID as a Hash32.
func (id TransactionID) Hash32() Hash32 {
return Hash32(id)
}
// ShortString returns a the first 10 characters of the ID, for logging purposes.
func (id TransactionID) ShortString() string {
return id.Hash32().ShortString()
}
// String returns a hexadecimal representation of the TransactionID with "0x" prepended, for logging purposes.
// It implements the fmt.Stringer interface.
func (id TransactionID) String() string {
return id.Hash32().String()
}
// Bytes returns the TransactionID as a byte slice.
func (id TransactionID) Bytes() []byte {
return id[:]
}
// Field returns a log field. Implements the LoggableField interface.
func (id TransactionID) Field() log.Field { return log.FieldNamed("tx_id", id.Hash32()) }
// Compare returns true if other (the given TransactionID) is less than this TransactionID, by lexicographic comparison.
func (id TransactionID) Compare(other TransactionID) bool {
return bytes.Compare(id.Bytes(), other.Bytes()) < 0
}
// EncodeScale implements scale codec interface.
func (id *TransactionID) EncodeScale(e *scale.Encoder) (int, error) {
return scale.EncodeByteArray(e, id[:])
}
// DecodeScale implements scale codec interface.
func (id *TransactionID) DecodeScale(d *scale.Decoder) (int, error) {
return scale.DecodeByteArray(d, id[:])
}
// Transaction is an alias to RawTx.
type Transaction struct {
RawTx
*TxHeader
}
// GetRaw returns raw bytes of the transaction with id.
func (t Transaction) GetRaw() RawTx {
return t.RawTx
}
// Verified returns true if header is set.
func (t Transaction) Verified() bool {
return t.TxHeader != nil
}
// Hash32 returns the TransactionID as a Hash32.
func (t *Transaction) Hash32() Hash32 {
return t.ID.Hash32()
}
// ShortString returns the first 5 characters of the ID, for logging purposes.
func (t *Transaction) ShortString() string {
return t.ID.ShortString()
}
// ToTransactionIDs returns a slice of TransactionID corresponding to the given transactions.
func ToTransactionIDs(txs []*Transaction) []TransactionID {
ids := make([]TransactionID, 0, len(txs))
for _, tx := range txs {
ids = append(ids, tx.ID)
}
return ids
}
// TransactionIDsToHashes turns a list of TransactionID into their Hash32 representation.
func TransactionIDsToHashes(ids []TransactionID) []Hash32 {
hashes := make([]Hash32, 0, len(ids))
for _, id := range ids {
hashes = append(hashes, id.Hash32())
}
return hashes
}
// TXState describes the state of a transaction.
type TXState uint32
const (
// PENDING represents the state when a transaction is syntactically valid, but its nonce and
// the principal's ability to cover gas have not been verified yet.
PENDING TXState = iota
// MEMPOOL represents the state when a transaction is in mempool.
MEMPOOL
// APPLIED represents the state when a transaction is applied to the state.
APPLIED
)
// MeshTransaction is stored in the mesh and included in the block.
type MeshTransaction struct {
Transaction
LayerID LayerID
BlockID BlockID
State TXState
Received time.Time
}
// Reward is a virtual reward transaction, which the node keeps track of for the gRPC api.
type Reward struct {
Layer LayerID
TotalReward uint64
LayerReward uint64
Coinbase Address
SmesherID NodeID
}
// NewRawTx computes id from raw bytes and returns the object.
func NewRawTx(raw []byte) RawTx {
return RawTx{
ID: hash.Sum(raw),
Raw: raw,
}
}
// RawTx stores an identity and a pointer to raw bytes.
type RawTx struct {
ID TransactionID
Raw []byte `scale:"max=4096"` // transactions should always be less than 4kb
}
// AddressNonce is an (address, nonce) named tuple.
type AddressNonce struct {
Address Address
Nonce Nonce
}