Skip to content

Commit

Permalink
add limitation for parallel requests from kvasir; optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Nov 14, 2024
1 parent ff25089 commit c31a63a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions kvasir/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kvasir

import (
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -39,6 +40,7 @@ type ReportMsgWithKey struct {
}

type Context struct {
wg sync.WaitGroup
client rpcclient.Client
validator sdk.ValAddress
validatorAccAddr sdk.AccAddress
Expand Down
30 changes: 30 additions & 0 deletions kvasir/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type rawRequest struct {
calldata string
}

type requestValues struct {
id string
contract string
}

// GetEventValues returns the list of all values in the given log with the given type and key.
func GetEventValues(log []abci.Event, evType string, evKey string) (res []string) {
for _, ev := range log {
Expand All @@ -25,3 +30,28 @@ func GetEventValues(log []abci.Event, evType string, evKey string) (res []string
}
return res
}

func GetEventRequestValues(log []abci.Event, evType string, idKey, contractKey string) (res []requestValues) {
for _, ev := range log {
if ev.Type != evType {
continue
}

values := requestValues{}

for _, attr := range ev.Attributes {
if attr.Key == idKey {
values.id = attr.Value
}

if attr.Key == contractKey {
values.contract = attr.Value
}
}

if values.id != "" && values.contract != "" {
res = append(res, values)
}
}
return res
}
17 changes: 10 additions & 7 deletions kvasir/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ func handleTransaction(c *Context, l *Logger, tx abci.TxResult) {
}

func handleRequestLog(c *Context, l *Logger, log []abci.Event) {
idStrs := GetEventValues(log, "wasm-wasm_request", "id")
contractStrs := GetEventValues(log, "wasm-wasm_request", "contract")
contractEvents := GetEventRequestValues(log, "wasm-wasm_request", "id", "contract")

for i, idStr := range idStrs {
id, err := strconv.ParseUint(idStr, 10, 64)
for _, contractEvent := range contractEvents {
id, err := strconv.ParseUint(contractEvent.id, 10, 64)
if err != nil {
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, idStr, err.Error())
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, contractEvent.id, err.Error())
return
}

// TODO: write better solution
contract := contractStrs[i]
contract := contractEvent.contract

// If id is in pending requests list, then skip it.
if c.pendingRequests[RequestKey{
Expand Down Expand Up @@ -117,6 +115,11 @@ func handleRawRequest(
return nil, err
}

// allow only one request at a time
c.wg.Wait()
c.wg.Add(1)
defer c.wg.Done()

result, err := c.executor.Exec(req.calldata, map[string]interface{}{
"ODIN_CHAIN_ID": vmsg.ChainID,
"ODIN_CONTRACT": vmsg.Contract,
Expand Down

0 comments on commit c31a63a

Please sign in to comment.