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

Use btcec v2 lib for ECDSA keys manipulation #105

Merged
merged 4 commits into from
Feb 12, 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
40 changes: 23 additions & 17 deletions consensus/polybft/block_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package polybft

import (
"crypto/ecdsa"
"math/big"
"testing"
"time"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/state"
Expand All @@ -29,10 +29,21 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
chainID = 100
)

accounts := [6]*wallet.Account{}
type account struct {
privKey *ecdsa.PrivateKey
address types.Address
}

accounts := [6]*account{}

for i := range accounts {
accounts[i] = generateTestAccount(t)
ecdsaKey, err := crypto.GenerateECDSAKey()
require.NoError(t, err)

accounts[i] = &account{
privKey: ecdsaKey,
address: crypto.PubKeyToAddress(&ecdsaKey.PublicKey),
}
}

forks := &chain.Forks{}
Expand Down Expand Up @@ -60,9 +71,7 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
for i, acc := range accounts {
// the third tx will fail because of insufficient balance
if i != 2 {
balanceMap[types.Address(acc.Ecdsa.Address())] = &chain.GenesisAccount{
Balance: ethgo.Ether(1),
}
balanceMap[acc.address] = &chain.GenesisAccount{Balance: ethgo.Ether(1)}
}
}

Expand All @@ -72,17 +81,12 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
require.NotEqual(t, types.ZeroHash, hash)

// Gas Limit is important to be high for tx pool
parentHeader := &types.Header{StateRoot: hash, GasLimit: 1_000_000_000_000_000}
parentHeader := &types.Header{StateRoot: hash, GasLimit: 1e15}

txPool := &txPoolMock{}
txPool.On("Prepare").Once()

for i, acc := range accounts {
receiver := types.Address(acc.Ecdsa.Address())
privateKey, err := acc.GetEcdsaPrivateKey()

require.NoError(t, err)

gas := uint64(gasLimit)
// fifth tx will cause filling to stop
if i == 4 {
Expand All @@ -94,10 +98,10 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
GasPrice: big.NewInt(gasPrice),
Gas: gas,
Nonce: 0,
To: &receiver,
To: &acc.address,
})

tx, err = signer.SignTx(tx, privateKey)
tx, err = signer.SignTx(tx, acc.privKey)
require.NoError(t, err)

// all tx until the fifth will be retrieved from the pool
Expand Down Expand Up @@ -147,15 +151,17 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T)
require.Len(t, bb.txns, 3, "Should have 3 transactions but has %d", len(bb.txns))
require.Len(t, bb.Receipts(), 3)

logsBloom := fb.Block.Header.LogsBloom

// assert logs bloom
for _, r := range bb.Receipts() {
for _, l := range r.Logs {
assert.True(t, fb.Block.Header.LogsBloom.IsLogInBloom(l))
assert.True(t, logsBloom.IsLogInBloom(l))
}
}

assert.False(t, fb.Block.Header.LogsBloom.IsLogInBloom(
assert.False(t, logsBloom.IsLogInBloom(
&types.Log{Address: types.StringToAddress("999911117777")}))
assert.False(t, fb.Block.Header.LogsBloom.IsLogInBloom(
assert.False(t, logsBloom.IsLogInBloom(
&types.Log{Address: types.StringToAddress("111177779999")}))
}
1 change: 0 additions & 1 deletion consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ func (c *consensusRuntime) initStakeManager(logger hcf.Logger, dbTx *bolt.Tx) er
c.stakeManager, err = newStakeManager(
logger.Named("stake-manager"),
c.state,
wallet.NewEcdsaSigner(c.config.Key),
contracts.StakeManagerContract,
c.config.blockchain,
c.config.polybftBackend,
Expand Down
2 changes: 0 additions & 2 deletions consensus/polybft/stake_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ type stakeManager struct {
func newStakeManager(
logger hclog.Logger,
state *State,
key ethgo.Key,
stakeManagerAddr types.Address,
blockchain blockchainBackend,
polybftBackend polybftBackend,
Expand All @@ -88,7 +87,6 @@ func newStakeManager(
sm := &stakeManager{
logger: logger,
state: state,
key: key,
stakeManagerContractAddr: stakeManagerAddr,
polybftBackend: polybftBackend,
blockchain: blockchain,
Expand Down
3 changes: 0 additions & 3 deletions consensus/polybft/stake_manager_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -111,7 +110,6 @@ func FuzzTestStakeManagerPostBlock(f *testing.F) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0002"),
bcMock,
nil,
Expand Down Expand Up @@ -154,7 +152,6 @@ func FuzzTestStakeManagerUpdateValidatorSet(f *testing.F) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"),
bcMock,
nil,
Expand Down
6 changes: 0 additions & 6 deletions consensus/polybft/stake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -52,7 +51,6 @@ func TestStakeManager_PostBlock(t *testing.T) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
stakeManagerAddr,
bcMock,
nil,
Expand Down Expand Up @@ -108,7 +106,6 @@ func TestStakeManager_PostBlock(t *testing.T) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"),
bcMock,
nil,
Expand Down Expand Up @@ -173,7 +170,6 @@ func TestStakeManager_PostBlock(t *testing.T) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"),
bcMock,
nil,
Expand Down Expand Up @@ -231,7 +227,6 @@ func TestStakeManager_UpdateValidatorSet(t *testing.T) {
stakeManager, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
types.StringToAddress("0x0001"),
bcMock,
nil,
Expand Down Expand Up @@ -420,7 +415,6 @@ func TestStakeManager_UpdateOnInit(t *testing.T) {
_, err := newStakeManager(
hclog.NewNullLogger(),
state,
wallet.NewEcdsaSigner(validators.GetValidator("A").Key()),
stakeManagerAddr,
nil,
polyBackendMock,
Expand Down
3 changes: 2 additions & 1 deletion consensus/polybft/wallet/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/hex"
"fmt"

"github.com/umbracle/ethgo/wallet"

"github.com/0xPolygon/polygon-edge/bls"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo/wallet"
)

// Account is an account for key signatures
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/wallet/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (k *Key) SignIBFTMessage(msg *proto.Message) (*proto.Message, error) {
// RecoverAddressFromSignature calculates keccak256 hash of provided rawContent
// and recovers signer address from given signature and hash
func RecoverAddressFromSignature(sig, rawContent []byte) (types.Address, error) {
pub, err := crypto.RecoverPubkey(sig, crypto.Keccak256(rawContent))
pub, err := crypto.RecoverPubKey(sig, crypto.Keccak256(rawContent))
if err != nil {
return types.Address{}, fmt.Errorf("cannot recover address from signature: %w", err)
}
Expand Down
Loading
Loading