diff --git a/go.mod b/go.mod index 38bf675d745..9b2600128e8 100644 --- a/go.mod +++ b/go.mod @@ -66,6 +66,7 @@ require ( require ( github.com/cenkalti/backoff/v5 v5.0.3 // indirect + github.com/ethersphere/bee-old v0.0.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect @@ -205,3 +206,5 @@ require ( ) replace github.com/codahale/hdrhistogram => github.com/HdrHistogram/hdrhistogram-go v0.0.0-20200919145931-8dac23c8dac1 + +replace github.com/ethersphere/bee-old => github.com/ethersphere/bee/v2 v2.8.1 diff --git a/go.sum b/go.sum index 3771e204947..599ea7ff71f 100644 --- a/go.sum +++ b/go.sum @@ -248,6 +248,8 @@ github.com/ethereum/go-ethereum v1.17.3 h1:Ev/sQHH+UdKZHWjuVzhu2pxhi/sXaPZl23Q+Q github.com/ethereum/go-ethereum v1.17.3/go.mod h1:f2EhRwqewIZkGoQekywI2Y2RZAMTSavLNkD9qItFy1A= github.com/ethersphere/batch-archive v0.0.8 h1:Y6ipqJfcjLbOn+2Rn5tMrOvrMH7pzF0YhliC/jsGRUc= github.com/ethersphere/batch-archive v0.0.8/go.mod h1:41BPb192NoK9CYjNB8BAE1J2MtiI/5aq0Wtas5O7A7Q= +github.com/ethersphere/bee/v2 v2.8.1 h1:MI4HP3TvwQp41XJvUSIKtw6FFkBCJ+st8LAQkND/7zc= +github.com/ethersphere/bee/v2 v2.8.1/go.mod h1:qLx+ocZjs7wAT7JzOozSIRmqz5BB472BzC0xtxOFg1g= github.com/ethersphere/go-price-oracle-abi v0.6.9 h1:bseen6he3PZv5GHOm+KD6s4awaFmVSD9LFx+HpB6rCU= github.com/ethersphere/go-price-oracle-abi v0.6.9/go.mod h1:sI/Qj4/zJ23/b1enzwMMv0/hLTpPNVNacEwCWjo6yBk= github.com/ethersphere/go-storage-incentives-abi v0.9.4 h1:mSIWXQXg5OQmH10QvXMV5w0vbSibFMaRlBL37gPLTM0= diff --git a/pkg/pullsync/delivery_test.go b/pkg/pullsync/delivery_test.go new file mode 100644 index 00000000000..4fe9fd2748d --- /dev/null +++ b/pkg/pullsync/delivery_test.go @@ -0,0 +1,347 @@ +package pullsync_test + +import ( + "context" + "fmt" + "net" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + pullsyncold "github.com/ethersphere/bee-old/pkg/pullsync" + "github.com/ethersphere/bee/v2/pkg/addressbook" + "github.com/ethersphere/bee/v2/pkg/crypto" + "github.com/ethersphere/bee/v2/pkg/log" + "github.com/ethersphere/bee/v2/pkg/p2p" + "github.com/ethersphere/bee/v2/pkg/p2p/libp2p" + "github.com/ethersphere/bee/v2/pkg/pullsync" + + "github.com/ethersphere/bee/v2/pkg/soc" + statestoremock "github.com/ethersphere/bee/v2/pkg/statestore/mock" + testingc "github.com/ethersphere/bee/v2/pkg/storage/testing" + "github.com/ethersphere/bee/v2/pkg/storer" + mock "github.com/ethersphere/bee/v2/pkg/storer/mock" + "github.com/ethersphere/bee/v2/pkg/swarm" + "github.com/ethersphere/bee/v2/pkg/topology/lightnode" +) + +func BenchmarkDeliveryLibP2P(b *testing.B) { + for _, n := range []int{25, 120} { + b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) { + testChunks := make([]swarm.Chunk, n) + testResults := make([]*storer.BinC, n) + for i := range n { + testChunks[i] = testingc.GenerateTestRandomChunk() + stampHash, _ := testChunks[i].Stamp().Hash() + testResults[i] = &storer.BinC{ + Address: testChunks[i].Address(), + BatchID: testChunks[i].Stamp().BatchID(), + BinID: uint64(testChunks[i].Address().Bytes()[0]), + StampHash: stampHash, + } + } + + b.Run("Batched", func(b *testing.B) { + logger := log.Noop + serverService, serverAddr := newLibp2pService(b, 1, logger) + clientService, _ := newLibp2pService(b, 1, logger) + + baseReserve := mock.NewReserve() + res1 := &customReserve{ + Reserve: baseReserve, + chunks: testChunks, + results: testResults, + } + + psServer := pullsync.New(serverService, res1, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, logger, uint64(n)) + + if err := serverService.AddProtocol(psServer.Protocol()); err != nil { + b.Fatal(err) + } + + store2 := &customClientReserve{Reserve: mock.NewReserve()} + psClient := pullsync.New(clientService, store2, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, logger, uint64(n)) + + if err := clientService.AddProtocol(psClient.Protocol()); err != nil { + b.Fatal(err) + } + + serverAddrs, err := serverService.Addresses() + if err != nil { + b.Fatal(err) + } + + _, err = clientService.Connect(context.Background(), serverAddrs) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _, count, err := psClient.Sync(ctx, serverAddr, 0, 0) + cancel() + + if err != nil { + b.Fatalf("Sync failed: %v", err) + } + if count != n { + b.Fatalf("Expected %d chunks, got %d", n, count) + } + } + }) + + b.Run("v2.8.1", func(b *testing.B) { + logger := log.Noop + serverService, serverAddr := newLibp2pService(b, 1, logger) + clientService, _ := newLibp2pService(b, 1, logger) + + baseReserve := mock.NewReserve() + res1 := &customReserve{ + Reserve: baseReserve, + chunks: testChunks, + results: testResults, + } + + psServer := pullsyncold.New(serverService, res1, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, logger, uint64(n)) + + if err := serverService.AddProtocol(psServer.Protocol()); err != nil { + b.Fatal(err) + } + + store2 := &customClientReserve{Reserve: mock.NewReserve()} + psClient := pullsyncold.New(clientService, store2, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, logger, uint64(n)) + + if err := clientService.AddProtocol(psClient.Protocol()); err != nil { + b.Fatal(err) + } + + serverAddrs, err := serverService.Addresses() + if err != nil { + b.Fatal(err) + } + + _, err = clientService.Connect(context.Background(), serverAddrs) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _, count, err := psClient.Sync(ctx, serverAddr, 0, 0) + cancel() + + if err != nil { + b.Fatalf("Sync failed: %v", err) + } + if count != n { + b.Fatalf("Expected %d chunks, got %d", n, count) + } + } + }) + }) + } +} + +func newLibp2pService(t testing.TB, networkID uint64, logger log.Logger) (*libp2p.Service, swarm.Address) { + t.Helper() + + swarmKey, err := crypto.GenerateSecp256k1Key() + if err != nil { + t.Fatal(err) + } + + nonce := common.HexToHash("0x1").Bytes() + + overlay, err := crypto.NewOverlayAddress(swarmKey.PublicKey, networkID, nonce) + if err != nil { + t.Fatal(err) + } + + addr := "127.0.0.1:0" + + statestore := statestoremock.NewStateStore() + ab := addressbook.New(statestore) + + libp2pKey, err := crypto.GenerateSecp256r1Key() + if err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + lightNodes := lightnode.NewContainer(overlay) + + opts := libp2p.Options{ + PrivateKey: libp2pKey, + Nonce: nonce, + FullNode: true, + NATAddr: "127.0.0.1:0", // Disable default NAT manager + AllowPrivateCIDRs: true, + } + + s, err := libp2p.New(ctx, crypto.NewDefaultSigner(swarmKey), networkID, overlay, addr, ab, statestore, lightNodes, logger, nil, opts) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + + _ = s.Ready() + + return s, overlay +} + +type customReserve struct { + storer.Reserve + chunks []swarm.Chunk + results []*storer.BinC +} + +func (c *customReserve) ReserveGet(ctx context.Context, addr swarm.Address, batchID []byte, stampHash []byte) (swarm.Chunk, error) { + for _, chunk := range c.chunks { + if chunk.Address().Equal(addr) { + return chunk, nil + } + } + return nil, fmt.Errorf("not found") +} + +func (c *customReserve) SubscribeBin(ctx context.Context, bin uint8, start uint64) (<-chan *storer.BinC, func(), <-chan error) { + out := make(chan *storer.BinC) + errC := make(chan error, 1) + + go func() { + defer close(out) + for _, r := range c.results { + select { + case out <- r: + case <-ctx.Done(): + errC <- ctx.Err() + return + } + } + }() + + return out, func() {}, errC +} + +type customClientReserve struct { + storer.Reserve +} + +func (c *customClientReserve) ReserveHas(addr swarm.Address, batchID []byte, stampHash []byte) (bool, error) { + return false, nil +} + +func (c *customClientReserve) Put(ctx context.Context, chunk swarm.Chunk) error { + return nil +} + +func BenchmarkDeliveryPipe(b *testing.B) { + for _, n := range []int{25, 120} { + b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) { + b.StopTimer() + + testChunks := make([]swarm.Chunk, n) + testResults := make([]*storer.BinC, n) + for i := range n { + testChunks[i] = testingc.GenerateTestRandomChunk() + stampHash, _ := testChunks[i].Stamp().Hash() + testResults[i] = &storer.BinC{ + Address: testChunks[i].Address(), + BatchID: testChunks[i].Stamp().BatchID(), + BinID: uint64(testChunks[i].Address().Bytes()[0]), + StampHash: stampHash, + } + } + + b.Run("Batched", func(b *testing.B) { + for i := 0; i < b.N; i++ { + b.StopTimer() + store1 := mock.NewReserve(mock.WithSubscribeResp(testResults, nil), mock.WithChunks(testChunks...)) + psServer := pullsync.New(nil, store1, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, log.Noop, uint64(n)) + + streamer := &pipeStreamer{serverHandler: psServer.Protocol().StreamSpecs[0].Handler} + + store2 := &customClientReserve{Reserve: mock.NewReserve()} + psClient := pullsync.New(streamer, store2, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, log.Noop, uint64(n)) + + b.StartTimer() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _, count, err := psClient.Sync(ctx, swarm.ZeroAddress, 0, 0) + cancel() + b.StopTimer() + + if err != nil { + b.Fatalf("Sync failed: %v", err) + } + if count != n { + b.Fatalf("Expected %d chunks, got %d", n, count) + } + psClient.Close() + psServer.Close() + } + }) + + b.Run("v2.8.1", func(b *testing.B) { + for i := 0; i < b.N; i++ { + b.StopTimer() + store1 := mock.NewReserve(mock.WithSubscribeResp(testResults, nil), mock.WithChunks(testChunks...)) + psServer := pullsyncold.New(nil, store1, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, log.Noop, uint64(n)) + + streamer := &pipeStreamer{serverHandler: psServer.Protocol().StreamSpecs[0].Handler} + + store2 := &customClientReserve{Reserve: mock.NewReserve()} + psClient := pullsyncold.New(streamer, store2, func(swarm.Chunk) {}, func(*soc.SOC) {}, func(ch swarm.Chunk) (swarm.Chunk, error) { return ch, nil }, log.Noop, uint64(n)) + + b.StartTimer() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _, count, err := psClient.Sync(ctx, swarm.ZeroAddress, 0, 0) + cancel() + b.StopTimer() + + if err != nil { + b.Fatalf("Sync failed: %v", err) + } + if count != n { + b.Fatalf("Expected %d chunks, got %d", n, count) + } + psClient.Close() + psServer.Close() + } + }) + }) + } +} + +type pipeStream struct { + net.Conn + headers p2p.Headers +} + +func (p *pipeStream) Read(b []byte) (n int, err error) { return p.Conn.Read(b) } +func (p *pipeStream) Write(b []byte) (n int, err error) { return p.Conn.Write(b) } +func (p *pipeStream) Headers() p2p.Headers { return p.headers } +func (p *pipeStream) ResponseHeaders() p2p.Headers { return p.headers } +func (p *pipeStream) FullClose() error { return p.Conn.Close() } +func (p *pipeStream) Reset() error { return p.Conn.Close() } + +type pipeStreamer struct { + serverHandler p2p.HandlerFunc +} + +func (s *pipeStreamer) NewStream(ctx context.Context, address swarm.Address, h p2p.Headers, protocol, version, stream string) (p2p.Stream, error) { + c1, c2 := net.Pipe() + + clientStream := &pipeStream{Conn: c1, headers: h} + serverStream := &pipeStream{Conn: c2, headers: h} + + go func() { + _ = s.serverHandler(context.Background(), p2p.Peer{Address: address}, serverStream) + _ = serverStream.FullClose() + }() + + return clientStream, nil +} diff --git a/pkg/pullsync/pb/pullsync.pb.go b/pkg/pullsync/pb/pullsync.pb.go index 6c3bec8c85a..abd8e923ead 100644 --- a/pkg/pullsync/pb/pullsync.pb.go +++ b/pkg/pullsync/pb/pullsync.pb.go @@ -378,6 +378,50 @@ func (m *Delivery) GetStamp() []byte { return nil } +type DeliveryBatch struct { + Deliveries []*Delivery `protobuf:"bytes,1,rep,name=Deliveries,proto3" json:"Deliveries,omitempty"` +} + +func (m *DeliveryBatch) Reset() { *m = DeliveryBatch{} } +func (m *DeliveryBatch) String() string { return proto.CompactTextString(m) } +func (*DeliveryBatch) ProtoMessage() {} +func (*DeliveryBatch) Descriptor() ([]byte, []int) { + return fileDescriptor_d1dee042cf9c065c, []int{7} +} +func (m *DeliveryBatch) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *DeliveryBatch) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_DeliveryBatch.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *DeliveryBatch) XXX_Merge(src proto.Message) { + xxx_messageInfo_DeliveryBatch.Merge(m, src) +} +func (m *DeliveryBatch) XXX_Size() int { + return m.Size() +} +func (m *DeliveryBatch) XXX_DiscardUnknown() { + xxx_messageInfo_DeliveryBatch.DiscardUnknown(m) +} + +var xxx_messageInfo_DeliveryBatch proto.InternalMessageInfo + +func (m *DeliveryBatch) GetDeliveries() []*Delivery { + if m != nil { + return m.Deliveries + } + return nil +} + func init() { proto.RegisterType((*Syn)(nil), "pullsync.Syn") proto.RegisterType((*Ack)(nil), "pullsync.Ack") @@ -386,32 +430,35 @@ func init() { proto.RegisterType((*Offer)(nil), "pullsync.Offer") proto.RegisterType((*Want)(nil), "pullsync.Want") proto.RegisterType((*Delivery)(nil), "pullsync.Delivery") + proto.RegisterType((*DeliveryBatch)(nil), "pullsync.DeliveryBatch") } func init() { proto.RegisterFile("pullsync.proto", fileDescriptor_d1dee042cf9c065c) } var fileDescriptor_d1dee042cf9c065c = []byte{ - // 319 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0xbb, 0x4e, 0xc3, 0x30, - 0x14, 0x86, 0xeb, 0x3a, 0x29, 0xe5, 0x50, 0x01, 0xb2, 0x18, 0x32, 0x54, 0x51, 0x64, 0x21, 0x91, - 0x85, 0x0e, 0x20, 0x1e, 0xa0, 0x69, 0x11, 0x97, 0x01, 0x24, 0x17, 0x81, 0x60, 0x73, 0xd3, 0x54, - 0xa9, 0x68, 0x63, 0xcb, 0x76, 0x91, 0xfa, 0x16, 0x3c, 0x16, 0x63, 0x47, 0x46, 0xd4, 0xbc, 0x08, - 0x8a, 0x1b, 0xd3, 0x8d, 0xed, 0x7c, 0xe7, 0xf6, 0xfb, 0x3f, 0x86, 0x43, 0xb9, 0x9c, 0xcf, 0xf5, - 0xaa, 0x48, 0x7b, 0x52, 0x09, 0x23, 0x48, 0xdb, 0x31, 0xf5, 0x01, 0x8f, 0x56, 0x05, 0xbd, 0x02, - 0xdc, 0x4f, 0xdf, 0x49, 0x00, 0x7b, 0x83, 0xa5, 0xd2, 0x42, 0xe9, 0x00, 0x45, 0x38, 0xf6, 0x98, - 0x43, 0x72, 0x02, 0xfe, 0xb5, 0x14, 0x69, 0x1e, 0x34, 0x23, 0x14, 0x7b, 0x6c, 0x0b, 0xf4, 0x1c, - 0xf0, 0x4d, 0x66, 0xc8, 0x31, 0xe0, 0x64, 0x56, 0x04, 0x28, 0x42, 0xb1, 0xcf, 0xaa, 0xb0, 0x6a, - 0x1f, 0x19, 0xae, 0x8c, 0x6b, 0xb7, 0x40, 0x5f, 0xc1, 0x1f, 0xe4, 0xcb, 0xc2, 0xea, 0xf4, 0x27, - 0x13, 0x95, 0x69, 0x6d, 0x87, 0x3a, 0xcc, 0x61, 0x55, 0x49, 0xb8, 0x49, 0xf3, 0xbb, 0xa1, 0x1d, - 0xed, 0x30, 0x87, 0xa4, 0x0b, 0xfb, 0x23, 0xc3, 0x17, 0xf2, 0x96, 0xeb, 0x3c, 0xc0, 0xb6, 0xb6, - 0x4b, 0xd0, 0x7b, 0xf0, 0x1f, 0xa7, 0xd3, 0x4c, 0x55, 0x0b, 0x9e, 0x84, 0x5c, 0x08, 0x6d, 0xec, - 0x6a, 0x8f, 0x39, 0x24, 0x67, 0xd0, 0xb2, 0xea, 0x3a, 0x68, 0x46, 0x38, 0x3e, 0xb8, 0x38, 0xea, - 0xfd, 0x5d, 0xc5, 0xe6, 0x59, 0x5d, 0xa6, 0xa7, 0xe0, 0xbd, 0xf0, 0xc2, 0x54, 0x8a, 0xc9, 0xcc, - 0x3c, 0x67, 0xa9, 0x11, 0xaa, 0x7e, 0xe7, 0x2e, 0x41, 0x1f, 0xa0, 0x3d, 0xcc, 0xe6, 0xb3, 0x8f, - 0x4c, 0xad, 0xfe, 0xf1, 0x43, 0xc0, 0x1b, 0x72, 0xc3, 0x6b, 0x33, 0x36, 0xae, 0x8f, 0xb3, 0x90, - 0xb5, 0x8b, 0x2d, 0x24, 0xdd, 0xaf, 0x4d, 0x88, 0xd6, 0x9b, 0x10, 0xfd, 0x6c, 0x42, 0xf4, 0x59, - 0x86, 0x8d, 0x75, 0x19, 0x36, 0xbe, 0xcb, 0xb0, 0xf1, 0xd6, 0x94, 0xe3, 0x71, 0xcb, 0x7e, 0xdc, - 0xe5, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd3, 0x02, 0x2f, 0x9a, 0xca, 0x01, 0x00, 0x00, + // 341 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0xbd, 0x4e, 0xc3, 0x30, + 0x14, 0x85, 0x9b, 0x26, 0x29, 0xe5, 0xb6, 0xfc, 0xc8, 0x62, 0xc8, 0x50, 0x45, 0x91, 0x85, 0x44, + 0x16, 0x3a, 0x14, 0xf1, 0x00, 0x4d, 0x8b, 0xf8, 0x19, 0x40, 0x72, 0x11, 0x08, 0x36, 0x37, 0x75, + 0x95, 0x88, 0x36, 0xb6, 0x6c, 0x17, 0xa9, 0x6f, 0xc1, 0x63, 0x31, 0x76, 0x64, 0x44, 0xed, 0x8b, + 0xa0, 0xb8, 0x31, 0x65, 0x62, 0xf3, 0x77, 0xee, 0xf1, 0xbd, 0x3e, 0xbe, 0x70, 0x28, 0x16, 0xb3, + 0x99, 0x5a, 0x16, 0x69, 0x57, 0x48, 0xae, 0x39, 0x6a, 0x5a, 0xc6, 0x3e, 0xb8, 0xa3, 0x65, 0x81, + 0x2f, 0xc1, 0xed, 0xa7, 0x6f, 0x28, 0x80, 0xbd, 0xc1, 0x42, 0x2a, 0x2e, 0x55, 0xe0, 0x44, 0x6e, + 0xec, 0x11, 0x8b, 0xe8, 0x04, 0xfc, 0x2b, 0xc1, 0xd3, 0x2c, 0xa8, 0x47, 0x4e, 0xec, 0x91, 0x2d, + 0xe0, 0x73, 0x70, 0xaf, 0x99, 0x46, 0xc7, 0xe0, 0x26, 0x79, 0x11, 0x38, 0x91, 0x13, 0xfb, 0xa4, + 0x3c, 0x96, 0xf6, 0x91, 0xa6, 0x52, 0x5b, 0xbb, 0x01, 0xfc, 0x02, 0xfe, 0x20, 0x5b, 0x14, 0x66, + 0x4e, 0x7f, 0x32, 0x91, 0x4c, 0x29, 0x73, 0xa9, 0x4d, 0x2c, 0x96, 0x95, 0x84, 0xea, 0x34, 0xbb, + 0x1d, 0x9a, 0xab, 0x6d, 0x62, 0x11, 0x75, 0x60, 0x7f, 0xa4, 0xe9, 0x5c, 0xdc, 0x50, 0x95, 0x05, + 0xae, 0xa9, 0xed, 0x04, 0x7c, 0x07, 0xfe, 0xc3, 0x74, 0xca, 0x64, 0xd9, 0xe0, 0x91, 0x8b, 0x39, + 0x57, 0xda, 0xb4, 0xf6, 0x88, 0x45, 0x74, 0x06, 0x0d, 0x33, 0x5d, 0x05, 0xf5, 0xc8, 0x8d, 0x5b, + 0xbd, 0xa3, 0xee, 0xef, 0xaf, 0x18, 0x9d, 0x54, 0x65, 0x7c, 0x0a, 0xde, 0x33, 0x2d, 0x74, 0x39, + 0x31, 0xc9, 0xf5, 0x13, 0x4b, 0x35, 0x97, 0xd5, 0x3b, 0x77, 0x02, 0xbe, 0x87, 0xe6, 0x90, 0xcd, + 0xf2, 0x77, 0x26, 0x97, 0xff, 0xe4, 0x41, 0xe0, 0x0d, 0xa9, 0xa6, 0x55, 0x18, 0x73, 0xae, 0x3e, + 0x67, 0x2e, 0xaa, 0x14, 0x5b, 0xc0, 0x03, 0x38, 0xb0, 0xfd, 0x4c, 0x64, 0xd4, 0x03, 0xa8, 0x84, + 0x9c, 0x6d, 0xf7, 0xd1, 0xea, 0xa1, 0xdd, 0x9b, 0xad, 0x99, 0xfc, 0x71, 0x25, 0x9d, 0xcf, 0x75, + 0xe8, 0xac, 0xd6, 0xa1, 0xf3, 0xbd, 0x0e, 0x9d, 0x8f, 0x4d, 0x58, 0x5b, 0x6d, 0xc2, 0xda, 0xd7, + 0x26, 0xac, 0xbd, 0xd6, 0xc5, 0x78, 0xdc, 0x30, 0xdb, 0xbf, 0xf8, 0x19, 0x00, 0xe7, 0xfb, 0xe5, + 0x7e, 0x0f, 0x02, 0x00, 0x00, } func (m *Syn) Marshal() (dAtA []byte, err error) { @@ -676,6 +723,43 @@ func (m *Delivery) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *DeliveryBatch) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *DeliveryBatch) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *DeliveryBatch) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Deliveries) > 0 { + for iNdEx := len(m.Deliveries) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Deliveries[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintPullsync(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintPullsync(dAtA []byte, offset int, v uint64) int { offset -= sovPullsync(v) base := offset @@ -803,6 +887,21 @@ func (m *Delivery) Size() (n int) { return n } +func (m *DeliveryBatch) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Deliveries) > 0 { + for _, e := range m.Deliveries { + l = e.Size() + n += 1 + l + sovPullsync(uint64(l)) + } + } + return n +} + func sovPullsync(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -844,7 +943,10 @@ func (m *Syn) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -989,7 +1091,10 @@ func (m *Ack) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -1077,7 +1182,10 @@ func (m *Get) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -1229,7 +1337,10 @@ func (m *Chunk) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -1332,7 +1443,10 @@ func (m *Offer) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -1416,7 +1530,10 @@ func (m *Want) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { @@ -1568,7 +1685,97 @@ func (m *Delivery) Unmarshal(dAtA []byte) error { if err != nil { return err } - if (skippy < 0) || (iNdEx+skippy) < 0 { + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *DeliveryBatch) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPullsync + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: DeliveryBatch: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: DeliveryBatch: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Deliveries", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPullsync + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthPullsync + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthPullsync + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Deliveries = append(m.Deliveries, &Delivery{}) + if err := m.Deliveries[len(m.Deliveries)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipPullsync(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthPullsync + } + if (iNdEx + skippy) < 0 { return ErrInvalidLengthPullsync } if (iNdEx + skippy) > l { diff --git a/pkg/pullsync/pb/pullsync.proto b/pkg/pullsync/pb/pullsync.proto index fd5783905e4..4b926d44d9c 100644 --- a/pkg/pullsync/pb/pullsync.proto +++ b/pkg/pullsync/pb/pullsync.proto @@ -41,3 +41,7 @@ message Delivery { bytes Data = 2; bytes Stamp = 3; } + +message DeliveryBatch { + repeated Delivery Deliveries = 1; +} diff --git a/pkg/pullsync/pullsync.go b/pkg/pullsync/pullsync.go index eb385f6f60d..20fc10c8e53 100644 --- a/pkg/pullsync/pullsync.go +++ b/pkg/pullsync/pullsync.go @@ -201,20 +201,34 @@ func (s *Syncer) handler(streamCtx context.Context, p p2p.Peer, stream p2p.Strea s.logger.Debug("rate limited peer", "wait_duration", waitDur, "peer_address", p.Address) } + const maxBatchSize = 25 // maximal number for the allowed delimitedReaderMaxSize + batch := pb.DeliveryBatch{Deliveries: make([]*pb.Delivery, 0, maxBatchSize)} + for _, c := range chs { var stamp []byte if c.Stamp() != nil { stamp, err = c.Stamp().MarshalBinary() if err != nil { - return fmt.Errorf("serialise stamp: %w", err) + return fmt.Errorf("serialize stamp: %w", err) + } + } + + batch.Deliveries = append(batch.Deliveries, &pb.Delivery{Address: c.Address().Bytes(), Data: c.Data(), Stamp: stamp}) + + if len(batch.Deliveries) >= maxBatchSize { + if err := w.WriteMsgWithContext(ctx, &batch); err != nil { + return fmt.Errorf("write delivery batch: %w", err) } + s.metrics.Sent.Add(float64(len(batch.Deliveries))) + batch.Deliveries = batch.Deliveries[:0] } + } - deliver := pb.Delivery{Address: c.Address().Bytes(), Data: c.Data(), Stamp: stamp} - if err := w.WriteMsgWithContext(ctx, &deliver); err != nil { - return fmt.Errorf("write delivery: %w", err) + if len(batch.Deliveries) > 0 { + if err := w.WriteMsgWithContext(ctx, &batch); err != nil { + return fmt.Errorf("write delivery batch: %w", err) } - s.metrics.Sent.Inc() + s.metrics.Sent.Add(float64(len(batch.Deliveries))) } return nil @@ -311,65 +325,70 @@ func (s *Syncer) Sync(ctx context.Context, peer swarm.Address, bin uint8, start chunksToPut := make([]swarm.Chunk, 0, ctr) var chunkErr error - for ; ctr > 0; ctr-- { - var delivery pb.Delivery - if err = r.ReadMsgWithContext(ctx, &delivery); err != nil { - return 0, 0, errors.Join(chunkErr, fmt.Errorf("read delivery: %w", err)) + for ctr > 0 { + var batch pb.DeliveryBatch + if err = r.ReadMsgWithContext(ctx, &batch); err != nil { + return 0, 0, errors.Join(chunkErr, fmt.Errorf("read delivery batch: %w", err)) } - addr := swarm.NewAddress(delivery.Address) - if addr.Equal(swarm.ZeroAddress) { - s.logger.Debug("received zero address chunk", "peer_address", peer) - s.metrics.ReceivedZeroAddress.Inc() - continue - } + for _, delivery := range batch.Deliveries { + ctr-- - newChunk := swarm.NewChunk(addr, delivery.Data) + addr := swarm.NewAddress(delivery.Address) + if addr.Equal(swarm.ZeroAddress) { + s.logger.Debug("received zero address chunk", "peer_address", peer) + s.metrics.ReceivedZeroAddress.Inc() + continue + } - stamp := new(postage.Stamp) - if err = stamp.UnmarshalBinary(delivery.Stamp); err != nil { - chunkErr = errors.Join(chunkErr, err) - continue - } - stampHash, err := stamp.Hash() - if err != nil { - chunkErr = errors.Join(chunkErr, err) - continue - } + newChunk := swarm.NewChunk(addr, delivery.Data) - wantChunkID := addr.ByteString() + string(stamp.BatchID()) + string(stampHash) - if _, ok := wantChunks[wantChunkID]; !ok { - s.logger.Debug("want chunks", "error", ErrUnsolicitedChunk, "peer_address", peer, "chunk_address", addr) - chunkErr = errors.Join(chunkErr, ErrUnsolicitedChunk) - continue - } + stamp := new(postage.Stamp) + if err = stamp.UnmarshalBinary(delivery.Stamp); err != nil { + chunkErr = errors.Join(chunkErr, err) + continue + } + stampHash, err := stamp.Hash() + if err != nil { + chunkErr = errors.Join(chunkErr, err) + continue + } - delete(wantChunks, wantChunkID) + wantChunkID := addr.ByteString() + string(stamp.BatchID()) + string(stampHash) + if _, ok := wantChunks[wantChunkID]; !ok { + s.logger.Debug("want chunks", "error", ErrUnsolicitedChunk, "peer_address", peer, "chunk_address", addr) + chunkErr = errors.Join(chunkErr, ErrUnsolicitedChunk) + continue + } - chunk, err := s.validStamp(newChunk.WithStamp(stamp)) - if err != nil { - s.logger.Debug("unverified stamp", "error", err, "peer_address", peer, "chunk_address", newChunk) - chunkErr = errors.Join(chunkErr, err) - continue - } + delete(wantChunks, wantChunkID) - if cac.Valid(chunk) { - go s.unwrap(chunk) - } else if chunk, err := soc.FromChunk(chunk); err == nil { - addr, err := chunk.Address() + chunk, err := s.validStamp(newChunk.WithStamp(stamp)) if err != nil { + s.logger.Debug("unverified stamp", "error", err, "peer_address", peer, "chunk_address", newChunk) chunkErr = errors.Join(chunkErr, err) continue } - s.logger.Debug("sync gsoc", "peer_address", peer, "chunk_address", addr, "wrapped_chunk_address", chunk.WrappedChunk().Address()) - s.gsocHandler(chunk) - } else { - s.logger.Debug("invalid cac/soc chunk", "error", swarm.ErrInvalidChunk, "peer_address", peer, "chunk", chunk) - chunkErr = errors.Join(chunkErr, swarm.ErrInvalidChunk) - s.metrics.ReceivedInvalidChunk.Inc() - continue + + if cac.Valid(chunk) { + go s.unwrap(chunk) + } else if chunk, err := soc.FromChunk(chunk); err == nil { + addr, err := chunk.Address() + if err != nil { + chunkErr = errors.Join(chunkErr, err) + continue + } + s.logger.Debug("sync gsoc", "peer_address", peer, "chunk_address", addr, "wrapped_chunk_address", chunk.WrappedChunk().Address()) + s.gsocHandler(chunk) + } else { + s.logger.Debug("invalid cac/soc chunk", "error", swarm.ErrInvalidChunk, "peer_address", peer, "chunk", chunk) + chunkErr = errors.Join(chunkErr, swarm.ErrInvalidChunk) + s.metrics.ReceivedInvalidChunk.Inc() + continue + } + + chunksToPut = append(chunksToPut, chunk) } - chunksToPut = append(chunksToPut, chunk) } chunksPut := 0 diff --git a/pkg/pullsync/pullsync_test.go b/pkg/pullsync/pullsync_test.go index 03d6927c1da..d1dd21c9089 100644 --- a/pkg/pullsync/pullsync_test.go +++ b/pkg/pullsync/pullsync_test.go @@ -329,7 +329,7 @@ func haveChunks(t *testing.T, s *mock.ReserveStore, chunks ...swarm.Chunk) { } func newPullSync( - t *testing.T, + t testing.TB, s p2p.Streamer, maxPage uint64, o ...mock.Option, @@ -344,7 +344,7 @@ func newPullSync( } func newPullSyncWithStamperValidator( - t *testing.T, + t testing.TB, s p2p.Streamer, maxPage uint64, validStamp postage.ValidStampFn,