diff --git a/go.mod b/go.mod index c320dc853c..2325adf1d2 100644 --- a/go.mod +++ b/go.mod @@ -79,7 +79,6 @@ require ( github.com/pborman/uuid v1.2.1 github.com/peterh/liner v1.2.0 // indirect github.com/pkg/errors v0.9.1 - github.com/prestonvanloon/go-recaptcha v0.0.0-20190217191114-0834cef6e8bd github.com/prometheus/client_golang v1.11.0 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.29.0 // indirect diff --git a/go.sum b/go.sum index 60b24f673e..4f8e2b38d0 100644 --- a/go.sum +++ b/go.sum @@ -1112,8 +1112,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prestonvanloon/go v1.1.7-0.20190722034630-4f2e55fcf87b h1:Bt5PzQCqfP4xiLXDSrMoqAfj6CBr3N9DAyyq8OiIWsc= github.com/prestonvanloon/go v1.1.7-0.20190722034630-4f2e55fcf87b/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/prestonvanloon/go-recaptcha v0.0.0-20190217191114-0834cef6e8bd h1:/JK1WfWJGBNDKY70uiB53iKKbFqxBx2CuYgj9hK2O70= -github.com/prestonvanloon/go-recaptcha v0.0.0-20190217191114-0834cef6e8bd/go.mod h1:VznrsvawieSl6CEevE2+VGbJPfKWUYfwjb00oJxSXvU= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= diff --git a/proto/faucet/BUILD.bazel b/proto/faucet/BUILD.bazel deleted file mode 100644 index 3b69cfff39..0000000000 --- a/proto/faucet/BUILD.bazel +++ /dev/null @@ -1,10 +0,0 @@ -# gazelle:ignore -load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") - -go_proto_library( - name = "faucet_go_proto", - compiler = "//:grpc_proto_compiler", - importpath = "github.com/prysmaticlabs/prysm/proto/faucet", - proto = "@prysm_testnet_site//:faucet_proto", - visibility = ["//visibility:public"], -) diff --git a/proto/faucet/faucet.pb.go b/proto/faucet/faucet.pb.go deleted file mode 100755 index ebe725d164..0000000000 --- a/proto/faucet/faucet.pb.go +++ /dev/null @@ -1,344 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.25.0 -// protoc v3.15.8 -// source: src/proto/faucet.proto - -package faucet - -import ( - context "context" - reflect "reflect" - sync "sync" - - proto "github.com/golang/protobuf/proto" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -// This is a compile-time assertion that a sufficiently up-to-date version -// of the legacy proto package is being used. -const _ = proto.ProtoPackageIsVersion4 - -type FundingRequest struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - WalletAddress string `protobuf:"bytes,1,opt,name=wallet_address,json=walletAddress,proto3" json:"wallet_address,omitempty"` - RecaptchaSiteKey string `protobuf:"bytes,2,opt,name=recaptcha_site_key,json=recaptchaSiteKey,proto3" json:"recaptcha_site_key,omitempty"` - RecaptchaResponse string `protobuf:"bytes,3,opt,name=recaptcha_response,json=recaptchaResponse,proto3" json:"recaptcha_response,omitempty"` -} - -func (x *FundingRequest) Reset() { - *x = FundingRequest{} - if protoimpl.UnsafeEnabled { - mi := &file_src_proto_faucet_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *FundingRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*FundingRequest) ProtoMessage() {} - -func (x *FundingRequest) ProtoReflect() protoreflect.Message { - mi := &file_src_proto_faucet_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use FundingRequest.ProtoReflect.Descriptor instead. -func (*FundingRequest) Descriptor() ([]byte, []int) { - return file_src_proto_faucet_proto_rawDescGZIP(), []int{0} -} - -func (x *FundingRequest) GetWalletAddress() string { - if x != nil { - return x.WalletAddress - } - return "" -} - -func (x *FundingRequest) GetRecaptchaSiteKey() string { - if x != nil { - return x.RecaptchaSiteKey - } - return "" -} - -func (x *FundingRequest) GetRecaptchaResponse() string { - if x != nil { - return x.RecaptchaResponse - } - return "" -} - -type FundingResponse struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"` - Amount string `protobuf:"bytes,2,opt,name=amount,proto3" json:"amount,omitempty"` - TransactionHash string `protobuf:"bytes,3,opt,name=transactionHash,proto3" json:"transactionHash,omitempty"` -} - -func (x *FundingResponse) Reset() { - *x = FundingResponse{} - if protoimpl.UnsafeEnabled { - mi := &file_src_proto_faucet_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *FundingResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*FundingResponse) ProtoMessage() {} - -func (x *FundingResponse) ProtoReflect() protoreflect.Message { - mi := &file_src_proto_faucet_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use FundingResponse.ProtoReflect.Descriptor instead. -func (*FundingResponse) Descriptor() ([]byte, []int) { - return file_src_proto_faucet_proto_rawDescGZIP(), []int{1} -} - -func (x *FundingResponse) GetError() string { - if x != nil { - return x.Error - } - return "" -} - -func (x *FundingResponse) GetAmount() string { - if x != nil { - return x.Amount - } - return "" -} - -func (x *FundingResponse) GetTransactionHash() string { - if x != nil { - return x.TransactionHash - } - return "" -} - -var File_src_proto_faucet_proto protoreflect.FileDescriptor - -var file_src_proto_faucet_proto_rawDesc = []byte{ - 0x0a, 0x16, 0x73, 0x72, 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x66, 0x61, 0x75, 0x63, - 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x66, 0x61, 0x75, 0x63, 0x65, 0x74, - 0x22, 0x94, 0x01, 0x0a, 0x0e, 0x46, 0x75, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x25, 0x0a, 0x0e, 0x77, 0x61, 0x6c, 0x6c, 0x65, 0x74, 0x5f, 0x61, 0x64, - 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x77, 0x61, 0x6c, - 0x6c, 0x65, 0x74, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x2c, 0x0a, 0x12, 0x72, 0x65, - 0x63, 0x61, 0x70, 0x74, 0x63, 0x68, 0x61, 0x5f, 0x73, 0x69, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x72, 0x65, 0x63, 0x61, 0x70, 0x74, 0x63, 0x68, - 0x61, 0x53, 0x69, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x12, 0x2d, 0x0a, 0x12, 0x72, 0x65, 0x63, 0x61, - 0x70, 0x74, 0x63, 0x68, 0x61, 0x5f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x11, 0x72, 0x65, 0x63, 0x61, 0x70, 0x74, 0x63, 0x68, 0x61, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x69, 0x0a, 0x0f, 0x46, 0x75, 0x6e, 0x64, 0x69, - 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, - 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, - 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x28, 0x0a, 0x0f, 0x74, 0x72, 0x61, 0x6e, - 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x48, 0x61, 0x73, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x0f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x48, 0x61, - 0x73, 0x68, 0x32, 0x52, 0x0a, 0x0d, 0x46, 0x61, 0x75, 0x63, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x41, 0x0a, 0x0c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x46, 0x75, - 0x6e, 0x64, 0x73, 0x12, 0x16, 0x2e, 0x66, 0x61, 0x75, 0x63, 0x65, 0x74, 0x2e, 0x46, 0x75, 0x6e, - 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x66, 0x61, - 0x75, 0x63, 0x65, 0x74, 0x2e, 0x46, 0x75, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_src_proto_faucet_proto_rawDescOnce sync.Once - file_src_proto_faucet_proto_rawDescData = file_src_proto_faucet_proto_rawDesc -) - -func file_src_proto_faucet_proto_rawDescGZIP() []byte { - file_src_proto_faucet_proto_rawDescOnce.Do(func() { - file_src_proto_faucet_proto_rawDescData = protoimpl.X.CompressGZIP(file_src_proto_faucet_proto_rawDescData) - }) - return file_src_proto_faucet_proto_rawDescData -} - -var file_src_proto_faucet_proto_msgTypes = make([]protoimpl.MessageInfo, 2) -var file_src_proto_faucet_proto_goTypes = []interface{}{ - (*FundingRequest)(nil), // 0: faucet.FundingRequest - (*FundingResponse)(nil), // 1: faucet.FundingResponse -} -var file_src_proto_faucet_proto_depIdxs = []int32{ - 0, // 0: faucet.FaucetService.RequestFunds:input_type -> faucet.FundingRequest - 1, // 1: faucet.FaucetService.RequestFunds:output_type -> faucet.FundingResponse - 1, // [1:2] is the sub-list for method output_type - 0, // [0:1] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_src_proto_faucet_proto_init() } -func file_src_proto_faucet_proto_init() { - if File_src_proto_faucet_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_src_proto_faucet_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*FundingRequest); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_src_proto_faucet_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*FundingResponse); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_src_proto_faucet_proto_rawDesc, - NumEnums: 0, - NumMessages: 2, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_src_proto_faucet_proto_goTypes, - DependencyIndexes: file_src_proto_faucet_proto_depIdxs, - MessageInfos: file_src_proto_faucet_proto_msgTypes, - }.Build() - File_src_proto_faucet_proto = out.File - file_src_proto_faucet_proto_rawDesc = nil - file_src_proto_faucet_proto_goTypes = nil - file_src_proto_faucet_proto_depIdxs = nil -} - -// Reference imports to suppress errors if they are not otherwise used. -var _ context.Context -var _ grpc.ClientConnInterface - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion6 - -// FaucetServiceClient is the client API for FaucetService service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. -type FaucetServiceClient interface { - RequestFunds(ctx context.Context, in *FundingRequest, opts ...grpc.CallOption) (*FundingResponse, error) -} - -type faucetServiceClient struct { - cc grpc.ClientConnInterface -} - -func NewFaucetServiceClient(cc grpc.ClientConnInterface) FaucetServiceClient { - return &faucetServiceClient{cc} -} - -func (c *faucetServiceClient) RequestFunds(ctx context.Context, in *FundingRequest, opts ...grpc.CallOption) (*FundingResponse, error) { - out := new(FundingResponse) - err := c.cc.Invoke(ctx, "/faucet.FaucetService/RequestFunds", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// FaucetServiceServer is the server API for FaucetService service. -type FaucetServiceServer interface { - RequestFunds(context.Context, *FundingRequest) (*FundingResponse, error) -} - -// UnimplementedFaucetServiceServer can be embedded to have forward compatible implementations. -type UnimplementedFaucetServiceServer struct { -} - -func (*UnimplementedFaucetServiceServer) RequestFunds(context.Context, *FundingRequest) (*FundingResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method RequestFunds not implemented") -} - -func RegisterFaucetServiceServer(s *grpc.Server, srv FaucetServiceServer) { - s.RegisterService(&_FaucetService_serviceDesc, srv) -} - -func _FaucetService_RequestFunds_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(FundingRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(FaucetServiceServer).RequestFunds(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/faucet.FaucetService/RequestFunds", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(FaucetServiceServer).RequestFunds(ctx, req.(*FundingRequest)) - } - return interceptor(ctx, in, info, handler) -} - -var _FaucetService_serviceDesc = grpc.ServiceDesc{ - ServiceName: "faucet.FaucetService", - HandlerType: (*FaucetServiceServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "RequestFunds", - Handler: _FaucetService_RequestFunds_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "src/proto/faucet.proto", -} diff --git a/tools/faucet/BUILD.bazel b/tools/faucet/BUILD.bazel deleted file mode 100644 index 12afaf2800..0000000000 --- a/tools/faucet/BUILD.bazel +++ /dev/null @@ -1,64 +0,0 @@ -# gazelle:ignore -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") -load("@io_bazel_rules_docker//go:image.bzl", "go_image") -load("@io_bazel_rules_docker//container:container.bzl", "container_bundle") -load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push") - -IMPORT_PATH = "github.com/prysmaticlabs/prysm/tools/faucet" - -SRCS = [ - "main.go", - "server.go", -] - -DEPS = [ - "//proto/faucet:faucet_go_proto", - "//shared/timeutils:go_default_library", - "//shared/maxprocs:go_default_library", - "@org_golang_google_grpc//:go_default_library", - "@org_golang_google_grpc//peer:go_default_library", - "@org_golang_google_grpc//metadata:go_default_library", - "@org_golang_google_grpc//reflection:go_default_library", - "@com_github_prestonvanloon_go_recaptcha//:go_default_library", - "@com_github_ethereum_go_ethereum//ethclient:go_default_library", - "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_ethereum_go_ethereum//params:go_default_library", - "@com_github_ethereum_go_ethereum//crypto:go_default_library", - "@com_github_ethereum_go_ethereum//core/types:go_default_library", -] - -go_library( - name = "go_default_library", - srcs = SRCS, - importpath = IMPORT_PATH, - visibility = ["//visibility:private"], - deps = DEPS, -) - -go_binary( - name = "faucet", - embed = [":go_default_library"], - visibility = ["//visibility:public"], -) - -go_image( - name = "image", - base = "//tools:go_image", - binary = ":faucet", - tags = ["manual"], -) - -container_bundle( - name = "image_bundle", - images = { - "gcr.io/prysmaticlabs/prysm/faucet:latest": ":image", - "gcr.io/prysmaticlabs/prysm/faucet:{DOCKER_TAG}": ":image", - }, - tags = ["manual"], -) - -docker_push( - name = "push_images", - bundle = ":image_bundle", - tags = ["manual"], -) diff --git a/tools/faucet/main.go b/tools/faucet/main.go deleted file mode 100644 index 8baaad1bab..0000000000 --- a/tools/faucet/main.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "net" - - "github.com/prestonvanloon/go-recaptcha" - faucetpb "github.com/prysmaticlabs/prysm/proto/faucet" - _ "github.com/prysmaticlabs/prysm/shared/maxprocs" - "google.golang.org/grpc" - "google.golang.org/grpc/reflection" -) - -var ( - port = flag.Int("port", 8000, "Port to server gRPC service") - recaptchaSecret = flag.String("recaptcha_secret", "", "Secret to verify recaptcha") - rpcPath = flag.String("rpc", "", "RPC address of a running geth node") - privateKey = flag.String("private-key", "", "The private key of funder") - minScore = flag.Float64("min-score", 0.9, "Minimum captcha score.") -) - -func main() { - flag.Parse() - - lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) - if err != nil { - panic(err) - } - s := grpc.NewServer() - fmt.Println("recaptcha = " + *recaptchaSecret) - faucetpb.RegisterFaucetServiceServer(s, - newFaucetServer( - recaptcha.Recaptcha{RecaptchaPrivateKey: *recaptchaSecret}, - *rpcPath, - *privateKey, - *minScore, - ), - ) - - reflection.Register(s) - go counterWatcher() - - fmt.Printf("Serving gRPC requests on port %d\n", *port) - if err := s.Serve(lis); err != nil { - fmt.Printf("Error: %v", err) - } -} diff --git a/tools/faucet/server.go b/tools/faucet/server.go deleted file mode 100644 index f3dd16f945..0000000000 --- a/tools/faucet/server.go +++ /dev/null @@ -1,208 +0,0 @@ -package main - -import ( - "context" - "crypto/ecdsa" - "errors" - "fmt" - "log" - "math/big" - "strings" - "sync" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/params" - "github.com/prestonvanloon/go-recaptcha" - faucetpb "github.com/prysmaticlabs/prysm/proto/faucet" - "github.com/prysmaticlabs/prysm/shared/timeutils" - "google.golang.org/grpc/metadata" -) - -const ipLimit = 5 - -var fundingAmount *big.Int -var funded = make(map[string]bool) -var ipCounter = make(map[string]int) -var fundingLock sync.Mutex -var pruneDuration = time.Hour * 4 - -const txGasLimit = 40000 -const fundingAmountWei = "32500000000000000000" // 32.5 ETH in Wei. - -type faucetServer struct { - r recaptcha.Recaptcha - client *ethclient.Client - funder common.Address - pk *ecdsa.PrivateKey - minScore float64 -} - -func init() { - var ok bool - fundingAmount, ok = new(big.Int).SetString(fundingAmountWei, 10) - if !ok { - log.Fatal("could not set funding amount") - } -} - -func newFaucetServer( - r recaptcha.Recaptcha, - rpcPath, - funderPrivateKey string, - minScore float64, -) *faucetServer { - client, err := ethclient.DialContext(context.Background(), rpcPath) - if err != nil { - panic(err) - } - - pk, err := crypto.HexToECDSA(funderPrivateKey) - if err != nil { - panic(err) - } - - funder := crypto.PubkeyToAddress(pk.PublicKey) - - bal, err := client.BalanceAt(context.Background(), funder, nil) - if err != nil { - panic(err) - } - - fmt.Printf("Funder is %s\n", funder.Hex()) - fmt.Printf("Funder has %d\n", bal) - - return &faucetServer{ - r: r, - client: client, - funder: funder, - pk: pk, - minScore: minScore, - } -} - -func (s *faucetServer) verifyRecaptcha(peer string, req *faucetpb.FundingRequest) error { - fmt.Printf("Sending captcha request for peer %s\n", peer) - - rr, err := s.r.Check(peer, req.RecaptchaResponse) - if err != nil { - return err - } - if !rr.Success { - fmt.Printf("Unsuccessful recaptcha request. Error codes: %+v\n", rr.ErrorCodes) - return errors.New("failed") - } - if rr.Score < s.minScore { - return fmt.Errorf("recaptcha score too low (%f)", rr.Score) - } - if timeutils.Now().After(rr.ChallengeTS.Add(2 * time.Minute)) { - return errors.New("captcha challenge too old") - } - if rr.Action != req.WalletAddress { - return fmt.Errorf("action was %s, wanted %s", rr.Action, req.WalletAddress) - } - if !strings.HasSuffix(rr.Hostname, "prylabs.net") && !strings.HasSuffix(rr.Hostname, "prylabs.network") { - return fmt.Errorf("expected hostname (%s) to end in prylabs.net", rr.Hostname) - } - - return nil -} - -// RequestFunds from the ethereum 1.x faucet. Requires a valid captcha -// response. -func (s *faucetServer) RequestFunds(ctx context.Context, req *faucetpb.FundingRequest) (*faucetpb.FundingResponse, error) { - peer, err := s.getPeer(ctx) - if err != nil { - fmt.Printf("peer failure %v\n", err) - return &faucetpb.FundingResponse{Error: "peer error"}, nil - } - - if err := s.verifyRecaptcha(peer, req); err != nil { - fmt.Printf("Recaptcha failure %v\n", err) - return &faucetpb.FundingResponse{Error: "recaptcha error"}, nil - } - - fundingLock.Lock() - exceedPeerLimit := ipCounter[peer] >= ipLimit - if funded[req.WalletAddress] || exceedPeerLimit { - if exceedPeerLimit { - fmt.Printf("peer %s trying to get funded despite being over peer limit\n", peer) - } - fundingLock.Unlock() - return &faucetpb.FundingResponse{Error: "funded too recently"}, nil - } - funded[req.WalletAddress] = true - fundingLock.Unlock() - - txHash, err := s.fundAndWait(common.HexToAddress(req.WalletAddress)) - if err != nil { - return &faucetpb.FundingResponse{Error: fmt.Sprintf("Failed to send transaction %v", err)}, nil - } - fundingLock.Lock() - ipCounter[peer]++ - fundingLock.Unlock() - - fmt.Printf("Funded with TX %s\n", txHash) - - return &faucetpb.FundingResponse{ - Amount: fundingAmount.String(), - TransactionHash: txHash, - }, nil -} - -func (s *faucetServer) fundAndWait(to common.Address) (string, error) { - nonce := uint64(0) - nonce, err := s.client.PendingNonceAt(context.Background(), s.funder) - if err != nil { - return "", err - } - - tx := types.NewTransaction(nonce, to, fundingAmount, txGasLimit, big.NewInt(1*params.GWei), nil /*data*/) - - tx, err = types.SignTx(tx, types.NewEIP155Signer(big.NewInt(5)), s.pk) - if err != nil { - return "", err - } - - if err := s.client.SendTransaction(context.Background(), tx); err != nil { - return "", err - } - - // Wait for contract to mine - for pending := true; pending; _, pending, err = s.client.TransactionByHash(context.Background(), tx.Hash()) { - if err != nil { - log.Fatal(err) - } - time.Sleep(1 * time.Second) - } - - return tx.Hash().Hex(), nil -} - -func (s *faucetServer) getPeer(ctx context.Context) (string, error) { - md, ok := metadata.FromIncomingContext(ctx) - if !ok || len(md.Get("x-forwarded-for")) < 1 { - return "", errors.New("metadata not ok") - } - peer := md.Get("x-forwarded-for")[0] - return peer, nil -} - -// reduce the counter for each ip every few hours. -func counterWatcher() { - ticker := time.NewTicker(pruneDuration) - for { - <-ticker.C - fundingLock.Lock() - for ip, ctr := range ipCounter { - if ctr == 0 { - continue - } - ipCounter[ip] = ctr - 1 - } - fundingLock.Unlock() - } -}