From d9ac92014bd87a1250612ad0940b202c887d050d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Tue, 19 Sep 2023 15:51:53 +0200 Subject: [PATCH] feat: soc inclusion proof data --- pkg/storageincentives/proof.go | 21 ++++++---- .../redistribution/inclusionproof.go | 39 ++++++++++++++++--- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/pkg/storageincentives/proof.go b/pkg/storageincentives/proof.go index 6eb6b9cd56b..26cea53f464 100644 --- a/pkg/storageincentives/proof.go +++ b/pkg/storageincentives/proof.go @@ -44,10 +44,6 @@ func makeInclusionProofs( require2++ } - stamp1 := reserveSampleItems[require1].Stamp - stamp2 := reserveSampleItems[require2].Stamp - stampLast := reserveSampleItems[storer.SampleSize-1].Stamp - // TODO: refactor, make it global / anchor (cleanup?) prefixHasherFactory := func() hash.Hash { return swarm.NewPrefixHasher(anchor1) @@ -173,10 +169,19 @@ func makeInclusionProofs( bmtpool.Put(chunkLastContent) prefixHasherPool.Put(chunkLastTrContent) - // map to output - A := redistribution.NewChunkInclusionProof(proof1p1, proof1p2, proof1p3, stamp1, reserveSampleItems[require1].ChunkAddress.Bytes()) - B := redistribution.NewChunkInclusionProof(proof2p1, proof2p2, proof2p3, stamp2, reserveSampleItems[require2].ChunkAddress.Bytes()) - C := redistribution.NewChunkInclusionProof(proofLastp1, proofLastp2, proofLastp3, stampLast, reserveSampleItems[require3].ChunkAddress.Bytes()) + // map to output and add SOC related data if it is necessary + A, err := redistribution.NewChunkInclusionProof(proof1p1, proof1p2, proof1p3, reserveSampleItems[require1]) + if err != nil { + return redistribution.ChunkInclusionProofs{}, err + } + B, err := redistribution.NewChunkInclusionProof(proof2p1, proof2p2, proof2p3, reserveSampleItems[require2]) + if err != nil { + return redistribution.ChunkInclusionProofs{}, err + } + C, err := redistribution.NewChunkInclusionProof(proofLastp1, proofLastp2, proofLastp3, reserveSampleItems[require3]) + if err != nil { + return redistribution.ChunkInclusionProofs{}, err + } return redistribution.ChunkInclusionProofs{ A: A, B: B, diff --git a/pkg/storageincentives/redistribution/inclusionproof.go b/pkg/storageincentives/redistribution/inclusionproof.go index 4825ecd4aae..1d62450a070 100644 --- a/pkg/storageincentives/redistribution/inclusionproof.go +++ b/pkg/storageincentives/redistribution/inclusionproof.go @@ -12,6 +12,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethersphere/bee/pkg/bmt" + "github.com/ethersphere/bee/pkg/soc" + "github.com/ethersphere/bee/pkg/storer" "github.com/ethersphere/bee/pkg/swarm" ) @@ -52,11 +54,16 @@ type SOCProof struct { } // Transforms arguments to ChunkInclusionProof object -func NewChunkInclusionProof(proofp1, proofp2 bmt.Proof, proofp3 bmt.Proof, stamp swarm.Stamp, chunkAddress []byte) ChunkInclusionProof { +func NewChunkInclusionProof(proofp1, proofp2 bmt.Proof, proofp3 bmt.Proof, sampleItem storer.SampleItem) (ChunkInclusionProof, error) { proofp1Hex := newHexProofs(proofp1) proofp2Hex := newHexProofs(proofp2) proofp3Hex := newHexProofs(proofp3) + socProof, err := makeSOCProof(sampleItem) + if err != nil { + return ChunkInclusionProof{}, err + } + return ChunkInclusionProof{ ProofSegments: proofp1Hex.ProofSegments, ProveSegment: proofp1Hex.ProveSegment, @@ -64,13 +71,33 @@ func NewChunkInclusionProof(proofp1, proofp2 bmt.Proof, proofp3 bmt.Proof, stamp ProveSegment2: proofp2Hex.ProveSegment, ChunkSpan: binary.LittleEndian.Uint64(proofp2.Span[:swarm.SpanSize]), // should be uint64 on the other size; copied from pkg/api/bytes.go ProofSegments3: proofp3Hex.ProofSegments, - Signature: hex.EncodeToString(stamp.Sig()), + Signature: hex.EncodeToString(sampleItem.Stamp.Sig()), ChunkAddr: proofp1Hex.ProveSegment, // TODO refactor ABI - PostageId: hex.EncodeToString(stamp.BatchID()), - Index: hex.EncodeToString(stamp.Index()), - TimeStamp: hex.EncodeToString(stamp.Timestamp()), - SocProofAttached: []SOCProof{}, // TODO + PostageId: hex.EncodeToString(sampleItem.Stamp.BatchID()), + Index: hex.EncodeToString(sampleItem.Stamp.Index()), + TimeStamp: hex.EncodeToString(sampleItem.Stamp.Timestamp()), + SocProofAttached: socProof, + }, nil +} + +func makeSOCProof(sampleItem storer.SampleItem) ([]SOCProof, error) { + var emptySOCProof = make([]SOCProof, 0) + ch := swarm.NewChunk(sampleItem.ChunkAddress, sampleItem.ChunkData) + if !soc.Valid(ch) { + return emptySOCProof, nil } + + socCh, err := soc.FromChunk(ch) + if err != nil { + return emptySOCProof, err + } + + return []SOCProof{{ + Signer: common.Address(socCh.OwnerAddress()), + Signature: hex.EncodeToString(socCh.Signature()), + Identifier: hex.EncodeToString(socCh.ID()), + ChunkAddr: hex.EncodeToString(socCh.WrappedChunk().Address().Bytes()), + }}, nil } type hexProof struct {