Skip to content

Commit

Permalink
feat: implement getInsecureTime precompile (#278)
Browse files Browse the repository at this point in the history
* feat: implement getInsecureTime precompile

* update description

* Return milliseconds in getInsecureTime
  • Loading branch information
pistomat authored Sep 5, 2024
1 parent 245155c commit 0ca15b7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/types/suave_structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/rand"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -50,6 +51,10 @@ func (b *suaveRuntime) randomBytes(numBytes uint8) ([]byte, error) {
return buf, nil
}

func (b *suaveRuntime) getInsecureTime() (*big.Int, error) {
return big.NewInt(time.Now().UnixMilli()), nil
}

/* Confidential store precompiles */

func (b *suaveRuntime) confidentialStore(dataId types.DataId, key string, data []byte) error {
Expand Down
44 changes: 42 additions & 2 deletions core/vm/contracts_suave_runtime_adapter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions core/vm/contracts_suave_runtime_adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -103,6 +104,10 @@ func (m *mockRuntime) randomBytes(length uint8) ([]byte, error) {
return bytes, nil
}

func (m *mockRuntime) getInsecureTime() (*big.Int, error) {
return big.NewInt(1), nil
}

func (m *mockRuntime) aesEncrypt(key []byte, message []byte) ([]byte, error) {
return []byte{0x1}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion suave/artifacts/SuaveLib.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion suave/artifacts/addresses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"df29ee92c6cbdf8433c246433ac49ec6","source_id_to_path":{"0":"lib/forge-std/src/console2.sol","1":"sol/libraries/Suave.sol","2":"sol/standard_peekers/bundles.sol","3":"sol/standard_peekers/example.sol"},"language":"Solidity"}
13 changes: 13 additions & 0 deletions suave/e2e/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,19 @@ func TestE2E_Precompile_RandomBytes(t *testing.T) {
require.Len(t, res[0], 64)
}

func TestE2E_Precompile_GetInsecureTime(t *testing.T) {
fr := newFramework(t)
defer fr.Close()

// get the current time from Unix
current := big.NewInt(time.Now().Unix())

// get the time from the precompile
res := fr.callPrecompile("getInsecureTime", []interface{}{})
require.NotZero(t, res[0])
require.GreaterOrEqual(t, res[0].(*big.Int).Int64(), current.Int64())
}

func TestE2E_EmptyAddress(t *testing.T) {
// it should not be possible to make a CCR to an empty address
fr := newFramework(t)
Expand Down
8 changes: 8 additions & 0 deletions suave/gen/suave_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,11 @@ functions:
- name: message
type: bytes
description: "Decrypted message"
- name: getInsecureTime
address: "0x000000000000000000000000000000007770000c"
description: "Returns the current Kettle Unix time in milliseconds. Insecure because it assumes trust in Kettle's clock."
output:
fields:
- name: time
type: uint256
description: "Current Unix time in milliseconds"
13 changes: 13 additions & 0 deletions suave/sol/libraries/Suave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ library Suave {

address public constant FILL_MEV_SHARE_BUNDLE = 0x0000000000000000000000000000000043200001;

address public constant GET_INSECURE_TIME = 0x000000000000000000000000000000007770000c;

address public constant NEW_BUILDER = 0x0000000000000000000000000000000053200001;

address public constant NEW_DATA_RECORD = 0x0000000000000000000000000000000042030000;
Expand Down Expand Up @@ -346,6 +348,17 @@ library Suave {
return data;
}

/// @notice Returns the current Kettle Unix time in seconds.
/// @return time Current Unix time in seconds
function getInsecureTime() internal returns (uint256) {
(bool success, bytes memory data) = GET_INSECURE_TIME.call(abi.encode());
if (!success) {
revert PeekerReverted(GET_INSECURE_TIME, data);
}

return abi.decode(data, (uint256));
}

/// @notice Initializes a new remote builder session
/// @return sessionid ID of the remote builder session
function newBuilder() internal returns (string memory) {
Expand Down

0 comments on commit 0ca15b7

Please sign in to comment.