From 68f97ce7d61f33591f7bad268e83686ee1616f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Tue, 15 Oct 2024 09:20:41 +0200 Subject: [PATCH] feat: postage stamping for gsoc --- pkg/api/api.go | 8 +++++++- pkg/api/envelope.go | 2 +- pkg/postage/mock/stamper.go | 7 ++++++- pkg/postage/stamp_test.go | 8 +++++++- pkg/postage/stamper.go | 18 ++++++++++++++---- pkg/postage/stamper_test.go | 16 ++++++++++------ pkg/pss/pss.go | 2 +- pkg/pss/pss_test.go | 13 ++++++++++--- pkg/steward/steward.go | 2 +- 9 files changed, 57 insertions(+), 19 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index bced503484c..74911b83912 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -48,6 +48,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/settlement/swap" "github.com/ethersphere/bee/v2/pkg/settlement/swap/chequebook" "github.com/ethersphere/bee/v2/pkg/settlement/swap/erc20" + "github.com/ethersphere/bee/v2/pkg/soc" "github.com/ethersphere/bee/v2/pkg/status" "github.com/ethersphere/bee/v2/pkg/steward" storage "github.com/ethersphere/bee/v2/pkg/storage" @@ -685,7 +686,12 @@ type putterSessionWrapper struct { } func (p *putterSessionWrapper) Put(ctx context.Context, chunk swarm.Chunk) error { - stamp, err := p.stamper.Stamp(chunk.Address()) + idAddress, err := soc.IdentityAddress(chunk) + if err != nil { + return err + } + + stamp, err := p.stamper.Stamp(chunk.Address(), idAddress) if err != nil { return err } diff --git a/pkg/api/envelope.go b/pkg/api/envelope.go index 6a99b98f61c..ea12283ac4d 100644 --- a/pkg/api/envelope.go +++ b/pkg/api/envelope.go @@ -59,7 +59,7 @@ func (s *Service) envelopePostHandler(w http.ResponseWriter, r *http.Request) { return } - stamp, err := stamper.Stamp(paths.Address) + stamp, err := stamper.Stamp(paths.Address, paths.Address) if err != nil { logger.Debug("split write all failed", "error", err) logger.Error(nil, "split write all failed") diff --git a/pkg/postage/mock/stamper.go b/pkg/postage/mock/stamper.go index f95700eb5e5..9fbc5268b1c 100644 --- a/pkg/postage/mock/stamper.go +++ b/pkg/postage/mock/stamper.go @@ -17,6 +17,11 @@ func NewStamper() postage.Stamper { } // Stamp implements the Stamper interface. It returns an empty postage stamp. -func (mockStamper) Stamp(_ swarm.Address) (*postage.Stamp, error) { +func (mockStamper) Stamp(_, _ swarm.Address) (*postage.Stamp, error) { return &postage.Stamp{}, nil } + +// Stamp implements the Stamper interface. It returns an empty postage stamp. +func (mockStamper) BatchId() []byte { + return nil +} diff --git a/pkg/postage/stamp_test.go b/pkg/postage/stamp_test.go index 8704cf91b65..b1547b6581c 100644 --- a/pkg/postage/stamp_test.go +++ b/pkg/postage/stamp_test.go @@ -14,6 +14,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/postage" "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock" postagetesting "github.com/ethersphere/bee/v2/pkg/postage/testing" + "github.com/ethersphere/bee/v2/pkg/soc" "github.com/ethersphere/bee/v2/pkg/storage/inmemstore" chunktesting "github.com/ethersphere/bee/v2/pkg/storage/testing" ) @@ -103,7 +104,12 @@ func TestValidStamp(t *testing.T) { // stamp on execution ch := chunktesting.GenerateTestRandomChunk() - st, err := stamper.Stamp(ch.Address()) + idAddress, err := soc.IdentityAddress(ch) + if err != nil { + t.Fatal(err) + } + + st, err := stamper.Stamp(ch.Address(), idAddress) if err != nil { t.Fatal(err) } diff --git a/pkg/postage/stamper.go b/pkg/postage/stamper.go index d4f3b8187fa..101d6f78673 100644 --- a/pkg/postage/stamper.go +++ b/pkg/postage/stamper.go @@ -21,7 +21,8 @@ var ( // Stamper can issue stamps from the given address of chunk. type Stamper interface { - Stamp(swarm.Address) (*Stamp, error) + Stamp(idAddr, addr swarm.Address) (*Stamp, error) + BatchId() []byte } // stamper connects a stampissuer with a signer. @@ -39,13 +40,13 @@ func NewStamper(store storage.Store, issuer *StampIssuer, signer crypto.Signer) // Stamp takes chunk, see if the chunk can be included in the batch and // signs it with the owner of the batch of this Stamp issuer. -func (st *stamper) Stamp(addr swarm.Address) (*Stamp, error) { +func (st *stamper) Stamp(addr, idAddr swarm.Address) (*Stamp, error) { st.issuer.mtx.Lock() defer st.issuer.mtx.Unlock() item := &StampItem{ BatchID: st.issuer.data.BatchID, - chunkAddress: addr, + chunkAddress: idAddr, } switch err := st.store.Get(item); { case err == nil: @@ -81,6 +82,11 @@ func (st *stamper) Stamp(addr swarm.Address) (*Stamp, error) { return NewStamp(st.issuer.data.BatchID, item.BatchIndex, item.BatchTimestamp, sig), nil } +// BatchId gives back batch id of stamper +func (st *stamper) BatchId() []byte { + return st.issuer.data.BatchID +} + type presignedStamper struct { stamp *Stamp owner []byte @@ -90,7 +96,7 @@ func NewPresignedStamper(stamp *Stamp, owner []byte) Stamper { return &presignedStamper{stamp, owner} } -func (st *presignedStamper) Stamp(addr swarm.Address) (*Stamp, error) { +func (st *presignedStamper) Stamp(addr, _ swarm.Address) (*Stamp, error) { // check stored stamp is against the chunk address // Recover the public key from the signature signerAddr, err := RecoverBatchOwner(addr, st.stamp) @@ -104,3 +110,7 @@ func (st *presignedStamper) Stamp(addr swarm.Address) (*Stamp, error) { return st.stamp, nil } + +func (st *presignedStamper) BatchId() []byte { + return st.stamp.BatchID() +} diff --git a/pkg/postage/stamper_test.go b/pkg/postage/stamper_test.go index a1c589b145c..4069a6daac6 100644 --- a/pkg/postage/stamper_test.go +++ b/pkg/postage/stamper_test.go @@ -33,7 +33,7 @@ func TestStamperStamping(t *testing.T) { t.Helper() chunkAddr := swarm.RandAddress(t) - stamp, err := stamper.Stamp(chunkAddr) + stamp, err := stamper.Stamp(chunkAddr, chunkAddr) if err != nil { t.Fatal(err) } @@ -71,12 +71,14 @@ func TestStamperStamping(t *testing.T) { // issue another 15 // collision depth is 8, committed batch depth is 12, bucket volume 2^4 for i := 0; i < 14; i++ { - _, err = stamper.Stamp(swarm.RandAddressAt(t, chunkAddr, 8)) + randAddr := swarm.RandAddressAt(t, chunkAddr, 8) + _, err = stamper.Stamp(randAddr, randAddr) if err != nil { t.Fatalf("error adding stamp at step %d: %v", i, err) } } - stamp, err := stamper.Stamp(swarm.RandAddressAt(t, chunkAddr, 8)) + randAddr := swarm.RandAddressAt(t, chunkAddr, 8) + stamp, err := stamper.Stamp(randAddr, randAddr) if err != nil { t.Fatalf("error adding last stamp: %v", err) } @@ -95,13 +97,15 @@ func TestStamperStamping(t *testing.T) { // issue another 15 // collision depth is 8, committed batch depth is 12, bucket volume 2^4 for i := 0; i < 15; i++ { - _, err = stamper.Stamp(swarm.RandAddressAt(t, chunkAddr, 8)) + randAddr := swarm.RandAddressAt(t, chunkAddr, 8) + _, err = stamper.Stamp(randAddr, randAddr) if err != nil { t.Fatalf("error adding stamp at step %d: %v", i, err) } } + randAddr := swarm.RandAddressAt(t, chunkAddr, 8) // the bucket should now be full, not allowing a stamp for the pivot chunk - if _, err = stamper.Stamp(swarm.RandAddressAt(t, chunkAddr, 8)); !errors.Is(err, postage.ErrBucketFull) { + if _, err = stamper.Stamp(randAddr, randAddr); !errors.Is(err, postage.ErrBucketFull) { t.Fatalf("expected ErrBucketFull, got %v", err) } }) @@ -117,7 +121,7 @@ func TestStamperStamping(t *testing.T) { WithBatchIndex(index) testSt := &testStore{Store: inmemstore.New(), stampItem: testItem} stamper := postage.NewStamper(testSt, st, signer) - stamp, err := stamper.Stamp(chunkAddr) + stamp, err := stamper.Stamp(chunkAddr, chunkAddr) if err != nil { t.Fatal(err) } diff --git a/pkg/pss/pss.go b/pkg/pss/pss.go index 3a09db0b633..454f5960129 100644 --- a/pkg/pss/pss.go +++ b/pkg/pss/pss.go @@ -99,7 +99,7 @@ func (p *pss) Send(ctx context.Context, topic Topic, payload []byte, stamper pos return err } - stamp, err := stamper.Stamp(tc.Address()) + stamp, err := stamper.Stamp(tc.Address(), tc.Address()) if err != nil { return err } diff --git a/pkg/pss/pss_test.go b/pkg/pss/pss_test.go index 1d04237bbaa..685adb59fb9 100644 --- a/pkg/pss/pss_test.go +++ b/pkg/pss/pss_test.go @@ -236,8 +236,15 @@ func ensureCalls(t *testing.T, calls *int, exp int) { } } -type stamper struct{} +type stamper struct { + stamp *postage.Stamp +} + +func (s *stamper) Stamp(_, _ swarm.Address) (*postage.Stamp, error) { + stamp := postagetesting.MustNewStamp() + return stamp, nil +} -func (s *stamper) Stamp(_ swarm.Address) (*postage.Stamp, error) { - return postagetesting.MustNewStamp(), nil +func (s *stamper) BatchId() []byte { + return s.stamp.BatchID() } diff --git a/pkg/steward/steward.go b/pkg/steward/steward.go index 9726ed1baa3..f318711d8ff 100644 --- a/pkg/steward/steward.go +++ b/pkg/steward/steward.go @@ -61,7 +61,7 @@ func (s *steward) Reupload(ctx context.Context, root swarm.Address, stamper post return err } - stamp, err := stamper.Stamp(c.Address()) + stamp, err := stamper.Stamp(c.Address(), c.Address()) if err != nil { return fmt.Errorf("stamping chunk %s: %w", c.Address(), err) }