Skip to content

Commit

Permalink
feat: soc inclusion proof data
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Sep 19, 2023
1 parent 0c976d4 commit d9ac920
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
21 changes: 13 additions & 8 deletions pkg/storageincentives/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 33 additions & 6 deletions pkg/storageincentives/redistribution/inclusionproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -52,25 +54,50 @@ 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,
ProofSegments2: proofp2Hex.ProofSegments,
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 {
Expand Down

0 comments on commit d9ac920

Please sign in to comment.