From 0e15485e9047ca8c476691a652c6ec406870e070 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 21 Sep 2023 06:45:20 +0200 Subject: [PATCH] feat(soc): mining --- pkg/soc/soc_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/pkg/soc/soc_test.go b/pkg/soc/soc_test.go index e4f6fb4e199..40d042e11b3 100644 --- a/pkg/soc/soc_test.go +++ b/pkg/soc/soc_test.go @@ -6,9 +6,11 @@ package soc_test import ( "bytes" + "context" "encoding/binary" "encoding/hex" "errors" + "fmt" "testing" "github.com/ethereum/go-ethereum/common" @@ -16,6 +18,7 @@ import ( "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) { @@ -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()) + case <-ectx.Done(): + return ectx.Err() + } + } + } + + return nil + }) + } + 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()) + + } +} + +func small(swarm.Address) bool { + + return true +}