Skip to content

Commit 8e716cb

Browse files
Add synctest in storage incentives (#5262)
1 parent 11f8c81 commit 8e716cb

File tree

2 files changed

+126
-123
lines changed

2 files changed

+126
-123
lines changed

pkg/storageincentives/events_test.go

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,88 @@ package storageincentives_test
77
import (
88
"context"
99
"testing"
10+
"testing/synctest"
1011
"time"
1112

1213
"github.com/ethersphere/bee/v2/pkg/storageincentives"
1314
)
1415

1516
func TestClose(t *testing.T) {
16-
t.Parallel()
17-
18-
ev := storageincentives.NewEvents()
19-
20-
done1 := make(chan struct{})
21-
done2 := make(chan struct{})
22-
done3 := make(chan struct{})
23-
24-
ev.On(1, func(ctx context.Context) {
25-
<-ctx.Done()
26-
close(done1)
27-
})
28-
29-
ev.On(1, func(ctx context.Context) {
30-
<-ctx.Done()
31-
close(done2)
32-
})
33-
34-
ev.On(2, func(ctx context.Context) {
35-
<-ctx.Done()
36-
close(done3)
37-
})
38-
39-
ev.Publish(1)
40-
ev.Publish(2)
41-
42-
ev.Close()
43-
44-
for range 3 {
45-
select {
46-
case <-done1:
47-
case <-done2:
48-
case <-done3:
49-
case <-time.After(time.Second):
50-
t.Fatal("timeout")
17+
synctest.Test(t, func(t *testing.T) {
18+
ev := storageincentives.NewEvents()
19+
20+
done1 := make(chan struct{})
21+
done2 := make(chan struct{})
22+
done3 := make(chan struct{})
23+
24+
ev.On(1, func(ctx context.Context) {
25+
<-ctx.Done()
26+
close(done1)
27+
})
28+
29+
ev.On(1, func(ctx context.Context) {
30+
<-ctx.Done()
31+
close(done2)
32+
})
33+
34+
ev.On(2, func(ctx context.Context) {
35+
<-ctx.Done()
36+
close(done3)
37+
})
38+
39+
ev.Publish(1)
40+
ev.Publish(2)
41+
42+
ev.Close()
43+
44+
for range 3 {
45+
select {
46+
case <-done1:
47+
case <-done2:
48+
case <-done3:
49+
case <-time.After(time.Second):
50+
t.Fatal("timeout")
51+
}
5152
}
52-
}
53+
})
5354
}
5455

5556
func TestPhaseCancel(t *testing.T) {
56-
t.Parallel()
57-
58-
ev := storageincentives.NewEvents()
59-
60-
done1 := make(chan struct{})
61-
done2 := make(chan struct{})
62-
defer ev.Close()
63-
64-
// ensure no panics occur on an empty publish
65-
ev.Publish(0)
66-
67-
ev.On(1, func(ctx context.Context) {
68-
<-ctx.Done()
69-
close(done1)
70-
})
71-
72-
ev.On(2, func(ctx context.Context) {
73-
<-ctx.Done()
74-
close(done2)
75-
})
76-
77-
ev.On(3, func(ctx context.Context) {
78-
ev.Cancel(1, 2)
79-
})
80-
81-
ev.Publish(1)
82-
ev.Publish(2)
83-
ev.Publish(3)
84-
85-
for range 2 {
86-
select {
87-
case <-done1:
88-
case <-done2:
89-
case <-time.After(time.Second):
90-
t.Fatal("timeout")
57+
synctest.Test(t, func(t *testing.T) {
58+
ev := storageincentives.NewEvents()
59+
60+
done1 := make(chan struct{})
61+
done2 := make(chan struct{})
62+
defer ev.Close()
63+
64+
// ensure no panics occur on an empty publish
65+
ev.Publish(0)
66+
67+
ev.On(1, func(ctx context.Context) {
68+
<-ctx.Done()
69+
close(done1)
70+
})
71+
72+
ev.On(2, func(ctx context.Context) {
73+
<-ctx.Done()
74+
close(done2)
75+
})
76+
77+
ev.On(3, func(ctx context.Context) {
78+
ev.Cancel(1, 2)
79+
})
80+
81+
ev.Publish(1)
82+
ev.Publish(2)
83+
ev.Publish(3)
84+
85+
for range 2 {
86+
select {
87+
case <-done1:
88+
case <-done2:
89+
case <-time.After(time.Second):
90+
t.Fatal("timeout")
91+
}
9192
}
92-
}
93+
})
9394
}

pkg/storageincentives/soc_mine_test.go

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"sync"
1616
"testing"
17+
"testing/synctest"
1718

1819
"github.com/ethersphere/bee/v2/pkg/bmt"
1920
"github.com/ethersphere/bee/v2/pkg/cac"
@@ -32,58 +33,59 @@ import (
3233
// to generate uploads using the input
3334
// cat socs.txt | tail 19 | head 16 | perl -pne 's/([a-f0-9]+)\t([a-f0-9]+)\t([a-f0-9]+)\t([a-f0-9]+)/echo -n $4 | xxd -r -p | curl -X POST \"http:\/\/localhost:1633\/soc\/$1\/$2?sig=$3\" -H \"accept: application\/json, text\/plain, \/\" -H \"content-type: application\/octet-stream\" -H \"swarm-postage-batch-id: 14b26beca257e763609143c6b04c2c487f01a051798c535c2f542ce75a97c05f\" --data-binary \@-/'
3435
func TestSocMine(t *testing.T) {
35-
t.Parallel()
36-
// the anchor used in neighbourhood selection and reserve salt for sampling
37-
prefix, err := hex.DecodeString("3617319a054d772f909f7c479a2cebe5066e836a939412e32403c99029b92eff")
38-
if err != nil {
39-
t.Fatal(err)
40-
}
41-
// the transformed address hasher factory function
42-
prefixhasher := func() hash.Hash { return swarm.NewPrefixHasher(prefix) }
43-
// Create a pool for efficient hasher reuse
44-
trHasherPool := bmt.NewPool(bmt.NewConf(prefixhasher, swarm.BmtBranches, 8))
45-
// the bignum cast of the maximum sample value (upper bound on transformed addresses as a 256-bit article)
46-
// this constant is for a minimum reserve size of 2 million chunks with sample size of 16
47-
// = 1.284401 * 10^71 = 1284401 + 66 0-s
48-
mstring := "1284401"
49-
for range 66 {
50-
mstring = mstring + "0"
51-
}
52-
n, ok := new(big.Int).SetString(mstring, 10)
53-
if !ok {
54-
t.Fatalf("SetString: error setting to '%s'", mstring)
55-
}
56-
// the filter function on the SOC address
57-
// meant to make sure we pass check for proof of retrievability for
58-
// a node of overlay 0x65xxx with a reserve depth of 1, i.e.,
59-
// SOC address must start with zero bit
60-
filterSOCAddr := func(a swarm.Address) bool {
61-
return a.Bytes()[0]&0x80 != 0x00
62-
}
63-
// the filter function on the transformed address using the density estimation constant
64-
filterTrAddr := func(a swarm.Address) (bool, error) {
65-
m := new(big.Int).SetBytes(a.Bytes())
66-
return m.Cmp(n) < 0, nil
67-
}
68-
// setup the signer with a private key from a fixture
69-
data, err := hex.DecodeString("634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd")
70-
if err != nil {
71-
t.Fatal(err)
72-
}
73-
privKey, err := crypto.DecodeSecp256k1PrivateKey(data)
74-
if err != nil {
75-
t.Fatal(err)
76-
}
77-
signer := crypto.NewDefaultSigner(privKey)
36+
synctest.Test(t, func(t *testing.T) {
37+
// the anchor used in neighbourhood selection and reserve salt for sampling
38+
prefix, err := hex.DecodeString("3617319a054d772f909f7c479a2cebe5066e836a939412e32403c99029b92eff")
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
// the transformed address hasher factory function
43+
prefixhasher := func() hash.Hash { return swarm.NewPrefixHasher(prefix) }
44+
// Create a pool for efficient hasher reuse
45+
trHasherPool := bmt.NewPool(bmt.NewConf(prefixhasher, swarm.BmtBranches, 8))
46+
// the bignum cast of the maximum sample value (upper bound on transformed addresses as a 256-bit article)
47+
// this constant is for a minimum reserve size of 2 million chunks with sample size of 16
48+
// = 1.284401 * 10^71 = 1284401 + 66 0-s
49+
mstring := "1284401"
50+
for range 66 {
51+
mstring = mstring + "0"
52+
}
53+
n, ok := new(big.Int).SetString(mstring, 10)
54+
if !ok {
55+
t.Fatalf("SetString: error setting to '%s'", mstring)
56+
}
57+
// the filter function on the SOC address
58+
// meant to make sure we pass check for proof of retrievability for
59+
// a node of overlay 0x65xxx with a reserve depth of 1, i.e.,
60+
// SOC address must start with zero bit
61+
filterSOCAddr := func(a swarm.Address) bool {
62+
return a.Bytes()[0]&0x80 != 0x00
63+
}
64+
// the filter function on the transformed address using the density estimation constant
65+
filterTrAddr := func(a swarm.Address) (bool, error) {
66+
m := new(big.Int).SetBytes(a.Bytes())
67+
return m.Cmp(n) < 0, nil
68+
}
69+
// setup the signer with a private key from a fixture
70+
data, err := hex.DecodeString("634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd")
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
privKey, err := crypto.DecodeSecp256k1PrivateKey(data)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
signer := crypto.NewDefaultSigner(privKey)
7879

79-
sampleSize := 16
80-
// for sanity check: given a filterSOCAddr requiring a 0 leading bit (chance of 1/2)
81-
// we expect an overall rough 4 million chunks to be mined to create this sample
82-
// for 8 workers that is half a million round on average per worker
83-
err = makeChunks(t, signer, sampleSize, filterSOCAddr, filterTrAddr, trHasherPool)
84-
if err != nil {
85-
t.Fatal(err)
86-
}
80+
sampleSize := 16
81+
// for sanity check: given a filterSOCAddr requiring a 0 leading bit (chance of 1/2)
82+
// we expect an overall rough 4 million chunks to be mined to create this sample
83+
// for 8 workers that is half a million round on average per worker
84+
err = makeChunks(t, signer, sampleSize, filterSOCAddr, filterTrAddr, trHasherPool)
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
})
8789
}
8890

8991
func makeChunks(t *testing.T, signer crypto.Signer, sampleSize int, filterSOCAddr func(swarm.Address) bool, filterTrAddr func(swarm.Address) (bool, error), trHasherPool *bmt.Pool) error {

0 commit comments

Comments
 (0)