From 56e0577d9214186b5cf9526c881daf5c3db03a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 15 Nov 2023 11:21:31 +0100 Subject: [PATCH] fix: payloadsize calculation --- pkg/file/joiner/joiner.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/file/joiner/joiner.go b/pkg/file/joiner/joiner.go index cd77b0bb8e8..398b7e7f6a9 100644 --- a/pkg/file/joiner/joiner.go +++ b/pkg/file/joiner/joiner.go @@ -56,9 +56,12 @@ func New(ctx context.Context, getter storage.Getter, address swarm.Address) (fil } rootParity, span := chunkToSpan(chunkData) maxBranching := swarm.ChunkSize / refLength - payloadSize, err := chunkPayloadSize(rootData) - if err != nil { - return nil, 0, err + payloadSize := int(span) + if span > swarm.ChunkSize { + payloadSize, err = chunkPayloadSize(rootData) + if err != nil { + return nil, 0, err + } } rootShards := payloadSize - (rootParity*swarm.HashSize)/refLength rLevel, err := redundancy.GetLevel(rootParity, rootShards, encryption) @@ -69,7 +72,7 @@ func New(ctx context.Context, getter storage.Getter, address swarm.Address) (fil return 0, int64(bmt.LengthFromSpan(data[:swarm.SpanSize])) } // override stuff if root chunk has redundancy - if rootParity > 0 { + if rLevel != redundancy.NONE { spanFn = chunkToSpan if encryption { maxBranching = rLevel.GetMaxEncShards() @@ -357,7 +360,7 @@ func (j *joiner) Size() int64 { // chunkPayloadSize returns the effective byte length of an intermediate chunk // assumes data is always chunk size (without span) func chunkPayloadSize(data []byte) (int, error) { - l := swarm.ChunkSize + l := len(data) for l > swarm.HashSize { if !bytes.Equal(data[l-swarm.HashSize:l], swarm.ZeroAddress.Bytes()) { return l, nil