-
Notifications
You must be signed in to change notification settings - Fork 13
/
transfer.go
289 lines (247 loc) · 8.76 KB
/
transfer.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package reservoir
import (
"time"
"github.com/bitmark-inc/bitmarkd/account"
"github.com/bitmark-inc/bitmarkd/constants"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/ownership"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/storage"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"github.com/bitmark-inc/logger"
)
// TransferInfo - result returned by store transfer
type TransferInfo struct {
Id pay.PayId
TxId merkle.Digest
IssueTxId merkle.Digest
Packed []byte
Payments []transactionrecord.PaymentAlternative
}
// returned data from verifyTransfer
type verifiedTransferInfo struct {
txId merkle.Digest
packed []byte
previousTransfer transactionrecord.BitmarkTransfer
issueTxId merkle.Digest
transferBlockNumber uint64
issueBlockNumber uint64
}
// storeTransfer - verify and store a transfer request
func storeTransfer(
transfer transactionrecord.BitmarkTransfer,
transactionHandle storage.Handle,
ownerTxHandle storage.Handle,
ownerDataHandle storage.Handle,
blockOwnerPaymentHandle storage.Handle,
) (*TransferInfo, bool, error) {
if nil == transactionHandle || nil == ownerTxHandle || nil == ownerDataHandle || nil == blockOwnerPaymentHandle {
return nil, false, fault.NilPointer
}
globalData.RLock()
defer globalData.RUnlock()
verifyResult, duplicate, err := verifyTransfer(transfer, transactionHandle, ownerTxHandle, ownerDataHandle)
if err != nil {
return nil, false, err
}
// compute pay id
packedTransfer := verifyResult.packed
payId := pay.NewPayId([][]byte{packedTransfer})
txId := verifyResult.txId
previousTransfer := verifyResult.previousTransfer
payments := getPayments(verifyResult.transferBlockNumber, verifyResult.issueBlockNumber, previousTransfer, blockOwnerPaymentHandle)
result := &TransferInfo{
Id: payId,
TxId: txId,
IssueTxId: verifyResult.issueTxId,
Packed: packedTransfer,
Payments: payments,
}
// if already seen just return pay id and previous payments if present
entry, ok := globalData.pendingTransactions[payId]
if ok {
if nil != entry.payments {
result.Payments = entry.payments
} else {
// this would mean that reservoir data is corrupt
logger.Panicf("storeTransfer: failed to get current payment data for: %s payid: %s", txId, payId)
}
return result, true, nil
}
// if duplicates were detected, but different duplicates were present
// then it is an error
if duplicate {
return nil, true, fault.TransactionAlreadyExists
}
transferredItem := &transactionData{
txId: txId,
transaction: transfer,
packed: packedTransfer,
}
// already received the payment for the transfer
// approve the transfer immediately if payment is ok
detail, ok := globalData.orphanPayments[payId]
if ok {
if acceptablePayment(detail, payments) {
globalData.verifiedTransactions[payId] = transferredItem
globalData.verifiedIndex[txId] = payId
globalData.inProgressLinks[transfer.GetLink()] = txId
delete(globalData.pendingTransactions, payId)
delete(globalData.pendingIndex, txId)
delete(globalData.orphanPayments, payId)
return result, false, nil
}
}
// waiting for the payment to come
payment := &transactionPaymentData{
payId: payId,
tx: transferredItem,
payments: payments,
expiresAt: time.Now().Add(constants.ReservoirTimeout),
}
if len(globalData.pendingTransactions) >= maximumPendingTransactions {
return nil, false, fault.BufferCapacityLimit
}
globalData.pendingTransactions[payId] = payment
globalData.pendingIndex[txId] = payId
globalData.inProgressLinks[transfer.GetLink()] = txId
return result, false, nil
}
// verify that a transfer is ok
// ensure lock is held before calling
func verifyTransfer(transfer transactionrecord.BitmarkTransfer, transactionHandle storage.Handle, ownerTxHandle storage.Handle, ownerDataHandle storage.Handle) (*verifiedTransferInfo, bool, error) {
// find the current owner via the link
_, previousPacked := transactionHandle.GetNB(transfer.GetLink().Bytes())
if nil == previousPacked {
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
previousTransaction, _, err := transactionrecord.Packed(previousPacked).Unpack(mode.IsTesting())
if nil != err {
return nil, false, err
}
var currentOwner *account.Account
var previousTransfer transactionrecord.BitmarkTransfer
// ensure that the transaction is a valid testChain transition
switch tx := previousTransaction.(type) {
case *transactionrecord.BitmarkIssue:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BitmarkTransferUnratified, *transactionrecord.BitmarkTransferCountersigned, *transactionrecord.BitmarkShare:
currentOwner = tx.Owner
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
case *transactionrecord.BitmarkTransferUnratified:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BitmarkTransferUnratified, *transactionrecord.BitmarkTransferCountersigned, *transactionrecord.BitmarkShare:
currentOwner = tx.Owner
previousTransfer = tx
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
case *transactionrecord.BitmarkTransferCountersigned:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BitmarkTransferUnratified, *transactionrecord.BitmarkTransferCountersigned, *transactionrecord.BitmarkShare:
currentOwner = tx.Owner
previousTransfer = tx
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
case *transactionrecord.OldBaseData:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BlockOwnerTransfer:
currentOwner = tx.Owner
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
case *transactionrecord.BlockFoundation:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BlockOwnerTransfer:
currentOwner = tx.Owner
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
case *transactionrecord.BlockOwnerTransfer:
// ensure link to correct transfer type
switch transfer.(type) {
case *transactionrecord.BlockOwnerTransfer:
currentOwner = tx.Owner
previousTransfer = tx
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
default:
return nil, false, fault.LinkToInvalidOrUnconfirmedTransaction
}
// pack transfer and check signature
packedTransfer, err := transfer.Pack(currentOwner)
if nil != err {
return nil, false, err
}
// transfer identifier and check for duplicate
txId := packedTransfer.MakeLink()
link := transfer.GetLink()
if txId == link {
// reject any transaction that links to itself
// this should never occur, but protect against this situation
return nil, false, fault.TransactionLinksToSelf
}
// check for double spend
linkTxId, okL := globalData.inProgressLinks[link]
_, okP := globalData.pendingIndex[txId]
_, okV := globalData.verifiedIndex[txId]
if okL && linkTxId != txId {
// not an exact match - must be a double transfer
return nil, false, fault.DoubleTransferAttempt
}
duplicate := false
if okP {
// if both then it is a possible duplicate
// (depends on later pay id check)
duplicate = true
}
// a single verified transfer fails the whole block
if okV {
return nil, false, fault.TransactionAlreadyExists
}
// a single confirmed transfer fails the whole block
if transactionHandle.Has(txId[:]) {
return nil, false, fault.TransactionAlreadyExists
}
// log.Infof("packed transfer: %x", packedTransfer)
// log.Infof("id: %v", txId)
// get count for current owner record
// to make sure that the record has not already been transferred
dKey := append(currentOwner.Bytes(), link[:]...)
// log.Infof("dKey: %x", dKey)
dCount := ownerTxHandle.Get(dKey)
if nil == dCount {
return nil, false, fault.DoubleTransferAttempt
}
// get ownership data
ownerData, err := ownership.GetOwnerData(nil, link, ownerDataHandle)
if nil != err {
globalData.log.Errorf("owner data error: %s", err)
return nil, false, err //fault.DoubleTransferAttempt
}
// log.Debugf("ownerData: %x", ownerData)
result := &verifiedTransferInfo{
txId: txId,
packed: packedTransfer,
previousTransfer: previousTransfer,
issueTxId: ownerData.IssueTxId(),
transferBlockNumber: ownerData.TransferBlockNumber(),
issueBlockNumber: ownerData.IssueBlockNumber(),
}
return result, duplicate, nil
}