@@ -27,15 +27,18 @@ type BlockOperation struct {
2727
2828 Type operation.OperationType `json:"type"`
2929 Source string `json:"source"`
30+ Target string `json:"target"`
3031 Body []byte `json:"body"`
3132 Height uint64 `json:"block_height"`
3233 Index uint64 `json:"index"`
3334 TxIndex uint64 `json:"tx_index"`
3435
35- // transaction will be used only for `Save` time.
36+ // bellows will be used only for `Save` time.
3637 transaction transaction.Transaction
38+ operation operation.Operation
3739 isSaved bool
3840 order * BlockOrder
41+ linked string
3942}
4043
4144func NewBlockOperationKey (opHash , txHash string , index uint64 ) string {
@@ -53,6 +56,18 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans
5356
5457 order := NewBlockOpOrder (blockHeight , txIndex , index )
5558
59+ target := ""
60+ if pop , ok := op .B .(operation.Targetable ); ok {
61+ target = pop .TargetAddress ()
62+ }
63+
64+ linked := ""
65+ if createAccount , ok := op .B .(* operation.CreateAccount ); ok {
66+ if createAccount .Linked != "" {
67+ linked = createAccount .Linked
68+ }
69+ }
70+
5671 return BlockOperation {
5772 Hash : NewBlockOperationKey (opHash , txHash , index ),
5873
@@ -61,16 +76,33 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans
6176
6277 Type : op .H .Type ,
6378 Source : tx .B .Source ,
79+ Target : target ,
6480 Body : body ,
6581 Height : blockHeight ,
6682 Index : index ,
6783 TxIndex : txIndex ,
6884
6985 transaction : tx ,
86+ operation : op ,
7087 order : order ,
88+ linked : linked ,
7189 }, nil
7290}
7391
92+ func (bo * BlockOperation ) hasTarget () bool {
93+ if bo .Target != "" {
94+ return true
95+ }
96+ return false
97+ }
98+
99+ func (bo * BlockOperation ) targetIsLinked () bool {
100+ if bo .hasTarget () && bo .linked != "" {
101+ return true
102+ }
103+ return false
104+ }
105+
74106func (bo * BlockOperation ) Save (st * storage.LevelDBBackend ) (err error ) {
75107 if bo .isSaved {
76108 return errors .AlreadySaved
@@ -91,47 +123,62 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) {
91123 if err = st .New (bo .NewBlockOperationTxHashKey (), bo .Hash ); err != nil {
92124 return
93125 }
94- if err = st .New (bo .NewBlockOperationSourceKey (), bo .Hash ); err != nil {
126+
127+ if err = st .New (bo .NewBlockOperationSourceKey (bo .Source ), bo .Hash ); err != nil {
95128 return
96129 }
97- if err = st .New (bo .NewBlockOperationSourceAndTypeKey (), bo .Hash ); err != nil {
130+ if err = st .New (bo .NewBlockOperationSourceAndTypeKey (bo .Source ), bo .Hash ); err != nil {
131+ return
132+ }
133+ if err = st .New (bo .NewBlockOperationPeersKey (bo .Source ), bo .Hash ); err != nil {
134+ return
135+ }
136+ if err = st .New (bo .NewBlockOperationPeersAndTypeKey (bo .Source ), bo .Hash ); err != nil {
98137 return
99138 }
100139
101- var body operation.Body
102- var casted operation.CreateAccount
103- var ok bool
104-
105- if bo .Type == operation .TypeCreateAccount {
106- if body , err = operation .UnmarshalBodyJSON (bo .Type , bo .Body ); err != nil {
107- return err
140+ if bo .hasTarget () {
141+ if err = st .New (bo .NewBlockOperationTargetKey (bo .Target ), bo .Hash ); err != nil {
142+ return
108143 }
109- if casted , ok = body .(operation. CreateAccount ); ! ok {
110- return errors . TypeOperationBodyNotMatched
144+ if err = st . New ( bo . NewBlockOperationTargetAndTypeKey ( bo . Target ), bo . Hash ); err != nil {
145+ return
111146 }
112- if casted .Linked != "" {
113- if err = st .New (GetBlockOperationCreateFrozenKey (casted .Target , bo .Height ), bo .Hash ); err != nil {
114- return err
115- }
116- if err = st .New (bo .NewBlockOperationFrozenLinkedKey (casted .Linked ), bo .Hash ); err != nil {
117- return err
118- }
147+ if err = st .New (bo .NewBlockOperationPeersKey (bo .Target ), bo .Hash ); err != nil {
148+ return
149+ }
150+ if err = st .New (bo .NewBlockOperationPeersAndTypeKey (bo .Target ), bo .Hash ); err != nil {
151+ return
119152 }
120153 }
121154
122- bo .isSaved = true
155+ if bo .targetIsLinked () {
156+ if err = st .New (GetBlockOperationCreateFrozenKey (bo .Target , bo .Height ), bo .Hash ); err != nil {
157+ return err
158+ }
159+ if err = st .New (bo .NewBlockOperationFrozenLinkedKey (bo .linked ), bo .Hash ); err != nil {
160+ return err
161+ }
162+ }
123163
124164 event := "saved"
125- event += " " + fmt .Sprintf ("source-%s" , bo .Source )
126165 event += " " + fmt .Sprintf ("hash-%s" , bo .Hash )
127166 event += " " + fmt .Sprintf ("txhash-%s" , bo .TxHash )
167+ event += " " + fmt .Sprintf ("source-%s" , bo .Source )
128168 event += " " + fmt .Sprintf ("source-type-%s%s" , bo .Source , bo .Type )
129- if casted .Linked != "" {
130- event += " frozen"
131- event += " " + fmt .Sprintf ("linked-%s" , casted .Linked )
169+
170+ if bo .hasTarget () {
171+ event += " " + fmt .Sprintf ("target-%s" , bo .Target )
172+ event += " " + fmt .Sprintf ("target-type-%s%s" , bo .Target , bo .Type )
173+ }
174+ if bo .targetIsLinked () {
175+ event += " " + "frozen"
176+ event += " " + fmt .Sprintf ("linked-%s" , bo .linked )
132177 }
133178 observer .BlockOperationObserver .Trigger (event , bo )
134179
180+ bo .isSaved = true
181+
135182 return nil
136183}
137184
@@ -140,6 +187,10 @@ func (bo BlockOperation) Serialize() (encoded []byte, err error) {
140187 return
141188}
142189
190+ func (bo BlockOperation ) BlockOrder () * BlockOrder {
191+ return bo .order
192+ }
193+
143194func GetBlockOperationKey (hash string ) string {
144195 idx := storage .NewIndex ()
145196 idx .WritePrefix (common .BlockOperationPrefixHash , hash )
@@ -171,9 +222,33 @@ func GetBlockOperationKeyPrefixSource(source string) string {
171222 return idx .String ()
172223}
173224
225+ func GetBlockOperationKeyPrefixTarget (target string ) string {
226+ idx := storage .NewIndex ()
227+ idx .WritePrefix (common .BlockOperationPrefixTarget , target )
228+ return idx .String ()
229+ }
230+
231+ func GetBlockOperationKeyPrefixPeers (addr string ) string {
232+ idx := storage .NewIndex ()
233+ idx .WritePrefix (common .BlockOperationPrefixPeers , addr )
234+ return idx .String ()
235+ }
236+
174237func GetBlockOperationKeyPrefixSourceAndType (source string , ty operation.OperationType ) string {
175238 idx := storage .NewIndex ()
176- idx .WritePrefix (common .BlockOperationPrefixSource , source , string (ty ))
239+ idx .WritePrefix (common .BlockOperationPrefixTypeSource , string (ty ), source )
240+ return idx .String ()
241+ }
242+
243+ func GetBlockOperationKeyPrefixTargetAndType (target string , ty operation.OperationType ) string {
244+ idx := storage .NewIndex ()
245+ idx .WritePrefix (common .BlockOperationPrefixTypeTarget , string (ty ), target )
246+ return idx .String ()
247+ }
248+
249+ func GetBlockOperationKeyPrefixPeersAndType (addr string , ty operation.OperationType ) string {
250+ idx := storage .NewIndex ()
251+ idx .WritePrefix (common .BlockOperationPrefixTypePeers , string (ty ), addr )
177252 return idx .String ()
178253}
179254
@@ -182,66 +257,55 @@ func (bo BlockOperation) NewBlockOperationTxHashKey() string {
182257 idx .WritePrefix (GetBlockOperationKeyPrefixTxHash (bo .TxHash ))
183258 bo .order .Index (idx )
184259 return idx .String ()
185- /*
186- return fmt.Sprintf(
187- "%s%s%s%s",
188- GetBlockOperationKeyPrefixTxHash(bo.TxHash),
189- common.EncodeUint64ToByteSlice(bo.Height),
190- common.EncodeUint64ToByteSlice(bo.TxIndex),
191- common.EncodeUint64ToByteSlice(bo.Index),
192- )
193- */
194- }
195-
196- func (bo BlockOperation ) NewBlockOperationSourceKey () string {
260+ }
261+
262+ func (bo BlockOperation ) NewBlockOperationSourceKey (source string ) string {
263+ idx := storage .NewIndex ()
264+ idx .WritePrefix (GetBlockOperationKeyPrefixSource (source ))
265+ bo .order .Index (idx )
266+ return idx .String ()
267+ }
268+
269+ func (bo BlockOperation ) NewBlockOperationTargetKey (target string ) string {
270+ idx := storage .NewIndex ()
271+ idx .WritePrefix (GetBlockOperationKeyPrefixTarget (target ))
272+ bo .order .Index (idx )
273+ return idx .String ()
274+ }
275+
276+ func (bo BlockOperation ) NewBlockOperationPeersKey (addr string ) string {
197277 idx := storage .NewIndex ()
198- idx .WritePrefix (GetBlockOperationKeyPrefixSource ( bo . Source ))
278+ idx .WritePrefix (GetBlockOperationKeyPrefixPeers ( addr ))
199279 bo .order .Index (idx )
200280 return idx .String ()
201- /*
202- return fmt.Sprintf(
203- "%s%s%s%s",
204- GetBlockOperationKeyPrefixSource(bo.Source),
205- common.EncodeUint64ToByteSlice(bo.Height),
206- common.EncodeUint64ToByteSlice(bo.TxIndex),
207- common.EncodeUint64ToByteSlice(bo.Index),
208- )
209- */
210281}
211282
212283func (bo BlockOperation ) NewBlockOperationFrozenLinkedKey (hash string ) string {
213284 idx := storage .NewIndex ()
214285 idx .WritePrefix (GetBlockOperationKeyPrefixFrozenLinked (hash ))
215286 bo .order .Index (idx )
216287 return idx .String ()
288+ }
217289
218- /*
219- return fmt.Sprintf(
220- "%s%s",
221- GetBlockOperationKeyPrefixFrozenLinked(hash),
222- common.EncodeUint64ToByteSlice(bo.Height),
223- )
224- */
290+ func (bo BlockOperation ) NewBlockOperationSourceAndTypeKey (source string ) string {
291+ idx := storage .NewIndex ()
292+ idx .WritePrefix (GetBlockOperationKeyPrefixSourceAndType (source , bo .Type ))
293+ bo .order .Index (idx )
294+ return idx .String ()
225295}
226296
227- func (bo BlockOperation ) NewBlockOperationSourceAndTypeKey ( ) string {
297+ func (bo BlockOperation ) NewBlockOperationTargetAndTypeKey ( target string ) string {
228298 idx := storage .NewIndex ()
229- idx .WritePrefix (GetBlockOperationKeyPrefixSourceAndType ( bo . Source , bo .Type ))
299+ idx .WritePrefix (GetBlockOperationKeyPrefixTargetAndType ( target , bo .Type ))
230300 bo .order .Index (idx )
231301 return idx .String ()
232- /*
233- return fmt.Sprintf(
234- "%s%s%s%s",
235- GetBlockOperationKeyPrefixSourceAndType(bo.Source, bo.Type),
236- common.EncodeUint64ToByteSlice(bo.Height),
237- common.EncodeUint64ToByteSlice(bo.TxIndex),
238- common.EncodeUint64ToByteSlice(bo.Index),
239- )
240- */
241302}
242303
243- func (bo BlockOperation ) BlockOrder () * BlockOrder {
244- return bo .order
304+ func (bo BlockOperation ) NewBlockOperationPeersAndTypeKey (addr string ) string {
305+ idx := storage .NewIndex ()
306+ idx .WritePrefix (GetBlockOperationKeyPrefixPeersAndType (addr , bo .Type ))
307+ bo .order .Index (idx )
308+ return idx .String ()
245309}
246310
247311func ExistsBlockOperation (st * storage.LevelDBBackend , hash string ) (bool , error ) {
@@ -296,6 +360,24 @@ func GetBlockOperationsByTxHash(st *storage.LevelDBBackend, txHash string, optio
296360 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
297361}
298362
363+ // Find all operations which created frozen account.
364+ func GetBlockOperationsByFrozen (st * storage.LevelDBBackend , options storage.ListOptions ) (
365+ func () (BlockOperation , bool , []byte ),
366+ func (),
367+ ) {
368+ iterFunc , closeFunc := st .GetIterator (common .BlockOperationPrefixCreateFrozen , options )
369+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
370+ }
371+
372+ // Find all operations which created frozen account and have the link of a general account's address.
373+ func GetBlockOperationsByLinked (st * storage.LevelDBBackend , hash string , options storage.ListOptions ) (
374+ func () (BlockOperation , bool , []byte ),
375+ func (),
376+ ) {
377+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixFrozenLinked (hash ), options )
378+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
379+ }
380+
299381func GetBlockOperationsBySource (st * storage.LevelDBBackend , source string , options storage.ListOptions ) (
300382 func () (BlockOperation , bool , []byte ),
301383 func (),
@@ -305,28 +387,44 @@ func GetBlockOperationsBySource(st *storage.LevelDBBackend, source string, optio
305387 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
306388}
307389
308- // Find all operations which created frozen account.
309- func GetBlockOperationsByFrozen (st * storage.LevelDBBackend , options storage.ListOptions ) (
390+ func GetBlockOperationsBySourceAndType (st * storage.LevelDBBackend , source string , ty operation.OperationType , options storage.ListOptions ) (
310391 func () (BlockOperation , bool , []byte ),
311392 func (),
312393) {
313- iterFunc , closeFunc := st .GetIterator (common . BlockOperationPrefixCreateFrozen , options )
394+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixSourceAndType ( source , ty ) , options )
314395 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
315396}
316397
317- // Find all operations which created frozen account and have the link of a general account's address.
318- func GetBlockOperationsByLinked (st * storage.LevelDBBackend , hash string , options storage.ListOptions ) (
398+ func GetBlockOperationsByTarget (st * storage.LevelDBBackend , target string , options storage.ListOptions ) (
319399 func () (BlockOperation , bool , []byte ),
320400 func (),
321401) {
322- iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixFrozenLinked (hash ), options )
402+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixTarget (target ), options )
403+
323404 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
324405}
325406
326- func GetBlockOperationsBySourceAndType (st * storage.LevelDBBackend , source string , ty operation.OperationType , options storage.ListOptions ) (
407+ func GetBlockOperationsByTargetAndType (st * storage.LevelDBBackend , target string , ty operation.OperationType , options storage.ListOptions ) (
327408 func () (BlockOperation , bool , []byte ),
328409 func (),
329410) {
330- iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixSourceAndType (source , ty ), options )
411+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixTargetAndType (target , ty ), options )
412+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
413+ }
414+
415+ func GetBlockOperationsByPeers (st * storage.LevelDBBackend , addr string , options storage.ListOptions ) (
416+ func () (BlockOperation , bool , []byte ),
417+ func (),
418+ ) {
419+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixPeers (addr ), options )
420+
421+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
422+ }
423+
424+ func GetBlockOperationsByPeersAndType (st * storage.LevelDBBackend , addr string , ty operation.OperationType , options storage.ListOptions ) (
425+ func () (BlockOperation , bool , []byte ),
426+ func (),
427+ ) {
428+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixPeersAndType (addr , ty ), options )
331429 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
332430}
0 commit comments