Skip to content

Commit

Permalink
feat(soc): mining
Browse files Browse the repository at this point in the history
  • Loading branch information
zelig committed Sep 21, 2023
1 parent 038dbfb commit 0e15485
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions pkg/soc/soc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ package soc_test

import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/cac"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/soc"
"github.com/ethersphere/bee/pkg/swarm"
"golang.org/x/sync/errgroup"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -335,3 +338,90 @@ func TestRecoverAddress(t *testing.T) {
t.Fatalf("owner address mismatch. got %x want %x", addr, owner.Bytes())
}
}

// TestSign tests whether a soc is correctly signed.
func TestSocMine(t *testing.T) {
t.Parallel()

privKey, err := crypto.GenerateSecp256k1Key()
if err != nil {
t.Fatal(err)
}
signer := crypto.NewDefaultSigner(privKey)
publicKey, err := signer.PublicKey()
if err != nil {
t.Fatal(err)
}
ownerAddressBytes, err := crypto.NewEthereumAddress(*publicKey)
if err != nil {
t.Fatal(err)
}
payload := []byte("foo")
ch, err := cac.New(payload)
if err != nil {
t.Fatal(err)
}
var done bool
count := 8
sampleSize := 16
sampleC := make(chan *soc.SOC, 1)
sample := make([]*soc.SOC, sampleSize)
ctx, cancel := context.WithCancel(context.Background())
eg, ectx := errgroup.WithContext(ctx)
eg.Go(func() error {
defer cancel()
for i := 0; i < sampleSize; i++ {
select {
case sample[i] = <-sampleC:
case <-ectx.Done():
return ectx.Err()
}
}
done = true
return nil
})
for i := 0; i < count; i++ {
i := i
eg.Go(func() (err error) {
offset := i * 4
for seed := uint32(1); ; seed++ {
id := make([]byte, 32)
binary.BigEndian.PutUint32(id[offset:], seed)
s := soc.New(id, ch)
addr, err := soc.CreateAddress(id, ownerAddressBytes)
if err != nil {
return err
}

if small(addr) {
select {
case sampleC <- s: //never blocks
fmt.Printf("%x\n", s.ID())

Check failure on line 399 in pkg/soc/soc_test.go

View workflow job for this annotation

GitHub Actions / Lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
case <-ectx.Done():
return ectx.Err()
}
}
}

return nil

Check failure on line 406 in pkg/soc/soc_test.go

View workflow job for this annotation

GitHub Actions / Lint

unreachable: unreachable code (govet)
})
}
if err := eg.Wait(); !done && err != nil {
t.Fatal(err.Error())
}
for _, s := range sample {

// signs the chunk
sch, err := s.Sign(signer)
if err != nil {
t.Fatal(err)
}
fmt.Printf("%x\n", sch.Data())

Check failure on line 419 in pkg/soc/soc_test.go

View workflow job for this annotation

GitHub Actions / Lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

}
}

func small(swarm.Address) bool {

return true
}

0 comments on commit 0e15485

Please sign in to comment.