Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ph4): agent and api #4323

Merged
merged 7 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/ethersphere/bee/pkg/storage/inmemstore"
testingc "github.com/ethersphere/bee/pkg/storage/testing"
"github.com/ethersphere/bee/pkg/storageincentives"
"github.com/ethersphere/bee/pkg/storageincentives/redistribution"
"github.com/ethersphere/bee/pkg/storageincentives/staking"
mock2 "github.com/ethersphere/bee/pkg/storageincentives/staking/mock"
mockstorer "github.com/ethersphere/bee/pkg/storer/mock"
Expand Down Expand Up @@ -775,7 +776,7 @@ func (m *mockContract) IsWinner(context.Context) (bool, error) {
return false, nil
}

func (m *mockContract) Claim(context.Context) (common.Hash, error) {
func (m *mockContract) Claim(context.Context, redistribution.ChunkInclusionProofs) (common.Hash, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.callsList = append(m.callsList, claimCall)
Expand Down
12 changes: 10 additions & 2 deletions pkg/api/rchash.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func (s *Service) rchash(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_rchash").Build()

paths := struct {
Depth *uint8 `map:"depth" validate:"required"`
Depth uint8 `map:"depth" validate:"min=0"`
Anchor1 string `map:"anchor1" validate:"required"`
Anchor2 string `map:"anchor2" validate:"required"`
}{}
if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
response("invalid path params", logger, w)
Expand All @@ -34,7 +35,14 @@ func (s *Service) rchash(w http.ResponseWriter, r *http.Request) {
return
}

resp, err := s.redistributionAgent.SampleWithProofs(r.Context(), anchor1, *paths.Depth)
anchor2, err := hex.DecodeString(paths.Anchor2)
if err != nil {
logger.Error(err, "invalid hex params")
jsonhttp.InternalServerError(w, "invalid hex params")
return
}

resp, err := s.redistributionAgent.SampleWithProofs(r.Context(), anchor1, anchor2, paths.Depth)
if err != nil {
logger.Error(err, "failed making sample with proofs")
jsonhttp.InternalServerError(w, "failed making sample with proofs")
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,6 @@ func (s *Service) mountAPI() {
web.FinalHandlerFunc(s.healthHandler),
))

handle("/rchash/{depth}/{anchor1}", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.rchash),
}),
))

if s.Restricted {
handle("/auth", jsonhttp.MethodHandler{
"POST": web.ChainHandlers(
Expand Down Expand Up @@ -601,4 +595,10 @@ func (s *Service) mountBusinessDebug(restricted bool) {
web.FinalHandlerFunc(s.statusGetPeersHandler),
),
})

handle("/rchash/{depth}/{anchor1}/{anchor2}", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.rchash),
}),
))
}
47 changes: 36 additions & 11 deletions pkg/storageincentives/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,23 @@ func (a *Agent) handleClaim(ctx context.Context, round uint64) error {
a.logger.Info("could not set balance", "err", err)
}

txHash, err := a.contract.Claim(ctx)
sampleData, exists := a.state.SampleData(round - 1)
if !exists {
return fmt.Errorf("sample not found")
}

anchor2, err := a.contract.ReserveSalt(ctx)
if err != nil {
a.logger.Info("failed getting anchor after second reveal", "err", err)
}

proofs, err := makeInclusionProofs(sampleData.ReserveSampleItems, sampleData.Anchor1, anchor2)

if err != nil {
return fmt.Errorf("making inclusion proofs: %w", err)
}

txHash, err := a.contract.Claim(ctx, proofs)
if err != nil {
a.metrics.ErrClaim.Inc()
return fmt.Errorf("claiming win: %w", err)
Expand Down Expand Up @@ -413,15 +429,14 @@ func (a *Agent) handleSample(ctx context.Context, round uint64) (bool, error) {
return false, nil
}

now := time.Now()
sample, err := a.makeSample(ctx, storageRadius)
if err != nil {
return false, err
}

a.logger.Info("produced sample", "hash", sample.ReserveSampleHash, "radius", sample.StorageRadius, "round", round)

a.state.SetSampleData(round, sample, time.Since(now))
a.state.SetSampleData(round, sample)

return true, nil
}
Expand All @@ -442,16 +457,19 @@ func (a *Agent) makeSample(ctx context.Context, storageRadius uint8) (SampleData
if err != nil {
return SampleData{}, err
}
a.metrics.SampleDuration.Set(time.Since(t).Seconds())
dur := time.Since(t)
a.metrics.SampleDuration.Set(dur.Seconds())

sampleHash, err := sampleHash(rSample.Items)
if err != nil {
return SampleData{}, err
}

sample := SampleData{
ReserveSampleHash: sampleHash,
StorageRadius: storageRadius,
Anchor1: salt,
ReserveSampleItems: rSample.Items,
ReserveSampleHash: sampleHash,
StorageRadius: storageRadius,
}

return sample, nil
Expand Down Expand Up @@ -538,14 +556,16 @@ func (a *Agent) Status() (*Status, error) {
}

type SampleWithProofs struct {
Items []storer.SampleItem
Hash swarm.Address
Duration time.Duration
Hash swarm.Address `json:"hash"`
Proofs redistribution.ChunkInclusionProofs `json:"proofs"`
Duration time.Duration `json:"duration"`
}

// Only called by rchash API
func (a *Agent) SampleWithProofs(
ctx context.Context,
anchor1 []byte,
anchor2 []byte,
storageRadius uint8,
) (SampleWithProofs, error) {
sampleStartTime := time.Now()
Expand All @@ -562,12 +582,17 @@ func (a *Agent) SampleWithProofs(

hash, err := sampleHash(rSample.Items)
if err != nil {
return SampleWithProofs{}, fmt.Errorf("sample hash: %w:", err)
return SampleWithProofs{}, fmt.Errorf("sample hash: %w", err)
}

proofs, err := makeInclusionProofs(rSample.Items, anchor1, anchor2)
if err != nil {
return SampleWithProofs{}, fmt.Errorf("make proofs: %w", err)
}

return SampleWithProofs{
Items: rSample.Items,
Hash: hash,
Proofs: proofs,
Duration: time.Since(sampleStartTime),
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storageincentives/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (m *mockContract) IsWinner(context.Context) (bool, error) {
return false, nil
}

func (m *mockContract) Claim(context.Context) (common.Hash, error) {
func (m *mockContract) Claim(context.Context, redistribution.ChunkInclusionProofs) (common.Hash, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.callsList = append(m.callsList, claimCall)
Expand Down
5 changes: 3 additions & 2 deletions pkg/storageincentives/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package storageincentives

var (
NewEvents = newEvents
SampleChunk = sampleChunk
NewEvents = newEvents
SampleChunk = sampleChunk
MakeInclusionProofs = makeInclusionProofs
)
Loading
Loading