*: migrate onto simplified dbft.PublicKey interface

Adopt https://github.com/nspcc-dev/dbft/pull/137.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2024-11-29 17:09:00 +03:00
parent a5cf1635d8
commit 9778fd88fb
9 changed files with 16 additions and 38 deletions

View file

@ -24,8 +24,8 @@ var _ dbft.Block[util.Uint256] = (*neoBlock)(nil)
// Sign implements the block.Block interface.
func (n *neoBlock) Sign(key dbft.PrivateKey) error {
k := key.(*privateKey)
sig := k.PrivateKey.SignHashable(uint32(n.network), &n.Block)
k := key.(*keys.PrivateKey)
sig := k.SignHashable(uint32(n.network), &n.Block)
n.signature = sig
return nil
}

View file

@ -15,7 +15,7 @@ func TestNeoBlock_Sign(t *testing.T) {
b := new(neoBlock)
priv, _ := keys.NewPrivateKey()
require.NoError(t, b.Sign(&privateKey{PrivateKey: priv}))
require.NoError(t, b.Sign(priv))
require.NoError(t, b.Verify(priv.PublicKey(), b.Signature()))
}

View file

@ -449,7 +449,7 @@ func (s *service) getKeyPair(pubs []dbft.PublicKey) (int, dbft.PrivateKey, dbft.
}
}
return i, &privateKey{PrivateKey: acc.PrivateKey()}, acc.PublicKey()
return i, acc.PrivateKey(), acc.PublicKey()
}
}
return -1, nil, nil
@ -495,7 +495,7 @@ func (s *service) OnTransaction(tx *transaction.Transaction) {
}
func (s *service) broadcast(p dbft.ConsensusPayload[util.Uint256]) {
if err := p.(*Payload).Sign(s.dbft.Priv.(*privateKey)); err != nil {
if err := p.(*Payload).Sign(s.dbft.Priv.(*keys.PrivateKey)); err != nil {
s.log.Warn("can't sign consensus payload", zap.Error(err))
}

View file

@ -532,9 +532,9 @@ func (bq testBlockQueuer) PutBlock(b *coreb.Block) error {
return bq.bc.AddBlock(b)
}
func getTestValidator(i int) (*privateKey, *keys.PublicKey) {
func getTestValidator(i int) (*keys.PrivateKey, *keys.PublicKey) {
key := testchain.PrivateKey(i)
return &privateKey{PrivateKey: key}, key.PublicKey()
return key, key.PublicKey()
}
func newSingleTestChain(t *testing.T) *core.Blockchain {

View file

@ -1,19 +0,0 @@
package consensus
import (
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
)
// privateKey is a wrapper around keys.PrivateKey
// which implements the crypto.PrivateKey interface.
type privateKey struct {
*keys.PrivateKey
}
var _ dbft.PrivateKey = &privateKey{}
// Sign implements the dbft's crypto.PrivateKey interface.
func (p *privateKey) Sign(data []byte) ([]byte, error) {
return p.PrivateKey.Sign(data), nil
}

View file

@ -9,18 +9,15 @@ import (
)
func TestCrypt(t *testing.T) {
key, err := keys.NewPrivateKey()
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
priv := privateKey{key}
pub := key.PublicKey()
pub := priv.PublicKey()
data := []byte{1, 2, 3, 4}
hash := sha256.Sum256(data)
sign, err := priv.Sign(data)
require.NoError(t, err)
sign := priv.Sign(data)
require.True(t, pub.Verify(sign, hash[:]))
sign[0] = ^sign[0]

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -119,9 +120,9 @@ func (p *Payload) EncodeBinary(w *io.BinWriter) {
// Sign signs payload using the private key.
// It also sets corresponding verification and invocation scripts.
func (p *Payload) Sign(key *privateKey) error {
func (p *Payload) Sign(key *keys.PrivateKey) error {
p.encodeData()
sig := key.PrivateKey.SignHashable(uint32(p.network), &p.Extensible)
sig := key.SignHashable(uint32(p.network), &p.Extensible)
buf := io.NewBufBinWriter()
emit.Bytes(buf.BinWriter, sig)

View file

@ -249,11 +249,9 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage {
}
func TestPayload_Sign(t *testing.T) {
key, err := keys.NewPrivateKey()
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
priv := &privateKey{key}
p := randomPayload(t, prepareRequestType)
h := priv.PublicKey().GetScriptHash()
bc := newTestChain(t, false)

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
@ -21,7 +22,7 @@ func TestRecoveryMessageSetters(t *testing.T) {
func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
srv := newTestServiceWithState(t, enableStateRoot)
privs := make([]*privateKey, testchain.Size())
privs := make([]*keys.PrivateKey, testchain.Size())
pubs := make([]dbft.PublicKey, testchain.Size())
for i := range testchain.Size() {
privs[i], pubs[i] = getTestValidator(i)