-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
333 lines (285 loc) · 12.7 KB
/
contract.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// (c) 2023, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.
package warp
import (
"errors"
"fmt"
"github.com/luxdefi/node/vms/platformvm/warp"
"github.com/luxdefi/node/vms/platformvm/warp/payload"
"github.com/luxdefi/subnet-evm/accounts/abi"
"github.com/luxdefi/subnet-evm/params"
"github.com/luxdefi/subnet-evm/precompile/contract"
"github.com/luxdefi/subnet-evm/vmerrs"
_ "embed"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
)
const (
GetVerifiedWarpMessageBaseCost uint64 = 2 // Base cost of entering getVerifiedWarpMessage
GetBlockchainIDGasCost uint64 = 2 // Based on GasQuickStep used in existing EVM instructions
AddWarpMessageGasCost uint64 = 20_000 // Cost of producing and serving a BLS Signature
// Sum of base log gas cost, cost of producing 4 topics, and producing + serving a BLS Signature (sign + trie write)
// Note: using trie write for the gas cost results in a conservative overestimate since the message is stored in a
// flat database that can be cleaned up after a period of time instead of the EVM trie.
SendWarpMessageGasCost uint64 = params.LogGas + 4*params.LogTopicGas + AddWarpMessageGasCost + contract.WriteGasCostPerSlot
// SendWarpMessageGasCostPerByte cost accounts for producing a signed message of a given size
SendWarpMessageGasCostPerByte uint64 = params.LogDataGas
GasCostPerWarpSigner uint64 = 500
GasCostPerWarpMessageBytes uint64 = 100
GasCostPerSignatureVerification uint64 = 200_000
)
var (
errInvalidSendInput = errors.New("invalid sendWarpMessage input")
errInvalidIndexInput = errors.New("invalid index to specify warp message")
)
// Singleton StatefulPrecompiledContract and signatures.
var (
// WarpRawABI contains the raw ABI of Warp contract.
//go:embed contract.abi
WarpRawABI string
WarpABI = contract.ParseABI(WarpRawABI)
WarpPrecompile = createWarpPrecompile()
)
// WarpBlockHash is an auto generated low-level Go binding around an user-defined struct.
type WarpBlockHash struct {
SourceChainID common.Hash
BlockHash common.Hash
}
type GetVerifiedWarpBlockHashOutput struct {
WarpBlockHash WarpBlockHash
Valid bool
}
// WarpMessage is an auto generated low-level Go binding around an user-defined struct.
type WarpMessage struct {
SourceChainID common.Hash
OriginSenderAddress common.Address
Payload []byte
}
type GetVerifiedWarpMessageOutput struct {
Message WarpMessage
Valid bool
}
type SendWarpMessageEventData struct {
Message []byte
}
// PackGetBlockchainID packs the include selector (first 4 func signature bytes).
// This function is mostly used for tests.
func PackGetBlockchainID() ([]byte, error) {
return WarpABI.Pack("getBlockchainID")
}
// PackGetBlockchainIDOutput attempts to pack given blockchainID of type common.Hash
// to conform the ABI outputs.
func PackGetBlockchainIDOutput(blockchainID common.Hash) ([]byte, error) {
return WarpABI.PackOutput("getBlockchainID", blockchainID)
}
// getBlockchainID returns the snow Chain Context ChainID of this blockchain.
func getBlockchainID(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
if remainingGas, err = contract.DeductGas(suppliedGas, GetBlockchainIDGasCost); err != nil {
return nil, 0, err
}
packedOutput, err := PackGetBlockchainIDOutput(common.Hash(accessibleState.GetSnowContext().ChainID))
if err != nil {
return nil, remainingGas, err
}
// Return the packed output and the remaining gas
return packedOutput, remainingGas, nil
}
// UnpackGetVerifiedWarpBlockHashInput attempts to unpack [input] into the uint32 type argument
// assumes that [input] does not include selector (omits first 4 func signature bytes)
func UnpackGetVerifiedWarpBlockHashInput(input []byte) (uint32, error) {
res, err := WarpABI.UnpackInput("getVerifiedWarpBlockHash", input)
if err != nil {
return 0, err
}
unpacked := *abi.ConvertType(res[0], new(uint32)).(*uint32)
return unpacked, nil
}
// PackGetVerifiedWarpBlockHash packs [index] of type uint32 into the appropriate arguments for getVerifiedWarpBlockHash.
// the packed bytes include selector (first 4 func signature bytes).
// This function is mostly used for tests.
func PackGetVerifiedWarpBlockHash(index uint32) ([]byte, error) {
return WarpABI.Pack("getVerifiedWarpBlockHash", index)
}
// PackGetVerifiedWarpBlockHashOutput attempts to pack given [outputStruct] of type GetVerifiedWarpBlockHashOutput
// to conform the ABI outputs.
func PackGetVerifiedWarpBlockHashOutput(outputStruct GetVerifiedWarpBlockHashOutput) ([]byte, error) {
return WarpABI.PackOutput("getVerifiedWarpBlockHash",
outputStruct.WarpBlockHash,
outputStruct.Valid,
)
}
// UnpackGetVerifiedWarpBlockHashOutput attempts to unpack [output] as GetVerifiedWarpBlockHashOutput
// assumes that [output] does not include selector (omits first 4 func signature bytes)
func UnpackGetVerifiedWarpBlockHashOutput(output []byte) (GetVerifiedWarpBlockHashOutput, error) {
outputStruct := GetVerifiedWarpBlockHashOutput{}
err := WarpABI.UnpackIntoInterface(&outputStruct, "getVerifiedWarpBlockHash", output)
return outputStruct, err
}
func getVerifiedWarpBlockHash(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
return handleWarpMessage(accessibleState, input, suppliedGas, blockHashHandler{})
}
// UnpackGetVerifiedWarpMessageInput attempts to unpack [input] into the uint32 type argument
// assumes that [input] does not include selector (omits first 4 func signature bytes)
func UnpackGetVerifiedWarpMessageInput(input []byte) (uint32, error) {
res, err := WarpABI.UnpackInput("getVerifiedWarpMessage", input)
if err != nil {
return 0, err
}
unpacked := *abi.ConvertType(res[0], new(uint32)).(*uint32)
return unpacked, nil
}
// PackGetVerifiedWarpMessage packs [index] of type uint32 into the appropriate arguments for getVerifiedWarpMessage.
// the packed bytes include selector (first 4 func signature bytes).
// This function is mostly used for tests.
func PackGetVerifiedWarpMessage(index uint32) ([]byte, error) {
return WarpABI.Pack("getVerifiedWarpMessage", index)
}
// PackGetVerifiedWarpMessageOutput attempts to pack given [outputStruct] of type GetVerifiedWarpMessageOutput
// to conform the ABI outputs.
func PackGetVerifiedWarpMessageOutput(outputStruct GetVerifiedWarpMessageOutput) ([]byte, error) {
return WarpABI.PackOutput("getVerifiedWarpMessage",
outputStruct.Message,
outputStruct.Valid,
)
}
// UnpackGetVerifiedWarpMessageOutput attempts to unpack [output] as GetVerifiedWarpMessageOutput
// assumes that [output] does not include selector (omits first 4 func signature bytes)
func UnpackGetVerifiedWarpMessageOutput(output []byte) (GetVerifiedWarpMessageOutput, error) {
outputStruct := GetVerifiedWarpMessageOutput{}
err := WarpABI.UnpackIntoInterface(&outputStruct, "getVerifiedWarpMessage", output)
return outputStruct, err
}
// getVerifiedWarpMessage retrieves the pre-verified warp message from the predicate storage slots and returns
// the expected ABI encoding of the message to the caller.
func getVerifiedWarpMessage(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
return handleWarpMessage(accessibleState, input, suppliedGas, addressedPayloadHandler{})
}
// UnpackSendWarpMessageInput attempts to unpack [input] as []byte
// assumes that [input] does not include selector (omits first 4 func signature bytes)
func UnpackSendWarpMessageInput(input []byte) ([]byte, error) {
res, err := WarpABI.UnpackInput("sendWarpMessage", input)
if err != nil {
return []byte{}, err
}
unpacked := *abi.ConvertType(res[0], new([]byte)).(*[]byte)
return unpacked, nil
}
// PackSendWarpMessage packs [inputStruct] of type []byte into the appropriate arguments for sendWarpMessage.
func PackSendWarpMessage(payloadData []byte) ([]byte, error) {
return WarpABI.Pack("sendWarpMessage", payloadData)
}
// PackSendWarpMessageOutput attempts to pack given messageID of type common.Hash
// to conform the ABI outputs.
func PackSendWarpMessageOutput(messageID common.Hash) ([]byte, error) {
return WarpABI.PackOutput("sendWarpMessage", messageID)
}
// UnpackSendWarpMessageOutput attempts to unpack given [output] into the common.Hash type output
// assumes that [output] does not include selector (omits first 4 func signature bytes)
func UnpackSendWarpMessageOutput(output []byte) (common.Hash, error) {
res, err := WarpABI.Unpack("sendWarpMessage", output)
if err != nil {
return common.Hash{}, err
}
unpacked := *abi.ConvertType(res[0], new(common.Hash)).(*common.Hash)
return unpacked, nil
}
// sendWarpMessage constructs an Lux Warp Message containing an AddressedPayload and emits a log to signal validators that they should
// be willing to sign this message.
func sendWarpMessage(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
if remainingGas, err = contract.DeductGas(suppliedGas, SendWarpMessageGasCost); err != nil {
return nil, 0, err
}
// This gas cost includes buffer room because it is based off of the total size of the input instead of the produced payload.
// This ensures that we charge gas before we unpack the variable sized input.
payloadGas, overflow := math.SafeMul(SendWarpMessageGasCostPerByte, uint64(len(input)))
if overflow {
return nil, 0, vmerrs.ErrOutOfGas
}
if remainingGas, err = contract.DeductGas(remainingGas, payloadGas); err != nil {
return nil, 0, err
}
if readOnly {
return nil, remainingGas, vmerrs.ErrWriteProtection
}
// unpack the arguments
payloadData, err := UnpackSendWarpMessageInput(input)
if err != nil {
return nil, remainingGas, fmt.Errorf("%w: %s", errInvalidSendInput, err)
}
var (
sourceChainID = accessibleState.GetSnowContext().ChainID
sourceAddress = caller
)
addressedPayload, err := payload.NewAddressedCall(
sourceAddress.Bytes(),
payloadData,
)
if err != nil {
return nil, remainingGas, err
}
unsignedWarpMessage, err := warp.NewUnsignedMessage(
accessibleState.GetSnowContext().NetworkID,
sourceChainID,
addressedPayload.Bytes(),
)
if err != nil {
return nil, remainingGas, err
}
// Add a log to be handled if this action is finalized.
topics, data, err := PackSendWarpMessageEvent(
sourceAddress,
common.Hash(unsignedWarpMessage.ID()),
unsignedWarpMessage.Bytes(),
)
if err != nil {
return nil, remainingGas, err
}
accessibleState.GetStateDB().AddLog(
ContractAddress,
topics,
data,
accessibleState.GetBlockContext().Number().Uint64(),
)
packed, err := PackSendWarpMessageOutput(common.Hash(unsignedWarpMessage.ID()))
if err != nil {
return nil, remainingGas, err
}
// Return the packed message ID and the remaining gas
return packed, remainingGas, nil
}
// PackSendWarpMessageEvent packs the given arguments into SendWarpMessage events including topics and data.
func PackSendWarpMessageEvent(sourceAddress common.Address, unsignedMessageID common.Hash, unsignedMessageBytes []byte) ([]common.Hash, []byte, error) {
return WarpABI.PackEvent("SendWarpMessage", sourceAddress, unsignedMessageID, unsignedMessageBytes)
}
// UnpackSendWarpEventDataToMessage attempts to unpack event [data] as warp.UnsignedMessage.
func UnpackSendWarpEventDataToMessage(data []byte) (*warp.UnsignedMessage, error) {
event := SendWarpMessageEventData{}
err := WarpABI.UnpackIntoInterface(&event, "SendWarpMessage", data)
if err != nil {
return nil, err
}
return warp.ParseUnsignedMessage(event.Message)
}
// createWarpPrecompile returns a StatefulPrecompiledContract with getters and setters for the precompile.
func createWarpPrecompile() contract.StatefulPrecompiledContract {
var functions []*contract.StatefulPrecompileFunction
abiFunctionMap := map[string]contract.RunStatefulPrecompileFunc{
"getBlockchainID": getBlockchainID,
"getVerifiedWarpBlockHash": getVerifiedWarpBlockHash,
"getVerifiedWarpMessage": getVerifiedWarpMessage,
"sendWarpMessage": sendWarpMessage,
}
for name, function := range abiFunctionMap {
method, ok := WarpABI.Methods[name]
if !ok {
panic(fmt.Errorf("given method (%s) does not exist in the ABI", name))
}
functions = append(functions, contract.NewStatefulPrecompileFunction(method.ID, function))
}
// Construct the contract with no fallback function.
statefulContract, err := contract.NewStatefulPrecompileContract(nil, functions)
if err != nil {
panic(err)
}
return statefulContract
}