Skip to content

Commit

Permalink
Merge pull request #137 from nspcc-dev/remove-sign
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov authored Nov 29, 2024
2 parents ba74133 + 4e0d5cf commit 55c4e10
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Behaviour changes:
* (*DBFT).Header() and (*DBFT).PreHeader() are moved to (*Context) receiver (#133)
* support error handling for ProcessBlock callback if anti-MEV extension is enabled
(#134)
* remove Sign method from PrivateKey interface (#137)

Improvements:
* minimum required Go version is 1.22 (#122, #126)
Expand Down
11 changes: 6 additions & 5 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ type (
// PublicKey is a generic public key interface used by dbft.
PublicKey any

// PrivateKey is a generic private key interface used by dbft.
PrivateKey interface {
// Sign returns msg's signature and error on failure.
Sign(msg []byte) (sig []byte, err error)
}
// PrivateKey is a generic private key interface used by dbft. PrivateKey is used
// only by [PreBlock] and [Block] signing callbacks ([PreBlock.SetData] and
// [Block.Sign]) to grant access to the private key abstraction to Block and
// PreBlock signing code. PrivateKey does not contain any methods, hence user
// supposed to perform type assertion before the PrivateKey usage.
PrivateKey any

// Hash is a generic hash interface used by dbft for payloads, blocks and
// transactions identification. It is recommended to implement this interface
Expand Down
4 changes: 3 additions & 1 deletion internal/consensus/amev_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type amevBlock struct {
hash *crypto.Uint256
}

var _ dbft.Block[crypto.Uint256] = new(amevBlock)

// NewAMEVBlock returns new block based on PreBlock and additional Commit-level data
// collected from M consensus nodes.
func NewAMEVBlock(pre dbft.PreBlock[crypto.Uint256], cnData [][]byte, m int) dbft.Block[crypto.Uint256] {
Expand Down Expand Up @@ -95,7 +97,7 @@ func (b *amevBlock) GetHashData() []byte {
func (b *amevBlock) Sign(key dbft.PrivateKey) error {
data := b.GetHashData()

sign, err := key.Sign(data)
sign, err := key.(*crypto.ECDSAPriv).Sign(data)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion internal/consensus/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ type (
signature []byte
hash *crypto.Uint256
}

// signable is an interface used within consensus package to abstract private key
// functionality. This interface is used instead of direct structure usage to be
// able to mock private key implementation in unit tests.
signable interface {
Sign([]byte) ([]byte, error)
}
)

var _ dbft.Block[crypto.Uint256] = new(neoBlock)

// PrevHash implements Block interface.
func (b *neoBlock) PrevHash() crypto.Uint256 {
return b.base.PrevHash
Expand Down Expand Up @@ -101,7 +110,7 @@ func (b *neoBlock) GetHashData() []byte {
func (b *neoBlock) Sign(key dbft.PrivateKey) error {
data := b.GetHashData()

sign, err := key.Sign(data)
sign, err := key.(signable).Sign(data)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/consensus/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestNeoBlock_Setters(t *testing.T) {

type testKey struct{}

var _ signable = testKey{}

func (t testKey) MarshalBinary() ([]byte, error) { return []byte{}, nil }
func (t testKey) UnmarshalBinary([]byte) error { return nil }
func (t testKey) Sign([]byte) ([]byte, error) {
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 @@ -15,7 +15,7 @@ func TestVerifySignature(t *testing.T) {
_, err := rand.Reader.Read(data)
require.NoError(t, err)

sign, err := priv.Sign(data)
sign, err := priv.(*ECDSAPriv).Sign(data)
require.NoError(t, err)
require.Equal(t, 64, len(sign))

Expand Down

0 comments on commit 55c4e10

Please sign in to comment.