@@ -32,6 +32,7 @@ type BlockOperation struct {
3232
3333 // transaction will be used only for `Save` time.
3434 transaction transaction.Transaction
35+ operation operation.Operation
3536 isSaved bool
3637}
3738
@@ -60,6 +61,7 @@ func NewBlockOperationFromOperation(op operation.Operation, tx transaction.Trans
6061 Height : blockHeight ,
6162
6263 transaction : tx ,
64+ operation : op ,
6365 }, nil
6466}
6567
@@ -87,27 +89,35 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) {
8789 return
8890 }
8991
92+ targetEvent := ""
93+ if pop , ok := bo .operation .B .(operation.Payable ); ok {
94+ target := pop .TargetAddress ()
95+ if err = st .New (bo .NewBlockOperationTargetKey (target ), bo .Hash ); err != nil {
96+ return
97+ }
98+ if err = st .New (bo .NewBlockOperationTargetAndTypeKey (target ), bo .Hash ); err != nil {
99+ return
100+ }
101+ targetEvent += " " + fmt .Sprintf ("target-%s" , target )
102+ targetEvent += " " + fmt .Sprintf ("target-type-%s%s" , target , bo .Type )
103+ }
104+
90105 if err = st .New (bo .NewBlockOperationSourceAndTypeKey (), bo .Hash ); err != nil {
91106 return
92107 }
93108
94- var body operation.Body
95- var casted operation.CreateAccount
96- var ok bool
97-
109+ frozenEvent := ""
98110 if bo .Type == operation .TypeCreateAccount {
99- if body , err = operation .UnmarshalBodyJSON (bo .Type , bo .Body ); err != nil {
100- return err
101- }
102- if casted , ok = body .(operation.CreateAccount ); ! ok {
103- return errors .TypeOperationBodyNotMatched
104- }
105- if casted .Linked != "" {
106- if err = st .New (GetBlockOperationCreateFrozenKey (casted .Target , bo .Height ), bo .Hash ); err != nil {
107- return err
108- }
109- if err = st .New (bo .NewBlockOperationFrozenLinkedKey (casted .Linked ), bo .Hash ); err != nil {
110- return err
111+ if createAccount , ok := bo .operation .B .(* operation.CreateAccount ); ok {
112+ if createAccount .Linked != "" {
113+ if err = st .New (GetBlockOperationCreateFrozenKey (createAccount .Target , bo .Height ), bo .Hash ); err != nil {
114+ return err
115+ }
116+ if err = st .New (bo .NewBlockOperationFrozenLinkedKey (createAccount .Linked ), bo .Hash ); err != nil {
117+ return err
118+ }
119+ frozenEvent += " " + "frozen"
120+ frozenEvent += " " + fmt .Sprintf ("linked-%s" , createAccount .Linked )
111121 }
112122 }
113123 }
@@ -119,9 +129,8 @@ func (bo *BlockOperation) Save(st *storage.LevelDBBackend) (err error) {
119129 event += " " + fmt .Sprintf ("hash-%s" , bo .Hash )
120130 event += " " + fmt .Sprintf ("txhash-%s" , bo .TxHash )
121131 event += " " + fmt .Sprintf ("source-type-%s%s" , bo .Source , bo .Type )
122- if casted .Linked != "" {
123- event += " frozen"
124- event += " " + fmt .Sprintf ("linked-%s" , casted .Linked )
132+ if len (frozenEvent ) > 0 {
133+ event += frozenEvent
125134 }
126135 observer .BlockOperationObserver .Trigger (event , bo )
127136
@@ -162,10 +171,18 @@ func GetBlockOperationKeyPrefixSource(source string) string {
162171 return fmt .Sprintf ("%s%s-" , common .BlockOperationPrefixSource , source )
163172}
164173
174+ func GetBlockOperationKeyPrefixTarget (target string ) string {
175+ return fmt .Sprintf ("%s%s-" , common .BlockOperationPrefixTarget , target )
176+ }
177+
165178func GetBlockOperationKeyPrefixSourceAndType (source string , ty operation.OperationType ) string {
166179 return fmt .Sprintf ("%s%s%s-" , common .BlockOperationPrefixSource , source , string (ty ))
167180}
168181
182+ func GetBlockOperationKeyPrefixTargetAndType (target string , ty operation.OperationType ) string {
183+ return fmt .Sprintf ("%s%s%s-" , common .BlockOperationPrefixTarget , target , string (ty ))
184+ }
185+
169186func (bo BlockOperation ) NewBlockOperationTxHashKey () string {
170187 return fmt .Sprintf (
171188 "%s%s%s%s" ,
@@ -204,6 +221,26 @@ func (bo BlockOperation) NewBlockOperationSourceAndTypeKey() string {
204221 )
205222}
206223
224+ func (bo BlockOperation ) NewBlockOperationTargetKey (target string ) string {
225+ return fmt .Sprintf (
226+ "%s%s%s%s" ,
227+ GetBlockOperationKeyPrefixTarget (target ),
228+ common .EncodeUint64ToByteSlice (bo .Height ),
229+ common .EncodeUint64ToByteSlice (bo .transaction .B .SequenceID ),
230+ common .GetUniqueIDFromUUID (),
231+ )
232+ }
233+
234+ func (bo BlockOperation ) NewBlockOperationTargetAndTypeKey (target string ) string {
235+ return fmt .Sprintf (
236+ "%s%s%s%s" ,
237+ GetBlockOperationKeyPrefixTargetAndType (target , bo .Type ),
238+ common .EncodeUint64ToByteSlice (bo .Height ),
239+ common .EncodeUint64ToByteSlice (bo .transaction .B .SequenceID ),
240+ common .GetUniqueIDFromUUID (),
241+ )
242+ }
243+
207244func ExistsBlockOperation (st * storage.LevelDBBackend , hash string ) (bool , error ) {
208245 return st .Has (GetBlockOperationKey (hash ))
209246}
@@ -289,3 +326,20 @@ func GetBlockOperationsBySourceAndType(st *storage.LevelDBBackend, source string
289326 iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixSourceAndType (source , ty ), options )
290327 return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
291328}
329+
330+ func GetBlockOperationsByTarget (st * storage.LevelDBBackend , target string , options storage.ListOptions ) (
331+ func () (BlockOperation , bool , []byte ),
332+ func (),
333+ ) {
334+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixTarget (target ), options )
335+
336+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
337+ }
338+
339+ func GetBlockOperationsByTargetAndType (st * storage.LevelDBBackend , target string , ty operation.OperationType , options storage.ListOptions ) (
340+ func () (BlockOperation , bool , []byte ),
341+ func (),
342+ ) {
343+ iterFunc , closeFunc := st .GetIterator (GetBlockOperationKeyPrefixTargetAndType (target , ty ), options )
344+ return LoadBlockOperationsInsideIterator (st , iterFunc , closeFunc )
345+ }
0 commit comments