-
Notifications
You must be signed in to change notification settings - Fork 13
/
transfer.go
323 lines (265 loc) · 9.21 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
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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ownership
import (
"encoding/binary"
"sync"
"github.com/bitmark-inc/bitmarkd/account"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/storage"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"github.com/bitmark-inc/logger"
)
// to ensure synchronised ownership updates
var toLock sync.Mutex
// from storage/setup.go:
//
// Ownership:
// OwnerNextCount BN - next count value to use for appending to owned items
// OwnerList txId - list of owned items
// OwnerTxIndex BN - position in list of owned items, for delete after transfer
// Share - setup for share ownership transfer, must have a lock
func Share(
trx storage.Transaction,
previousTxId merkle.Digest,
transferTxId merkle.Digest,
transferBlockNumber uint64,
currentOwner *account.Account,
balance uint64,
) {
// ensure single threaded
toLock.Lock()
defer toLock.Unlock()
// delete current ownership
transfer(trx, previousTxId, transferTxId, transferBlockNumber, currentOwner, nil, balance)
}
// Transfer - transfer ownership
func Transfer(
trx storage.Transaction,
previousTxId merkle.Digest,
transferTxId merkle.Digest,
transferBlockNumber uint64,
currentOwner *account.Account,
newOwner *account.Account,
) {
// ensure single threaded
toLock.Lock()
defer toLock.Unlock()
transfer(trx, previousTxId, transferTxId, transferBlockNumber, currentOwner, newOwner, 0)
}
// need to hold the lock before calling this
func transfer(
trx storage.Transaction,
previousTxId merkle.Digest,
transferTxId merkle.Digest,
transferBlockNumber uint64,
currentOwner *account.Account,
newOwner *account.Account,
quantity uint64,
) {
// get count for current owner record
dKey := append(currentOwner.Bytes(), previousTxId[:]...)
dCount := trx.Get(storage.Pool.OwnerTxIndex, dKey)
if nil == dCount {
logger.Criticalf("ownership.Transfer: dKey: %x", dKey)
logger.Criticalf("ownership.Transfer: block number: %d", transferBlockNumber)
logger.Criticalf("ownership.Transfer: previous tx id: %#v", previousTxId)
logger.Criticalf("ownership.Transfer: transfer tx id: %#v", transferTxId)
logger.Criticalf("ownership.Transfer: current owner: %x %v", currentOwner.Bytes(), currentOwner)
if nil != newOwner {
logger.Criticalf("ownership.Transfer: new owner: %x %v", newOwner.Bytes(), newOwner)
}
// ow, err := ListBitmarksFor(currentOwner, 0, 999)
// if nil != err {
// logger.Criticalf("lbf: error: %s", err)
// } else {
// logger.Criticalf("lbf: %#v", ow)
// }
logger.Panic("ownership.Transfer: OwnerTxIndex database corrupt")
}
// delete the current owners records
ownerData, err := GetOwnerData(trx, previousTxId)
if nil != err {
logger.Criticalf("ownership.Transfer: invalid owner data for tx id: %s error: %s", previousTxId, err)
logger.Panic("ownership.Transfer: Ownership database corrupt")
}
oKey := append(currentOwner.Bytes(), dCount...)
trx.Delete(storage.Pool.OwnerList, oKey)
trx.Delete(storage.Pool.OwnerTxIndex, dKey)
// and the old owner data
trx.Delete(storage.Pool.OwnerData, previousTxId[:])
// if no new owner only above delete was needed
if nil == newOwner && 0 == quantity {
return
}
switch ownerData := ownerData.(type) {
case *AssetOwnerData:
// create a share - only from an asset
if 0 != quantity {
// convert initial quantity to 8 byte big endian
quantityBytes := make([]byte, 8)
binary.BigEndian.PutUint64(quantityBytes, quantity)
// the ID of the share is the issue id of the bitmark
shareId := ownerData.issueTxId
// the total quantity of this type of share
shareData := append(quantityBytes, transferTxId[:]...)
trx.Put(storage.Pool.Shares, shareId[:], shareData, []byte{})
// initially total quantity goes to the creator
fKey := append(currentOwner.Bytes(), shareId[:]...)
trx.Put(storage.Pool.ShareQuantity, fKey, quantityBytes, []byte{})
// convert to share and update
newOwnerData := ShareOwnerData{
transferBlockNumber: transferBlockNumber,
issueTxId: ownerData.issueTxId,
issueBlockNumber: ownerData.issueBlockNumber,
assetId: ownerData.assetId,
}
create(trx, transferTxId, newOwnerData, currentOwner)
return
}
// otherwise create new ownership record
ownerData.transferBlockNumber = transferBlockNumber
create(trx, transferTxId, ownerData, newOwner)
case *BlockOwnerData:
// create a share - only from an asset
if 0 != quantity {
// panic if not an asset (this should have been checked earlier)
logger.Criticalf("ownership.Transfer: ownerData for key: %x is not an asset", oKey)
logger.Panic("ownership.Transfer: Ownership database corrupt")
}
// otherwise create new ownership record
ownerData.transferBlockNumber = transferBlockNumber
create(trx, transferTxId, ownerData, newOwner)
case *ShareOwnerData:
// create a share - only from an asset
if 0 != quantity {
// panic if not an asset (this should have been checked earlier)
logger.Criticalf("ownership.Transfer: ownerData for key: %x is not an asset", oKey)
logger.Panic("ownership.Transfer: Ownership database corrupt")
}
// Note: only called on delete (block/store.go prevents share back to asset)
// convert to transfer and update
newOwnerData := AssetOwnerData{
transferBlockNumber: transferBlockNumber,
issueTxId: ownerData.issueTxId,
issueBlockNumber: ownerData.issueBlockNumber,
assetId: ownerData.assetId,
}
create(trx, transferTxId, newOwnerData, currentOwner)
default:
// panic if not an asset (this should have been checked earlier)
logger.Criticalf("ownership.Transfer: unhandled owner data type: %+v", ownerData)
logger.Panic("ownership.Transfer: missing owner data handler")
}
}
// internal creation routine, must be called with lock held
// adds items to owner's list of properties and stores the owner data
func create(
trx storage.Transaction,
txId merkle.Digest,
ownerData OwnerData,
owner *account.Account,
) {
// increment the count for owner
nKey := owner.Bytes()
count := trx.Get(storage.Pool.OwnerNextCount, nKey)
if nil == count {
count = []byte{0, 0, 0, 0, 0, 0, 0, 0}
} else if uint64ByteSize != len(count) {
logger.Panic("OwnerNextCount database corrupt")
}
newCount := make([]byte, uint64ByteSize)
binary.BigEndian.PutUint64(newCount, binary.BigEndian.Uint64(count)+1)
trx.Put(storage.Pool.OwnerNextCount, nKey, newCount, []byte{})
// write to the owner list
oKey := append(owner.Bytes(), count...)
trx.Put(storage.Pool.OwnerList, oKey, txId[:], []byte{})
// write new index record
dKey := append(owner.Bytes(), txId[:]...)
trx.Put(storage.Pool.OwnerTxIndex, dKey, count, []byte{})
// save owner data record
trx.Put(storage.Pool.OwnerData, txId[:], ownerData.Pack(), []byte{})
}
// CreateAsset - create the ownership record for an asset
func CreateAsset(
trx storage.Transaction,
issueTxId merkle.Digest,
issueBlockNumber uint64,
assetId transactionrecord.AssetIdentifier,
newOwner *account.Account,
) {
// ensure single threaded
toLock.Lock()
defer toLock.Unlock()
newData := &AssetOwnerData{
transferBlockNumber: issueBlockNumber,
issueTxId: issueTxId,
issueBlockNumber: issueBlockNumber,
assetId: assetId,
}
// store to database
create(trx, issueTxId, newData, newOwner)
}
// CreateBlock - create the ownership record for a block
func CreateBlock(
trx storage.Transaction,
issueTxId merkle.Digest,
blockNumber uint64,
newOwner *account.Account,
) {
// ensure single threaded
toLock.Lock()
defer toLock.Unlock()
newData := &BlockOwnerData{
transferBlockNumber: blockNumber,
issueTxId: issueTxId,
issueBlockNumber: blockNumber,
}
// store to database
create(trx, issueTxId, newData, newOwner)
}
// OwnerOf - find the owner of a specific transaction
func OwnerOf(trx storage.Transaction, txId merkle.Digest) (uint64, *account.Account) {
var blockNumber uint64
var packed []byte
if nil == trx {
blockNumber, packed = storage.Pool.Transactions.GetNB(txId[:])
} else {
blockNumber, packed = trx.GetNB(storage.Pool.Transactions, txId[:])
}
if nil == packed {
return 0, nil
}
transaction, _, err := transactionrecord.Packed(packed).Unpack(mode.IsTesting())
logger.PanicIfError("ownership.OwnerOf", err)
switch tx := transaction.(type) {
case *transactionrecord.BitmarkIssue:
return blockNumber, tx.Owner
case *transactionrecord.BitmarkTransferUnratified:
return blockNumber, tx.Owner
case *transactionrecord.BitmarkTransferCountersigned:
return blockNumber, tx.Owner
case *transactionrecord.BlockFoundation:
return blockNumber, tx.Owner
case *transactionrecord.BlockOwnerTransfer:
return blockNumber, tx.Owner
default:
logger.Panicf("block.OwnerOf: incorrect transaction: %v", transaction)
return 0, nil
}
}
// CurrentlyOwns - check owner currently owns this transaction id
func CurrentlyOwns(
trx storage.Transaction,
owner *account.Account,
txId merkle.Digest,
) bool {
dKey := append(owner.Bytes(), txId[:]...)
if nil == trx {
return storage.Pool.OwnerTxIndex.Has(dKey)
}
return trx.Has(storage.Pool.OwnerTxIndex, dKey)
}