forked from eosspark/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
executable file
·251 lines (217 loc) · 7.04 KB
/
block.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package types
import (
"encoding/binary"
"encoding/json"
"github.com/zhangsifeng92/geos/common"
"github.com/zhangsifeng92/geos/crypto"
"github.com/zhangsifeng92/geos/crypto/ecc"
"github.com/zhangsifeng92/geos/crypto/rlp"
)
type TransactionStatus uint8
const (
TransactionStatusExecuted TransactionStatus = iota ///< succeed, no error handler executed
TransactionStatusSoftFail ///< objectively failed (not executed), error handler executed
TransactionStatusHardFail ///< objectively failed and error handler objectively failed thus no state change
TransactionStatusDelayed ///< transaction delayed
TransactionStatusExpired
TransactionStatusUnknown = TransactionStatus(255)
)
type BlockStatus uint8
const (
Irreversible BlockStatus = iota ///< this block has already been applied before by this node and is considered irreversible
Validated ///< this is a complete block signed by a valid producer and has been previously applied by this node and therefore validated but it is not yet irreversible
Complete ///< this is a complete block signed by a valid producer but is not yet irreversible nor has it yet been applied by this node
Incomplete ///< this is an incomplete block (either being produced by a producer or speculatively produced by a node)
)
func (s *TransactionStatus) UnmarshalJSON(data []byte) error {
var decoded string
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}
switch decoded {
case "executed":
*s = TransactionStatusExecuted
case "soft_fail":
*s = TransactionStatusSoftFail
case "hard_fail":
*s = TransactionStatusHardFail
case "delayed":
*s = TransactionStatusDelayed
default:
*s = TransactionStatusUnknown
}
return nil
}
func (s TransactionStatus) MarshalJSON() (data []byte, err error) {
out := "unknown"
switch s {
case TransactionStatusExecuted:
out = "executed"
case TransactionStatusSoftFail:
out = "soft_fail"
case TransactionStatusHardFail:
out = "hard_fail"
case TransactionStatusDelayed:
out = "delayed"
}
return json.Marshal(out)
}
func (s TransactionStatus) String() string {
switch s {
case TransactionStatusExecuted:
return "executed"
case TransactionStatusSoftFail:
return "soft fail"
case TransactionStatusHardFail:
return "hard fail"
case TransactionStatusDelayed:
return "delayed"
default:
return "unknown"
}
}
type TransactionReceiptHeader struct {
Status TransactionStatus `json:"status"`
CpuUsageUs uint32 `json:"cpu_usage_us"`
NetUsageWords common.Vuint32 `json:"net_usage_words" eos:"vuint32"`
}
type TransactionReceipt struct {
TransactionReceiptHeader
Trx TransactionWithID `json:"trx" eos:"trxID"`
}
type SignedBlock struct {
SignedBlockHeader `multiIndex:"inline"`
Transactions []TransactionReceipt `json:"transactions"`
BlockExtensions []Extension `json:"block_extensions"`
}
func NewSignedBlock() *SignedBlock {
return &SignedBlock{SignedBlockHeader: *NewSignedBlockHeader()}
}
func NewSignedBlock1(h *SignedBlockHeader) *SignedBlock {
return &SignedBlock{SignedBlockHeader: *h}
}
/*func (m *SignedBlock) String() string {
return "SignedBlock"
}*/
type ProducerConfirmation struct {
BlockID common.BlockIdType
BlockDigest [4]uint64
Producer common.AccountName
Sig ecc.Signature
}
type Optional struct {
Valid bool
Pair map[common.ChainIdType][]ecc.PublicKey
}
type TransactionWithID struct {
PackedTransaction *PackedTransaction `json:"packed_transaction" eos:"tag0"`
TransactionID common.TransactionIdType `json:"transaction_id" eos:"tag1"`
}
func (t TransactionWithID) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{
t.PackedTransaction,
t.TransactionID,
})
}
func (t *TransactionWithID) UnmarshalJSON(data []byte) error {
var packed PackedTransaction
if data[0] == '{' {
if err := json.Unmarshal(data, &packed); err != nil {
return err
}
*t = TransactionWithID{
PackedTransaction: &packed,
}
return nil
} else if data[0] == '"' {
var id common.TransactionIdType
err := json.Unmarshal(data, &id)
if err != nil {
return err
}
*t = TransactionWithID{
TransactionID: id,
}
return nil
}
return nil
}
//func (t TransactionWithID) Pack() ([]byte, error) { //TODO
// re := make([]byte, 0, 0)
// if t.TransactionID.Equals(common.TransactionIdNil()) {
// re = append(re, 1)
// bytes, err := rlp.EncodeToBytes(t.PackedTransaction)
// if err != nil {
// return nil, err
// }
// re = append(re, bytes...)
// } else {
// re = append(re, 0)
// bytes, err := rlp.EncodeToBytes(t.TransactionID)
// if err != nil {
// return nil, err
// }
// re = append(re, bytes...)
// }
//
// return re, nil
//}
//func (t *TransactionWithID) Unpack(in []byte) (l int, err error) {
// decoder:=rlp.NewDecoder(in)
// tag,_:=decoder.ReadByte()
// if tag ==0{
// err :=decoder.Decode(&t.TransactionID)
// if err !=nil{
// return 0,err
// }
// }else if tag ==1{
// err :=decoder.Decode(&t.PackedTransaction)
// if err !=nil{
// fmt.Println("error:",err.Error())
// return 0,err
// }
// }
// fmt.Println("unpack transactionWithID :",t)
// return decoder.GetPos(),nil
//}
func NewTransactionReceiptHeader() *TransactionReceiptHeader {
return &TransactionReceiptHeader{Status: TransactionStatusHardFail}
}
func NewTransactionReceiptHeader1(status TransactionStatus) *TransactionReceiptHeader {
return &TransactionReceiptHeader{Status: status}
}
func NewTransactionReceipt() *TransactionReceipt {
return &TransactionReceipt{TransactionReceiptHeader: *NewTransactionReceiptHeader()}
}
func NewTransactionReceiptWithID(tid common.TransactionIdType) *TransactionReceipt {
return &TransactionReceipt{TransactionReceiptHeader: *NewTransactionReceiptHeader1(TransactionStatusExecuted), Trx: TransactionWithID{TransactionID: tid}}
}
func NewTransactionReceiptWithPtrx(ptrx PackedTransaction) *TransactionReceipt {
return &TransactionReceipt{TransactionReceiptHeader: *NewTransactionReceiptHeader1(TransactionStatusExecuted), Trx: TransactionWithID{PackedTransaction: &ptrx}}
}
func (t *TransactionReceipt) Digest() common.DigestType {
enc := crypto.NewSha256()
status, _ := rlp.EncodeToBytes(t.Status)
cpuUsageUs, _ := rlp.EncodeToBytes(t.CpuUsageUs)
//netUsageWords, _ := rlp.EncodeToBytes(t.NetUsageWords)
buf := make([]byte, 8) //TODO t.NetUsageWords is a vuint32!!
l := binary.PutUvarint(buf, uint64(25))
netUsageWords := buf[:l]
enc.Write(status)
enc.Write(cpuUsageUs)
enc.Write(netUsageWords)
if !t.Trx.TransactionID.Equals(common.TransactionIdNil()) {
trxID, _ := rlp.EncodeToBytes(t.Trx.TransactionID)
enc.Write(trxID)
} else {
packedTrx, _ := rlp.EncodeToBytes(t.Trx.PackedTransaction.PackedDigest())
enc.Write(packedTrx)
}
return *crypto.NewSha256Byte(enc.Sum(nil))
}
func (s SignedBlock) IsEmpty() bool {
return len(s.Transactions) == 0 && len(s.BlockExtensions) == 0 && s.SignedBlockHeader.IsEmpty()
}
func (t TransactionReceiptHeader) IsEmpty() bool {
return t.Status == 0 && t.CpuUsageUs == 0 && t.NetUsageWords == 0
}