@@ -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 \@-/'
3435func 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
8991func 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