Skip to content

Commit

Permalink
feat: postage stamping for gsoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Oct 23, 2024
1 parent 7134e02 commit 68f97ce
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
8 changes: 7 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion pkg/postage/mock/stamper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion pkg/postage/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/postage/stamper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
}
16 changes: 10 additions & 6 deletions pkg/postage/stamper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
})
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pss/pss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/pss/pss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion pkg/steward/steward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 68f97ce

Please sign in to comment.