Skip to content

Commit

Permalink
bid -> dataRecord (#128)
Browse files Browse the repository at this point in the history
* bid -> dataRecord

* working renames

* change FetchBid to FetchRecord

* update vm contracts

* update devenv mevshare script

* the last references of bid

* the LAST improper bid reference

* Add format to Solidity

---------

Co-authored-by: Ferran Borreguero <[email protected]>
  • Loading branch information
dmarzzz and ferranbt committed Dec 21, 2023
1 parent ce153ea commit 026c84c
Show file tree
Hide file tree
Showing 36 changed files with 892 additions and 846 deletions.
22 changes: 11 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.

50 changes: 25 additions & 25 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,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 @@ -52,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 @@ -83,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 (s *suaveRuntime) signMessage(digest []byte, signingKey string) ([]byte, error) {
Expand Down
113 changes: 61 additions & 52 deletions core/vm/contracts_suave_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,101 +110,101 @@ func (b *suaveRuntime) ethcall(contractAddr common.Address, input []byte) ([]byt
return b.suaveContext.Backend.ConfidentialEthBackend.Call(context.Background(), contractAddr, input)
}

func (b *suaveRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, bidId types.BidId, namespace string) ([]byte, []byte, error) {
bidIds := [][16]byte{}
// first check for merged bid, else assume regular bid
if mergedBidsBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bidId, buildEthBlockAddr, "default:v0:mergedBids"); err == nil {
unpacked, err := bidIdsAbi.Inputs.Unpack(mergedBidsBytes)
func (b *suaveRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, dataID types.DataId, namespace string) ([]byte, []byte, error) {
dataIDs := [][16]byte{}
// first check for merged record, else assume regular record
if mergedDataRecordsBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(dataID, buildEthBlockAddr, "default:v0:mergedDataRecords"); err == nil {
unpacked, err := dataIDsAbi.Inputs.Unpack(mergedDataRecordsBytes)

if err != nil {
return nil, nil, fmt.Errorf("could not unpack merged bid ids: %w", err)
return nil, nil, fmt.Errorf("could not unpack merged record ids: %w", err)
}
bidIds = unpacked[0].([][16]byte)
dataIDs = unpacked[0].([][16]byte)
} else {
bidIds = append(bidIds, bidId)
dataIDs = append(dataIDs, dataID)
}

var bidsToMerge = make([]types.Bid, len(bidIds))
for i, bidId := range bidIds {
var recordsToMerge = make([]types.DataRecord, len(dataIDs))
for i, dataID := range dataIDs {
var err error

bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
record, err := b.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataID)
if err != nil {
return nil, nil, fmt.Errorf("could not fetch bid id %v: %w", bidId, err)
return nil, nil, fmt.Errorf("could not fetch record id %v: %w", dataID, err)
}

if _, err := checkIsPrecompileCallAllowed(b.suaveContext, buildEthBlockAddr, bid); err != nil {
if _, err := checkIsPrecompileCallAllowed(b.suaveContext, buildEthBlockAddr, record); err != nil {
return nil, nil, err
}

bidsToMerge[i] = bid.ToInnerBid()
recordsToMerge[i] = record.ToInnerRecord()
}

var mergedBundles []types.SBundle
for _, bid := range bidsToMerge {
switch bid.Version {
case "mevshare:v0:matchBids":
for _, record := range recordsToMerge {
switch record.Version {
case "mevshare:v0:matchDataRecords":
// fetch the matched ids and merge the bundle
matchedBundleIdsBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bid.Id, buildEthBlockAddr, "mevshare:v0:mergedBids")
matchedBundleIdsBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(record.Id, buildEthBlockAddr, "mevshare:v0:mergedDataRecords")
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve bid ids data for bid %v, from cdas: %w", bid, err)
return nil, nil, fmt.Errorf("could not retrieve record ids data for record %v, from cdas: %w", record, err)
}

unpackedBidIds, err := bidIdsAbi.Inputs.Unpack(matchedBundleIdsBytes)
unpackeddataIDs, err := dataIDsAbi.Inputs.Unpack(matchedBundleIdsBytes)
if err != nil {
return nil, nil, fmt.Errorf("could not unpack bid ids data for bid %v, from cdas: %w", bid, err)
return nil, nil, fmt.Errorf("could not unpack record ids data for record %v, from cdas: %w", record, err)
}

matchBidIds := unpackedBidIds[0].([][16]byte)
matchdataIDs := unpackeddataIDs[0].([][16]byte)

userBundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(matchBidIds[0], buildEthBlockAddr, "mevshare:v0:ethBundles")
userBundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(matchdataIDs[0], buildEthBlockAddr, "mevshare:v0:ethBundles")
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve bundle data for bidId %v: %w", matchBidIds[0], err)
return nil, nil, fmt.Errorf("could not retrieve bundle data for dataID %v: %w", matchdataIDs[0], err)
}

var userBundle types.SBundle
if err := json.Unmarshal(userBundleBytes, &userBundle); err != nil {
return nil, nil, fmt.Errorf("could not unmarshal user bundle data for bidId %v: %w", matchBidIds[0], err)
return nil, nil, fmt.Errorf("could not unmarshal user bundle data for dataID %v: %w", matchdataIDs[0], err)
}

matchBundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(matchBidIds[1], buildEthBlockAddr, "mevshare:v0:ethBundles")
matchBundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(matchdataIDs[1], buildEthBlockAddr, "mevshare:v0:ethBundles")
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve match bundle data for bidId %v: %w", matchBidIds[1], err)
return nil, nil, fmt.Errorf("could not retrieve match bundle data for dataID %v: %w", matchdataIDs[1], err)
}

var matchBundle types.SBundle
if err := json.Unmarshal(matchBundleBytes, &matchBundle); err != nil {
return nil, nil, fmt.Errorf("could not unmarshal match bundle data for bidId %v: %w", matchBidIds[1], err)
return nil, nil, fmt.Errorf("could not unmarshal match bundle data for dataID %v: %w", matchdataIDs[1], err)
}

userBundle.Txs = append(userBundle.Txs, matchBundle.Txs...)

mergedBundles = append(mergedBundles, userBundle)

case "mevshare:v0:unmatchedBundles":
bundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bid.Id, buildEthBlockAddr, "mevshare:v0:ethBundles")
bundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(record.Id, buildEthBlockAddr, "mevshare:v0:ethBundles")
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve bundle data for bidId %v, from cdas: %w", bid.Id, err)
return nil, nil, fmt.Errorf("could not retrieve bundle data for dataID %v, from cdas: %w", record.Id, err)
}

var bundle types.SBundle
if err := json.Unmarshal(bundleBytes, &bundle); err != nil {
return nil, nil, fmt.Errorf("could not unmarshal bundle data for bidId %v, from cdas: %w", bid.Id, err)
return nil, nil, fmt.Errorf("could not unmarshal bundle data for dataID %v, from cdas: %w", record.Id, err)
}
mergedBundles = append(mergedBundles, bundle)
case "default:v0:ethBundles":
bundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bid.Id, buildEthBlockAddr, "default:v0:ethBundles")
bundleBytes, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(record.Id, buildEthBlockAddr, "default:v0:ethBundles")
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve bundle data for bidId %v, from cdas: %w", bid.Id, err)
return nil, nil, fmt.Errorf("could not retrieve bundle data for dataID %v, from cdas: %w", record.Id, err)
}

var bundle types.SBundle
if err := json.Unmarshal(bundleBytes, &bundle); err != nil {
return nil, nil, fmt.Errorf("could not unmarshal bundle data for bidId %v, from cdas: %w", bid.Id, err)
return nil, nil, fmt.Errorf("could not unmarshal bundle data for dataID %v, from cdas: %w", record.Id, err)
}
mergedBundles = append(mergedBundles, bundle)
default:
return nil, nil, fmt.Errorf("unknown bid version %s", bid.Version)
return nil, nil, fmt.Errorf("unknown record version %s", record.Version)
}
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func (b *suaveRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, bidId types
builderSigningDomain := ssz.ComputeDomain(ssz.DomainTypeAppBuilder, genesisForkVersion, phase0.Root{})
signature, err := ssz.SignMessage(&blockBidMsg, builderSigningDomain, b.suaveContext.Backend.EthBlockSigningKey)
if err != nil {
return nil, nil, fmt.Errorf("could not sign builder bid: %w", err)
return nil, nil, fmt.Errorf("could not sign builder record: %w", err)
}

bidRequest := builderCapella.SubmitBlockRequest{
Expand All @@ -266,7 +266,7 @@ func (b *suaveRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, bidId types

bidBytes, err := bidRequest.MarshalJSON()
if err != nil {
return nil, nil, fmt.Errorf("could not marshal builder bid request: %w", err)
return nil, nil, fmt.Errorf("could not marshal builder record request: %w", err)
}

envelopeBytes, err := json.Marshal(envelope)
Expand Down Expand Up @@ -375,58 +375,67 @@ func (c *suaveRuntime) submitBundleJsonRPC(url string, method string, params []b
return nil, nil
}

func (c *suaveRuntime) fillMevShareBundle(bidId types.BidId) ([]byte, error) {
bid, err := c.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
func (c *suaveRuntime) fillMevShareBundle(dataID types.DataId) ([]byte, error) {
record, err := c.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataID)
if err != nil {
fmt.Println(err.Error())
return nil, err
}

if _, err := checkIsPrecompileCallAllowed(c.suaveContext, fillMevShareBundleAddr, bid); err != nil {
if _, err := checkIsPrecompileCallAllowed(c.suaveContext, fillMevShareBundleAddr, record); err != nil {
fmt.Println(err.Error())
return nil, err
}

matchedBundleIdsBytes, err := c.confidentialRetrieve(bidId, "mevshare:v0:mergedBids")
matchedBundleIdsBytes, err := c.confidentialRetrieve(dataID, "mevshare:v0:mergedDataRecords")
if err != nil {
fmt.Println(err.Error())
return nil, err
}

unpackedBidIds, err := bidIdsAbi.Inputs.Unpack(matchedBundleIdsBytes)
unpackedDataIDs, err := dataIDsAbi.Inputs.Unpack(matchedBundleIdsBytes)
if err != nil {
return nil, fmt.Errorf("could not unpack bid ids data for bid %v, from cdas: %w", bid, err)
fmt.Println(err.Error())
return nil, fmt.Errorf("could not unpack record ids data for record %v, from cdas: %w", record, err)
}

matchBidIds := unpackedBidIds[0].([][16]byte)
matchDataIDs := unpackedDataIDs[0].([][16]byte)

userBundleBytes, err := c.confidentialRetrieve(matchBidIds[0], "mevshare:v0:ethBundles")
userBundleBytes, err := c.confidentialRetrieve(matchDataIDs[0], "mevshare:v0:ethBundles")
if err != nil {
return nil, fmt.Errorf("could not retrieve bundle data for bidId %v: %w", matchBidIds[0], err)
fmt.Println(err.Error())
return nil, fmt.Errorf("could not retrieve bundle data for dataID %v: %w", matchDataIDs[0], err)
}

var userBundle types.SBundle
if err := json.Unmarshal(userBundleBytes, &userBundle); err != nil {
return nil, fmt.Errorf("could not unmarshal user bundle data for bidId %v: %w", matchBidIds[0], err)
fmt.Println(err.Error())
return nil, fmt.Errorf("could not unmarshal user bundle data for dataID %v: %w", matchDataIDs[0], err)
}

matchBundleBytes, err := c.confidentialRetrieve(matchBidIds[1], "mevshare:v0:ethBundles")
matchBundleBytes, err := c.confidentialRetrieve(matchDataIDs[1], "mevshare:v0:ethBundles")
if err != nil {
return nil, fmt.Errorf("could not retrieve match bundle data for bidId %v: %w", matchBidIds[1], err)
fmt.Println(err.Error())
return nil, fmt.Errorf("could not retrieve match bundle data for dataID %v: %w", matchDataIDs[1], err)
}

var matchBundle types.SBundle
if err := json.Unmarshal(matchBundleBytes, &matchBundle); err != nil {
return nil, fmt.Errorf("could not unmarshal match bundle data for bidId %v: %w", matchBidIds[1], err)
fmt.Println(err.Error())
return nil, fmt.Errorf("could not unmarshal match bundle data for dataID %v: %w", matchDataIDs[1], err)
}

shareBundle := &types.RPCMevShareBundle{
Version: "v0.1",
}

shareBundle.Inclusion.Block = hexutil.EncodeUint64(bid.DecryptionCondition)
shareBundle.Inclusion.MaxBlock = hexutil.EncodeUint64(bid.DecryptionCondition + 25) // Assumes 25 block inclusion range
shareBundle.Inclusion.Block = hexutil.EncodeUint64(record.DecryptionCondition)
shareBundle.Inclusion.MaxBlock = hexutil.EncodeUint64(record.DecryptionCondition + 25) // Assumes 25 block inclusion range

for _, tx := range append(userBundle.Txs, matchBundle.Txs...) {
txBytes, err := tx.MarshalBinary()
if err != nil {
fmt.Println(err.Error())
return nil, fmt.Errorf("could not marshal transaction: %w", err)
}

Expand Down
Loading

0 comments on commit 026c84c

Please sign in to comment.