-
Notifications
You must be signed in to change notification settings - Fork 52
/
contracts_suave.go
139 lines (107 loc) · 3.53 KB
/
contracts_suave.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package vm
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
suave "github.com/ethereum/go-ethereum/suave/core"
)
var (
confStorePrecompileStoreMeter = metrics.NewRegisteredMeter("suave/confstore/store", nil)
confStorePrecompileRetrieveMeter = metrics.NewRegisteredMeter("suave/confstore/retrieve", nil)
)
var (
isConfidentialAddress = common.HexToAddress("0x42010000")
)
/* General utility precompiles */
func (b *suaveRuntime) confidentialInputs() ([]byte, error) {
return b.suaveContext.ConfidentialInputs, nil
}
/* Confidential store precompiles */
func (b *suaveRuntime) confidentialStore(bidId types.BidId, key string, data []byte) error {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
if err != nil {
return suave.ErrBidNotFound
}
log.Info("confStore", "bidId", bidId, "key", key)
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialStoreAddr, bid)
if err != nil {
return err
}
if metrics.Enabled {
confStorePrecompileStoreMeter.Mark(int64(len(data)))
}
_, err = b.suaveContext.Backend.ConfidentialStore.Store(bidId, 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)
if err != nil {
return nil, suave.ErrBidNotFound
}
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialRetrieveAddr, bid)
if err != nil {
return nil, err
}
data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bidId, caller, key)
if err != nil {
return []byte(err.Error()), err
}
if metrics.Enabled {
confStorePrecompileRetrieveMeter.Mark(int64(len(data)))
}
return data, nil
}
/* Bid precompiles */
func (b *suaveRuntime) newBid(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, BidType string) (types.Bid, error) {
if b.suaveContext.ConfidentialComputeRequestTx == nil {
panic("newBid: source transaction not present")
}
bid, err := b.suaveContext.Backend.ConfidentialStore.InitializeBid(types.Bid{
Salt: suave.RandomBidId(),
DecryptionCondition: decryptionCondition,
AllowedPeekers: allowedPeekers,
AllowedStores: allowedStores,
Version: BidType, // TODO : make generic
})
if err != nil {
return types.Bid{}, err
}
return bid, nil
}
func (b *suaveRuntime) fetchBids(targetBlock uint64, namespace string) ([]types.Bid, error) {
bids1 := b.suaveContext.Backend.ConfidentialStore.FetchBidsByProtocolAndBlock(targetBlock, namespace)
bids := make([]types.Bid, 0, len(bids1))
for _, bid := range bids1 {
bids = append(bids, bid.ToInnerBid())
}
return bids, nil
}
func mustParseAbi(data string) abi.ABI {
inoutAbi, err := abi.JSON(strings.NewReader(data))
if err != nil {
panic(err.Error())
}
return inoutAbi
}
func mustParseMethodAbi(data string, method string) abi.Method {
inoutAbi := mustParseAbi(data)
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
}
var _ SuaveRuntime = &suaveRuntime{}
func (s *suaveRuntime) getBlockNumber() (uint64, error) {
return s.suaveContext.Backend.ConfidentialEthBackend.BlockNumber()
}