From f19ae4bfacd46eb8e1ce0c34cb96a9bff43d1ed9 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sun, 25 Jul 2021 19:19:33 +0700 Subject: [PATCH 01/10] tx propagate, pool mock, try some tests --- Makefile | 4 + go.mod | 17 +- go.sum | 61 +- gointerfaces/sentry/mocks.go | 1131 ++++++++++++++++++++++++++++++++++ gointerfaces/test_util.go | 3 + log/logger.go | 36 ++ tools.go | 22 + txpool/fetch.go | 56 +- txpool/fetch_test.go | 30 +- txpool/mocks.go | 116 ++++ txpool/pool.go | 101 ++- txpool/send.go | 271 ++++++++ txpool/test_util.go | 11 +- txpool/types.go | 3 + 14 files changed, 1817 insertions(+), 45 deletions(-) create mode 100644 gointerfaces/sentry/mocks.go create mode 100644 gointerfaces/test_util.go create mode 100644 log/logger.go create mode 100644 tools.go create mode 100644 txpool/mocks.go create mode 100644 txpool/send.go diff --git a/Makefile b/Makefile index 94a596a5d56..4389c56b77c 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,8 @@ grpc: $(GOBUILD) -o $(GOBIN)/protoc-gen-go google.golang.org/protobuf/cmd/protoc-gen-go # generates proto messages $(GOBUILD) -o $(GOBIN)/protoc-gen-go-grpc google.golang.org/grpc/cmd/protoc-gen-go-grpc # generates grpc services + $(GOBUILD) -o $(GOBIN)/moq github.com/matryer/moq + PATH=$(GOBIN):$(PATH) protoc --proto_path=interfaces --go_out=gointerfaces -I=build/include/google \ types/types.proto PATH=$(GOBIN):$(PATH) protoc --proto_path=interfaces --go_out=gointerfaces --go-grpc_out=gointerfaces -I=build/include/google \ @@ -32,3 +34,5 @@ grpc: consensus_engine/consensus.proto \ testing/testing.proto \ txpool/txpool.proto txpool/mining.proto + + PATH=$(GOBIN):$(PATH) go generate ./... diff --git a/go.mod b/go.mod index 86d5ce9085a..bf280ea9fc8 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,15 @@ module github.com/ledgerwatch/erigon-lib go 1.16 require ( - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.2 github.com/holiman/uint256 v1.2.0 - github.com/kisielk/gotool v1.0.0 // indirect - github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d // indirect - golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect - google.golang.org/grpc v1.39.0 // indirect - google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 // indirect - google.golang.org/protobuf v1.27.1 // indirect + github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d + github.com/matryer/moq v0.2.3 + github.com/stretchr/testify v1.7.0 // indirect + go.uber.org/zap v1.18.1 + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e + golang.org/x/tools v0.1.1 // indirect + google.golang.org/grpc v1.39.0 + google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 + google.golang.org/protobuf v1.27.1 ) diff --git a/go.sum b/go.sum index 86e1e02b345..5926dbd0bc2 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,16 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -28,7 +32,6 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -37,23 +40,45 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/holiman/uint256 v1.2.0 h1:gpSYcPLWGv4sG43I2mVLiDZCNDh/EpGjSk8tmtxitHM= github.com/holiman/uint256 v1.2.0/go.mod h1:y4ga/t+u+Xwd7CpDgZESaRcWy0I7XMlTMA25ApIH5Jw= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/ledgerwatch/secp256k1 v0.0.0-20210620094219-1f13e1400c9b h1:pVxTy+0G5MBgzgq/Jr8FuAWvTQhFo76vEAgREm+KNXo= -github.com/ledgerwatch/secp256k1 v0.0.0-20210620094219-1f13e1400c9b/go.mod h1:SPmqJFciiF/Q0mPt2jVs2dTr/1TZBTIA+kPMmKgBAak= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d h1:/IKMrJdfRsoYNc36PXqP4xMH3vhW/8IQyBKGQbKZUno= github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d/go.mod h1:SPmqJFciiF/Q0mPt2jVs2dTr/1TZBTIA+kPMmKgBAak= +github.com/matryer/moq v0.2.3 h1:Q06vEqnBYjjfx5KKgHfYRKE/lvlRu+Nj+xodG4YdHnU= +github.com/matryer/moq v0.2.3/go.mod h1:9RtPYjTnH1bSBIkpvtHkFN7nbWAnO7oRpdJkEIn6UtE= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0= +go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.18.1 h1:CSUJ2mjFszzEWt4CdKISEuChVIXGBn3lAPwkRGyVrc4= +go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -61,26 +86,38 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -92,7 +129,15 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200815165600-90abf76919f3/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -125,7 +170,13 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/gointerfaces/sentry/mocks.go b/gointerfaces/sentry/mocks.go new file mode 100644 index 00000000000..c67921e49e7 --- /dev/null +++ b/gointerfaces/sentry/mocks.go @@ -0,0 +1,1131 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package sentry + +import ( + context "context" + grpc "google.golang.org/grpc" + emptypb "google.golang.org/protobuf/types/known/emptypb" + sync "sync" +) + +// Ensure, that SentryServerMock does implement SentryServer. +// If this is not the case, regenerate this file with moq. +var _ SentryServer = &SentryServerMock{} + +// SentryServerMock is a mock implementation of SentryServer. +// +// func TestSomethingThatUsesSentryServer(t *testing.T) { +// +// // make and configure a mocked SentryServer +// mockedSentryServer := &SentryServerMock{ +// MessagesFunc: func(messagesRequest *MessagesRequest, sentry_MessagesServer Sentry_MessagesServer) error { +// panic("mock out the Messages method") +// }, +// PeerCountFunc: func(contextMoqParam context.Context, peerCountRequest *PeerCountRequest) (*PeerCountReply, error) { +// panic("mock out the PeerCount method") +// }, +// PeerMinBlockFunc: func(contextMoqParam context.Context, peerMinBlockRequest *PeerMinBlockRequest) (*emptypb.Empty, error) { +// panic("mock out the PeerMinBlock method") +// }, +// PeersFunc: func(peersRequest *PeersRequest, sentry_PeersServer Sentry_PeersServer) error { +// panic("mock out the Peers method") +// }, +// PenalizePeerFunc: func(contextMoqParam context.Context, penalizePeerRequest *PenalizePeerRequest) (*emptypb.Empty, error) { +// panic("mock out the PenalizePeer method") +// }, +// SendMessageByIdFunc: func(contextMoqParam context.Context, sendMessageByIdRequest *SendMessageByIdRequest) (*SentPeers, error) { +// panic("mock out the SendMessageById method") +// }, +// SendMessageByMinBlockFunc: func(contextMoqParam context.Context, sendMessageByMinBlockRequest *SendMessageByMinBlockRequest) (*SentPeers, error) { +// panic("mock out the SendMessageByMinBlock method") +// }, +// SendMessageToAllFunc: func(contextMoqParam context.Context, outboundMessageData *OutboundMessageData) (*SentPeers, error) { +// panic("mock out the SendMessageToAll method") +// }, +// SendMessageToRandomPeersFunc: func(contextMoqParam context.Context, sendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest) (*SentPeers, error) { +// panic("mock out the SendMessageToRandomPeers method") +// }, +// SetStatusFunc: func(contextMoqParam context.Context, statusData *StatusData) (*SetStatusReply, error) { +// panic("mock out the SetStatus method") +// }, +// mustEmbedUnimplementedSentryServerFunc: func() { +// panic("mock out the mustEmbedUnimplementedSentryServer method") +// }, +// } +// +// // use mockedSentryServer in code that requires SentryServer +// // and then make assertions. +// +// } +type SentryServerMock struct { + // MessagesFunc mocks the Messages method. + MessagesFunc func(messagesRequest *MessagesRequest, sentry_MessagesServer Sentry_MessagesServer) error + + // PeerCountFunc mocks the PeerCount method. + PeerCountFunc func(contextMoqParam context.Context, peerCountRequest *PeerCountRequest) (*PeerCountReply, error) + + // PeerMinBlockFunc mocks the PeerMinBlock method. + PeerMinBlockFunc func(contextMoqParam context.Context, peerMinBlockRequest *PeerMinBlockRequest) (*emptypb.Empty, error) + + // PeersFunc mocks the Peers method. + PeersFunc func(peersRequest *PeersRequest, sentry_PeersServer Sentry_PeersServer) error + + // PenalizePeerFunc mocks the PenalizePeer method. + PenalizePeerFunc func(contextMoqParam context.Context, penalizePeerRequest *PenalizePeerRequest) (*emptypb.Empty, error) + + // SendMessageByIdFunc mocks the SendMessageById method. + SendMessageByIdFunc func(contextMoqParam context.Context, sendMessageByIdRequest *SendMessageByIdRequest) (*SentPeers, error) + + // SendMessageByMinBlockFunc mocks the SendMessageByMinBlock method. + SendMessageByMinBlockFunc func(contextMoqParam context.Context, sendMessageByMinBlockRequest *SendMessageByMinBlockRequest) (*SentPeers, error) + + // SendMessageToAllFunc mocks the SendMessageToAll method. + SendMessageToAllFunc func(contextMoqParam context.Context, outboundMessageData *OutboundMessageData) (*SentPeers, error) + + // SendMessageToRandomPeersFunc mocks the SendMessageToRandomPeers method. + SendMessageToRandomPeersFunc func(contextMoqParam context.Context, sendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest) (*SentPeers, error) + + // SetStatusFunc mocks the SetStatus method. + SetStatusFunc func(contextMoqParam context.Context, statusData *StatusData) (*SetStatusReply, error) + + // mustEmbedUnimplementedSentryServerFunc mocks the mustEmbedUnimplementedSentryServer method. + mustEmbedUnimplementedSentryServerFunc func() + + // calls tracks calls to the methods. + calls struct { + // Messages holds details about calls to the Messages method. + Messages []struct { + // MessagesRequest is the messagesRequest argument value. + MessagesRequest *MessagesRequest + // Sentry_MessagesServer is the sentry_MessagesServer argument value. + Sentry_MessagesServer Sentry_MessagesServer + } + // PeerCount holds details about calls to the PeerCount method. + PeerCount []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // PeerCountRequest is the peerCountRequest argument value. + PeerCountRequest *PeerCountRequest + } + // PeerMinBlock holds details about calls to the PeerMinBlock method. + PeerMinBlock []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // PeerMinBlockRequest is the peerMinBlockRequest argument value. + PeerMinBlockRequest *PeerMinBlockRequest + } + // Peers holds details about calls to the Peers method. + Peers []struct { + // PeersRequest is the peersRequest argument value. + PeersRequest *PeersRequest + // Sentry_PeersServer is the sentry_PeersServer argument value. + Sentry_PeersServer Sentry_PeersServer + } + // PenalizePeer holds details about calls to the PenalizePeer method. + PenalizePeer []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // PenalizePeerRequest is the penalizePeerRequest argument value. + PenalizePeerRequest *PenalizePeerRequest + } + // SendMessageById holds details about calls to the SendMessageById method. + SendMessageById []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // SendMessageByIdRequest is the sendMessageByIdRequest argument value. + SendMessageByIdRequest *SendMessageByIdRequest + } + // SendMessageByMinBlock holds details about calls to the SendMessageByMinBlock method. + SendMessageByMinBlock []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // SendMessageByMinBlockRequest is the sendMessageByMinBlockRequest argument value. + SendMessageByMinBlockRequest *SendMessageByMinBlockRequest + } + // SendMessageToAll holds details about calls to the SendMessageToAll method. + SendMessageToAll []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // OutboundMessageData is the outboundMessageData argument value. + OutboundMessageData *OutboundMessageData + } + // SendMessageToRandomPeers holds details about calls to the SendMessageToRandomPeers method. + SendMessageToRandomPeers []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // SendMessageToRandomPeersRequest is the sendMessageToRandomPeersRequest argument value. + SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest + } + // SetStatus holds details about calls to the SetStatus method. + SetStatus []struct { + // ContextMoqParam is the contextMoqParam argument value. + ContextMoqParam context.Context + // StatusData is the statusData argument value. + StatusData *StatusData + } + // mustEmbedUnimplementedSentryServer holds details about calls to the mustEmbedUnimplementedSentryServer method. + mustEmbedUnimplementedSentryServer []struct { + } + } + lockMessages sync.RWMutex + lockPeerCount sync.RWMutex + lockPeerMinBlock sync.RWMutex + lockPeers sync.RWMutex + lockPenalizePeer sync.RWMutex + lockSendMessageById sync.RWMutex + lockSendMessageByMinBlock sync.RWMutex + lockSendMessageToAll sync.RWMutex + lockSendMessageToRandomPeers sync.RWMutex + lockSetStatus sync.RWMutex + lockmustEmbedUnimplementedSentryServer sync.RWMutex +} + +// Messages calls MessagesFunc. +func (mock *SentryServerMock) Messages(messagesRequest *MessagesRequest, sentry_MessagesServer Sentry_MessagesServer) error { + if mock.MessagesFunc == nil { + panic("SentryServerMock.MessagesFunc: method is nil but SentryServer.Messages was just called") + } + callInfo := struct { + MessagesRequest *MessagesRequest + Sentry_MessagesServer Sentry_MessagesServer + }{ + MessagesRequest: messagesRequest, + Sentry_MessagesServer: sentry_MessagesServer, + } + mock.lockMessages.Lock() + mock.calls.Messages = append(mock.calls.Messages, callInfo) + mock.lockMessages.Unlock() + return mock.MessagesFunc(messagesRequest, sentry_MessagesServer) +} + +// MessagesCalls gets all the calls that were made to Messages. +// Check the length with: +// len(mockedSentryServer.MessagesCalls()) +func (mock *SentryServerMock) MessagesCalls() []struct { + MessagesRequest *MessagesRequest + Sentry_MessagesServer Sentry_MessagesServer +} { + var calls []struct { + MessagesRequest *MessagesRequest + Sentry_MessagesServer Sentry_MessagesServer + } + mock.lockMessages.RLock() + calls = mock.calls.Messages + mock.lockMessages.RUnlock() + return calls +} + +// PeerCount calls PeerCountFunc. +func (mock *SentryServerMock) PeerCount(contextMoqParam context.Context, peerCountRequest *PeerCountRequest) (*PeerCountReply, error) { + if mock.PeerCountFunc == nil { + panic("SentryServerMock.PeerCountFunc: method is nil but SentryServer.PeerCount was just called") + } + callInfo := struct { + ContextMoqParam context.Context + PeerCountRequest *PeerCountRequest + }{ + ContextMoqParam: contextMoqParam, + PeerCountRequest: peerCountRequest, + } + mock.lockPeerCount.Lock() + mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) + mock.lockPeerCount.Unlock() + return mock.PeerCountFunc(contextMoqParam, peerCountRequest) +} + +// PeerCountCalls gets all the calls that were made to PeerCount. +// Check the length with: +// len(mockedSentryServer.PeerCountCalls()) +func (mock *SentryServerMock) PeerCountCalls() []struct { + ContextMoqParam context.Context + PeerCountRequest *PeerCountRequest +} { + var calls []struct { + ContextMoqParam context.Context + PeerCountRequest *PeerCountRequest + } + mock.lockPeerCount.RLock() + calls = mock.calls.PeerCount + mock.lockPeerCount.RUnlock() + return calls +} + +// PeerMinBlock calls PeerMinBlockFunc. +func (mock *SentryServerMock) PeerMinBlock(contextMoqParam context.Context, peerMinBlockRequest *PeerMinBlockRequest) (*emptypb.Empty, error) { + if mock.PeerMinBlockFunc == nil { + panic("SentryServerMock.PeerMinBlockFunc: method is nil but SentryServer.PeerMinBlock was just called") + } + callInfo := struct { + ContextMoqParam context.Context + PeerMinBlockRequest *PeerMinBlockRequest + }{ + ContextMoqParam: contextMoqParam, + PeerMinBlockRequest: peerMinBlockRequest, + } + mock.lockPeerMinBlock.Lock() + mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) + mock.lockPeerMinBlock.Unlock() + return mock.PeerMinBlockFunc(contextMoqParam, peerMinBlockRequest) +} + +// PeerMinBlockCalls gets all the calls that were made to PeerMinBlock. +// Check the length with: +// len(mockedSentryServer.PeerMinBlockCalls()) +func (mock *SentryServerMock) PeerMinBlockCalls() []struct { + ContextMoqParam context.Context + PeerMinBlockRequest *PeerMinBlockRequest +} { + var calls []struct { + ContextMoqParam context.Context + PeerMinBlockRequest *PeerMinBlockRequest + } + mock.lockPeerMinBlock.RLock() + calls = mock.calls.PeerMinBlock + mock.lockPeerMinBlock.RUnlock() + return calls +} + +// Peers calls PeersFunc. +func (mock *SentryServerMock) Peers(peersRequest *PeersRequest, sentry_PeersServer Sentry_PeersServer) error { + if mock.PeersFunc == nil { + panic("SentryServerMock.PeersFunc: method is nil but SentryServer.Peers was just called") + } + callInfo := struct { + PeersRequest *PeersRequest + Sentry_PeersServer Sentry_PeersServer + }{ + PeersRequest: peersRequest, + Sentry_PeersServer: sentry_PeersServer, + } + mock.lockPeers.Lock() + mock.calls.Peers = append(mock.calls.Peers, callInfo) + mock.lockPeers.Unlock() + return mock.PeersFunc(peersRequest, sentry_PeersServer) +} + +// PeersCalls gets all the calls that were made to Peers. +// Check the length with: +// len(mockedSentryServer.PeersCalls()) +func (mock *SentryServerMock) PeersCalls() []struct { + PeersRequest *PeersRequest + Sentry_PeersServer Sentry_PeersServer +} { + var calls []struct { + PeersRequest *PeersRequest + Sentry_PeersServer Sentry_PeersServer + } + mock.lockPeers.RLock() + calls = mock.calls.Peers + mock.lockPeers.RUnlock() + return calls +} + +// PenalizePeer calls PenalizePeerFunc. +func (mock *SentryServerMock) PenalizePeer(contextMoqParam context.Context, penalizePeerRequest *PenalizePeerRequest) (*emptypb.Empty, error) { + if mock.PenalizePeerFunc == nil { + panic("SentryServerMock.PenalizePeerFunc: method is nil but SentryServer.PenalizePeer was just called") + } + callInfo := struct { + ContextMoqParam context.Context + PenalizePeerRequest *PenalizePeerRequest + }{ + ContextMoqParam: contextMoqParam, + PenalizePeerRequest: penalizePeerRequest, + } + mock.lockPenalizePeer.Lock() + mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) + mock.lockPenalizePeer.Unlock() + return mock.PenalizePeerFunc(contextMoqParam, penalizePeerRequest) +} + +// PenalizePeerCalls gets all the calls that were made to PenalizePeer. +// Check the length with: +// len(mockedSentryServer.PenalizePeerCalls()) +func (mock *SentryServerMock) PenalizePeerCalls() []struct { + ContextMoqParam context.Context + PenalizePeerRequest *PenalizePeerRequest +} { + var calls []struct { + ContextMoqParam context.Context + PenalizePeerRequest *PenalizePeerRequest + } + mock.lockPenalizePeer.RLock() + calls = mock.calls.PenalizePeer + mock.lockPenalizePeer.RUnlock() + return calls +} + +// SendMessageById calls SendMessageByIdFunc. +func (mock *SentryServerMock) SendMessageById(contextMoqParam context.Context, sendMessageByIdRequest *SendMessageByIdRequest) (*SentPeers, error) { + if mock.SendMessageByIdFunc == nil { + panic("SentryServerMock.SendMessageByIdFunc: method is nil but SentryServer.SendMessageById was just called") + } + callInfo := struct { + ContextMoqParam context.Context + SendMessageByIdRequest *SendMessageByIdRequest + }{ + ContextMoqParam: contextMoqParam, + SendMessageByIdRequest: sendMessageByIdRequest, + } + mock.lockSendMessageById.Lock() + mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) + mock.lockSendMessageById.Unlock() + return mock.SendMessageByIdFunc(contextMoqParam, sendMessageByIdRequest) +} + +// SendMessageByIdCalls gets all the calls that were made to SendMessageById. +// Check the length with: +// len(mockedSentryServer.SendMessageByIdCalls()) +func (mock *SentryServerMock) SendMessageByIdCalls() []struct { + ContextMoqParam context.Context + SendMessageByIdRequest *SendMessageByIdRequest +} { + var calls []struct { + ContextMoqParam context.Context + SendMessageByIdRequest *SendMessageByIdRequest + } + mock.lockSendMessageById.RLock() + calls = mock.calls.SendMessageById + mock.lockSendMessageById.RUnlock() + return calls +} + +// SendMessageByMinBlock calls SendMessageByMinBlockFunc. +func (mock *SentryServerMock) SendMessageByMinBlock(contextMoqParam context.Context, sendMessageByMinBlockRequest *SendMessageByMinBlockRequest) (*SentPeers, error) { + if mock.SendMessageByMinBlockFunc == nil { + panic("SentryServerMock.SendMessageByMinBlockFunc: method is nil but SentryServer.SendMessageByMinBlock was just called") + } + callInfo := struct { + ContextMoqParam context.Context + SendMessageByMinBlockRequest *SendMessageByMinBlockRequest + }{ + ContextMoqParam: contextMoqParam, + SendMessageByMinBlockRequest: sendMessageByMinBlockRequest, + } + mock.lockSendMessageByMinBlock.Lock() + mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) + mock.lockSendMessageByMinBlock.Unlock() + return mock.SendMessageByMinBlockFunc(contextMoqParam, sendMessageByMinBlockRequest) +} + +// SendMessageByMinBlockCalls gets all the calls that were made to SendMessageByMinBlock. +// Check the length with: +// len(mockedSentryServer.SendMessageByMinBlockCalls()) +func (mock *SentryServerMock) SendMessageByMinBlockCalls() []struct { + ContextMoqParam context.Context + SendMessageByMinBlockRequest *SendMessageByMinBlockRequest +} { + var calls []struct { + ContextMoqParam context.Context + SendMessageByMinBlockRequest *SendMessageByMinBlockRequest + } + mock.lockSendMessageByMinBlock.RLock() + calls = mock.calls.SendMessageByMinBlock + mock.lockSendMessageByMinBlock.RUnlock() + return calls +} + +// SendMessageToAll calls SendMessageToAllFunc. +func (mock *SentryServerMock) SendMessageToAll(contextMoqParam context.Context, outboundMessageData *OutboundMessageData) (*SentPeers, error) { + if mock.SendMessageToAllFunc == nil { + panic("SentryServerMock.SendMessageToAllFunc: method is nil but SentryServer.SendMessageToAll was just called") + } + callInfo := struct { + ContextMoqParam context.Context + OutboundMessageData *OutboundMessageData + }{ + ContextMoqParam: contextMoqParam, + OutboundMessageData: outboundMessageData, + } + mock.lockSendMessageToAll.Lock() + mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) + mock.lockSendMessageToAll.Unlock() + return mock.SendMessageToAllFunc(contextMoqParam, outboundMessageData) +} + +// SendMessageToAllCalls gets all the calls that were made to SendMessageToAll. +// Check the length with: +// len(mockedSentryServer.SendMessageToAllCalls()) +func (mock *SentryServerMock) SendMessageToAllCalls() []struct { + ContextMoqParam context.Context + OutboundMessageData *OutboundMessageData +} { + var calls []struct { + ContextMoqParam context.Context + OutboundMessageData *OutboundMessageData + } + mock.lockSendMessageToAll.RLock() + calls = mock.calls.SendMessageToAll + mock.lockSendMessageToAll.RUnlock() + return calls +} + +// SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. +func (mock *SentryServerMock) SendMessageToRandomPeers(contextMoqParam context.Context, sendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest) (*SentPeers, error) { + if mock.SendMessageToRandomPeersFunc == nil { + panic("SentryServerMock.SendMessageToRandomPeersFunc: method is nil but SentryServer.SendMessageToRandomPeers was just called") + } + callInfo := struct { + ContextMoqParam context.Context + SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest + }{ + ContextMoqParam: contextMoqParam, + SendMessageToRandomPeersRequest: sendMessageToRandomPeersRequest, + } + mock.lockSendMessageToRandomPeers.Lock() + mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) + mock.lockSendMessageToRandomPeers.Unlock() + return mock.SendMessageToRandomPeersFunc(contextMoqParam, sendMessageToRandomPeersRequest) +} + +// SendMessageToRandomPeersCalls gets all the calls that were made to SendMessageToRandomPeers. +// Check the length with: +// len(mockedSentryServer.SendMessageToRandomPeersCalls()) +func (mock *SentryServerMock) SendMessageToRandomPeersCalls() []struct { + ContextMoqParam context.Context + SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest +} { + var calls []struct { + ContextMoqParam context.Context + SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest + } + mock.lockSendMessageToRandomPeers.RLock() + calls = mock.calls.SendMessageToRandomPeers + mock.lockSendMessageToRandomPeers.RUnlock() + return calls +} + +// SetStatus calls SetStatusFunc. +func (mock *SentryServerMock) SetStatus(contextMoqParam context.Context, statusData *StatusData) (*SetStatusReply, error) { + if mock.SetStatusFunc == nil { + panic("SentryServerMock.SetStatusFunc: method is nil but SentryServer.SetStatus was just called") + } + callInfo := struct { + ContextMoqParam context.Context + StatusData *StatusData + }{ + ContextMoqParam: contextMoqParam, + StatusData: statusData, + } + mock.lockSetStatus.Lock() + mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) + mock.lockSetStatus.Unlock() + return mock.SetStatusFunc(contextMoqParam, statusData) +} + +// SetStatusCalls gets all the calls that were made to SetStatus. +// Check the length with: +// len(mockedSentryServer.SetStatusCalls()) +func (mock *SentryServerMock) SetStatusCalls() []struct { + ContextMoqParam context.Context + StatusData *StatusData +} { + var calls []struct { + ContextMoqParam context.Context + StatusData *StatusData + } + mock.lockSetStatus.RLock() + calls = mock.calls.SetStatus + mock.lockSetStatus.RUnlock() + return calls +} + +// mustEmbedUnimplementedSentryServer calls mustEmbedUnimplementedSentryServerFunc. +func (mock *SentryServerMock) mustEmbedUnimplementedSentryServer() { + if mock.mustEmbedUnimplementedSentryServerFunc == nil { + panic("SentryServerMock.mustEmbedUnimplementedSentryServerFunc: method is nil but SentryServer.mustEmbedUnimplementedSentryServer was just called") + } + callInfo := struct { + }{} + mock.lockmustEmbedUnimplementedSentryServer.Lock() + mock.calls.mustEmbedUnimplementedSentryServer = append(mock.calls.mustEmbedUnimplementedSentryServer, callInfo) + mock.lockmustEmbedUnimplementedSentryServer.Unlock() + mock.mustEmbedUnimplementedSentryServerFunc() +} + +// mustEmbedUnimplementedSentryServerCalls gets all the calls that were made to mustEmbedUnimplementedSentryServer. +// Check the length with: +// len(mockedSentryServer.mustEmbedUnimplementedSentryServerCalls()) +func (mock *SentryServerMock) mustEmbedUnimplementedSentryServerCalls() []struct { +} { + var calls []struct { + } + mock.lockmustEmbedUnimplementedSentryServer.RLock() + calls = mock.calls.mustEmbedUnimplementedSentryServer + mock.lockmustEmbedUnimplementedSentryServer.RUnlock() + return calls +} + +// Ensure, that SentryClientMock does implement SentryClient. +// If this is not the case, regenerate this file with moq. +var _ SentryClient = &SentryClientMock{} + +// SentryClientMock is a mock implementation of SentryClient. +// +// func TestSomethingThatUsesSentryClient(t *testing.T) { +// +// // make and configure a mocked SentryClient +// mockedSentryClient := &SentryClientMock{ +// MessagesFunc: func(ctx context.Context, in *MessagesRequest, opts ...grpc.CallOption) (Sentry_MessagesClient, error) { +// panic("mock out the Messages method") +// }, +// PeerCountFunc: func(ctx context.Context, in *PeerCountRequest, opts ...grpc.CallOption) (*PeerCountReply, error) { +// panic("mock out the PeerCount method") +// }, +// PeerMinBlockFunc: func(ctx context.Context, in *PeerMinBlockRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { +// panic("mock out the PeerMinBlock method") +// }, +// PeersFunc: func(ctx context.Context, in *PeersRequest, opts ...grpc.CallOption) (Sentry_PeersClient, error) { +// panic("mock out the Peers method") +// }, +// PenalizePeerFunc: func(ctx context.Context, in *PenalizePeerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { +// panic("mock out the PenalizePeer method") +// }, +// SendMessageByIdFunc: func(ctx context.Context, in *SendMessageByIdRequest, opts ...grpc.CallOption) (*SentPeers, error) { +// panic("mock out the SendMessageById method") +// }, +// SendMessageByMinBlockFunc: func(ctx context.Context, in *SendMessageByMinBlockRequest, opts ...grpc.CallOption) (*SentPeers, error) { +// panic("mock out the SendMessageByMinBlock method") +// }, +// SendMessageToAllFunc: func(ctx context.Context, in *OutboundMessageData, opts ...grpc.CallOption) (*SentPeers, error) { +// panic("mock out the SendMessageToAll method") +// }, +// SendMessageToRandomPeersFunc: func(ctx context.Context, in *SendMessageToRandomPeersRequest, opts ...grpc.CallOption) (*SentPeers, error) { +// panic("mock out the SendMessageToRandomPeers method") +// }, +// SetStatusFunc: func(ctx context.Context, in *StatusData, opts ...grpc.CallOption) (*SetStatusReply, error) { +// panic("mock out the SetStatus method") +// }, +// } +// +// // use mockedSentryClient in code that requires SentryClient +// // and then make assertions. +// +// } +type SentryClientMock struct { + // MessagesFunc mocks the Messages method. + MessagesFunc func(ctx context.Context, in *MessagesRequest, opts ...grpc.CallOption) (Sentry_MessagesClient, error) + + // PeerCountFunc mocks the PeerCount method. + PeerCountFunc func(ctx context.Context, in *PeerCountRequest, opts ...grpc.CallOption) (*PeerCountReply, error) + + // PeerMinBlockFunc mocks the PeerMinBlock method. + PeerMinBlockFunc func(ctx context.Context, in *PeerMinBlockRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + + // PeersFunc mocks the Peers method. + PeersFunc func(ctx context.Context, in *PeersRequest, opts ...grpc.CallOption) (Sentry_PeersClient, error) + + // PenalizePeerFunc mocks the PenalizePeer method. + PenalizePeerFunc func(ctx context.Context, in *PenalizePeerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + + // SendMessageByIdFunc mocks the SendMessageById method. + SendMessageByIdFunc func(ctx context.Context, in *SendMessageByIdRequest, opts ...grpc.CallOption) (*SentPeers, error) + + // SendMessageByMinBlockFunc mocks the SendMessageByMinBlock method. + SendMessageByMinBlockFunc func(ctx context.Context, in *SendMessageByMinBlockRequest, opts ...grpc.CallOption) (*SentPeers, error) + + // SendMessageToAllFunc mocks the SendMessageToAll method. + SendMessageToAllFunc func(ctx context.Context, in *OutboundMessageData, opts ...grpc.CallOption) (*SentPeers, error) + + // SendMessageToRandomPeersFunc mocks the SendMessageToRandomPeers method. + SendMessageToRandomPeersFunc func(ctx context.Context, in *SendMessageToRandomPeersRequest, opts ...grpc.CallOption) (*SentPeers, error) + + // SetStatusFunc mocks the SetStatus method. + SetStatusFunc func(ctx context.Context, in *StatusData, opts ...grpc.CallOption) (*SetStatusReply, error) + + // calls tracks calls to the methods. + calls struct { + // Messages holds details about calls to the Messages method. + Messages []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *MessagesRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // PeerCount holds details about calls to the PeerCount method. + PeerCount []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *PeerCountRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // PeerMinBlock holds details about calls to the PeerMinBlock method. + PeerMinBlock []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *PeerMinBlockRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // Peers holds details about calls to the Peers method. + Peers []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *PeersRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // PenalizePeer holds details about calls to the PenalizePeer method. + PenalizePeer []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *PenalizePeerRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // SendMessageById holds details about calls to the SendMessageById method. + SendMessageById []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *SendMessageByIdRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // SendMessageByMinBlock holds details about calls to the SendMessageByMinBlock method. + SendMessageByMinBlock []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *SendMessageByMinBlockRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // SendMessageToAll holds details about calls to the SendMessageToAll method. + SendMessageToAll []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *OutboundMessageData + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // SendMessageToRandomPeers holds details about calls to the SendMessageToRandomPeers method. + SendMessageToRandomPeers []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *SendMessageToRandomPeersRequest + // Opts is the opts argument value. + Opts []grpc.CallOption + } + // SetStatus holds details about calls to the SetStatus method. + SetStatus []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // In is the in argument value. + In *StatusData + // Opts is the opts argument value. + Opts []grpc.CallOption + } + } + lockMessages sync.RWMutex + lockPeerCount sync.RWMutex + lockPeerMinBlock sync.RWMutex + lockPeers sync.RWMutex + lockPenalizePeer sync.RWMutex + lockSendMessageById sync.RWMutex + lockSendMessageByMinBlock sync.RWMutex + lockSendMessageToAll sync.RWMutex + lockSendMessageToRandomPeers sync.RWMutex + lockSetStatus sync.RWMutex +} + +// Messages calls MessagesFunc. +func (mock *SentryClientMock) Messages(ctx context.Context, in *MessagesRequest, opts ...grpc.CallOption) (Sentry_MessagesClient, error) { + if mock.MessagesFunc == nil { + panic("SentryClientMock.MessagesFunc: method is nil but SentryClient.Messages was just called") + } + callInfo := struct { + Ctx context.Context + In *MessagesRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockMessages.Lock() + mock.calls.Messages = append(mock.calls.Messages, callInfo) + mock.lockMessages.Unlock() + return mock.MessagesFunc(ctx, in, opts...) +} + +// MessagesCalls gets all the calls that were made to Messages. +// Check the length with: +// len(mockedSentryClient.MessagesCalls()) +func (mock *SentryClientMock) MessagesCalls() []struct { + Ctx context.Context + In *MessagesRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *MessagesRequest + Opts []grpc.CallOption + } + mock.lockMessages.RLock() + calls = mock.calls.Messages + mock.lockMessages.RUnlock() + return calls +} + +// PeerCount calls PeerCountFunc. +func (mock *SentryClientMock) PeerCount(ctx context.Context, in *PeerCountRequest, opts ...grpc.CallOption) (*PeerCountReply, error) { + if mock.PeerCountFunc == nil { + panic("SentryClientMock.PeerCountFunc: method is nil but SentryClient.PeerCount was just called") + } + callInfo := struct { + Ctx context.Context + In *PeerCountRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockPeerCount.Lock() + mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) + mock.lockPeerCount.Unlock() + return mock.PeerCountFunc(ctx, in, opts...) +} + +// PeerCountCalls gets all the calls that were made to PeerCount. +// Check the length with: +// len(mockedSentryClient.PeerCountCalls()) +func (mock *SentryClientMock) PeerCountCalls() []struct { + Ctx context.Context + In *PeerCountRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *PeerCountRequest + Opts []grpc.CallOption + } + mock.lockPeerCount.RLock() + calls = mock.calls.PeerCount + mock.lockPeerCount.RUnlock() + return calls +} + +// PeerMinBlock calls PeerMinBlockFunc. +func (mock *SentryClientMock) PeerMinBlock(ctx context.Context, in *PeerMinBlockRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + if mock.PeerMinBlockFunc == nil { + panic("SentryClientMock.PeerMinBlockFunc: method is nil but SentryClient.PeerMinBlock was just called") + } + callInfo := struct { + Ctx context.Context + In *PeerMinBlockRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockPeerMinBlock.Lock() + mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) + mock.lockPeerMinBlock.Unlock() + return mock.PeerMinBlockFunc(ctx, in, opts...) +} + +// PeerMinBlockCalls gets all the calls that were made to PeerMinBlock. +// Check the length with: +// len(mockedSentryClient.PeerMinBlockCalls()) +func (mock *SentryClientMock) PeerMinBlockCalls() []struct { + Ctx context.Context + In *PeerMinBlockRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *PeerMinBlockRequest + Opts []grpc.CallOption + } + mock.lockPeerMinBlock.RLock() + calls = mock.calls.PeerMinBlock + mock.lockPeerMinBlock.RUnlock() + return calls +} + +// Peers calls PeersFunc. +func (mock *SentryClientMock) Peers(ctx context.Context, in *PeersRequest, opts ...grpc.CallOption) (Sentry_PeersClient, error) { + if mock.PeersFunc == nil { + panic("SentryClientMock.PeersFunc: method is nil but SentryClient.Peers was just called") + } + callInfo := struct { + Ctx context.Context + In *PeersRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockPeers.Lock() + mock.calls.Peers = append(mock.calls.Peers, callInfo) + mock.lockPeers.Unlock() + return mock.PeersFunc(ctx, in, opts...) +} + +// PeersCalls gets all the calls that were made to Peers. +// Check the length with: +// len(mockedSentryClient.PeersCalls()) +func (mock *SentryClientMock) PeersCalls() []struct { + Ctx context.Context + In *PeersRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *PeersRequest + Opts []grpc.CallOption + } + mock.lockPeers.RLock() + calls = mock.calls.Peers + mock.lockPeers.RUnlock() + return calls +} + +// PenalizePeer calls PenalizePeerFunc. +func (mock *SentryClientMock) PenalizePeer(ctx context.Context, in *PenalizePeerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + if mock.PenalizePeerFunc == nil { + panic("SentryClientMock.PenalizePeerFunc: method is nil but SentryClient.PenalizePeer was just called") + } + callInfo := struct { + Ctx context.Context + In *PenalizePeerRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockPenalizePeer.Lock() + mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) + mock.lockPenalizePeer.Unlock() + return mock.PenalizePeerFunc(ctx, in, opts...) +} + +// PenalizePeerCalls gets all the calls that were made to PenalizePeer. +// Check the length with: +// len(mockedSentryClient.PenalizePeerCalls()) +func (mock *SentryClientMock) PenalizePeerCalls() []struct { + Ctx context.Context + In *PenalizePeerRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *PenalizePeerRequest + Opts []grpc.CallOption + } + mock.lockPenalizePeer.RLock() + calls = mock.calls.PenalizePeer + mock.lockPenalizePeer.RUnlock() + return calls +} + +// SendMessageById calls SendMessageByIdFunc. +func (mock *SentryClientMock) SendMessageById(ctx context.Context, in *SendMessageByIdRequest, opts ...grpc.CallOption) (*SentPeers, error) { + if mock.SendMessageByIdFunc == nil { + panic("SentryClientMock.SendMessageByIdFunc: method is nil but SentryClient.SendMessageById was just called") + } + callInfo := struct { + Ctx context.Context + In *SendMessageByIdRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockSendMessageById.Lock() + mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) + mock.lockSendMessageById.Unlock() + return mock.SendMessageByIdFunc(ctx, in, opts...) +} + +// SendMessageByIdCalls gets all the calls that were made to SendMessageById. +// Check the length with: +// len(mockedSentryClient.SendMessageByIdCalls()) +func (mock *SentryClientMock) SendMessageByIdCalls() []struct { + Ctx context.Context + In *SendMessageByIdRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *SendMessageByIdRequest + Opts []grpc.CallOption + } + mock.lockSendMessageById.RLock() + calls = mock.calls.SendMessageById + mock.lockSendMessageById.RUnlock() + return calls +} + +// SendMessageByMinBlock calls SendMessageByMinBlockFunc. +func (mock *SentryClientMock) SendMessageByMinBlock(ctx context.Context, in *SendMessageByMinBlockRequest, opts ...grpc.CallOption) (*SentPeers, error) { + if mock.SendMessageByMinBlockFunc == nil { + panic("SentryClientMock.SendMessageByMinBlockFunc: method is nil but SentryClient.SendMessageByMinBlock was just called") + } + callInfo := struct { + Ctx context.Context + In *SendMessageByMinBlockRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockSendMessageByMinBlock.Lock() + mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) + mock.lockSendMessageByMinBlock.Unlock() + return mock.SendMessageByMinBlockFunc(ctx, in, opts...) +} + +// SendMessageByMinBlockCalls gets all the calls that were made to SendMessageByMinBlock. +// Check the length with: +// len(mockedSentryClient.SendMessageByMinBlockCalls()) +func (mock *SentryClientMock) SendMessageByMinBlockCalls() []struct { + Ctx context.Context + In *SendMessageByMinBlockRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *SendMessageByMinBlockRequest + Opts []grpc.CallOption + } + mock.lockSendMessageByMinBlock.RLock() + calls = mock.calls.SendMessageByMinBlock + mock.lockSendMessageByMinBlock.RUnlock() + return calls +} + +// SendMessageToAll calls SendMessageToAllFunc. +func (mock *SentryClientMock) SendMessageToAll(ctx context.Context, in *OutboundMessageData, opts ...grpc.CallOption) (*SentPeers, error) { + if mock.SendMessageToAllFunc == nil { + panic("SentryClientMock.SendMessageToAllFunc: method is nil but SentryClient.SendMessageToAll was just called") + } + callInfo := struct { + Ctx context.Context + In *OutboundMessageData + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockSendMessageToAll.Lock() + mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) + mock.lockSendMessageToAll.Unlock() + return mock.SendMessageToAllFunc(ctx, in, opts...) +} + +// SendMessageToAllCalls gets all the calls that were made to SendMessageToAll. +// Check the length with: +// len(mockedSentryClient.SendMessageToAllCalls()) +func (mock *SentryClientMock) SendMessageToAllCalls() []struct { + Ctx context.Context + In *OutboundMessageData + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *OutboundMessageData + Opts []grpc.CallOption + } + mock.lockSendMessageToAll.RLock() + calls = mock.calls.SendMessageToAll + mock.lockSendMessageToAll.RUnlock() + return calls +} + +// SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. +func (mock *SentryClientMock) SendMessageToRandomPeers(ctx context.Context, in *SendMessageToRandomPeersRequest, opts ...grpc.CallOption) (*SentPeers, error) { + if mock.SendMessageToRandomPeersFunc == nil { + panic("SentryClientMock.SendMessageToRandomPeersFunc: method is nil but SentryClient.SendMessageToRandomPeers was just called") + } + callInfo := struct { + Ctx context.Context + In *SendMessageToRandomPeersRequest + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockSendMessageToRandomPeers.Lock() + mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) + mock.lockSendMessageToRandomPeers.Unlock() + return mock.SendMessageToRandomPeersFunc(ctx, in, opts...) +} + +// SendMessageToRandomPeersCalls gets all the calls that were made to SendMessageToRandomPeers. +// Check the length with: +// len(mockedSentryClient.SendMessageToRandomPeersCalls()) +func (mock *SentryClientMock) SendMessageToRandomPeersCalls() []struct { + Ctx context.Context + In *SendMessageToRandomPeersRequest + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *SendMessageToRandomPeersRequest + Opts []grpc.CallOption + } + mock.lockSendMessageToRandomPeers.RLock() + calls = mock.calls.SendMessageToRandomPeers + mock.lockSendMessageToRandomPeers.RUnlock() + return calls +} + +// SetStatus calls SetStatusFunc. +func (mock *SentryClientMock) SetStatus(ctx context.Context, in *StatusData, opts ...grpc.CallOption) (*SetStatusReply, error) { + if mock.SetStatusFunc == nil { + panic("SentryClientMock.SetStatusFunc: method is nil but SentryClient.SetStatus was just called") + } + callInfo := struct { + Ctx context.Context + In *StatusData + Opts []grpc.CallOption + }{ + Ctx: ctx, + In: in, + Opts: opts, + } + mock.lockSetStatus.Lock() + mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) + mock.lockSetStatus.Unlock() + return mock.SetStatusFunc(ctx, in, opts...) +} + +// SetStatusCalls gets all the calls that were made to SetStatus. +// Check the length with: +// len(mockedSentryClient.SetStatusCalls()) +func (mock *SentryClientMock) SetStatusCalls() []struct { + Ctx context.Context + In *StatusData + Opts []grpc.CallOption +} { + var calls []struct { + Ctx context.Context + In *StatusData + Opts []grpc.CallOption + } + mock.lockSetStatus.RLock() + calls = mock.calls.SetStatus + mock.lockSetStatus.RUnlock() + return calls +} diff --git a/gointerfaces/test_util.go b/gointerfaces/test_util.go new file mode 100644 index 00000000000..64df6f9ee75 --- /dev/null +++ b/gointerfaces/test_util.go @@ -0,0 +1,3 @@ +package gointerfaces + +//go:generate moq -out ./sentry/mocks.go ./sentry SentryServer SentryClient diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 00000000000..89cf35f4b10 --- /dev/null +++ b/log/logger.go @@ -0,0 +1,36 @@ +package log + +import ( + "testing" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest" +) + +func New() *zap.SugaredLogger { + cfg := zap.NewProductionConfig() + cfg.Encoding = "console" + cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder + cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + ll, err := cfg.Build() + if err != nil { + panic(err) + } + return ll.Sugar() +} + +func NewTest(tb testing.TB) *zap.SugaredLogger { + return zaptest.NewLogger(tb, + zaptest.Level(zap.InfoLevel), + zaptest.WrapOptions(zap.AddCaller()), + zaptest.WrapOptions(zap.AddStacktrace(zap.ErrorLevel)), + ).Sugar() +} + +//zap.NewProductionConfig() +//logger := zap.New(zapcore.NewCore( +// zapcore.NewConsoleEncoder(encoderCfg), +// zapcore.Lock(os.Stdout), +// zap.DebugLevel, +//), zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel)).Sugar() diff --git a/tools.go b/tools.go new file mode 100644 index 00000000000..44a1d1706b9 --- /dev/null +++ b/tools.go @@ -0,0 +1,22 @@ +// +build tools + +package tools + +// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module +// +// This module is just a hack for 'go mod tidy' command +// +// Problem is - 'go mod tidy' removes from go.mod file lines if you don't use them in source code +// +// But code generators we use as binaries - not by including them into source code +// To provide reproducible builds - go.mod file must be source of truth about code generators version +// 'go mod tidy' will not remove binary deps from go.mod file if you add them here +// +// use `make devtools` - does install all binary deps of right version + +// build tag 'trick_go_mod_tidy' - is used to hide warnings of IDEA (because we can't import `main` packages in go) + +import ( + _ "github.com/matryer/moq" + _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" +) diff --git a/txpool/fetch.go b/txpool/fetch.go index ed9bd87d8a3..56156a811b6 100644 --- a/txpool/fetch.go +++ b/txpool/fetch.go @@ -21,13 +21,13 @@ import ( "errors" "fmt" "io" - "log" "sync" "time" "github.com/holiman/uint256" "github.com/ledgerwatch/erigon-lib/gointerfaces" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" + "go.uber.org/zap" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -43,7 +43,19 @@ type Fetch struct { statusData *sentry.StatusData // Status data used for "handshaking" with sentries pool Pool // Transaction pool implementation wg *sync.WaitGroup // Waitgroup used for synchronisation in the tests (nil when not in tests) - logger *log.Logger + logger *zap.SugaredLogger +} + +type Timings struct { + propagateAllNewTxsEvery time.Duration + syncToNewPeersEvery time.Duration + broadcastLocalTransactionsEvery time.Duration +} + +var DefaultTimings = Timings{ + propagateAllNewTxsEvery: 5 * time.Second, + broadcastLocalTransactionsEvery: 2 * time.Minute, + syncToNewPeersEvery: 2 * time.Minute, } // NewFetch creates a new fetch object that will work with given sentry clients. Since the @@ -55,6 +67,7 @@ func NewFetch(ctx context.Context, networkId uint64, forks []uint64, pool Pool, + logger *zap.SugaredLogger, ) *Fetch { statusData := &sentry.StatusData{ NetworkId: networkId, @@ -71,7 +84,7 @@ func NewFetch(ctx context.Context, sentryClients: sentryClients, statusData: statusData, pool: pool, - logger: log.Default(), + logger: logger.Named("txpool.fetch"), } } @@ -79,10 +92,6 @@ func (f *Fetch) SetWaitGroup(wg *sync.WaitGroup) { f.wg = wg } -func (f *Fetch) SetLogger(logger *log.Logger) { - f.logger = logger -} - // Start initialises connection to the sentry func (f *Fetch) Start() { for i := range f.sentryClients { @@ -96,6 +105,7 @@ func (f *Fetch) Start() { } func (f *Fetch) receiveMessageLoop(sentryClient sentry.SentryClient) { + logger := f.logger.Named("receiveMessageLoop") for { select { case <-f.ctx.Done(): @@ -108,7 +118,7 @@ func (f *Fetch) receiveMessageLoop(sentryClient sentry.SentryClient) { return } // Report error and wait more - f.logger.Printf("[receiveMessageLoop] sentry not ready yet: %v\n", err) + logger.Warnf("sentry not ready yet: %s", err) time.Sleep(time.Second) continue } @@ -137,7 +147,7 @@ func (f *Fetch) receiveMessageLoop(sentryClient sentry.SentryClient) { if errors.Is(err, io.EOF) { return } - f.logger.Printf("[receiveMessageLoop] Messages: %v\n", err) + logger.Warnf("messages: %s", err) return } @@ -155,14 +165,14 @@ func (f *Fetch) receiveMessageLoop(sentryClient sentry.SentryClient) { if errors.Is(err, io.EOF) { return } - f.logger.Printf("[receiveMessageLoop] stream.Recv: %v\n", err) + logger.Warnf("stream.Recv: %s", err) return } if req == nil { return } if err = f.handleInboundMessage(req, sentryClient); err != nil { - f.logger.Printf("[receiveMessageLoop] Handling incoming message: %v\n", err) + logger.Warnf("Handling incoming message: %s", err) } if f.wg != nil { f.wg.Done() @@ -216,6 +226,7 @@ func (f *Fetch) handleInboundMessage(req *sentry.InboundMessage, sentryClient se } func (f *Fetch) receivePeerLoop(sentryClient sentry.SentryClient) { + logger := f.logger.Named("receivePeerLoop") for { select { case <-f.ctx.Done(): @@ -228,7 +239,7 @@ func (f *Fetch) receivePeerLoop(sentryClient sentry.SentryClient) { return } // Report error and wait more - f.logger.Printf("[receivePeerLoop] sentry not ready yet: %v\n", err) + logger.Warnf("sentry not ready yet: %s", err) time.Sleep(time.Second) continue } @@ -248,7 +259,7 @@ func (f *Fetch) receivePeerLoop(sentryClient sentry.SentryClient) { if errors.Is(err, io.EOF) { return } - f.logger.Printf("[receivePeerLoop] Peers: %v\n", err) + logger.Warnf("peers: %s", err) return } @@ -266,17 +277,14 @@ func (f *Fetch) receivePeerLoop(sentryClient sentry.SentryClient) { if errors.Is(err, io.EOF) { return } - f.logger.Printf("[receivePeerLoop] stream.Recv: %v\n", err) + logger.Warnf("stream.Recv: %s", err) return } if req == nil { return } - switch req.Event { - case sentry.PeersReply_Connect: - if err = f.handleNewPeer(req, sentryClient); err != nil { - f.logger.Printf("[receivePeerLoop] Handling new peer: %v\n", err) - } + if err = f.handleNewPeer(req); err != nil { + logger.Warnf("Handling new peer: %s", err) } if f.wg != nil { f.wg.Done() @@ -285,6 +293,14 @@ func (f *Fetch) receivePeerLoop(sentryClient sentry.SentryClient) { } } -func (f *Fetch) handleNewPeer(req *sentry.PeersReply, sentryClient sentry.SentryClient) error { +func (f *Fetch) handleNewPeer(req *sentry.PeersReply) error { + if req == nil { + return nil + } + switch req.Event { + case sentry.PeersReply_Connect: + f.pool.NotifyNewPeer(req.PeerId) + } + return nil } diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 21c1becf2f8..f48c1e5fd72 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -24,18 +24,27 @@ import ( "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" + "github.com/ledgerwatch/erigon-lib/log" + "github.com/stretchr/testify/require" ) func TestFetch(t *testing.T) { + logger := log.NewTest(t) + + ctx, cancelFn := context.WithCancel(context.Background()) + defer cancelFn() + var genesisHash [32]byte var networkId uint64 = 1 forks := []uint64{1, 5, 10} - ctx, cancelFn := context.WithCancel(context.Background()) - defer cancelFn() + mock := NewMockSentry(ctx) sentryClient := direct.NewSentryClientDirect(direct.ETH66, mock) - mockPool := &MockPool{} - fetch := NewFetch(ctx, []sentry.SentryClient{sentryClient}, genesisHash, networkId, forks, mockPool) + pool := &PoolMock{ + IdHashKnownFunc: func(hash []byte) bool { return false }, + } + + fetch := NewFetch(ctx, []sentry.SentryClient{sentryClient}, genesisHash, networkId, forks, pool, logger) var wg sync.WaitGroup fetch.SetWaitGroup(&wg) mock.StreamWg.Add(2) @@ -56,3 +65,16 @@ func TestFetch(t *testing.T) { } wg.Wait() } + +func TestSendTxPropagate(t *testing.T) { + logger := log.NewTest(t) + + ctx, cancelFn := context.WithCancel(context.Background()) + defer cancelFn() + + mock := NewMockSentry(ctx) + send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, mock)}, nil, logger) + // Send one transaction id + send.BroadcastRemotePooledTxs([][32]byte{[32]byte{1}, [32]byte{42}}) + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, mock.sentMessages[0].Id) +} diff --git a/txpool/mocks.go b/txpool/mocks.go new file mode 100644 index 00000000000..bd0b363c835 --- /dev/null +++ b/txpool/mocks.go @@ -0,0 +1,116 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package txpool + +import ( + "sync" +) + +// Ensure, that PoolMock does implement Pool. +// If this is not the case, regenerate this file with moq. +var _ Pool = &PoolMock{} + +// PoolMock is a mock implementation of Pool. +// +// func TestSomethingThatUsesPool(t *testing.T) { +// +// // make and configure a mocked Pool +// mockedPool := &PoolMock{ +// IdHashKnownFunc: func(hash []byte) bool { +// panic("mock out the IdHashKnown method") +// }, +// NotifyNewPeerFunc: func(peerID PeerID) { +// panic("mock out the NotifyNewPeer method") +// }, +// } +// +// // use mockedPool in code that requires Pool +// // and then make assertions. +// +// } +type PoolMock struct { + // IdHashKnownFunc mocks the IdHashKnown method. + IdHashKnownFunc func(hash []byte) bool + + // NotifyNewPeerFunc mocks the NotifyNewPeer method. + NotifyNewPeerFunc func(peerID PeerID) + + // calls tracks calls to the methods. + calls struct { + // IdHashKnown holds details about calls to the IdHashKnown method. + IdHashKnown []struct { + // Hash is the hash argument value. + Hash []byte + } + // NotifyNewPeer holds details about calls to the NotifyNewPeer method. + NotifyNewPeer []struct { + // PeerID is the peerID argument value. + PeerID PeerID + } + } + lockIdHashKnown sync.RWMutex + lockNotifyNewPeer sync.RWMutex +} + +// IdHashKnown calls IdHashKnownFunc. +func (mock *PoolMock) IdHashKnown(hash []byte) bool { + if mock.IdHashKnownFunc == nil { + panic("PoolMock.IdHashKnownFunc: method is nil but Pool.IdHashKnown was just called") + } + callInfo := struct { + Hash []byte + }{ + Hash: hash, + } + mock.lockIdHashKnown.Lock() + mock.calls.IdHashKnown = append(mock.calls.IdHashKnown, callInfo) + mock.lockIdHashKnown.Unlock() + return mock.IdHashKnownFunc(hash) +} + +// IdHashKnownCalls gets all the calls that were made to IdHashKnown. +// Check the length with: +// len(mockedPool.IdHashKnownCalls()) +func (mock *PoolMock) IdHashKnownCalls() []struct { + Hash []byte +} { + var calls []struct { + Hash []byte + } + mock.lockIdHashKnown.RLock() + calls = mock.calls.IdHashKnown + mock.lockIdHashKnown.RUnlock() + return calls +} + +// NotifyNewPeer calls NotifyNewPeerFunc. +func (mock *PoolMock) NotifyNewPeer(peerID PeerID) { + if mock.NotifyNewPeerFunc == nil { + panic("PoolMock.NotifyNewPeerFunc: method is nil but Pool.NotifyNewPeer was just called") + } + callInfo := struct { + PeerID PeerID + }{ + PeerID: peerID, + } + mock.lockNotifyNewPeer.Lock() + mock.calls.NotifyNewPeer = append(mock.calls.NotifyNewPeer, callInfo) + mock.lockNotifyNewPeer.Unlock() + mock.NotifyNewPeerFunc(peerID) +} + +// NotifyNewPeerCalls gets all the calls that were made to NotifyNewPeer. +// Check the length with: +// len(mockedPool.NotifyNewPeerCalls()) +func (mock *PoolMock) NotifyNewPeerCalls() []struct { + PeerID PeerID +} { + var calls []struct { + PeerID PeerID + } + mock.lockNotifyNewPeer.RLock() + calls = mock.calls.NotifyNewPeer + mock.lockNotifyNewPeer.RUnlock() + return calls +} diff --git a/txpool/pool.go b/txpool/pool.go index 286ad387f94..453cc122556 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -16,10 +16,109 @@ package txpool +import ( + "context" + "sync" + "time" + + "github.com/ledgerwatch/erigon-lib/gointerfaces/types" +) + // Pool is interface for the transaction pool // This interface exists for the convinience of testing, and not yet because // there are multiple implementations type Pool interface { - // Check whether transaction with given Id hash is known to the pool + // IdHashKnown check whether transaction with given Id hash is known to the pool IdHashKnown(hash []byte) bool + + NotifyNewPeer(peerID PeerID) +} + +type PoolImpl struct { + recentlyConnectedPeers *recentlyConnectedPeers + lastTxPropagationTimestamp time.Time +} + +func NewPool() *PoolImpl { + return &PoolImpl{ + recentlyConnectedPeers: &recentlyConnectedPeers{}, + } +} + +// Loop - does: +// send pending txs to p2p: +// - new txs +// - all pooled txs to recently connected peers +// - all local pooled txs to random peers periodically +// promote/demote transactions +// reorgs +func (p *PoolImpl) Loop(ctx context.Context, send *Send, timings Timings) { + propagateAllNewTxsEvery := time.NewTicker(timings.propagateAllNewTxsEvery) + defer propagateAllNewTxsEvery.Stop() + + syncToNewPeersEvery := time.NewTicker(timings.syncToNewPeersEvery) + defer syncToNewPeersEvery.Stop() + + broadcastLocalTransactionsEvery := time.NewTicker(timings.broadcastLocalTransactionsEvery) + defer broadcastLocalTransactionsEvery.Stop() + + localTxHashes := make([][32]byte, 128) + remoteTxHashes := make([][32]byte, 128) + + for { + select { + case <-ctx.Done(): + return + case <-propagateAllNewTxsEvery.C: // new txs + last := p.lastTxPropagationTimestamp + p.lastTxPropagationTimestamp = time.Now() + + // first broadcast all local txs to all peers, then non-local to random sqrt(peersAmount) peers + localTxHashes = localTxHashes[:0] + p.FillLocalHashesSince(last, localTxHashes) + send.BroadcastLocalPooledTxs(localTxHashes) + + remoteTxHashes = remoteTxHashes[:0] + p.FillRemoteHashesSince(last, remoteTxHashes) + send.BroadcastRemotePooledTxs(remoteTxHashes) + case <-syncToNewPeersEvery.C: // new peer + newPeers := p.recentlyConnectedPeers.GetAndClean() + if len(newPeers) == 0 { + continue + } + p.FillRemoteHashes(remoteTxHashes[:0]) + send.PropagatePooledTxsToPeersList(newPeers, remoteTxHashes) + case <-broadcastLocalTransactionsEvery.C: // periodically broadcast local txs to random peers + p.FillLocalHashes(localTxHashes[:0]) + send.BroadcastLocalPooledTxs(localTxHashes) + } + } +} + +func (p *PoolImpl) FillLocalHashesSince(since time.Time, to [][32]byte) {} +func (p *PoolImpl) FillRemoteHashesSince(since time.Time, to [][32]byte) {} +func (p *PoolImpl) FillLocalHashes(to [][32]byte) {} +func (p *PoolImpl) FillRemoteHashes(to [][32]byte) {} + +// recentlyConnectedPeers does buffer IDs of recently connected good peers +// then sync of pooled Transaction can happen to all of then at once +// DoS protection and performance saving +// it doesn't track if peer disconnected, it's fine +type recentlyConnectedPeers struct { + lock sync.RWMutex + peers []*types.H512 +} + +func (l *recentlyConnectedPeers) AddPeer(p *types.H512) { + l.lock.Lock() + defer l.lock.Unlock() + l.peers = append(l.peers, p) +} + +func (l *recentlyConnectedPeers) GetAndClean() []*types.H512 { + l.lock.Lock() + defer l.lock.Unlock() + peers := l.peers + l.peers = nil + return peers } diff --git a/txpool/send.go b/txpool/send.go new file mode 100644 index 00000000000..22abbde3b98 --- /dev/null +++ b/txpool/send.go @@ -0,0 +1,271 @@ +/* + Copyright 2021 Erigon contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package txpool + +import ( + "context" + "sync" + + "github.com/ledgerwatch/erigon-lib/direct" + "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" + "github.com/ledgerwatch/erigon-lib/gointerfaces/types" + "go.uber.org/zap" + "google.golang.org/grpc" +) + +// Send - does send concrete P2P messages to Sentry. Same as Fetch but for outbound traffic +// does not initiate any messages by self +type Send struct { + ctx context.Context + sentryClients []sentry.SentryClient // sentry clients that will be used for accessing the network + pool Pool + + logger *zap.SugaredLogger + wg *sync.WaitGroup +} + +func NewSend(ctx context.Context, sentryClients []sentry.SentryClient, pool Pool, logger *zap.SugaredLogger) *Send { + return &Send{ + ctx: ctx, + pool: pool, + sentryClients: sentryClients, + logger: logger.Named("txpool.send"), + } +} + +func (f *Send) SetWaitGroup(wg *sync.WaitGroup) { + f.wg = wg +} + +const ( + // This is the target size for the packs of transactions or announcements. A + // pack can get larger than this if a single transactions exceeds this size. + p2pTxPacketLimit = 100 * 1024 +) + +func (f *Send) notifyTests() { + if f.wg != nil { + f.wg.Done() + } +} + +func (f *Send) BroadcastLocalPooledTxs(txs [][32]byte) { + defer f.notifyTests() + if len(txs) == 0 { + return + } + + initialAmount := len(txs) + avgPeersPerSent65 := 0 + avgPeersPerSent66 := 0 + initialTxs := txs + for len(txs) > 0 { + + pendingLen := p2pTxPacketLimit / 32 + pending := make([][32]byte, 0, pendingLen) + + for i := 0; i < pendingLen && i < len(txs); i++ { + pending = append(pending, txs[i]) + } + txs = txs[len(pending):] + + //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) + var ( + data []byte + err error + ) + if err != nil { + f.logger.Warn(err) + return + } + var req66, req65 *sentry.OutboundMessageData + for _, sentryClient := range f.sentryClients { + //if !sentryClient.Ready() { + // continue + //} + //protocol:=sentryClient.Protocol() + protocol := direct.ETH66 + switch protocol { + case direct.ETH65: + if req65 == nil { + req65 = &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_65, + Data: data, + } + } + + peers, err := sentryClient.SendMessageToAll(f.ctx, req65, &grpc.EmptyCallOption{}) + if err != nil { + f.logger.Warn(err) + } + avgPeersPerSent65 += len(peers.Peers) + + case direct.ETH66: + if req66 == nil { + req66 = &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, + Data: data, + } + } + peers, err := sentryClient.SendMessageToAll(f.ctx, req66, &grpc.EmptyCallOption{}) + if err != nil { + f.logger.Warn(err) + return + } + avgPeersPerSent66 += len(peers.Peers) + } + } + } + if initialAmount == 1 { + f.logger.Infof("local tx %x, propageted to %d peers", initialTxs, avgPeersPerSent65+avgPeersPerSent66) + } else { + f.logger.Infof("%d local txs propagated to %d peers", initialAmount, avgPeersPerSent65+avgPeersPerSent66) + } + return +} + +func (f *Send) BroadcastRemotePooledTxs(txs [][32]byte) { + defer f.notifyTests() + + if len(txs) == 0 { + return + } + + for len(txs) > 0 { + pendingLen := p2pTxPacketLimit / 32 + pending := make([][32]byte, 0, pendingLen) + for i := 0; i < pendingLen && i < len(txs); i++ { + pending = append(pending, txs[i]) + } + txs = txs[len(pending):] + + //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) + var ( + data []byte + err error + ) + if err != nil { + f.logger.Warn(err) + return + } + var req66, req65 *sentry.SendMessageToRandomPeersRequest + for _, sentryClient := range f.sentryClients { + //if !sentryClient.Ready() { + // continue + //} + + //protocol:=sentryClient.Protocol() + protocol := direct.ETH66 + switch protocol { + case direct.ETH65: + if req65 == nil { + req65 = &sentry.SendMessageToRandomPeersRequest{ + MaxPeers: 1024, + Data: &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_65, + Data: data, + }, + } + } + + if _, err = sentryClient.SendMessageToRandomPeers(f.ctx, req65, &grpc.EmptyCallOption{}); err != nil { + f.logger.Warn(err) + } + + case direct.ETH66: + if req66 == nil { + req66 = &sentry.SendMessageToRandomPeersRequest{ + MaxPeers: 1024, + Data: &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, + Data: data, + }, + } + } + if _, err = sentryClient.SendMessageToRandomPeers(f.ctx, req66, &grpc.EmptyCallOption{}); err != nil { + f.logger.Warn(err) + } + } + } + } + return +} + +func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs [][32]byte) { + defer f.notifyTests() + + if len(txs) == 0 { + return + } + + for len(txs) > 0 { + pendingLen := p2pTxPacketLimit / 32 + pending := make([][32]byte, 0, pendingLen) + + for i := 0; i < pendingLen && i < len(txs); i++ { + pending = append(pending, txs[i]) + } + txs = txs[len(pending):] + + //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) + var ( + data []byte + err error + ) + if err != nil { + f.logger.Warn(err) + return + } + for _, sentryClient := range f.sentryClients { + //if !sentryClient.Ready() { + // continue + //} + + for _, peer := range peers { + //protocol:=sentryClient.Protocol() + protocol := direct.ETH66 + switch protocol { + case direct.ETH65: + req65 := &sentry.SendMessageByIdRequest{ + PeerId: peer, + Data: &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_65, + Data: data, + }, + } + + if _, err = sentryClient.SendMessageById(f.ctx, req65, &grpc.EmptyCallOption{}); err != nil { + f.logger.Warn(err) + } + + case direct.ETH66: + req66 := &sentry.SendMessageByIdRequest{ + PeerId: peer, + Data: &sentry.OutboundMessageData{ + Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, + Data: data, + }, + } + if _, err = sentryClient.SendMessageById(f.ctx, req66, &grpc.EmptyCallOption{}); err != nil { + f.logger.Warn(err) + } + } + } + } + } + return +} diff --git a/txpool/test_util.go b/txpool/test_util.go index c1e4ee3e230..c7c96e047de 100644 --- a/txpool/test_util.go +++ b/txpool/test_util.go @@ -25,6 +25,8 @@ import ( "google.golang.org/protobuf/types/known/emptypb" ) +//go:generate moq -out mocks.go . Pool + type MockSentry struct { sentry.UnimplementedSentryServer streams map[sentry.MessageId][]sentry.Sentry_MessagesServer @@ -39,7 +41,7 @@ func NewMockSentry(ctx context.Context) *MockSentry { return &MockSentry{ctx: ctx} } -var PeerId = gointerfaces.ConvertBytesToH512([]byte("12345")) +var PeerId PeerID = gointerfaces.ConvertBytesToH512([]byte("12345")) // Stream returns stream, waiting if necessary func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) { @@ -107,10 +109,3 @@ func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_Peers return nil } } - -type MockPool struct { -} - -func (mp *MockPool) IdHashKnown(hash []byte) bool { - return false -} diff --git a/txpool/types.go b/txpool/types.go index bf59997f556..8f21992b16a 100644 --- a/txpool/types.go +++ b/txpool/types.go @@ -23,10 +23,13 @@ import ( "math/bits" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon-lib/gointerfaces/types" "github.com/ledgerwatch/secp256k1" "golang.org/x/crypto/sha3" ) +type PeerID *types.H512 + // TxContext is object that is required to parse transactions and turn transaction payload into TxSlot objects // usage of TxContext helps avoid extra memory allocations type TxParseContext struct { From c55a9f46bedd5bd4f5829a269c07cf86c8f742bc Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 07:57:49 +0700 Subject: [PATCH 02/10] encode txs --- txpool/fetch.go | 2 +- txpool/fetch_test.go | 23 ++++++++++++---- txpool/packets.go | 4 ++- txpool/pool.go | 12 ++++----- txpool/send.go | 63 +++++++++++++++++++------------------------- txpool/test_util.go | 7 +++++ txpool/types.go | 2 ++ 7 files changed, 64 insertions(+), 49 deletions(-) diff --git a/txpool/fetch.go b/txpool/fetch.go index 56156a811b6..e19cb48e1ce 100644 --- a/txpool/fetch.go +++ b/txpool/fetch.go @@ -189,7 +189,7 @@ func (f *Fetch) handleInboundMessage(req *sentry.InboundMessage, sentryClient se return fmt.Errorf("parsing NewPooledTransactionHashes: %w", err) } var hashbuf [32]byte - var unknownHashes []byte + var unknownHashes Hashes var unknownCount int for i := 0; i < hashCount; i++ { _, pos, err = ParseHash(req.Data, pos, hashbuf[:0]) diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index f48c1e5fd72..9e04824ffa6 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -71,10 +71,23 @@ func TestSendTxPropagate(t *testing.T) { ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() + t.Run("small message", func(t *testing.T) { + m := NewMockSentry(ctx) + send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) + require.Equal(t, 68, len(m.sentMessages[0].Data)) - mock := NewMockSentry(ctx) - send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, mock)}, nil, logger) - // Send one transaction id - send.BroadcastRemotePooledTxs([][32]byte{[32]byte{1}, [32]byte{42}}) - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, mock.sentMessages[0].Id) + m.sentMessages = m.sentMessages[:0] + }) + t.Run("3x of max p2p size", func(t *testing.T) { + // method must slice them + m := NewMockSentry(ctx) + send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) + require.Equal(t, 68, len(m.sentMessages[0].Data)) + + m.sentMessages = m.sentMessages[:0] + }) } diff --git a/txpool/packets.go b/txpool/packets.go index ee84a1f131d..0c7c99136ce 100644 --- a/txpool/packets.go +++ b/txpool/packets.go @@ -24,6 +24,8 @@ import ( const ParseHashErrorPrefix = "parse hash payload" +type NewPooledTransactionHashesPacket [][32]byte + // ParseHash extracts the next hash from the RLP encoding (payload) from a given position. // It appends the hash to the given slice, reusing the space if there is enough capacity // The first returned value is the slice where hash is appended to. @@ -79,7 +81,7 @@ func ParseHashesCount(payload []byte, pos int) (int, int, error) { // It appends encoding to the given given slice (encodeBuf), reusing the space // there is there is enough capacity. // The first returned value is the slice where encodinfg -func EncodeHashes(hashes []byte, count int, encodeBuf []byte) ([]byte, error) { +func EncodeHashes(hashes Hashes, count int, encodeBuf []byte) ([]byte, error) { dataLen := count * 33 var beLen int if dataLen >= 56 { diff --git a/txpool/pool.go b/txpool/pool.go index 453cc122556..fa6065f3a49 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -62,8 +62,8 @@ func (p *PoolImpl) Loop(ctx context.Context, send *Send, timings Timings) { broadcastLocalTransactionsEvery := time.NewTicker(timings.broadcastLocalTransactionsEvery) defer broadcastLocalTransactionsEvery.Stop() - localTxHashes := make([][32]byte, 128) - remoteTxHashes := make([][32]byte, 128) + localTxHashes := make([]byte, 0, 128) + remoteTxHashes := make([]byte, 0, 128) for { select { @@ -95,10 +95,10 @@ func (p *PoolImpl) Loop(ctx context.Context, send *Send, timings Timings) { } } -func (p *PoolImpl) FillLocalHashesSince(since time.Time, to [][32]byte) {} -func (p *PoolImpl) FillRemoteHashesSince(since time.Time, to [][32]byte) {} -func (p *PoolImpl) FillLocalHashes(to [][32]byte) {} -func (p *PoolImpl) FillRemoteHashes(to [][32]byte) {} +func (p *PoolImpl) FillLocalHashesSince(since time.Time, to []byte) {} +func (p *PoolImpl) FillRemoteHashesSince(since time.Time, to []byte) {} +func (p *PoolImpl) FillLocalHashes(to []byte) {} +func (p *PoolImpl) FillRemoteHashes(to []byte) {} // recentlyConnectedPeers does buffer IDs of recently connected good peers // then sync of pooled Transaction can happen to all of then at once diff --git a/txpool/send.go b/txpool/send.go index 22abbde3b98..796c244250f 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -63,7 +63,7 @@ func (f *Send) notifyTests() { } } -func (f *Send) BroadcastLocalPooledTxs(txs [][32]byte) { +func (f *Send) BroadcastLocalPooledTxs(txs Hashes) { defer f.notifyTests() if len(txs) == 0 { return @@ -74,20 +74,16 @@ func (f *Send) BroadcastLocalPooledTxs(txs [][32]byte) { avgPeersPerSent66 := 0 initialTxs := txs for len(txs) > 0 { - - pendingLen := p2pTxPacketLimit / 32 - pending := make([][32]byte, 0, pendingLen) - - for i := 0; i < pendingLen && i < len(txs); i++ { - pending = append(pending, txs[i]) + var pending Hashes + if len(txs) > p2pTxPacketLimit { + pending = txs[:p2pTxPacketLimit] + txs = txs[p2pTxPacketLimit:] + } else { + pending = txs[:] + txs = txs[:0] } - txs = txs[len(pending):] - //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) - var ( - data []byte - err error - ) + data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { f.logger.Warn(err) return @@ -138,7 +134,7 @@ func (f *Send) BroadcastLocalPooledTxs(txs [][32]byte) { return } -func (f *Send) BroadcastRemotePooledTxs(txs [][32]byte) { +func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { defer f.notifyTests() if len(txs) == 0 { @@ -146,18 +142,16 @@ func (f *Send) BroadcastRemotePooledTxs(txs [][32]byte) { } for len(txs) > 0 { - pendingLen := p2pTxPacketLimit / 32 - pending := make([][32]byte, 0, pendingLen) - for i := 0; i < pendingLen && i < len(txs); i++ { - pending = append(pending, txs[i]) + var pending Hashes + if len(txs) > p2pTxPacketLimit { + pending = txs[:p2pTxPacketLimit] + txs = txs[p2pTxPacketLimit:] + } else { + pending = txs[:] + txs = txs[:0] } - txs = txs[len(pending):] - //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) - var ( - data []byte - err error - ) + data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { f.logger.Warn(err) return @@ -205,7 +199,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs [][32]byte) { return } -func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs [][32]byte) { +func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs []byte) { defer f.notifyTests() if len(txs) == 0 { @@ -213,19 +207,16 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs [][32]byte } for len(txs) > 0 { - pendingLen := p2pTxPacketLimit / 32 - pending := make([][32]byte, 0, pendingLen) - - for i := 0; i < pendingLen && i < len(txs); i++ { - pending = append(pending, txs[i]) + var pending Hashes + if len(txs) > p2pTxPacketLimit { + pending = txs[:p2pTxPacketLimit] + txs = txs[p2pTxPacketLimit:] + } else { + pending = txs[:] + txs = txs[:0] } - txs = txs[len(pending):] - //data, err := rlp.EncodeToBytes(eth.NewPooledTransactionHashesPacket(pending)) - var ( - data []byte - err error - ) + data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { f.logger.Warn(err) return diff --git a/txpool/test_util.go b/txpool/test_util.go index c7c96e047de..c4911ea0e44 100644 --- a/txpool/test_util.go +++ b/txpool/test_util.go @@ -109,3 +109,10 @@ func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_Peers return nil } } + +func toHashes(h ...[32]byte) (out Hashes) { + for i := range h { + out = append(out, h[i][:]...) + } + return out +} diff --git a/txpool/types.go b/txpool/types.go index 8f21992b16a..6c1f2626a00 100644 --- a/txpool/types.go +++ b/txpool/types.go @@ -30,6 +30,8 @@ import ( type PeerID *types.H512 +type Hashes []byte // flatten list of 32-byte hashes + // TxContext is object that is required to parse transactions and turn transaction payload into TxSlot objects // usage of TxContext helps avoid extra memory allocations type TxParseContext struct { From ea50561aa56ded0e1e499b98a7af791d4dc5e173 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 07:59:41 +0700 Subject: [PATCH 03/10] encode txs --- txpool/fetch_test.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 9e04824ffa6..2fa684170eb 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -19,6 +19,7 @@ package txpool import ( "context" "encoding/hex" + "fmt" "sync" "testing" @@ -77,17 +78,20 @@ func TestSendTxPropagate(t *testing.T) { send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) require.Equal(t, 68, len(m.sentMessages[0].Data)) - - m.sentMessages = m.sentMessages[:0] }) - t.Run("3x of max p2p size", func(t *testing.T) { - // method must slice them + t.Run("slice large messages", func(t *testing.T) { m := NewMockSentry(ctx) send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) - send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) - require.Equal(t, 68, len(m.sentMessages[0].Data)) - - m.sentMessages = m.sentMessages[:0] + list := make(Hashes, p2pTxPacketLimit*3) + for i := 0; i < len(list); i += 32 { + b := []byte(fmt.Sprintf("%x", i)) + copy(list[i:i+32], b) + } + send.BroadcastRemotePooledTxs(list) + require.Equal(t, 3, len(m.sentMessages)) + for i := 0; i < 3; i++ { + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[i].Id) + require.Less(t, 0, len(m.sentMessages[i].Data)) + } }) } From 1010156065a4ede2cd2e32bb0c91acfb3031f2a1 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 09:06:17 +0700 Subject: [PATCH 04/10] encode txs --- gointerfaces/sentry/mocks.go | 204 ++++++++++++++++++++++++----------- gointerfaces/test_util.go | 2 +- txpool/fetch_test.go | 53 ++++++--- txpool/mocks.go | 15 +-- txpool/pool.go | 10 +- txpool/send.go | 12 +-- txpool/test_util.go | 30 +----- 7 files changed, 204 insertions(+), 122 deletions(-) diff --git a/gointerfaces/sentry/mocks.go b/gointerfaces/sentry/mocks.go index c67921e49e7..9edb63f53e7 100644 --- a/gointerfaces/sentry/mocks.go +++ b/gointerfaces/sentry/mocks.go @@ -184,9 +184,6 @@ type SentryServerMock struct { // Messages calls MessagesFunc. func (mock *SentryServerMock) Messages(messagesRequest *MessagesRequest, sentry_MessagesServer Sentry_MessagesServer) error { - if mock.MessagesFunc == nil { - panic("SentryServerMock.MessagesFunc: method is nil but SentryServer.Messages was just called") - } callInfo := struct { MessagesRequest *MessagesRequest Sentry_MessagesServer Sentry_MessagesServer @@ -197,6 +194,12 @@ func (mock *SentryServerMock) Messages(messagesRequest *MessagesRequest, sentry_ mock.lockMessages.Lock() mock.calls.Messages = append(mock.calls.Messages, callInfo) mock.lockMessages.Unlock() + if mock.MessagesFunc == nil { + var ( + errOut error + ) + return errOut + } return mock.MessagesFunc(messagesRequest, sentry_MessagesServer) } @@ -219,9 +222,6 @@ func (mock *SentryServerMock) MessagesCalls() []struct { // PeerCount calls PeerCountFunc. func (mock *SentryServerMock) PeerCount(contextMoqParam context.Context, peerCountRequest *PeerCountRequest) (*PeerCountReply, error) { - if mock.PeerCountFunc == nil { - panic("SentryServerMock.PeerCountFunc: method is nil but SentryServer.PeerCount was just called") - } callInfo := struct { ContextMoqParam context.Context PeerCountRequest *PeerCountRequest @@ -232,6 +232,13 @@ func (mock *SentryServerMock) PeerCount(contextMoqParam context.Context, peerCou mock.lockPeerCount.Lock() mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) mock.lockPeerCount.Unlock() + if mock.PeerCountFunc == nil { + var ( + peerCountReplyOut *PeerCountReply + errOut error + ) + return peerCountReplyOut, errOut + } return mock.PeerCountFunc(contextMoqParam, peerCountRequest) } @@ -254,9 +261,6 @@ func (mock *SentryServerMock) PeerCountCalls() []struct { // PeerMinBlock calls PeerMinBlockFunc. func (mock *SentryServerMock) PeerMinBlock(contextMoqParam context.Context, peerMinBlockRequest *PeerMinBlockRequest) (*emptypb.Empty, error) { - if mock.PeerMinBlockFunc == nil { - panic("SentryServerMock.PeerMinBlockFunc: method is nil but SentryServer.PeerMinBlock was just called") - } callInfo := struct { ContextMoqParam context.Context PeerMinBlockRequest *PeerMinBlockRequest @@ -267,6 +271,13 @@ func (mock *SentryServerMock) PeerMinBlock(contextMoqParam context.Context, peer mock.lockPeerMinBlock.Lock() mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) mock.lockPeerMinBlock.Unlock() + if mock.PeerMinBlockFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PeerMinBlockFunc(contextMoqParam, peerMinBlockRequest) } @@ -289,9 +300,6 @@ func (mock *SentryServerMock) PeerMinBlockCalls() []struct { // Peers calls PeersFunc. func (mock *SentryServerMock) Peers(peersRequest *PeersRequest, sentry_PeersServer Sentry_PeersServer) error { - if mock.PeersFunc == nil { - panic("SentryServerMock.PeersFunc: method is nil but SentryServer.Peers was just called") - } callInfo := struct { PeersRequest *PeersRequest Sentry_PeersServer Sentry_PeersServer @@ -302,6 +310,12 @@ func (mock *SentryServerMock) Peers(peersRequest *PeersRequest, sentry_PeersServ mock.lockPeers.Lock() mock.calls.Peers = append(mock.calls.Peers, callInfo) mock.lockPeers.Unlock() + if mock.PeersFunc == nil { + var ( + errOut error + ) + return errOut + } return mock.PeersFunc(peersRequest, sentry_PeersServer) } @@ -324,9 +338,6 @@ func (mock *SentryServerMock) PeersCalls() []struct { // PenalizePeer calls PenalizePeerFunc. func (mock *SentryServerMock) PenalizePeer(contextMoqParam context.Context, penalizePeerRequest *PenalizePeerRequest) (*emptypb.Empty, error) { - if mock.PenalizePeerFunc == nil { - panic("SentryServerMock.PenalizePeerFunc: method is nil but SentryServer.PenalizePeer was just called") - } callInfo := struct { ContextMoqParam context.Context PenalizePeerRequest *PenalizePeerRequest @@ -337,6 +348,13 @@ func (mock *SentryServerMock) PenalizePeer(contextMoqParam context.Context, pena mock.lockPenalizePeer.Lock() mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) mock.lockPenalizePeer.Unlock() + if mock.PenalizePeerFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PenalizePeerFunc(contextMoqParam, penalizePeerRequest) } @@ -359,9 +377,6 @@ func (mock *SentryServerMock) PenalizePeerCalls() []struct { // SendMessageById calls SendMessageByIdFunc. func (mock *SentryServerMock) SendMessageById(contextMoqParam context.Context, sendMessageByIdRequest *SendMessageByIdRequest) (*SentPeers, error) { - if mock.SendMessageByIdFunc == nil { - panic("SentryServerMock.SendMessageByIdFunc: method is nil but SentryServer.SendMessageById was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageByIdRequest *SendMessageByIdRequest @@ -372,6 +387,13 @@ func (mock *SentryServerMock) SendMessageById(contextMoqParam context.Context, s mock.lockSendMessageById.Lock() mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) mock.lockSendMessageById.Unlock() + if mock.SendMessageByIdFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByIdFunc(contextMoqParam, sendMessageByIdRequest) } @@ -394,9 +416,6 @@ func (mock *SentryServerMock) SendMessageByIdCalls() []struct { // SendMessageByMinBlock calls SendMessageByMinBlockFunc. func (mock *SentryServerMock) SendMessageByMinBlock(contextMoqParam context.Context, sendMessageByMinBlockRequest *SendMessageByMinBlockRequest) (*SentPeers, error) { - if mock.SendMessageByMinBlockFunc == nil { - panic("SentryServerMock.SendMessageByMinBlockFunc: method is nil but SentryServer.SendMessageByMinBlock was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageByMinBlockRequest *SendMessageByMinBlockRequest @@ -407,6 +426,13 @@ func (mock *SentryServerMock) SendMessageByMinBlock(contextMoqParam context.Cont mock.lockSendMessageByMinBlock.Lock() mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) mock.lockSendMessageByMinBlock.Unlock() + if mock.SendMessageByMinBlockFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByMinBlockFunc(contextMoqParam, sendMessageByMinBlockRequest) } @@ -429,9 +455,6 @@ func (mock *SentryServerMock) SendMessageByMinBlockCalls() []struct { // SendMessageToAll calls SendMessageToAllFunc. func (mock *SentryServerMock) SendMessageToAll(contextMoqParam context.Context, outboundMessageData *OutboundMessageData) (*SentPeers, error) { - if mock.SendMessageToAllFunc == nil { - panic("SentryServerMock.SendMessageToAllFunc: method is nil but SentryServer.SendMessageToAll was just called") - } callInfo := struct { ContextMoqParam context.Context OutboundMessageData *OutboundMessageData @@ -442,6 +465,13 @@ func (mock *SentryServerMock) SendMessageToAll(contextMoqParam context.Context, mock.lockSendMessageToAll.Lock() mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) mock.lockSendMessageToAll.Unlock() + if mock.SendMessageToAllFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToAllFunc(contextMoqParam, outboundMessageData) } @@ -464,9 +494,6 @@ func (mock *SentryServerMock) SendMessageToAllCalls() []struct { // SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. func (mock *SentryServerMock) SendMessageToRandomPeers(contextMoqParam context.Context, sendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest) (*SentPeers, error) { - if mock.SendMessageToRandomPeersFunc == nil { - panic("SentryServerMock.SendMessageToRandomPeersFunc: method is nil but SentryServer.SendMessageToRandomPeers was just called") - } callInfo := struct { ContextMoqParam context.Context SendMessageToRandomPeersRequest *SendMessageToRandomPeersRequest @@ -477,6 +504,13 @@ func (mock *SentryServerMock) SendMessageToRandomPeers(contextMoqParam context.C mock.lockSendMessageToRandomPeers.Lock() mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) mock.lockSendMessageToRandomPeers.Unlock() + if mock.SendMessageToRandomPeersFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToRandomPeersFunc(contextMoqParam, sendMessageToRandomPeersRequest) } @@ -499,9 +533,6 @@ func (mock *SentryServerMock) SendMessageToRandomPeersCalls() []struct { // SetStatus calls SetStatusFunc. func (mock *SentryServerMock) SetStatus(contextMoqParam context.Context, statusData *StatusData) (*SetStatusReply, error) { - if mock.SetStatusFunc == nil { - panic("SentryServerMock.SetStatusFunc: method is nil but SentryServer.SetStatus was just called") - } callInfo := struct { ContextMoqParam context.Context StatusData *StatusData @@ -512,6 +543,13 @@ func (mock *SentryServerMock) SetStatus(contextMoqParam context.Context, statusD mock.lockSetStatus.Lock() mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) mock.lockSetStatus.Unlock() + if mock.SetStatusFunc == nil { + var ( + setStatusReplyOut *SetStatusReply + errOut error + ) + return setStatusReplyOut, errOut + } return mock.SetStatusFunc(contextMoqParam, statusData) } @@ -534,14 +572,14 @@ func (mock *SentryServerMock) SetStatusCalls() []struct { // mustEmbedUnimplementedSentryServer calls mustEmbedUnimplementedSentryServerFunc. func (mock *SentryServerMock) mustEmbedUnimplementedSentryServer() { - if mock.mustEmbedUnimplementedSentryServerFunc == nil { - panic("SentryServerMock.mustEmbedUnimplementedSentryServerFunc: method is nil but SentryServer.mustEmbedUnimplementedSentryServer was just called") - } callInfo := struct { }{} mock.lockmustEmbedUnimplementedSentryServer.Lock() mock.calls.mustEmbedUnimplementedSentryServer = append(mock.calls.mustEmbedUnimplementedSentryServer, callInfo) mock.lockmustEmbedUnimplementedSentryServer.Unlock() + if mock.mustEmbedUnimplementedSentryServerFunc == nil { + return + } mock.mustEmbedUnimplementedSentryServerFunc() } @@ -742,9 +780,6 @@ type SentryClientMock struct { // Messages calls MessagesFunc. func (mock *SentryClientMock) Messages(ctx context.Context, in *MessagesRequest, opts ...grpc.CallOption) (Sentry_MessagesClient, error) { - if mock.MessagesFunc == nil { - panic("SentryClientMock.MessagesFunc: method is nil but SentryClient.Messages was just called") - } callInfo := struct { Ctx context.Context In *MessagesRequest @@ -757,6 +792,13 @@ func (mock *SentryClientMock) Messages(ctx context.Context, in *MessagesRequest, mock.lockMessages.Lock() mock.calls.Messages = append(mock.calls.Messages, callInfo) mock.lockMessages.Unlock() + if mock.MessagesFunc == nil { + var ( + sentry_MessagesClientOut Sentry_MessagesClient + errOut error + ) + return sentry_MessagesClientOut, errOut + } return mock.MessagesFunc(ctx, in, opts...) } @@ -781,9 +823,6 @@ func (mock *SentryClientMock) MessagesCalls() []struct { // PeerCount calls PeerCountFunc. func (mock *SentryClientMock) PeerCount(ctx context.Context, in *PeerCountRequest, opts ...grpc.CallOption) (*PeerCountReply, error) { - if mock.PeerCountFunc == nil { - panic("SentryClientMock.PeerCountFunc: method is nil but SentryClient.PeerCount was just called") - } callInfo := struct { Ctx context.Context In *PeerCountRequest @@ -796,6 +835,13 @@ func (mock *SentryClientMock) PeerCount(ctx context.Context, in *PeerCountReques mock.lockPeerCount.Lock() mock.calls.PeerCount = append(mock.calls.PeerCount, callInfo) mock.lockPeerCount.Unlock() + if mock.PeerCountFunc == nil { + var ( + peerCountReplyOut *PeerCountReply + errOut error + ) + return peerCountReplyOut, errOut + } return mock.PeerCountFunc(ctx, in, opts...) } @@ -820,9 +866,6 @@ func (mock *SentryClientMock) PeerCountCalls() []struct { // PeerMinBlock calls PeerMinBlockFunc. func (mock *SentryClientMock) PeerMinBlock(ctx context.Context, in *PeerMinBlockRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { - if mock.PeerMinBlockFunc == nil { - panic("SentryClientMock.PeerMinBlockFunc: method is nil but SentryClient.PeerMinBlock was just called") - } callInfo := struct { Ctx context.Context In *PeerMinBlockRequest @@ -835,6 +878,13 @@ func (mock *SentryClientMock) PeerMinBlock(ctx context.Context, in *PeerMinBlock mock.lockPeerMinBlock.Lock() mock.calls.PeerMinBlock = append(mock.calls.PeerMinBlock, callInfo) mock.lockPeerMinBlock.Unlock() + if mock.PeerMinBlockFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PeerMinBlockFunc(ctx, in, opts...) } @@ -859,9 +909,6 @@ func (mock *SentryClientMock) PeerMinBlockCalls() []struct { // Peers calls PeersFunc. func (mock *SentryClientMock) Peers(ctx context.Context, in *PeersRequest, opts ...grpc.CallOption) (Sentry_PeersClient, error) { - if mock.PeersFunc == nil { - panic("SentryClientMock.PeersFunc: method is nil but SentryClient.Peers was just called") - } callInfo := struct { Ctx context.Context In *PeersRequest @@ -874,6 +921,13 @@ func (mock *SentryClientMock) Peers(ctx context.Context, in *PeersRequest, opts mock.lockPeers.Lock() mock.calls.Peers = append(mock.calls.Peers, callInfo) mock.lockPeers.Unlock() + if mock.PeersFunc == nil { + var ( + sentry_PeersClientOut Sentry_PeersClient + errOut error + ) + return sentry_PeersClientOut, errOut + } return mock.PeersFunc(ctx, in, opts...) } @@ -898,9 +952,6 @@ func (mock *SentryClientMock) PeersCalls() []struct { // PenalizePeer calls PenalizePeerFunc. func (mock *SentryClientMock) PenalizePeer(ctx context.Context, in *PenalizePeerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { - if mock.PenalizePeerFunc == nil { - panic("SentryClientMock.PenalizePeerFunc: method is nil but SentryClient.PenalizePeer was just called") - } callInfo := struct { Ctx context.Context In *PenalizePeerRequest @@ -913,6 +964,13 @@ func (mock *SentryClientMock) PenalizePeer(ctx context.Context, in *PenalizePeer mock.lockPenalizePeer.Lock() mock.calls.PenalizePeer = append(mock.calls.PenalizePeer, callInfo) mock.lockPenalizePeer.Unlock() + if mock.PenalizePeerFunc == nil { + var ( + emptyOut *emptypb.Empty + errOut error + ) + return emptyOut, errOut + } return mock.PenalizePeerFunc(ctx, in, opts...) } @@ -937,9 +995,6 @@ func (mock *SentryClientMock) PenalizePeerCalls() []struct { // SendMessageById calls SendMessageByIdFunc. func (mock *SentryClientMock) SendMessageById(ctx context.Context, in *SendMessageByIdRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageByIdFunc == nil { - panic("SentryClientMock.SendMessageByIdFunc: method is nil but SentryClient.SendMessageById was just called") - } callInfo := struct { Ctx context.Context In *SendMessageByIdRequest @@ -952,6 +1007,13 @@ func (mock *SentryClientMock) SendMessageById(ctx context.Context, in *SendMessa mock.lockSendMessageById.Lock() mock.calls.SendMessageById = append(mock.calls.SendMessageById, callInfo) mock.lockSendMessageById.Unlock() + if mock.SendMessageByIdFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByIdFunc(ctx, in, opts...) } @@ -976,9 +1038,6 @@ func (mock *SentryClientMock) SendMessageByIdCalls() []struct { // SendMessageByMinBlock calls SendMessageByMinBlockFunc. func (mock *SentryClientMock) SendMessageByMinBlock(ctx context.Context, in *SendMessageByMinBlockRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageByMinBlockFunc == nil { - panic("SentryClientMock.SendMessageByMinBlockFunc: method is nil but SentryClient.SendMessageByMinBlock was just called") - } callInfo := struct { Ctx context.Context In *SendMessageByMinBlockRequest @@ -991,6 +1050,13 @@ func (mock *SentryClientMock) SendMessageByMinBlock(ctx context.Context, in *Sen mock.lockSendMessageByMinBlock.Lock() mock.calls.SendMessageByMinBlock = append(mock.calls.SendMessageByMinBlock, callInfo) mock.lockSendMessageByMinBlock.Unlock() + if mock.SendMessageByMinBlockFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageByMinBlockFunc(ctx, in, opts...) } @@ -1015,9 +1081,6 @@ func (mock *SentryClientMock) SendMessageByMinBlockCalls() []struct { // SendMessageToAll calls SendMessageToAllFunc. func (mock *SentryClientMock) SendMessageToAll(ctx context.Context, in *OutboundMessageData, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageToAllFunc == nil { - panic("SentryClientMock.SendMessageToAllFunc: method is nil but SentryClient.SendMessageToAll was just called") - } callInfo := struct { Ctx context.Context In *OutboundMessageData @@ -1030,6 +1093,13 @@ func (mock *SentryClientMock) SendMessageToAll(ctx context.Context, in *Outbound mock.lockSendMessageToAll.Lock() mock.calls.SendMessageToAll = append(mock.calls.SendMessageToAll, callInfo) mock.lockSendMessageToAll.Unlock() + if mock.SendMessageToAllFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToAllFunc(ctx, in, opts...) } @@ -1054,9 +1124,6 @@ func (mock *SentryClientMock) SendMessageToAllCalls() []struct { // SendMessageToRandomPeers calls SendMessageToRandomPeersFunc. func (mock *SentryClientMock) SendMessageToRandomPeers(ctx context.Context, in *SendMessageToRandomPeersRequest, opts ...grpc.CallOption) (*SentPeers, error) { - if mock.SendMessageToRandomPeersFunc == nil { - panic("SentryClientMock.SendMessageToRandomPeersFunc: method is nil but SentryClient.SendMessageToRandomPeers was just called") - } callInfo := struct { Ctx context.Context In *SendMessageToRandomPeersRequest @@ -1069,6 +1136,13 @@ func (mock *SentryClientMock) SendMessageToRandomPeers(ctx context.Context, in * mock.lockSendMessageToRandomPeers.Lock() mock.calls.SendMessageToRandomPeers = append(mock.calls.SendMessageToRandomPeers, callInfo) mock.lockSendMessageToRandomPeers.Unlock() + if mock.SendMessageToRandomPeersFunc == nil { + var ( + sentPeersOut *SentPeers + errOut error + ) + return sentPeersOut, errOut + } return mock.SendMessageToRandomPeersFunc(ctx, in, opts...) } @@ -1093,9 +1167,6 @@ func (mock *SentryClientMock) SendMessageToRandomPeersCalls() []struct { // SetStatus calls SetStatusFunc. func (mock *SentryClientMock) SetStatus(ctx context.Context, in *StatusData, opts ...grpc.CallOption) (*SetStatusReply, error) { - if mock.SetStatusFunc == nil { - panic("SentryClientMock.SetStatusFunc: method is nil but SentryClient.SetStatus was just called") - } callInfo := struct { Ctx context.Context In *StatusData @@ -1108,6 +1179,13 @@ func (mock *SentryClientMock) SetStatus(ctx context.Context, in *StatusData, opt mock.lockSetStatus.Lock() mock.calls.SetStatus = append(mock.calls.SetStatus, callInfo) mock.lockSetStatus.Unlock() + if mock.SetStatusFunc == nil { + var ( + setStatusReplyOut *SetStatusReply + errOut error + ) + return setStatusReplyOut, errOut + } return mock.SetStatusFunc(ctx, in, opts...) } diff --git a/gointerfaces/test_util.go b/gointerfaces/test_util.go index 64df6f9ee75..500328691a2 100644 --- a/gointerfaces/test_util.go +++ b/gointerfaces/test_util.go @@ -1,3 +1,3 @@ package gointerfaces -//go:generate moq -out ./sentry/mocks.go ./sentry SentryServer SentryClient +//go:generate moq -stub -out ./sentry/mocks.go ./sentry SentryServer SentryClient diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 2fa684170eb..4962ce9186d 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -25,7 +25,9 @@ import ( "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" + "github.com/ledgerwatch/erigon-lib/gointerfaces/types" "github.com/ledgerwatch/erigon-lib/log" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -39,22 +41,20 @@ func TestFetch(t *testing.T) { var networkId uint64 = 1 forks := []uint64{1, 5, 10} - mock := NewMockSentry(ctx) - sentryClient := direct.NewSentryClientDirect(direct.ETH66, mock) - pool := &PoolMock{ - IdHashKnownFunc: func(hash []byte) bool { return false }, - } + m := NewMockSentry(ctx) + sentryClient := direct.NewSentryClientDirect(direct.ETH66, m) + pool := &PoolMock{} fetch := NewFetch(ctx, []sentry.SentryClient{sentryClient}, genesisHash, networkId, forks, pool, logger) var wg sync.WaitGroup fetch.SetWaitGroup(&wg) - mock.StreamWg.Add(2) + m.StreamWg.Add(2) fetch.Start() - mock.StreamWg.Wait() + m.StreamWg.Wait() // Send one transaction id wg.Add(1) data, _ := hex.DecodeString("e1a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328") - errs := mock.Send(&sentry.InboundMessage{ + errs := m.Send(&sentry.InboundMessage{ Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, Data: data, PeerId: PeerId, @@ -65,6 +65,7 @@ func TestFetch(t *testing.T) { } } wg.Wait() + } func TestSendTxPropagate(t *testing.T) { @@ -72,14 +73,18 @@ func TestSendTxPropagate(t *testing.T) { ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() - t.Run("small message", func(t *testing.T) { + t.Run("few remote txs", func(t *testing.T) { m := NewMockSentry(ctx) send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[0].Id) - require.Equal(t, 68, len(m.sentMessages[0].Data)) + + calls := m.SendMessageToRandomPeersCalls() + assert.Equal(t, 1, len(calls)) + first := calls[0].SendMessageToRandomPeersRequest.Data + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + require.Equal(t, 68, len(first.Data)) }) - t.Run("slice large messages", func(t *testing.T) { + t.Run("much remote txs", func(t *testing.T) { m := NewMockSentry(ctx) send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) list := make(Hashes, p2pTxPacketLimit*3) @@ -88,10 +93,28 @@ func TestSendTxPropagate(t *testing.T) { copy(list[i:i+32], b) } send.BroadcastRemotePooledTxs(list) - require.Equal(t, 3, len(m.sentMessages)) + calls := m.SendMessageToRandomPeersCalls() + require.Equal(t, 3, len(calls)) for i := 0; i < 3; i++ { - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, m.sentMessages[i].Id) - require.Less(t, 0, len(m.sentMessages[i].Data)) + call := calls[i].SendMessageToRandomPeersRequest.Data + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, call.Id) + require.Less(t, 0, len(call.Data)) } }) + t.Run("few local txs", func(t *testing.T) { + m := NewMockSentry(ctx) + m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) { + return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil + } + send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send.BroadcastLocalPooledTxs(toHashes([32]byte{1}, [32]byte{42})) + + calls := m.SendMessageToAllCalls() + assert.Equal(t, 1, len(calls)) + first := calls[0].OutboundMessageData + require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + require.Equal(t, 68, len(first.Data)) + t.Fail() + }) + } diff --git a/txpool/mocks.go b/txpool/mocks.go index bd0b363c835..715913dc8c6 100644 --- a/txpool/mocks.go +++ b/txpool/mocks.go @@ -55,9 +55,6 @@ type PoolMock struct { // IdHashKnown calls IdHashKnownFunc. func (mock *PoolMock) IdHashKnown(hash []byte) bool { - if mock.IdHashKnownFunc == nil { - panic("PoolMock.IdHashKnownFunc: method is nil but Pool.IdHashKnown was just called") - } callInfo := struct { Hash []byte }{ @@ -66,6 +63,12 @@ func (mock *PoolMock) IdHashKnown(hash []byte) bool { mock.lockIdHashKnown.Lock() mock.calls.IdHashKnown = append(mock.calls.IdHashKnown, callInfo) mock.lockIdHashKnown.Unlock() + if mock.IdHashKnownFunc == nil { + var ( + bOut bool + ) + return bOut + } return mock.IdHashKnownFunc(hash) } @@ -86,9 +89,6 @@ func (mock *PoolMock) IdHashKnownCalls() []struct { // NotifyNewPeer calls NotifyNewPeerFunc. func (mock *PoolMock) NotifyNewPeer(peerID PeerID) { - if mock.NotifyNewPeerFunc == nil { - panic("PoolMock.NotifyNewPeerFunc: method is nil but Pool.NotifyNewPeer was just called") - } callInfo := struct { PeerID PeerID }{ @@ -97,6 +97,9 @@ func (mock *PoolMock) NotifyNewPeer(peerID PeerID) { mock.lockNotifyNewPeer.Lock() mock.calls.NotifyNewPeer = append(mock.calls.NotifyNewPeer, callInfo) mock.lockNotifyNewPeer.Unlock() + if mock.NotifyNewPeerFunc == nil { + return + } mock.NotifyNewPeerFunc(peerID) } diff --git a/txpool/pool.go b/txpool/pool.go index fa6065f3a49..304a6edec91 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -22,6 +22,7 @@ import ( "time" "github.com/ledgerwatch/erigon-lib/gointerfaces/types" + "go.uber.org/zap" ) // Pool is interface for the transaction pool @@ -37,6 +38,7 @@ type Pool interface { type PoolImpl struct { recentlyConnectedPeers *recentlyConnectedPeers lastTxPropagationTimestamp time.Time + logger *zap.SugaredLogger } func NewPool() *PoolImpl { @@ -76,7 +78,13 @@ func (p *PoolImpl) Loop(ctx context.Context, send *Send, timings Timings) { // first broadcast all local txs to all peers, then non-local to random sqrt(peersAmount) peers localTxHashes = localTxHashes[:0] p.FillLocalHashesSince(last, localTxHashes) - send.BroadcastLocalPooledTxs(localTxHashes) + initialAmount := len(localTxHashes) + sentToPeers := send.BroadcastLocalPooledTxs(localTxHashes) + if initialAmount == 1 { + p.logger.Infof("local tx %x, propagated to %d peers", localTxHashes, sentToPeers) + } else { + p.logger.Infof("%d local txs propagated to %d peers", initialAmount, sentToPeers) + } remoteTxHashes = remoteTxHashes[:0] p.FillRemoteHashesSince(last, remoteTxHashes) diff --git a/txpool/send.go b/txpool/send.go index 796c244250f..f5ff8854782 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -63,16 +63,14 @@ func (f *Send) notifyTests() { } } -func (f *Send) BroadcastLocalPooledTxs(txs Hashes) { +func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { defer f.notifyTests() if len(txs) == 0 { return } - initialAmount := len(txs) avgPeersPerSent65 := 0 avgPeersPerSent66 := 0 - initialTxs := txs for len(txs) > 0 { var pending Hashes if len(txs) > p2pTxPacketLimit { @@ -123,15 +121,11 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) { return } avgPeersPerSent66 += len(peers.Peers) + f.logger.Warnf("yay: %d", avgPeersPerSent66) } } } - if initialAmount == 1 { - f.logger.Infof("local tx %x, propageted to %d peers", initialTxs, avgPeersPerSent65+avgPeersPerSent66) - } else { - f.logger.Infof("%d local txs propagated to %d peers", initialAmount, avgPeersPerSent65+avgPeersPerSent66) - } - return + return avgPeersPerSent65 + avgPeersPerSent66 } func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { diff --git a/txpool/test_util.go b/txpool/test_util.go index c4911ea0e44..3582d1e5e71 100644 --- a/txpool/test_util.go +++ b/txpool/test_util.go @@ -22,28 +22,25 @@ import ( "github.com/ledgerwatch/erigon-lib/gointerfaces" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" - "google.golang.org/protobuf/types/known/emptypb" ) -//go:generate moq -out mocks.go . Pool +//go:generate moq -stub -out mocks.go . Pool type MockSentry struct { - sentry.UnimplementedSentryServer + *sentry.SentryServerMock streams map[sentry.MessageId][]sentry.Sentry_MessagesServer peersStreams []sentry.Sentry_PeersServer StreamWg sync.WaitGroup - sentMessages []*sentry.OutboundMessageData ctx context.Context lock sync.RWMutex } func NewMockSentry(ctx context.Context) *MockSentry { - return &MockSentry{ctx: ctx} + return &MockSentry{ctx: ctx, SentryServerMock: &sentry.SentryServerMock{}} } var PeerId PeerID = gointerfaces.ConvertBytesToH512([]byte("12345")) -// Stream returns stream, waiting if necessary func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) { ms.lock.RLock() defer ms.lock.RUnlock() @@ -55,24 +52,6 @@ func (ms *MockSentry) Send(req *sentry.InboundMessage) (errs []error) { return errs } -func (ms *MockSentry) PenalizePeer(context.Context, *sentry.PenalizePeerRequest) (*emptypb.Empty, error) { - return nil, nil -} -func (ms *MockSentry) SendMessageById(_ context.Context, r *sentry.SendMessageByIdRequest) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r.Data) - return nil, nil -} -func (ms *MockSentry) SendMessageToRandomPeers(_ context.Context, r *sentry.SendMessageToRandomPeersRequest) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r.Data) - return nil, nil -} -func (ms *MockSentry) SendMessageToAll(_ context.Context, r *sentry.OutboundMessageData) (*sentry.SentPeers, error) { - ms.sentMessages = append(ms.sentMessages, r) - return nil, nil -} -func (ms *MockSentry) SentMessage(i int) *sentry.OutboundMessageData { - return ms.sentMessages[i] -} func (ms *MockSentry) SetStatus(context.Context, *sentry.StatusData) (*sentry.SetStatusReply, error) { return &sentry.SetStatusReply{Protocol: sentry.Protocol_ETH66}, nil } @@ -93,9 +72,6 @@ func (ms *MockSentry) Messages(req *sentry.MessagesRequest, stream sentry.Sentry return nil } } -func (ms *MockSentry) PeerCount(_ context.Context, req *sentry.PeerCountRequest) (*sentry.PeerCountReply, error) { - return &sentry.PeerCountReply{Count: 1}, nil -} func (ms *MockSentry) Peers(req *sentry.PeersRequest, stream sentry.Sentry_PeersServer) error { ms.lock.Lock() From 3bf11f8ad3c002ccf929363ccb52a59303c1bcc1 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 09:33:50 +0700 Subject: [PATCH 05/10] encode txs --- log/logger.go | 51 ++++++++++++++++++++++++++++++-------------- txpool/fetch_test.go | 1 + 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/log/logger.go b/log/logger.go index 89cf35f4b10..ca134a91f22 100644 --- a/log/logger.go +++ b/log/logger.go @@ -2,35 +2,54 @@ package log import ( "testing" + "time" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest" ) func New() *zap.SugaredLogger { + ll, err := cfg().Build() + if err != nil { + panic(err) + } + return ll.Sugar() +} + +func cfg() zap.Config { cfg := zap.NewProductionConfig() cfg.Encoding = "console" - cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder + cfg.EncoderConfig.EncodeTime = GethCompatibleTime //zapcore.RFC3339TimeEncoder cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - ll, err := cfg.Build() + return cfg +} +func NewTest(tb testing.TB) *zap.SugaredLogger { + cfg := cfg() + cfg.Level.SetLevel(zapcore.DebugLevel) + ll, err := cfg.Build(zap.AddCaller()) if err != nil { panic(err) } - return ll.Sugar() + logger := ll.Sugar() + tb.Cleanup(func() { + _ = logger.Sync() + }) + return logger } -func NewTest(tb testing.TB) *zap.SugaredLogger { - return zaptest.NewLogger(tb, - zaptest.Level(zap.InfoLevel), - zaptest.WrapOptions(zap.AddCaller()), - zaptest.WrapOptions(zap.AddStacktrace(zap.ErrorLevel)), - ).Sugar() +func GethCompatibleTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + encodeTimeLayout(t, "01-02|15:04:05", enc) } -//zap.NewProductionConfig() -//logger := zap.New(zapcore.NewCore( -// zapcore.NewConsoleEncoder(encoderCfg), -// zapcore.Lock(os.Stdout), -// zap.DebugLevel, -//), zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel)).Sugar() +func encodeTimeLayout(t time.Time, layout string, enc zapcore.PrimitiveArrayEncoder) { + type appendTimeEncoder interface { + AppendTimeLayout(time.Time, string) + } + + if enc, ok := enc.(appendTimeEncoder); ok { + enc.AppendTimeLayout(t, layout) + return + } + + enc.AppendString(t.Format(layout)) +} diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 4962ce9186d..1a7dd8490d2 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -70,6 +70,7 @@ func TestFetch(t *testing.T) { func TestSendTxPropagate(t *testing.T) { logger := log.NewTest(t) + //logger := log.New() ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() From 334058cf5d24954cbb3a16246984dbac4e14a545 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 09:39:37 +0700 Subject: [PATCH 06/10] encode txs --- txpool/fetch.go | 2 +- txpool/fetch_test.go | 2 -- txpool/send.go | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/txpool/fetch.go b/txpool/fetch.go index e19cb48e1ce..f7fd3d43a4d 100644 --- a/txpool/fetch.go +++ b/txpool/fetch.go @@ -84,7 +84,7 @@ func NewFetch(ctx context.Context, sentryClients: sentryClients, statusData: statusData, pool: pool, - logger: logger.Named("txpool.fetch"), + logger: logger.Named("[TxPool.Fetch]"), } } diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 1a7dd8490d2..c10eaf21c93 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -115,7 +115,5 @@ func TestSendTxPropagate(t *testing.T) { first := calls[0].OutboundMessageData require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) require.Equal(t, 68, len(first.Data)) - t.Fail() }) - } diff --git a/txpool/send.go b/txpool/send.go index f5ff8854782..9e8234f3aab 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -43,7 +43,7 @@ func NewSend(ctx context.Context, sentryClients []sentry.SentryClient, pool Pool ctx: ctx, pool: pool, sentryClients: sentryClients, - logger: logger.Named("txpool.send"), + logger: logger.Named("[TxPool.Send]"), } } @@ -121,7 +121,6 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { return } avgPeersPerSent66 += len(peers.Peers) - f.logger.Warnf("yay: %d", avgPeersPerSent66) } } } From 634953b4c02281c653bc6e0bc7dab3b26bcd2ec3 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 09:52:20 +0700 Subject: [PATCH 07/10] encode txs --- txpool/fetch_test.go | 6 +++--- txpool/send.go | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index c10eaf21c93..82cb9aafcba 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -76,7 +76,7 @@ func TestSendTxPropagate(t *testing.T) { defer cancelFn() t.Run("few remote txs", func(t *testing.T) { m := NewMockSentry(ctx) - send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send := NewSend(ctx, []SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) calls := m.SendMessageToRandomPeersCalls() @@ -87,7 +87,7 @@ func TestSendTxPropagate(t *testing.T) { }) t.Run("much remote txs", func(t *testing.T) { m := NewMockSentry(ctx) - send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send := NewSend(ctx, []SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) list := make(Hashes, p2pTxPacketLimit*3) for i := 0; i < len(list); i += 32 { b := []byte(fmt.Sprintf("%x", i)) @@ -107,7 +107,7 @@ func TestSendTxPropagate(t *testing.T) { m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) { return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil } - send := NewSend(ctx, []sentry.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + send := NewSend(ctx, []SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) send.BroadcastLocalPooledTxs(toHashes([32]byte{1}, [32]byte{42})) calls := m.SendMessageToAllCalls() diff --git a/txpool/send.go b/txpool/send.go index 9e8234f3aab..41a0a61ef38 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -27,18 +27,23 @@ import ( "google.golang.org/grpc" ) +type SentryClient interface { + sentry.SentryClient + Protocol() uint +} + // Send - does send concrete P2P messages to Sentry. Same as Fetch but for outbound traffic // does not initiate any messages by self type Send struct { ctx context.Context - sentryClients []sentry.SentryClient // sentry clients that will be used for accessing the network + sentryClients []SentryClient // sentry clients that will be used for accessing the network pool Pool logger *zap.SugaredLogger wg *sync.WaitGroup } -func NewSend(ctx context.Context, sentryClients []sentry.SentryClient, pool Pool, logger *zap.SugaredLogger) *Send { +func NewSend(ctx context.Context, sentryClients []SentryClient, pool Pool, logger *zap.SugaredLogger) *Send { return &Send{ ctx: ctx, pool: pool, @@ -91,9 +96,7 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { //if !sentryClient.Ready() { // continue //} - //protocol:=sentryClient.Protocol() - protocol := direct.ETH66 - switch protocol { + switch sentryClient.Protocol() { case direct.ETH65: if req65 == nil { req65 = &sentry.OutboundMessageData{ @@ -155,9 +158,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { // continue //} - //protocol:=sentryClient.Protocol() - protocol := direct.ETH66 - switch protocol { + switch sentryClient.Protocol() { case direct.ETH65: if req65 == nil { req65 = &sentry.SendMessageToRandomPeersRequest{ @@ -220,9 +221,7 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs []byte) { //} for _, peer := range peers { - //protocol:=sentryClient.Protocol() - protocol := direct.ETH66 - switch protocol { + switch sentryClient.Protocol() { case direct.ETH65: req65 := &sentry.SendMessageByIdRequest{ PeerId: peer, From b06a46e9cffbd1a2c1facccab4d4503162694628 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 10:04:53 +0700 Subject: [PATCH 08/10] encode txs --- txpool/fetch_test.go | 34 ++++++++++++++++++++++++++-------- txpool/pool.go | 7 +++---- txpool/send.go | 3 +-- txpool/test_util.go | 8 ++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index 82cb9aafcba..b1e35d84cc2 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -70,7 +70,6 @@ func TestFetch(t *testing.T) { func TestSendTxPropagate(t *testing.T) { logger := log.NewTest(t) - //logger := log.New() ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() @@ -80,10 +79,10 @@ func TestSendTxPropagate(t *testing.T) { send.BroadcastRemotePooledTxs(toHashes([32]byte{1}, [32]byte{42})) calls := m.SendMessageToRandomPeersCalls() - assert.Equal(t, 1, len(calls)) + require.Equal(t, 1, len(calls)) first := calls[0].SendMessageToRandomPeersRequest.Data - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) - require.Equal(t, 68, len(first.Data)) + assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + assert.Equal(t, 68, len(first.Data)) }) t.Run("much remote txs", func(t *testing.T) { m := NewMockSentry(ctx) @@ -99,7 +98,7 @@ func TestSendTxPropagate(t *testing.T) { for i := 0; i < 3; i++ { call := calls[i].SendMessageToRandomPeersRequest.Data require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, call.Id) - require.Less(t, 0, len(call.Data)) + require.True(t, len(call.Data) > 0) } }) t.Run("few local txs", func(t *testing.T) { @@ -111,9 +110,28 @@ func TestSendTxPropagate(t *testing.T) { send.BroadcastLocalPooledTxs(toHashes([32]byte{1}, [32]byte{42})) calls := m.SendMessageToAllCalls() - assert.Equal(t, 1, len(calls)) + require.Equal(t, 1, len(calls)) first := calls[0].OutboundMessageData - require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) - require.Equal(t, 68, len(first.Data)) + assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id) + assert.Equal(t, 68, len(first.Data)) + }) + t.Run("sync with new peer", func(t *testing.T) { + m := NewMockSentry(ctx) + + m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) { + return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil + } + send := NewSend(ctx, []SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil, logger) + expectPeers := toPeerIDs(1, 2, 42) + send.PropagatePooledTxsToPeersList(expectPeers, toHashes([32]byte{1}, [32]byte{42})) + + calls := m.SendMessageByIdCalls() + require.Equal(t, 3, len(calls)) + for i, call := range calls { + req := call.SendMessageByIdRequest + assert.Equal(t, expectPeers[i], PeerID(req.PeerId)) + assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, req.Data.Id) + assert.True(t, len(req.Data.Data) > 0) + } }) } diff --git a/txpool/pool.go b/txpool/pool.go index 304a6edec91..d512429d0d7 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -21,7 +21,6 @@ import ( "sync" "time" - "github.com/ledgerwatch/erigon-lib/gointerfaces/types" "go.uber.org/zap" ) @@ -114,16 +113,16 @@ func (p *PoolImpl) FillRemoteHashes(to []byte) {} // it doesn't track if peer disconnected, it's fine type recentlyConnectedPeers struct { lock sync.RWMutex - peers []*types.H512 + peers []PeerID } -func (l *recentlyConnectedPeers) AddPeer(p *types.H512) { +func (l *recentlyConnectedPeers) AddPeer(p PeerID) { l.lock.Lock() defer l.lock.Unlock() l.peers = append(l.peers, p) } -func (l *recentlyConnectedPeers) GetAndClean() []*types.H512 { +func (l *recentlyConnectedPeers) GetAndClean() []PeerID { l.lock.Lock() defer l.lock.Unlock() peers := l.peers diff --git a/txpool/send.go b/txpool/send.go index 41a0a61ef38..e721ddf416c 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -22,7 +22,6 @@ import ( "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" - "github.com/ledgerwatch/erigon-lib/gointerfaces/types" "go.uber.org/zap" "google.golang.org/grpc" ) @@ -193,7 +192,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { return } -func (f *Send) PropagatePooledTxsToPeersList(peers []*types.H512, txs []byte) { +func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { defer f.notifyTests() if len(txs) == 0 { diff --git a/txpool/test_util.go b/txpool/test_util.go index 3582d1e5e71..bc2646994d9 100644 --- a/txpool/test_util.go +++ b/txpool/test_util.go @@ -18,6 +18,7 @@ package txpool import ( "context" + "fmt" "sync" "github.com/ledgerwatch/erigon-lib/gointerfaces" @@ -92,3 +93,10 @@ func toHashes(h ...[32]byte) (out Hashes) { } return out } + +func toPeerIDs(h ...int) (out []PeerID) { + for i := range h { + out = append(out, gointerfaces.ConvertBytesToH512([]byte(fmt.Sprintf("%x", h[i])))) + } + return out +} From c5fa9ef1bd6edf0375344081a7473104e1f253a6 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 10:38:01 +0700 Subject: [PATCH 09/10] limit loggerr interface --- log/logger.go | 29 +++++++++++++++++++++++------ txpool/fetch.go | 6 +++--- txpool/fetch_test.go | 1 - txpool/send.go | 25 ++++++++++++------------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/log/logger.go b/log/logger.go index ca134a91f22..1c23565ade4 100644 --- a/log/logger.go +++ b/log/logger.go @@ -8,22 +8,39 @@ import ( "go.uber.org/zap/zapcore" ) -func New() *zap.SugaredLogger { +// Logger - limited *zap.SugaredLogger interface +type Logger interface { + Debugf(template string, args ...interface{}) + Infof(template string, args ...interface{}) + Warnf(template string, args ...interface{}) + Errorf(template string, args ...interface{}) + Named(name string) Logger +} + +type LoggerImpl struct { + *zap.SugaredLogger +} + +func (l *LoggerImpl) Named(name string) Logger { + return &LoggerImpl{l.SugaredLogger.Named(name)} +} + +func New() *LoggerImpl { ll, err := cfg().Build() if err != nil { panic(err) } - return ll.Sugar() + return &LoggerImpl{ll.Sugar()} } func cfg() zap.Config { cfg := zap.NewProductionConfig() cfg.Encoding = "console" - cfg.EncoderConfig.EncodeTime = GethCompatibleTime //zapcore.RFC3339TimeEncoder + cfg.EncoderConfig.EncodeTime = gethCompatibleTime //zapcore.RFC3339TimeEncoder cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder return cfg } -func NewTest(tb testing.TB) *zap.SugaredLogger { +func NewTest(tb testing.TB) *LoggerImpl { cfg := cfg() cfg.Level.SetLevel(zapcore.DebugLevel) ll, err := cfg.Build(zap.AddCaller()) @@ -34,10 +51,10 @@ func NewTest(tb testing.TB) *zap.SugaredLogger { tb.Cleanup(func() { _ = logger.Sync() }) - return logger + return &LoggerImpl{logger} } -func GethCompatibleTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { +func gethCompatibleTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { encodeTimeLayout(t, "01-02|15:04:05", enc) } diff --git a/txpool/fetch.go b/txpool/fetch.go index f7fd3d43a4d..0bae0e9817b 100644 --- a/txpool/fetch.go +++ b/txpool/fetch.go @@ -27,7 +27,7 @@ import ( "github.com/holiman/uint256" "github.com/ledgerwatch/erigon-lib/gointerfaces" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" - "go.uber.org/zap" + "github.com/ledgerwatch/erigon-lib/log" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -43,7 +43,7 @@ type Fetch struct { statusData *sentry.StatusData // Status data used for "handshaking" with sentries pool Pool // Transaction pool implementation wg *sync.WaitGroup // Waitgroup used for synchronisation in the tests (nil when not in tests) - logger *zap.SugaredLogger + logger log.Logger } type Timings struct { @@ -67,7 +67,7 @@ func NewFetch(ctx context.Context, networkId uint64, forks []uint64, pool Pool, - logger *zap.SugaredLogger, + logger log.Logger, ) *Fetch { statusData := &sentry.StatusData{ NetworkId: networkId, diff --git a/txpool/fetch_test.go b/txpool/fetch_test.go index b1e35d84cc2..a363b8a569c 100644 --- a/txpool/fetch_test.go +++ b/txpool/fetch_test.go @@ -33,7 +33,6 @@ import ( func TestFetch(t *testing.T) { logger := log.NewTest(t) - ctx, cancelFn := context.WithCancel(context.Background()) defer cancelFn() diff --git a/txpool/send.go b/txpool/send.go index e721ddf416c..27c35ea0ab3 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -22,7 +22,7 @@ import ( "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" - "go.uber.org/zap" + "github.com/ledgerwatch/erigon-lib/log" "google.golang.org/grpc" ) @@ -38,11 +38,11 @@ type Send struct { sentryClients []SentryClient // sentry clients that will be used for accessing the network pool Pool - logger *zap.SugaredLogger + logger log.Logger wg *sync.WaitGroup } -func NewSend(ctx context.Context, sentryClients []SentryClient, pool Pool, logger *zap.SugaredLogger) *Send { +func NewSend(ctx context.Context, sentryClients []SentryClient, pool Pool, logger log.Logger) *Send { return &Send{ ctx: ctx, pool: pool, @@ -87,7 +87,7 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { - f.logger.Warn(err) + f.logger.Warnf("encode hashes: %s", err) return } var req66, req65 *sentry.OutboundMessageData @@ -106,7 +106,7 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { peers, err := sentryClient.SendMessageToAll(f.ctx, req65, &grpc.EmptyCallOption{}) if err != nil { - f.logger.Warn(err) + f.logger.Warnf("sentry response: %s", err) } avgPeersPerSent65 += len(peers.Peers) @@ -119,8 +119,7 @@ func (f *Send) BroadcastLocalPooledTxs(txs Hashes) (sentToPeers int) { } peers, err := sentryClient.SendMessageToAll(f.ctx, req66, &grpc.EmptyCallOption{}) if err != nil { - f.logger.Warn(err) - return + f.logger.Warnf("sentry response: %s", err) } avgPeersPerSent66 += len(peers.Peers) } @@ -148,7 +147,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { - f.logger.Warn(err) + f.logger.Warnf("encode hashes: %s", err) return } var req66, req65 *sentry.SendMessageToRandomPeersRequest @@ -170,7 +169,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { } if _, err = sentryClient.SendMessageToRandomPeers(f.ctx, req65, &grpc.EmptyCallOption{}); err != nil { - f.logger.Warn(err) + f.logger.Warnf("sentry response: %s", err) } case direct.ETH66: @@ -184,7 +183,7 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { } } if _, err = sentryClient.SendMessageToRandomPeers(f.ctx, req66, &grpc.EmptyCallOption{}); err != nil { - f.logger.Warn(err) + f.logger.Warnf("sentry response: %s", err) } } } @@ -211,7 +210,7 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { data, err := EncodeHashes(pending, len(pending)/32, nil) if err != nil { - f.logger.Warn(err) + f.logger.Warnf("encode hashes: %s", err) return } for _, sentryClient := range f.sentryClients { @@ -231,7 +230,7 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { } if _, err = sentryClient.SendMessageById(f.ctx, req65, &grpc.EmptyCallOption{}); err != nil { - f.logger.Warn(err) + f.logger.Warnf("sentry response: %s", err) } case direct.ETH66: @@ -243,7 +242,7 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { }, } if _, err = sentryClient.SendMessageById(f.ctx, req66, &grpc.EmptyCallOption{}); err != nil { - f.logger.Warn(err) + f.logger.Warnf("sentry response: %s", err) } } } From 7d2a8c7e85641507857ab5d77bf16a01d79e0f67 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 26 Jul 2021 11:57:04 +0700 Subject: [PATCH 10/10] limit loggerr interface --- txpool/send.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/txpool/send.go b/txpool/send.go index 27c35ea0ab3..644dd01a4bb 100644 --- a/txpool/send.go +++ b/txpool/send.go @@ -188,7 +188,6 @@ func (f *Send) BroadcastRemotePooledTxs(txs Hashes) { } } } - return } func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { @@ -248,5 +247,4 @@ func (f *Send) PropagatePooledTxsToPeersList(peers []PeerID, txs []byte) { } } } - return }