forked from TrueCloudLab/frostfs-contract
[#48] Add commonclient package
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
parent
5cc810096f
commit
b76f592095
3 changed files with 118 additions and 0 deletions
35
commonclient/iterator.go
Normal file
35
commonclient/iterator.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package commonclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
||||
// ReadIteratorItems calls method that returns iterator and traverses the iterator until all items are read into array.
|
||||
func ReadIteratorItems(inv Invoker, batchSize int, contract util.Uint160, method string, params ...any) ([]stackitem.Item, error) {
|
||||
if batchSize <= 0 {
|
||||
panic("batch size must be positive")
|
||||
}
|
||||
|
||||
sessionID, iter, err := unwrap.SessionIterator(inv.Call(contract, method, params...))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unwrap session iterator: %w", err)
|
||||
}
|
||||
|
||||
var shouldStop bool
|
||||
res := make([]stackitem.Item, 0, len(iter.Values))
|
||||
for !shouldStop {
|
||||
items, err := inv.TraverseIterator(sessionID, &iter, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res = append(res, items...)
|
||||
shouldStop = len(items) < batchSize
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue