Skip to content

Commit

Permalink
Merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Dec 19, 2023
2 parents a11e7aa + cdbb74a commit c7c482d
Show file tree
Hide file tree
Showing 43 changed files with 1,368 additions and 918 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.20
go-version: 1.21.1
id: go

- name: Check out code into the Go module directory
Expand Down
20 changes: 11 additions & 9 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ var (
utils.SuaveConfidentialStorePebbleDbPathFlag,
utils.SuaveEthBundleSigningKeyFlag,
utils.SuaveEthBlockSigningKeyFlag,
utils.SuaveExternalWhitelistFlag,
utils.SuaveDevModeFlag,
}
)
Expand Down Expand Up @@ -271,6 +272,16 @@ func main() {
// prepare manipulates memory cache allowance and setups metric system.
// This function should be called before launching devp2p stack.
func prepare(ctx *cli.Context) {
if err := prepareSuaveDev(ctx); err != nil {
log.Error("failed to setup suave dev mode", "err", err)
os.Exit(1)
}

if err := prepareSuaveNetworksRemapping(ctx); err != nil {
log.Error("failed to setup suave networks remapping", "err", err)
os.Exit(1)
}

// If we're running a known preset, log it for convenience.
switch {
case ctx.IsSet(utils.RinkebyFlag.Name):
Expand Down Expand Up @@ -340,14 +351,6 @@ func geth(ctx *cli.Context) error {
return fmt.Errorf("invalid command: %q", args[0])
}

if err := prepareSuaveDev(ctx); err != nil {
return fmt.Errorf("failed to setup suave development mode: %v", err)
}

if err := prepareSuaveNetworksRemapping(ctx); err != nil {
return err
}

prepare(ctx)
stack, backend := makeFullNode(ctx)
defer stack.Close()
Expand Down Expand Up @@ -531,7 +534,6 @@ func prepareSuaveDev(ctx *cli.Context) error {
utils.DeveloperFlag.Name: "true",
utils.DeveloperGasLimitFlag.Name: "30000000",
utils.HTTPEnabledFlag.Name: "true",
utils.HTTPPortFlag.Name: "8545",
utils.HTTPVirtualHostsFlag.Name: "*",
utils.HTTPCORSDomainFlag.Name: "*",
utils.WSEnabledFlag.Name: "true",
Expand Down
19 changes: 19 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ var (
Category: flags.SuaveCategory,
}

SuaveExternalWhitelistFlag = &cli.StringSliceFlag{
Name: "suave.eth.external-whitelist",
EnvVars: []string{"SUAVE_EXTERNAL_WHITELIST"},
Usage: "List of external whitelisted addresses",
Category: flags.SuaveCategory,
}

SuaveDevModeFlag = &cli.BoolFlag{
Name: "suave.dev",
Usage: "Dev mode for suave",
Expand Down Expand Up @@ -1736,6 +1743,18 @@ func SetSuaveConfig(ctx *cli.Context, stack *node.Node, cfg *suave.Config) {
if ctx.IsSet(SuaveEthBlockSigningKeyFlag.Name) {
cfg.EthBlockSigningKeyHex = ctx.String(SuaveEthBlockSigningKeyFlag.Name)
}

if ctx.IsSet(SuaveEthBundleSigningKeyFlag.Name) {
cfg.EthBundleSigningKeyHex = ctx.String(SuaveEthBundleSigningKeyFlag.Name)
}

if ctx.IsSet(SuaveExternalWhitelistFlag.Name) {
cfg.ExternalWhitelist = ctx.StringSlice(SuaveEthBundleSigningKeyFlag.Name)
if len(cfg.ExternalWhitelist) == 0 {
// As of now, default to wildcard
cfg.ExternalWhitelist = []string{"*"}
}
}
}

// SetEthConfig applies eth-related command line flags to the config.
Expand Down
6 changes: 5 additions & 1 deletion core/types/sbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (

type SBundle struct {
BlockNumber *big.Int `json:"blockNumber,omitempty"` // if BlockNumber is set it must match DecryptionCondition!
MaxBlock *big.Int `json:"maxBlock,omitempty"`
Txs Transactions `json:"txs"`
RevertingHashes []common.Hash `json:"revertingHashes,omitempty"`
RefundPercent *int `json:"percent,omitempty"`
}

type RpcSBundle struct {
BlockNumber *hexutil.Big `json:"blockNumber,omitempty"`
MaxBlock *hexutil.Big `json:"maxBlock,omitempty"`
Txs []hexutil.Bytes `json:"txs"`
RevertingHashes []common.Hash `json:"revertingHashes,omitempty"`
RefundPercent *int `json:"percent,omitempty"`
Expand Down Expand Up @@ -66,6 +68,7 @@ func (s *SBundle) UnmarshalJSON(data []byte) error {
}

s.BlockNumber = (*big.Int)(rpcSBundle.BlockNumber)
s.MaxBlock = (*big.Int)(rpcSBundle.MaxBlock)
s.Txs = txs
s.RevertingHashes = rpcSBundle.RevertingHashes
s.RefundPercent = rpcSBundle.RefundPercent
Expand All @@ -76,7 +79,8 @@ func (s *SBundle) UnmarshalJSON(data []byte) error {
type RPCMevShareBundle struct {
Version string `json:"version"`
Inclusion struct {
Block string `json:"block"`
Block string `json:"block"`
MaxBlock string `json:"maxBlock"`
} `json:"inclusion"`
Body []struct {
Tx string `json:"tx"`
Expand Down
29 changes: 18 additions & 11 deletions core/types/suave_structs.go

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

123 changes: 93 additions & 30 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package vm

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -30,15 +35,15 @@ func (b *suaveRuntime) confidentialInputs() ([]byte, error) {

/* Confidential store precompiles */

func (b *suaveRuntime) confidentialStore(bidId types.BidId, key string, data []byte) error {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
func (b *suaveRuntime) confidentialStore(dataId types.DataId, key string, data []byte) error {
record, err := b.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataId)
if err != nil {
return suave.ErrBidNotFound
return suave.ErrRecordNotFound
}

log.Info("confStore", "bidId", bidId, "key", key)
log.Debug("confStore", "dataId", dataId, "key", key)

caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialStoreAddr, bid)
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialStoreAddr, record)
if err != nil {
return err
}
Expand All @@ -47,26 +52,26 @@ func (b *suaveRuntime) confidentialStore(bidId types.BidId, key string, data []b
confStorePrecompileStoreMeter.Mark(int64(len(data)))
}

_, err = b.suaveContext.Backend.ConfidentialStore.Store(bidId, caller, key, data)
_, err = b.suaveContext.Backend.ConfidentialStore.Store(dataId, caller, key, data)
if err != nil {
return err
}

return nil
}

func (b *suaveRuntime) confidentialRetrieve(bidId types.BidId, key string) ([]byte, error) {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
func (b *suaveRuntime) confidentialRetrieve(dataId types.DataId, key string) ([]byte, error) {
record, err := b.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataId)
if err != nil {
return nil, suave.ErrBidNotFound
return nil, suave.ErrRecordNotFound
}

caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialRetrieveAddr, bid)
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialRetrieveAddr, record)
if err != nil {
return nil, err
}

data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bidId, caller, key)
data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(dataId, caller, key)
if err != nil {
return []byte(err.Error()), err
}
Expand All @@ -78,36 +83,36 @@ func (b *suaveRuntime) confidentialRetrieve(bidId types.BidId, key string) ([]by
return data, nil
}

/* Bid precompiles */
/* Data Record precompiles */

func (b *suaveRuntime) newBid(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, BidType string) (types.Bid, error) {
func (b *suaveRuntime) newDataRecord(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, RecordType string) (types.DataRecord, error) {
if b.suaveContext.ConfidentialComputeRequestTx == nil {
panic("newBid: source transaction not present")
panic("newRecord: source transaction not present")
}

bid, err := b.suaveContext.Backend.ConfidentialStore.InitializeBid(types.Bid{
Salt: suave.RandomBidId(),
record, err := b.suaveContext.Backend.ConfidentialStore.InitRecord(types.DataRecord{
Salt: suave.RandomDataRecordId(),
DecryptionCondition: decryptionCondition,
AllowedPeekers: allowedPeekers,
AllowedStores: allowedStores,
Version: BidType, // TODO : make generic
Version: RecordType, // TODO : make generic
})
if err != nil {
return types.Bid{}, err
return types.DataRecord{}, err
}

return bid, nil
return record, nil
}

func (b *suaveRuntime) fetchBids(targetBlock uint64, namespace string) ([]types.Bid, error) {
bids1 := b.suaveContext.Backend.ConfidentialStore.FetchBidsByProtocolAndBlock(targetBlock, namespace)
func (b *suaveRuntime) fetchDataRecords(targetBlock uint64, namespace string) ([]types.DataRecord, error) {
records1 := b.suaveContext.Backend.ConfidentialStore.FetchRecordsByProtocolAndBlock(targetBlock, namespace)

bids := make([]types.Bid, 0, len(bids1))
for _, bid := range bids1 {
bids = append(bids, bid.ToInnerBid())
records := make([]types.DataRecord, 0, len(records1))
for _, record := range records1 {
records = append(records, record.ToInnerRecord())
}

return bids, nil
return records, nil
}

func mustParseAbi(data string) abi.ABI {
Expand All @@ -124,11 +129,6 @@ func mustParseMethodAbi(data string, method string) abi.Method {
return inoutAbi.Methods[method]
}

func formatPeekerError(format string, args ...any) ([]byte, error) {
err := fmt.Errorf(format, args...)
return []byte(err.Error()), err
}

type suaveRuntime struct {
suaveContext *SuaveContext
}
Expand All @@ -146,3 +146,66 @@ func (c *consoleLogPrecompile) Run(input []byte) ([]byte, error) {
consolelog.Print(input)
return nil, nil
}

func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error) {
if request.Method != "GET" && request.Method != "POST" {
return nil, fmt.Errorf("only GET and POST methods are supported")
}
if request.Url == "" {
return nil, fmt.Errorf("url is empty")
}

var body io.Reader
if request.Body != nil {
body = bytes.NewReader(request.Body)
}

// decode the url and check if the domain is allowed
parsedURL, err := url.Parse(request.Url)
if err != nil {
panic(err)
}

var allowed bool
for _, allowedDomain := range s.suaveContext.Backend.ExternalWhitelist {
if allowedDomain == "*" || allowedDomain == parsedURL.Hostname() {
allowed = true
break
}
}
if !allowed {
return nil, fmt.Errorf("domain %s is not allowed", parsedURL.Hostname())
}

req, err := http.NewRequest(request.Method, request.Url, body)
if err != nil {
return nil, err
}

for _, header := range request.Headers {
indx := strings.Index(header, ":")
if indx == -1 {
return nil, fmt.Errorf("incorrect header format '%s', no ':' present", header)
}
req.Header.Add(header[:indx], header[indx+1:])
}

client := &http.Client{
Timeout: 5 * time.Second, // TODO: test
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode > 299 {
return nil, fmt.Errorf("http error: %s: %v", resp.Status, data)
}
return data, nil
}
Loading

0 comments on commit c7c482d

Please sign in to comment.