Skip to content

Commit

Permalink
update rpc InvokeScriptAndIterate
Browse files Browse the repository at this point in the history
  • Loading branch information
joeqian10 committed Nov 15, 2022
1 parent b6208a8 commit 8452fe1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
18 changes: 18 additions & 0 deletions rpc/models/rpcInvoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ type InvokeStack struct {
Interface string `json:"interface,omitempty"`
Id string `json:"id,omitempty"`
}

// ConvertInvokeStackArray converts an "Array" type InvokeStack to an InvokeStack array
func ConvertInvokeStackArray(s InvokeStack) []InvokeStack {
if s.Type != "Array" {
return []InvokeStack{s}
}
vs := s.Value.([]interface{})
result := make([]InvokeStack, len(vs))
for i, v := range vs {
m := v.(map[string]interface{})
s2 := InvokeStack{
Type: m["type"].(string),
Value: m["value"],
}
result[i] = s2
}
return result
}
27 changes: 20 additions & 7 deletions rpc/rpcClient.SmartContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,14 @@ func (n *RpcClient) InvokeFunctionAndIterate(scriptHash string, method string, a
func (n *RpcClient) InvokeScriptAndIterate(scriptInBase64 string, signersOrWitnesses interface{}, useDiagnostic bool,
count int32) ([][]models.InvokeStack, error) {

response := n.InvokeScript(scriptInBase64, signersOrWitnesses, useDiagnostic)
if count > 10000 {
return nil, fmt.Errorf("max count exceeded, count should not exceed 10000")
}
if count < 0 {
return nil, fmt.Errorf("count should not be less than zero")
}

response := n.InvokeScript(scriptInBase64, signersOrWitnesses, useDiagnostic)
invokeStacks, err := PopInvokeStacks(response)
if err != nil {
return nil, err
Expand All @@ -176,17 +182,24 @@ func (n *RpcClient) InvokeScriptAndIterate(scriptInBase64 string, signersOrWitne
return nil, nil
}

var iterateStacks [][]models.InvokeStack
var iterateStacks [][]models.InvokeStack // this array is for all the iterators in this session
for _, invokeStack := range invokeStacks {
if invokeStack.Type == "InteropInterface" &&
invokeStack.Interface == "IIterator" &&
invokeStack.Id != "" {
// call TraverseIterator
iterateResponse := n.TraverseIterator(sessionId, invokeStack.Id, count)
if iterateResponse.HasError() {
return nil, fmt.Errorf(iterateResponse.GetErrorInfo())
var iteStacks []models.InvokeStack // this array is for one unique iterator id
for ; count > 0; count -= 100 {
iterateResponse := n.TraverseIterator(sessionId, invokeStack.Id, 100) // max iterate count is 100 for one request
if iterateResponse.HasError() {
return nil, fmt.Errorf(iterateResponse.GetErrorInfo())
}
if len(iterateResponse.Result) == 0 { // the iterator reaches the end
break
}
iteStacks = append(iteStacks, iterateResponse.Result...)
}
iterateStacks = append(iterateStacks, iterateResponse.Result)

iterateStacks = append(iterateStacks, iteStacks)
}
}
return iterateStacks, nil
Expand Down

0 comments on commit 8452fe1

Please sign in to comment.