Skip to content

Commit be9f1d4

Browse files
committed
Remove Prev,NextOrder in BlockOrder and change operation handelrs
1 parent c8852c0 commit be9f1d4

File tree

7 files changed

+41
-62
lines changed

7 files changed

+41
-62
lines changed

lib/block/operation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ func (bo BlockOperation) NewBlockOperationSourceAndTypeKey() string {
240240
*/
241241
}
242242

243+
func (bo BlockOperation) BlockOrder() *BlockOrder {
244+
return bo.order
245+
}
246+
243247
func ExistsBlockOperation(st *storage.LevelDBBackend, hash string) (bool, error) {
244248
return st.Has(GetBlockOperationKey(hash))
245249
}

lib/block/order.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,3 @@ func (o *BlockOrder) formatText(xs []uint64) string {
5151
func (o *BlockOrder) String() string {
5252
return o.formatText(o.parts)
5353
}
54-
55-
func (o *BlockOrder) NextString() string {
56-
return o.formatText(o.NextOrder())
57-
}
58-
59-
func (o *BlockOrder) PrevString() string {
60-
return o.formatText(o.PrevOrder())
61-
}
62-
63-
func (o *BlockOrder) NextOrder() (xs []uint64) {
64-
if o == nil || len(o.parts) <= 0 {
65-
return
66-
}
67-
for i, x := range o.parts {
68-
if i == len(xs)-1 {
69-
x++
70-
}
71-
xs = append(xs, x)
72-
}
73-
return
74-
}
75-
76-
func (o *BlockOrder) PrevOrder() []uint64 {
77-
if o == nil || len(o.parts) <= 0 {
78-
return []uint64{}
79-
}
80-
return blockOrderPrevOrder(o.parts, len(o.parts)-1)
81-
}
82-
83-
func blockOrderPrevOrder(xs []uint64, pos int) []uint64 {
84-
if pos == 0 || len(xs)-1 < pos {
85-
return xs
86-
}
87-
if xs[pos] > 0 {
88-
xs[pos]--
89-
return xs
90-
}
91-
return blockOrderPrevOrder(xs, pos-1)
92-
}

lib/node/runner/api/operation.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,26 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter
2424
return
2525
}
2626

27-
options := p.ListOptions()
28-
2927
oTypeStr := r.URL.Query().Get("type")
3028
if len(oTypeStr) > 0 && !operation.IsValidOperationType(oTypeStr) {
3129
httputils.WriteJSONError(w, errors.InvalidQueryString)
3230
return
3331
}
3432

3533
oType := operation.OperationType(oTypeStr)
36-
var cursor []byte
34+
35+
prefix := block.GetBlockOperationKeyPrefixSourceAndType(address, oType)
36+
if len(oType) > 0 {
37+
prefix = block.GetBlockOperationKeyPrefixSource(address)
38+
}
39+
40+
options, err := p.PageCursorListOptions(prefix)
41+
if err != nil {
42+
httputils.WriteJSONError(w, err)
43+
return
44+
}
45+
46+
var order *block.BlockOrder
3747
readFunc := func() []resource.Resource {
3848
var txs []resource.Resource
3949

@@ -45,11 +55,11 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter
4555
iterFunc, closeFunc = block.GetBlockOperationsBySource(api.storage, address, options)
4656
}
4757
for {
48-
t, hasNext, c := iterFunc()
49-
cursor = c
58+
t, hasNext, _ := iterFunc()
5059
if !hasNext {
5160
break
5261
}
62+
order = t.BlockOrder()
5363
txs = append(txs, resource.NewOperation(&t))
5464
}
5565
closeFunc()
@@ -79,6 +89,6 @@ func (api NetworkHandlerAPI) GetOperationsByAccountHandler(w http.ResponseWriter
7989
}
8090

8191
txs := readFunc()
82-
list := p.ResourceList(txs, cursor)
92+
list := p.ResourceListWithOrder(txs, order)
8393
httputils.MustWriteJSON(w, 200, list)
8494
}

lib/node/runner/api/pagequery.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ func (p *PageQuery) ResourceList(rs []resource.Resource, cursor []byte) *resourc
8686
}
8787

