Skip to content

Commit

Permalink
most debug yet
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 11, 2025
1 parent cb90d0f commit 5298c9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/runtime/hostfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ func RegisterHostFunctions(runtime wazero.Runtime, env *RuntimeEnvironment) (waz
return hostQueryExternal(ctx, m, reqPtr, reqLen, gasLimit)
}).
WithParameterNames("req_ptr", "req_len", "gas_limit").
WithResultNames("res_ptr", "res_len").
Export("querier_query")

// Register secp256k1_verify function
Expand Down
61 changes: 55 additions & 6 deletions internal/runtime/wazeroruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,25 +800,59 @@ func (w *WazeroRuntime) Reply(checksum, env, reply []byte, otherParams ...interf
return w.callContractFn("reply", checksum, env, nil, reply, gasMeter, store, api, querier, gasLimit, printDebug)
}

// ByteSliceView represents a view into a Go byte slice without copying
type ByteSliceView struct {
IsNil bool
Data []byte
}

func NewByteSliceView(data []byte) ByteSliceView {
if data == nil {
return ByteSliceView{
IsNil: true,
Data: nil,
}
}
return ByteSliceView{
IsNil: false,
Data: data,
}
}

func (w *WazeroRuntime) Query(checksum, env, query []byte, otherParams ...interface{}) ([]byte, types.GasReport, error) {
gasMeter, store, api, querier, gasLimit, printDebug, err := w.parseParams(otherParams)
if err != nil {
return nil, types.GasReport{}, err
}

// Create ByteSliceView for query to avoid unnecessary copying
queryView := NewByteSliceView(query)
defer func() {
// Clear the view when done
queryView.Data = nil
}()

// Create gas state for tracking memory operations
gasState := NewGasState(gasLimit)

// Account for memory view creation
if !queryView.IsNil {
gasState.ConsumeGas(uint64(len(queryView.Data))*DefaultGasConfig().PerByte, "query memory view")
}

// Set the contract execution environment
w.kvStore = store
w.api = api
w.querier = *querier

// Create runtime environment
// Create runtime environment with gas tracking
runtimeEnv := &RuntimeEnvironment{
DB: store,
API: *api,
Querier: *querier,
Gas: *gasMeter,
gasLimit: gasLimit,
gasUsed: 0,
gasLimit: gasState.GetGasLimit() - gasState.GetGasUsed(), // Adjust gas limit for memory operations
gasUsed: gasState.GetGasUsed(),
iterators: make(map[uint64]map[uint64]types.Iterator),
nextCallID: 1,
}
Expand Down Expand Up @@ -895,6 +929,21 @@ func (w *WazeroRuntime) Query(checksum, env, query []byte, otherParams ...interf
return nil, types.GasReport{}, fmt.Errorf("failed to write request to memory: %w", err)
}

if err := json.Unmarshal(requestBytes, &request); err != nil {
return nil, types.GasReport{}, fmt.Errorf("failed to parse request: %w", err)
}

// Write env and msg to memory separately
envPtr, _, err := mm.writeToMemory(request.Env, printDebug)
if err != nil {
return nil, types.GasReport{}, fmt.Errorf("failed to write env to memory: %w", err)
}

msgPtr, _, err := mm.writeToMemory(request.Msg, printDebug)
if err != nil {
return nil, types.GasReport{}, fmt.Errorf("failed to write msg to memory: %w", err)
}

// Get the query function
fn := contractModule.ExportedFunction("query")
if fn == nil {
Expand All @@ -907,8 +956,8 @@ func (w *WazeroRuntime) Query(checksum, env, query []byte, otherParams ...interf
fmt.Printf("[DEBUG] Calling query function...\n")
}

// Call the query function with the request pointer
results, err := fn.Call(ctx, uint64(requestPtr))
// Call the query function with environment and message pointers
results, err := fn.Call(ctx, uint64(envPtr), uint64(msgPtr))
if err != nil {
if printDebug {
fmt.Printf("[DEBUG] Query call failed: %v\n", err)
Expand Down Expand Up @@ -950,7 +999,7 @@ func (w *WazeroRuntime) Query(checksum, env, query []byte, otherParams ...interf
if printDebug {
fmt.Printf("[DEBUG] Failed to read result data from memory\n")
}
return nil, types.GasReport{}, fmt.Errorf("failed to read result data from memory")
return nil, types.GasReport{}, fmt.Errorf("failed to read result from memory")
}

// Create gas report
Expand Down

0 comments on commit 5298c9d

Please sign in to comment.