2023-11-07 10:57:30 +00:00
|
|
|
package commonclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-12-18 13:13:36 +00:00
|
|
|
"github.com/google/uuid"
|
2023-11-07 10:57:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2023-12-18 13:13:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2023-11-07 10:57:30 +00:00
|
|
|
"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")
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:13:36 +00:00
|
|
|
script, err := smartcontract.CreateCallAndPrefetchIteratorScript(contract, method, batchSize, params...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't create unwrap script: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, sessionID, iter, err := unwrap.ArrayAndSessionIterator(inv.Run(script))
|
2023-11-07 10:57:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unwrap session iterator: %w", err)
|
|
|
|
}
|
2023-12-18 13:13:36 +00:00
|
|
|
if (sessionID == uuid.UUID{}) {
|
|
|
|
return arr, nil
|
|
|
|
}
|
2023-11-07 10:57:30 +00:00
|
|
|
|
2024-04-09 11:15:39 +00:00
|
|
|
defer func() {
|
|
|
|
_ = inv.TerminateSession(sessionID)
|
|
|
|
}()
|
|
|
|
|
2023-11-07 10:57:30 +00:00
|
|
|
var shouldStop bool
|
2023-12-18 13:13:36 +00:00
|
|
|
res := arr
|
2023-11-07 10:57:30 +00:00
|
|
|
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
|
|
|
|
}
|