/
ds.go
337 lines (311 loc) · 8.09 KB
/
ds.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package objs
import (
mdefs "github.com/MadBase/MadNet/application/objs/capn"
"github.com/MadBase/MadNet/application/objs/datastore"
"github.com/MadBase/MadNet/application/objs/uint256"
"github.com/MadBase/MadNet/constants"
"github.com/MadBase/MadNet/errorz"
"github.com/MadBase/MadNet/utils"
capnp "zombiezen.com/go/capnproto2"
)
// DataStore is a datastore UTXO
type DataStore struct {
DSLinker *DSLinker
Signature *DataStoreSignature
}
// UnmarshalBinary takes a byte slice and returns the corresponding
// DataStore object
func (b *DataStore) UnmarshalBinary(data []byte) error {
bc, err := datastore.Unmarshal(data)
if err != nil {
return err
}
return b.UnmarshalCapn(bc)
}
// MarshalBinary takes the DataStore object and returns the canonical
// byte slice
func (b *DataStore) MarshalBinary() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
bc, err := b.MarshalCapn(nil)
if err != nil {
return nil, err
}
return datastore.Marshal(bc)
}
// UnmarshalCapn unmarshals the capnproto definition of the object
func (b *DataStore) UnmarshalCapn(bc mdefs.DataStore) error {
if err := datastore.Validate(bc); err != nil {
return err
}
dSLinker := &DSLinker{}
if err := dSLinker.UnmarshalCapn(bc.DSLinker()); err != nil {
return err
}
b.DSLinker = dSLinker
sig := &DataStoreSignature{}
if err := sig.UnmarshalBinary(bc.Signature()); err != nil {
return err
}
b.Signature = sig
return nil
}
// MarshalCapn marshals the object into its capnproto definition
func (b *DataStore) MarshalCapn(seg *capnp.Segment) (mdefs.DataStore, error) {
if b == nil {
return mdefs.DataStore{}, errorz.ErrInvalid{}.New("not initialized")
}
var bc mdefs.DataStore
if seg == nil {
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return bc, err
}
tmp, err := mdefs.NewRootDataStore(seg)
if err != nil {
return bc, err
}
bc = tmp
} else {
tmp, err := mdefs.NewDataStore(seg)
if err != nil {
return bc, err
}
bc = tmp
}
seg = bc.Struct.Segment()
bt, err := b.DSLinker.MarshalCapn(seg)
if err != nil {
return bc, err
}
if err := bc.SetDSLinker(bt); err != nil {
return bc, err
}
sig, err := b.Signature.MarshalBinary()
if err != nil {
return bc, err
}
if err := bc.SetSignature(sig); err != nil {
return bc, err
}
return bc, nil
}
// IssuedAt returns the IssuedAt of the object
func (b *DataStore) IssuedAt() (uint32, error) {
if b == nil {
return 0, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.IssuedAt()
}
// ChainID returns the ChainID of the object
func (b *DataStore) ChainID() (uint32, error) {
if b == nil {
return 0, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.ChainID()
}
// Index returns the Index of the object
func (b *DataStore) Index() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.Index()
}
// PreHash returns the PreHash of the object
func (b *DataStore) PreHash() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.PreHash()
}
// UTXOID returns the UTXOID of the object
func (b *DataStore) UTXOID() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.UTXOID()
}
// TXOutIdx returns the TXOutIdx of the object
func (b *DataStore) TXOutIdx() (uint32, error) {
if b == nil {
return 0, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.TXOutIdx()
}
// SetTXOutIdx sets the TXOutIdx of the object
func (b *DataStore) SetTXOutIdx(idx uint32) error {
if b == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.SetTXOutIdx(idx)
}
// TxHash returns the TxHash of the object
func (b *DataStore) TxHash() ([]byte, error) {
if b == nil || b.DSLinker == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
if len(b.DSLinker.TxHash) != constants.HashLen {
return nil, errorz.ErrInvalid{}.New("invalid TxHash")
}
return utils.CopySlice(b.DSLinker.TxHash), nil
}
// SetTxHash sets the TxHash of the object
func (b *DataStore) SetTxHash(txHash []byte) error {
if b == nil || b.DSLinker == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
if len(txHash) != constants.HashLen {
return errorz.ErrInvalid{}.New("Invalid hash length")
}
b.DSLinker.TxHash = utils.CopySlice(txHash)
return nil
}
// RawData returns the RawData field of the sub object
func (b *DataStore) RawData() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.RawData()
}
// Owner returns the DataStoreOwner of the DataStore
func (b *DataStore) Owner() (*DataStoreOwner, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.Owner()
}
// GenericOwner returns the Owner of the DataStore
func (b *DataStore) GenericOwner() (*Owner, error) {
dso, err := b.Owner()
if err != nil {
return nil, err
}
onr := &Owner{}
err = onr.NewFromDataStoreOwner(dso)
if err != nil {
return nil, err
}
return onr, nil
}
// EpochOfExpiration returns the epoch in which the datastore may be garbage
// collected
func (b *DataStore) EpochOfExpiration() (uint32, error) {
if b == nil {
return 0, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.EpochOfExpiration()
}
// RemainingValue returns remaining value at the time of consumption
func (b *DataStore) RemainingValue(currentHeight uint32) (*uint256.Uint256, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.RemainingValue(currentHeight)
}
// Value returns the value stored in the object at the time of creation
func (b *DataStore) Value() (*uint256.Uint256, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.DSLinker.Value()
}
// ValidatePreSignature validates the signature of the datastore at the time of
// creation
func (b *DataStore) ValidatePreSignature() error {
if b == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
msg, err := b.DSLinker.MarshalBinary()
if err != nil {
return err
}
return b.DSLinker.ValidatePreSignature(msg, b.Signature)
}
// PreSign generates the signature for a DataStore at the time of creation
func (b *DataStore) PreSign(s Signer) error {
if b == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
msg, err := b.DSLinker.MarshalBinary()
if err != nil {
return err
}
owner, err := b.Owner()
if err != nil {
return err
}
sig, err := owner.Sign(msg, s)
if err != nil {
return err
}
b.Signature = sig
return nil
}
// Sign generates the signature for a DataStore at the time of consumption
func (b *DataStore) Sign(txIn *TXIn, s Signer) error {
if b == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
msg, err := txIn.TXInLinker.MarshalBinary()
if err != nil {
return err
}
owner, err := b.Owner()
if err != nil {
return err
}
sig, err := owner.Sign(msg, s)
if err != nil {
return err
}
sigb, err := sig.MarshalBinary()
if err != nil {
return err
}
txIn.Signature = sigb
return nil
}
// ValidateSignature validates the signature of the datastore at the time of
// consumption
func (b *DataStore) ValidateSignature(currentHeight uint32, txIn *TXIn) error {
if b == nil {
return errorz.ErrInvalid{}.New("not initialized")
}
msg, err := txIn.TXInLinker.MarshalBinary()
if err != nil {
return err
}
sig := &DataStoreSignature{}
if err := sig.UnmarshalBinary(txIn.Signature); err != nil {
return err
}
return b.DSLinker.ValidateSignature(currentHeight, msg, sig)
}
// MakeTxIn constructs a TXIn object for the current object
func (b *DataStore) MakeTxIn() (*TXIn, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
txOutIdx, err := b.TXOutIdx()
if err != nil {
return nil, err
}
cid, err := b.ChainID()
if err != nil {
return nil, err
}
txHash, err := b.TxHash()
if err != nil {
return nil, err
}
return &TXIn{
TXInLinker: &TXInLinker{
TXInPreImage: &TXInPreImage{
ConsumedTxIdx: txOutIdx,
ConsumedTxHash: utils.CopySlice(txHash),
ChainID: cid,
},
},
}, nil
}