diff --git a/Makefile b/Makefile index 7b91be935..86b608d01 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,11 @@ devtools: suavedevtools: go run ./suave/gen/main.go -write + cd core/types && \ + go run github.com/fjl/gencodec -type BuildBlockArgs -field-override buildBlockArgsMarshaling -out gen_build_block_args_json.go && \ + go run github.com/fjl/gencodec -type DataRecord -field-override dataRecordMarshaling -out gen_data_record_json.go && \ + go run github.com/fjl/gencodec -type HttpRequest -field-override httpRequestMarshaling -out gen_http_request_json.go && \ + go run github.com/fjl/gencodec -type SimulatedLog -field-override simulatedLogMarshaling -out gen_simulated_log_json.go devnet-up: docker-compose -f ./suave/devenv/docker-compose.yml up -d --build diff --git a/core/types/gen_build_block_args_json.go b/core/types/gen_build_block_args_json.go new file mode 100644 index 000000000..6bf4be4a6 --- /dev/null +++ b/core/types/gen_build_block_args_json.go @@ -0,0 +1,97 @@ +// Code generated by github.com/fjl/gencodec. DO NOT EDIT. + +package types + +import ( + "encoding/json" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) + +var _ = (*buildBlockArgsMarshaling)(nil) + +// MarshalJSON marshals as JSON. +func (b BuildBlockArgs) MarshalJSON() ([]byte, error) { + type BuildBlockArgs struct { + Slot hexutil.Uint64 + ProposerPubkey hexutil.Bytes + Parent common.Hash + Timestamp hexutil.Uint64 + FeeRecipient common.Address + GasLimit hexutil.Uint64 + Random common.Hash + Withdrawals []*Withdrawal + Extra hexutil.Bytes + BeaconRoot common.Hash + FillPending bool + } + var enc BuildBlockArgs + enc.Slot = hexutil.Uint64(b.Slot) + enc.ProposerPubkey = b.ProposerPubkey + enc.Parent = b.Parent + enc.Timestamp = hexutil.Uint64(b.Timestamp) + enc.FeeRecipient = b.FeeRecipient + enc.GasLimit = hexutil.Uint64(b.GasLimit) + enc.Random = b.Random + enc.Withdrawals = b.Withdrawals + enc.Extra = b.Extra + enc.BeaconRoot = b.BeaconRoot + enc.FillPending = b.FillPending + return json.Marshal(&enc) +} + +// UnmarshalJSON unmarshals from JSON. +func (b *BuildBlockArgs) UnmarshalJSON(input []byte) error { + type BuildBlockArgs struct { + Slot *hexutil.Uint64 + ProposerPubkey *hexutil.Bytes + Parent *common.Hash + Timestamp *hexutil.Uint64 + FeeRecipient *common.Address + GasLimit *hexutil.Uint64 + Random *common.Hash + Withdrawals []*Withdrawal + Extra *hexutil.Bytes + BeaconRoot *common.Hash + FillPending *bool + } + var dec BuildBlockArgs + if err := json.Unmarshal(input, &dec); err != nil { + return err + } + if dec.Slot != nil { + b.Slot = uint64(*dec.Slot) + } + if dec.ProposerPubkey != nil { + b.ProposerPubkey = *dec.ProposerPubkey + } + if dec.Parent != nil { + b.Parent = *dec.Parent + } + if dec.Timestamp != nil { + b.Timestamp = uint64(*dec.Timestamp) + } + if dec.FeeRecipient != nil { + b.FeeRecipient = *dec.FeeRecipient + } + if dec.GasLimit != nil { + b.GasLimit = uint64(*dec.GasLimit) + } + if dec.Random != nil { + b.Random = *dec.Random + } + if dec.Withdrawals != nil { + b.Withdrawals = dec.Withdrawals + } + if dec.Extra != nil { + b.Extra = *dec.Extra + } + if dec.BeaconRoot != nil { + b.BeaconRoot = *dec.BeaconRoot + } + if dec.FillPending != nil { + b.FillPending = *dec.FillPending + } + return nil +} diff --git a/core/types/gen_data_record_json.go b/core/types/gen_data_record_json.go new file mode 100644 index 000000000..b2176b6aa --- /dev/null +++ b/core/types/gen_data_record_json.go @@ -0,0 +1,67 @@ +// Code generated by github.com/fjl/gencodec. DO NOT EDIT. + +package types + +import ( + "encoding/json" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) + +var _ = (*dataRecordMarshaling)(nil) + +// MarshalJSON marshals as JSON. +func (d DataRecord) MarshalJSON() ([]byte, error) { + type DataRecord struct { + Id DataId + Salt DataId + DecryptionCondition hexutil.Uint64 + AllowedPeekers []common.Address + AllowedStores []common.Address + Version string + } + var enc DataRecord + enc.Id = d.Id + enc.Salt = d.Salt + enc.DecryptionCondition = hexutil.Uint64(d.DecryptionCondition) + enc.AllowedPeekers = d.AllowedPeekers + enc.AllowedStores = d.AllowedStores + enc.Version = d.Version + return json.Marshal(&enc) +} + +// UnmarshalJSON unmarshals from JSON. +func (d *DataRecord) UnmarshalJSON(input []byte) error { + type DataRecord struct { + Id *DataId + Salt *DataId + DecryptionCondition *hexutil.Uint64 + AllowedPeekers []common.Address + AllowedStores []common.Address + Version *string + } + var dec DataRecord + if err := json.Unmarshal(input, &dec); err != nil { + return err + } + if dec.Id != nil { + d.Id = *dec.Id + } + if dec.Salt != nil { + d.Salt = *dec.Salt + } + if dec.DecryptionCondition != nil { + d.DecryptionCondition = uint64(*dec.DecryptionCondition) + } + if dec.AllowedPeekers != nil { + d.AllowedPeekers = dec.AllowedPeekers + } + if dec.AllowedStores != nil { + d.AllowedStores = dec.AllowedStores + } + if dec.Version != nil { + d.Version = *dec.Version + } + return nil +} diff --git a/core/types/gen_http_request_json.go b/core/types/gen_http_request_json.go new file mode 100644 index 000000000..4928a4b6f --- /dev/null +++ b/core/types/gen_http_request_json.go @@ -0,0 +1,60 @@ +// Code generated by github.com/fjl/gencodec. DO NOT EDIT. + +package types + +import ( + "encoding/json" + + "github.com/ethereum/go-ethereum/common/hexutil" +) + +var _ = (*httpRequestMarshaling)(nil) + +// MarshalJSON marshals as JSON. +func (h HttpRequest) MarshalJSON() ([]byte, error) { + type HttpRequest struct { + Url string + Method string + Headers []string + Body hexutil.Bytes + WithFlashbotsSignature bool + } + var enc HttpRequest + enc.Url = h.Url + enc.Method = h.Method + enc.Headers = h.Headers + enc.Body = h.Body + enc.WithFlashbotsSignature = h.WithFlashbotsSignature + return json.Marshal(&enc) +} + +// UnmarshalJSON unmarshals from JSON. +func (h *HttpRequest) UnmarshalJSON(input []byte) error { + type HttpRequest struct { + Url *string + Method *string + Headers []string + Body *hexutil.Bytes + WithFlashbotsSignature *bool + } + var dec HttpRequest + if err := json.Unmarshal(input, &dec); err != nil { + return err + } + if dec.Url != nil { + h.Url = *dec.Url + } + if dec.Method != nil { + h.Method = *dec.Method + } + if dec.Headers != nil { + h.Headers = dec.Headers + } + if dec.Body != nil { + h.Body = *dec.Body + } + if dec.WithFlashbotsSignature != nil { + h.WithFlashbotsSignature = *dec.WithFlashbotsSignature + } + return nil +} diff --git a/core/types/gen_simulated_log_json.go b/core/types/gen_simulated_log_json.go new file mode 100644 index 000000000..54093f3e6 --- /dev/null +++ b/core/types/gen_simulated_log_json.go @@ -0,0 +1,49 @@ +// Code generated by github.com/fjl/gencodec. DO NOT EDIT. + +package types + +import ( + "encoding/json" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) + +var _ = (*simulatedLogMarshaling)(nil) + +// MarshalJSON marshals as JSON. +func (s SimulatedLog) MarshalJSON() ([]byte, error) { + type SimulatedLog struct { + Data hexutil.Bytes + Addr common.Address + Topics []common.Hash + } + var enc SimulatedLog + enc.Data = s.Data + enc.Addr = s.Addr + enc.Topics = s.Topics + return json.Marshal(&enc) +} + +// UnmarshalJSON unmarshals from JSON. +func (s *SimulatedLog) UnmarshalJSON(input []byte) error { + type SimulatedLog struct { + Data *hexutil.Bytes + Addr *common.Address + Topics []common.Hash + } + var dec SimulatedLog + if err := json.Unmarshal(input, &dec); err != nil { + return err + } + if dec.Data != nil { + s.Data = *dec.Data + } + if dec.Addr != nil { + s.Addr = *dec.Addr + } + if dec.Topics != nil { + s.Topics = dec.Topics + } + return nil +} diff --git a/core/types/suave_structs.go b/core/types/suave_structs.go index 0307f5fae..3508c2ab5 100755 --- a/core/types/suave_structs.go +++ b/core/types/suave_structs.go @@ -2,7 +2,10 @@ // Hash: 64563d17acef48bebfc901fe4b887d02b1efc267e269e9b2aec1fd02027a3f1a package types -import "github.com/ethereum/go-ethereum/common" +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) type DataId [16]byte @@ -22,6 +25,17 @@ type BuildBlockArgs struct { FillPending bool } +type buildBlockArgsMarshaling struct { + Slot hexutil.Uint64 + ProposerPubkey hexutil.Bytes + + Timestamp hexutil.Uint64 + + GasLimit hexutil.Uint64 + + Extra hexutil.Bytes +} + type DataRecord struct { Id DataId Salt DataId @@ -31,6 +45,10 @@ type DataRecord struct { Version string } +type dataRecordMarshaling struct { + DecryptionCondition hexutil.Uint64 +} + type HttpRequest struct { Url string Method string @@ -39,6 +57,10 @@ type HttpRequest struct { WithFlashbotsSignature bool } +type httpRequestMarshaling struct { + Body hexutil.Bytes +} + type SimulateTransactionResult struct { Egp uint64 Logs []*SimulatedLog @@ -46,12 +68,20 @@ type SimulateTransactionResult struct { Error string } +type simulateTransactionResultMarshaling struct { + Egp hexutil.Uint64 +} + type SimulatedLog struct { Data []byte Addr common.Address Topics []common.Hash } +type simulatedLogMarshaling struct { + Data hexutil.Bytes +} + type Withdrawal struct { Index uint64 Validator uint64 @@ -59,6 +89,13 @@ type Withdrawal struct { Amount uint64 } +type withdrawalMarshaling struct { + Index hexutil.Uint64 + Validator hexutil.Uint64 + + Amount hexutil.Uint64 +} + type CryptoSignature uint8 const ( diff --git a/core/types/withdrawal.go b/core/types/withdrawal.go index 712d7b6d5..b4db693ad 100644 --- a/core/types/withdrawal.go +++ b/core/types/withdrawal.go @@ -19,20 +19,12 @@ package types import ( "bytes" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/rlp" ) //go:generate go run github.com/fjl/gencodec -type Withdrawal -field-override withdrawalMarshaling -out gen_withdrawal_json.go //go:generate go run ../../rlp/rlpgen -type Withdrawal -out gen_withdrawal_rlp.go -// field type overrides for gencodec -type withdrawalMarshaling struct { - Index hexutil.Uint64 - Validator hexutil.Uint64 - Amount hexutil.Uint64 -} - // Withdrawals implements DerivableList for withdrawals. type Withdrawals []*Withdrawal diff --git a/suave/gen/main.go b/suave/gen/main.go index 31ff90b0e..85a740f95 100644 --- a/suave/gen/main.go +++ b/suave/gen/main.go @@ -39,10 +39,20 @@ func applyTemplate(templateText string, input desc, out string) error { return hex.EncodeToString(hash) }, "typ2": func(param interface{}) string { - return encodeTypeToGolang(param.(string), false, false) + return encodeTypeToGolang(param.(string), false, false, false) }, "typ3": func(param interface{}) string { - return encodeTypeToGolang(param.(string), true, true) + return encodeTypeToGolang(param.(string), true, true, false) + }, + "hextype": func(param interface{}) string { + return encodeTypeToGolang(param.(string), true, true, true) + }, + "isHex": func(param interface{}) bool { + typ := param.(string) + return typ == "bytes" || typ == "uint64" || typ == "uint256" + }, + "lower": func(param interface{}) string { + return strings.ToLower(param.(string)[0:1]) + param.(string)[1:] }, "title": func(param interface{}) string { return strings.Title(param.(string)) @@ -174,20 +184,26 @@ func main() { } } -func encodeTypeToGolang(str string, insideTypes bool, slicePointers bool) string { +func encodeTypeToGolang(str string, insideTypes bool, slicePointers bool, hexEncode bool) string { typ, err := abi.NewType(str, "", nil) if err == nil { // basic type that has an easy match with Go if typ.T == abi.SliceTy { - return "[]" + encodeTypeToGolang(typ.Elem.String(), insideTypes, slicePointers) + return "[]" + encodeTypeToGolang(typ.Elem.String(), insideTypes, slicePointers, hexEncode) } switch str { case "uint256": + if hexEncode { + return "(*hexutil.Big)" + } return "*big.Int" case "address": return "common.Address" case "bytes": + if hexEncode { + return "hexutil.Bytes" + } return "[]byte" case "bytes32": return "common.Hash" @@ -198,6 +214,9 @@ func encodeTypeToGolang(str string, insideTypes bool, slicePointers bool) string } if strings.HasPrefix(str, "uint") { + if str == "uint64" && hexEncode { + return "hexutil.Uint64" + } // uint8, uint16, uint32, uint64 are encoded the same way in Go return str } @@ -231,7 +250,10 @@ var structsTemplate = `// Code generated by suave/gen. DO NOT EDIT. // Hash: {{hash}} package types -import "github.com/ethereum/go-ethereum/common" +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) {{range .Types}} type {{.Name}} {{typ3 .Typ}} @@ -243,6 +265,13 @@ type {{.Name}} struct { {{range .Fields}}{{title .Name}} {{typ3 .Typ}} {{end}} } + +type {{.Name | lower}}Marshaling struct { +{{range .Fields}} +{{if isHex .Typ}}{{title .Name}} {{hextype .Typ}}{{else}} +{{end}}{{end}} +} + {{end}} {{range .Enums}} @@ -322,7 +351,7 @@ func (b *SuaveRuntimeAdapter) {{.Name}}(input []byte) (res []byte, err error) { var ( {{range .Input}}{{.Name}} {{typ2 .Typ}} {{end}}) - + {{range $index, $item := .Input}}{{ if isComplex .Typ }} if err = mapstructure.Decode(unpacked[{{$index}}], &{{.Name}}); err != nil { err = errFailedToDecodeField @@ -334,7 +363,7 @@ func (b *SuaveRuntimeAdapter) {{.Name}}(input []byte) (res []byte, err error) { var ( {{range .Output.Fields}}{{.Name}} {{typ2 .Typ}} {{end}}) - + if {{range .Output.Fields}}{{.Name}},{{end}} err = b.impl.{{.Name}}({{range .Input}}{{.Name}}, {{end}}); err != nil { return } @@ -407,7 +436,7 @@ struct {{.Name}} { {{end}} } {{end}} -address public constant ANYALLOWED = +address public constant ANYALLOWED = 0xC8df3686b4Afb2BB53e60EAe97EF043FE03Fb829; address public constant IS_CONFIDENTIAL_ADDR = diff --git a/suave/gen/main_test.go b/suave/gen/main_test.go index 486c97718..1c87b7cfc 100644 --- a/suave/gen/main_test.go +++ b/suave/gen/main_test.go @@ -42,7 +42,7 @@ func TestEncodeTypeToGolang(t *testing.T) { } for _, c := range cases { - actual := encodeTypeToGolang(c.name, true, true) + actual := encodeTypeToGolang(c.name, true, true, false) require.Equal(t, c.expected, actual) } }