Skip to content

Commit 232dc62

Browse files
committed
Operation index for target
1 parent 4483b18 commit 232dc62

3 files changed

Lines changed: 109 additions & 34 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/nullstyle/go-xdr v0.0.0-20170810174627-a875e7c9fa23 // indirect
3131
github.com/nvellon/hal v0.3.0
3232
github.com/oklog/run v1.0.0
33-
github.com/onsi/ginkgo v1.6.0 // indirect
33+
github.com/onsi/ginkgo v1.6.0
3434
github.com/onsi/gomega v1.4.1 // indirect
3535
github.com/pkg/errors v0.8.0
3636
github.com/pmezard/go-difflib v1.0.0 // indirect

lib/block/operation.go

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
165178
func 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+
169186
func (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+
207244
func 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+
}

lib/node/runner/api/operation.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,55 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter
3939

4040
var iterFunc func() (block.BlockOperation, bool, []byte)
4141
var closeFunc func()
42-
if len(oType) > 0 {
43-
iterFunc, closeFunc = block.GetBlockOperationsBySourceAndType(api.storage, address, oType, options)
44-
} else {
45-
iterFunc, closeFunc = block.GetBlockOperationsBySource(api.storage, address, options)
42+
{
43+
if len(oType) > 0 {
44+
iterFunc, closeFunc = block.GetBlockOperationsBySourceAndType(api.storage, address, oType, options)
45+
} else {
46+
iterFunc, closeFunc = block.GetBlockOperationsBySource(api.storage, address, options)
47+
}
48+
for {
49+
t, hasNext, c := iterFunc()
50+
cursor = c
51+
if !hasNext {
52+
break
53+
}
54+
txs = append(txs, resource.NewOperation(&t))
55+
}
56+
closeFunc()
4657
}
47-
for {
48-
t, hasNext, c := iterFunc()
49-
cursor = c
50-
if !hasNext {
51-
break
58+
{
59+
if len(oType) > 0 {
60+
iterFunc, closeFunc = block.GetBlockOperationsByTargetAndType(api.storage, address, oType, options)
61+
} else {
62+
iterFunc, closeFunc = block.GetBlockOperationsByTarget(api.storage, address, options)
63+
}
64+
for {
65+
t, hasNext, c := iterFunc()
66+
cursor = c
67+
if !hasNext {
68+
break
69+
}
70+
txs = append(txs, resource.NewOperation(&t))
5271
}
53-
txs = append(txs, resource.NewOperation(&t))
72+
closeFunc()
5473
}
55-
closeFunc()
5674
return txs
5775
}
5876

5977
if httputils.IsEventStream(r) {
60-
event := fmt.Sprintf("source-%s", address)
78+
var events []string
79+
events = append(events, fmt.Sprintf("source-%s", address))
80+
events = append(events, fmt.Sprintf("target-%s", address))
6181
if len(oType) > 0 {
62-
event = fmt.Sprintf("source-type-%s%s", address, oType)
82+
events = append(events, fmt.Sprintf("source-type-%s%s", address, oType))
83+
events = append(events, fmt.Sprintf("target-type-%s%s", address, oType))
6384
}
6485
es := NewEventStream(w, r, renderEventStream, DefaultContentType)
6586
txs := readFunc()
6687
for _, tx := range txs {
6788
es.Render(tx)
6889
}
69-
es.Run(observer.BlockOperationObserver, event)
90+
es.Run(observer.BlockOperationObserver, events...)
7091
return
7192
}
7293

0 commit comments

Comments
 (0)