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

chore: fix stressnet with new BLS keys #534

Merged
merged 4 commits into from
Dec 14, 2024
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
28 changes: 16 additions & 12 deletions p2p/integrationtest/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -99,17 +100,17 @@ func (b *ProviderClient) Close() error {
return b.conn.Close()
}

func (b *ProviderClient) CheckAndStake(_ []string) error {
func (b *ProviderClient) CheckAndStake() (string, error) {
stakeAmt, err := b.client.GetStake(context.Background(), &providerapiv1.EmptyMessage{})
if err != nil {
b.logger.Error("failed to get stake amount", "err", err)
return err
return "", err
}

topology, err := b.debugClient.GetTopology(context.Background(), &debugapi.EmptyMessage{})
if err != nil {
b.logger.Error("failed to get topology", "err", err)
return err
return "", err
}

ethAddress := topology.GetTopology().Fields["self"].GetStructValue().Fields["Ethereum Address"].GetStringValue()
Expand All @@ -120,34 +121,38 @@ func (b *ProviderClient) CheckAndStake(_ []string) error {
stakedAmt, set := big.NewInt(0).SetString(stakeAmt.Amount, 10)
if !set {
b.logger.Error("failed to parse stake amount")
return errors.New("failed to parse stake amount")
return "", errors.New("failed to parse stake amount")
}

if stakedAmt.Cmp(big.NewInt(0)) > 0 {
b.logger.Error("bidder already staked")
return nil
b.logger.Error("provider already staked")
if len(stakeAmt.BlsPublicKeys) == 0 {
return "", errors.New("provider already staked but no BLS public keys found")
}
return stakeAmt.BlsPublicKeys[0], nil
}

hashedMessage := crypto.Keccak256(common.HexToAddress(ethAddress).Bytes())
ikm := make([]byte, 32)
_, _ = rand.Read(ikm)
privateKey, err := bls.KeyGen[bls.G1](ikm, nil, nil)
if err != nil {
b.logger.Error("failed to generate private key", "error", err)
return fmt.Errorf("failed to generate private key: %w", err)
return "", fmt.Errorf("failed to generate private key: %w", err)
}
publicKey := privateKey.PublicKey()
signature := bls.Sign(privateKey, hashedMessage)

// Verify the signature
if !bls.Verify(publicKey, hashedMessage, signature) {
b.logger.Error("failed to verify generated BLS signature")
return fmt.Errorf("failed to verify generated BLS signature")
return "", fmt.Errorf("failed to verify generated BLS signature")
}

pubkeyb, err := publicKey.MarshalBinary()
if err != nil {
b.logger.Error("failed to marshal public key", "error", err)
return fmt.Errorf("failed to marshal public key: %w", err)
return "", fmt.Errorf("failed to marshal public key: %w", err)
}
b.logger.Info("generated BLS key pair",
"public_key", hex.EncodeToString(pubkeyb),
Expand All @@ -161,13 +166,12 @@ func (b *ProviderClient) CheckAndStake(_ []string) error {
})
if err != nil {
b.logger.Error("failed to register stake", "err", err)
return err
return "", err
}

b.logger.Info("staked 10 ETH")

return nil

return hex.EncodeToString(pubkeyb), nil
}

func (b *ProviderClient) startSender() error {
Expand Down
14 changes: 3 additions & 11 deletions p2p/integrationtest/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"bytes"
"context"
crand "crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"flag"
Expand Down Expand Up @@ -162,19 +160,13 @@ func main() {
}
defer providerClient.Close()

blsPubKey := make([]byte, 48)
if _, err = crand.Read(blsPubKey); err != nil {
logger.Error("failed to generate BLS public key", "error", err)
return
}

payload := hex.EncodeToString(blsPubKey)
if err = providerClient.CheckAndStake([]string{payload}); err != nil {
pubKey, err := providerClient.CheckAndStake()
if err != nil {
logger.Error("failed to check and stake", "error", err)
return
}

body, err := json.Marshal([]string{payload})
body, err := json.Marshal([]string{pubKey})
if err != nil {
logger.Error("failed to marshal body", "error", err)
return
Expand Down
Loading