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

Simplify library interfaces a bit #114

Merged
merged 4 commits into from
Apr 24, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This document outlines major changes between releases.
New features:

Behaviour changes:
* simplify PublicKey interface (#114)
* remove WithKeyPair callback from dBFT (#114)

Improvements:

Expand Down
25 changes: 0 additions & 25 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dbft

import (
"bytes"
"errors"
"time"

Expand Down Expand Up @@ -137,30 +136,6 @@ func checkConfig[H Hash](cfg *Config[H]) error {
return nil
}

// WithKeyPair sets GetKeyPair to a function returning default key pair
// if it is present in a list of validators.
func WithKeyPair[H Hash](priv PrivateKey, pub PublicKey) func(config *Config[H]) {
myPub, err := pub.MarshalBinary()
if err != nil {
return nil
}

return func(cfg *Config[H]) {
cfg.GetKeyPair = func(ps []PublicKey) (int, PrivateKey, PublicKey) {
for i := range ps {
pi, err := ps[i].MarshalBinary()
if err != nil {
continue
} else if bytes.Equal(myPub, pi) {
return i, priv, pub
}
}

return -1, nil, nil
}
}
}

// WithGetKeyPair sets GetKeyPair.
func WithGetKeyPair[H Hash](f func(pubs []PublicKey) (int, PrivateKey, PublicKey)) func(config *Config[H]) {
return func(cfg *Config[H]) {
Expand Down
8 changes: 6 additions & 2 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ func TestDBFT_Invalid(t *testing.T) {
require.NotNil(t, priv)
require.NotNil(t, pub)

opts := []func(*dbft.Config[crypto.Uint256]){dbft.WithKeyPair[crypto.Uint256](priv, pub)}
opts := []func(*dbft.Config[crypto.Uint256]){dbft.WithGetKeyPair[crypto.Uint256](func(_ []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
return -1, nil, nil
})}
t.Run("without Timer", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
Expand Down Expand Up @@ -829,7 +831,9 @@ func (s *testState) getOptions() []func(*dbft.Config[crypto.Uint256]) {
dbft.WithCurrentHeight[crypto.Uint256](func() uint32 { return s.currHeight }),
dbft.WithCurrentBlockHash[crypto.Uint256](func() crypto.Uint256 { return s.currHash }),
dbft.WithGetValidators[crypto.Uint256](func(...dbft.Transaction[crypto.Uint256]) []dbft.PublicKey { return s.pubs }),
dbft.WithKeyPair[crypto.Uint256](s.privs[s.myIndex], s.pubs[s.myIndex]),
dbft.WithGetKeyPair[crypto.Uint256](func(_ []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
return s.myIndex, s.privs[s.myIndex], s.pubs[s.myIndex]
}),
dbft.WithBroadcast[crypto.Uint256](func(p Payload) { s.ch = append(s.ch, p) }),
dbft.WithGetTx[crypto.Uint256](s.pool.Get),
dbft.WithProcessBlock[crypto.Uint256](func(b dbft.Block[crypto.Uint256]) { s.blocks = append(s.blocks, b) }),
Expand Down
9 changes: 1 addition & 8 deletions identity.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package dbft

import (
"encoding"
"fmt"
)

type (
// PublicKey is a generic public key interface used by dbft.
PublicKey interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler

// Verify verifies if sig is indeed msg's signature.
Verify(msg, sig []byte) error
}
PublicKey any
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved

// PrivateKey is a generic private key interface used by dbft.
PrivateKey interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/consensus/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (b *neoBlock) Sign(key dbft.PrivateKey) error {
// Verify implements Block interface.
func (b *neoBlock) Verify(pub dbft.PublicKey, sign []byte) error {
data := b.GetHashData()
return pub.Verify(data, sign)
return pub.(*crypto.ECDSAPub).Verify(data, sign)
}

// Hash implements Block interface.
Expand Down
10 changes: 9 additions & 1 deletion internal/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
dbft.WithTimer[crypto.Uint256](timer.New()),
dbft.WithLogger[crypto.Uint256](logger),
dbft.WithSecondsPerBlock[crypto.Uint256](time.Second*5),
dbft.WithKeyPair[crypto.Uint256](key, pub),
dbft.WithGetKeyPair[crypto.Uint256](func(pubs []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
for i := range pubs {
if pub.(*crypto.ECDSAPub).Equals(pubs[i]) {
return i, key, pub

Check warning on line 28 in internal/consensus/consensus.go

View check run for this annotation

Codecov / codecov/patch

internal/consensus/consensus.go#L25-L28

Added lines #L25 - L28 were not covered by tests
}
}

return -1, nil, nil

Check warning on line 32 in internal/consensus/consensus.go

View check run for this annotation

Codecov / codecov/patch

internal/consensus/consensus.go#L32

Added line #L32 was not covered by tests
}),
dbft.WithGetTx[crypto.Uint256](getTx),
dbft.WithGetVerified[crypto.Uint256](getVerified),
dbft.WithBroadcast[crypto.Uint256](broadcast),
Expand Down
2 changes: 1 addition & 1 deletion internal/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestVerifySignature(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 64, len(sign))

err = pub.Verify(data, sign)
err = pub.(*ECDSAPub).Verify(data, sign)
require.NoError(t, err)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
return sig, nil
}

// Equals implements dbft.PublicKey interface.
func (e *ECDSAPub) Equals(other dbft.PublicKey) bool {
return e.Equal(other.(*ECDSAPub).PublicKey)

Check warning on line 67 in internal/crypto/ecdsa.go

View check run for this annotation

Codecov / codecov/patch

internal/crypto/ecdsa.go#L66-L67

Added lines #L66 - L67 were not covered by tests
}

// MarshalBinary implements encoding.BinaryMarshaler interface.
func (e ECDSAPub) MarshalBinary() ([]byte, error) {
return elliptic.MarshalCompressed(e.PublicKey.Curve, e.PublicKey.X, e.PublicKey.Y), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/crypto/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestECDSA_MarshalUnmarshal(t *testing.T) {
require.NotNil(t, priv)
require.NotNil(t, pub)

data, err := pub.MarshalBinary()
data, err := pub.(*ECDSAPub).MarshalBinary()
require.NoError(t, err)

pub1 := new(ECDSAPub)
Expand Down
4 changes: 2 additions & 2 deletions internal/simulation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@

func sortValidators(pubs []dbft.PublicKey) {
sort.Slice(pubs, func(i, j int) bool {
p1, _ := pubs[i].MarshalBinary()
p2, _ := pubs[j].MarshalBinary()
p1, _ := pubs[i].(*crypto.ECDSAPub).MarshalBinary()
p2, _ := pubs[j].(*crypto.ECDSAPub).MarshalBinary()

Check warning on line 159 in internal/simulation/main.go

View check run for this annotation

Codecov / codecov/patch

internal/simulation/main.go#L158-L159

Added lines #L158 - L159 were not covered by tests
return murmur3.Sum64(p1) < murmur3.Sum64(p2)
})
}
Expand Down
Loading