Skip to content

Commit

Permalink
Add leaked logs example (#43)
Browse files Browse the repository at this point in the history
* Add leaked logs example

* Update suave-std
  • Loading branch information
ferranbt authored Mar 1, 2024
1 parent bba2fe5 commit ca97abd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ run-integration:
go run examples/mevm-is-confidential/main.go
go run examples/onchain-callback/main.go
go run examples/onchain-state/main.go
go run examples/offchain-logs/main.go
go run examples/mevm-context/main.go
35 changes: 35 additions & 0 deletions examples/offchain-logs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Example Suapp with Offchain logs

This example shows how Suapps can emit logs during the confidential execution that are leaked during the onchain callback. To do so, the Suapp has to import the `suave-std/Suapp.sol` contract and use the `emitOffchainLogs` modifier in the onchain callback function. Then, logs emitted during the confidential execution which triggers the onchain computation will be emitted on the Suave chain.

The Suapp will look like this:

```
import "suave-std/Suapp.sol";
contract ExampleSuapp is Suapp {
function onchainCallback() public emitOffchainLogs {
}
event OffchainLog();
function offchain() public {
emit OffchainLog();
return abi.encodeWithSelector(this.onchainCallback.selector);
}
}
```

## How to use

Run `Suave` in development mode:

```
$ suave --suave.dev
```

Execute the deployment script:

```
$ go run main.go
```
23 changes: 23 additions & 0 deletions examples/offchain-logs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"log"

"github.com/flashbots/suapp-examples/framework"
)

func main() {
fr := framework.New()
contract := fr.Suave.DeployContract("offchain-logs.sol/OffchainLogs.json")

receipt := contract.SendTransaction("example", nil, nil)
if len(receipt.Logs) != 2 {
log.Fatal("two logs expected")
}

// emit the CCR but DO NOT leak the logs
receipt = contract.SendTransaction("exampleNoLogs", nil, nil)
if len(receipt.Logs) != 1 {
log.Fatal("only one log expected")
}
}
32 changes: 32 additions & 0 deletions examples/offchain-logs/offchain-logs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.8;

import "suave-std/Suapp.sol";

contract OffchainLogs is Suapp {
event OnchainEvent(uint256 num);
event OffchainEvent(uint256 num);

function emitCallbackWithLogs(uint256 num) public emitOffchainLogs {
emit OnchainEvent(num);
}

function example() external returns (bytes memory) {
emit OffchainEvent(101);

return bytes.concat(this.emitCallbackWithLogs.selector, abi.encode(101));
}

/* This function pair do not leak the events in the onchain transaction */

function emitCallbackWithoutLogs(uint256 num) public {
// this callback **does not** emit logs
emit OnchainEvent(num);
}

function exampleNoLogs() external returns (bytes memory) {
emit OffchainEvent(101);

return bytes.concat(this.emitCallbackWithoutLogs.selector, abi.encode(101));
}
}
5 changes: 0 additions & 5 deletions examples/onchain-callback/onchain-callback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import "suave-std/suavelib/Suave.sol";
contract OnChainCallback {
event CallbackEvent(uint256 num);

event NilEvent();

function emitCallback(uint256 num) public {
emit CallbackEvent(num);
}

function example() external returns (bytes memory) {
// event emitted in the off-chain confidential context, no effect.
emit NilEvent();

return bytes.concat(this.emitCallback.selector, abi.encode(1));
}
}

0 comments on commit ca97abd

Please sign in to comment.