8888
func (p *PageQuery) ResourceListWithOrder(rs []resource.Resource, order *block.BlockOrder) *resource.ResourceList {
89-
var cursor []byte
90-
if p.reverse == false {
91-
cursor = []byte(order.NextString())
92-
} else {
93-
cursor = []byte(order.PrevString())
94-
}
89+
cursor := []byte(order.String())
9590
return p.ResourceList(rs, cursor)
9691
}
9792

lib/node/runner/api/transaction.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,21 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit
109109
return
110110
}
111111

112-
var options = p.ListOptions()
113-
var cursor []byte
112+
options, err := p.PageCursorListOptions(block.GetBlockTransactionKeyPrefixAccount(address))
113+
if err != nil {
114+
httputils.WriteJSONError(w, err)
115+
return
116+
}
117+
var order *block.BlockOrder
114118
readFunc := func() []resource.Resource {
115119
var txs []resource.Resource
116120
iterFunc, closeFunc := block.GetBlockTransactionsByAccount(api.storage, address, options)
117121
for {
118-
t, hasNext, c := iterFunc()
119-
cursor = c
122+
t, hasNext, _ := iterFunc()
120123
if !hasNext {
121124
break
122125
}
126+
order = t.BlockOrder()
123127
txs = append(txs, resource.NewTransaction(&t))
124128
}
125129
closeFunc()
@@ -139,6 +143,6 @@ func (api NetworkHandlerAPI) GetTransactionsByAccountHandler(w http.ResponseWrit
139143
}
140144

141145
txs := readFunc()
142-
list := p.ResourceList(txs, cursor)
146+
list := p.ResourceListWithOrder(txs, order)
143147
httputils.MustWriteJSON(w, 200, list)
144148
}

lib/node/runner/api/transaction_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ func TestGetTransactionsHandlerPage(t *testing.T) {
350350

351351
{
352352
records, _ := requestFunction(nextLink)
353+
println(len(records))
353354
require.Equal(t, len(btList[5:]), len(records), "length is not the same")
354355

355356
for i, r := range records {

lib/node/runner/api/tx_operations.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ func (api NetworkHandlerAPI) GetOperationsByTxHashHandler(w http.ResponseWriter,
2828
return
2929
}
3030

31-
options := p.ListOptions()
31+
options, err := p.PageCursorListOptions(block.GetBlockOperationKeyPrefixTxHash(hash))
32+
if err != nil {
33+
httputils.WriteJSONError(w, err)
34+
return
35+
}
3236

3337
if httputils.IsEventStream(r) {
3438
event := fmt.Sprintf("txhash-%s", hash)
@@ -41,24 +45,24 @@ func (api NetworkHandlerAPI) GetOperationsByTxHashHandler(w http.ResponseWriter,
4145
return
4246
}
4347

44-
ops, cursor := api.getOperationsByTxHash(hash, options)
48+
ops, order := api.getOperationsByTxHash(hash, options)
4549
if len(ops) < 1 {
4650
httputils.WriteJSONError(w, errors.BlockTransactionDoesNotExists)
4751
return
4852
}
4953

50-
list := p.ResourceList(ops, cursor)
54+
list := p.ResourceListWithOrder(ops, order)
5155
httputils.MustWriteJSON(w, 200, list)
5256
}
5357

54-
func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, options storage.ListOptions) (txs []resource.Resource, cursor []byte) {
58+
func (api NetworkHandlerAPI) getOperationsByTxHash(txHash string, options storage.ListOptions) (txs []resource.Resource, order *block.BlockOrder) {
5559
iterFunc, closeFunc := block.GetBlockOperationsByTxHash(api.storage, txHash, options)
5660
for {
57-
o, hasNext, c := iterFunc()
58-
cursor = c
61+
o, hasNext, _ := iterFunc()
5962
if !hasNext {
6063
break
6164
}
65+
order = o.BlockOrder()
6266
txs = append(txs, resource.NewOperation(&o))
6367
}
6468
closeFunc()

0 commit comments

Comments
 (0)