-
Notifications
You must be signed in to change notification settings - Fork 13
/
assets.go
165 lines (126 loc) · 3.51 KB
/
assets.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
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpc
import (
"golang.org/x/time/rate"
"github.com/bitmark-inc/bitmarkd/asset"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/storage"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"github.com/bitmark-inc/logger"
)
// Assets type
// ------=----
type Assets struct {
log *logger.L
limiter *rate.Limiter
}
const (
maximumAssets = 100
)
// Assets registration
// -------------------
type AssetStatus struct {
AssetId *transactionrecord.AssetIdentifier `json:"id"`
Duplicate bool `json:"duplicate"`
}
type AssetsRegisterReply struct {
Assets []AssetStatus `json:"assets"`
}
// internal function to register some assets
func assetRegister(assets []*transactionrecord.AssetData) ([]AssetStatus, []byte, error) {
assetStatus := make([]AssetStatus, len(assets))
// pack each transaction
packed := []byte{}
for i, argument := range assets {
assetId, packedAsset, err := asset.Cache(argument)
if nil != err {
return nil, nil, err
}
assetStatus[i].AssetId = assetId
if nil == packedAsset {
assetStatus[i].Duplicate = true
} else {
packed = append(packed, packedAsset...)
}
}
return assetStatus, packed, nil
}
// Asset get
// ---------
type AssetGetArguments struct {
Fingerprints []string `json:"fingerprints"`
}
type AssetGetReply struct {
Assets []AssetRecord `json:"assets"`
}
type AssetRecord struct {
Record string `json:"record"`
Confirmed bool `json:"confirmed"`
AssetId interface{} `json:"id,omitempty"`
Data interface{} `json:"data"`
}
func (assets *Assets) Get(arguments *AssetGetArguments, reply *AssetGetReply) error {
log := assets.log
count := len(arguments.Fingerprints)
if err := rateLimitN(assets.limiter, count, maximumAssets); nil != err {
return err
}
if !mode.Is(mode.Normal) {
return fault.ErrNotAvailableDuringSynchronise
}
log.Infof("Assets.Get: %+v", arguments)
a := make([]AssetRecord, count)
loop:
for i, fingerprint := range arguments.Fingerprints {
assetId := transactionrecord.NewAssetIdentifier([]byte(fingerprint))
confirmed := true
_, packedAsset := storage.Pool.Assets.GetNB(assetId[:])
if nil == packedAsset {
confirmed = false
packedAsset = asset.Get(assetId)
if nil == packedAsset {
continue loop
}
}
assetTx, _, err := transactionrecord.Packed(packedAsset).Unpack(mode.IsTesting())
if nil != err {
continue loop
}
record, _ := transactionrecord.RecordName(assetTx)
a[i] = AssetRecord{
Record: record,
Confirmed: confirmed,
AssetId: assetId,
Data: assetTx,
}
}
reply.Assets = a
return nil
}
// // Asset identifier
// // -----------
// type AssetIdentifiersArguments struct {
// Ids []transaction.AssetId `json:"ids"`
// }
// type AssetIdentifiersReply struct {
// Assets []transaction.Decoded `json:"assets"`
// }
// func (assets *Assets) Identifier(arguments *AssetIdentifieresArguments, reply *AssetIdentifieresReply) error {
// // restrict arguments size to reasonable value
// size := len(arguments.Ids)
// if size > MaximumGetSize {
// size = MaximumGetSize
// }
// txIds := make([]transaction.Link, size)
// for i, assetId := range arguments.Ids[:size] {
// _, txId, found := assetId.Read()
// if found {
// txIds[i] = txId
// }
// }
// reply.Assets = transaction.Decode(txIds)
// return nil
// }