Skip to content

Commit

Permalink
*: migrate onto simplified dbft.PublicKey interface
Browse files Browse the repository at this point in the history
Fetch nspcc-dev/dbft#137.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Nov 29, 2024
1 parent 7dce920 commit 8fe8f4f
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 41 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/holiman/uint256 v1.3.1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/dbft v0.3.1-0.20241128141448-ba7413350d42
github.com/nspcc-dev/dbft v0.3.1-0.20241129135622-4e0d5cf4fe52
github.com/nspcc-dev/go-ordered-json v0.0.0-20240830112754-291b000d1f3b
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20240727093519-1a48f1ce43ec
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nspcc-dev/dbft v0.3.1-0.20241128141448-ba7413350d42 h1:33354ES9GS2/PQsPWFUP0RzfX/hDkwfHQztIiV0h8fE=
github.com/nspcc-dev/dbft v0.3.1-0.20241128141448-ba7413350d42/go.mod h1:BNvJkPKTE28r+qRaAk2C3VoL2J9qzox3fvEeJbh7EWE=
github.com/nspcc-dev/dbft v0.3.1-0.20241129135622-4e0d5cf4fe52 h1:gTpVXwV+iWVNu6CmzyFDgIt8TQlbHR7dFFB1ocaLwc4=
github.com/nspcc-dev/dbft v0.3.1-0.20241129135622-4e0d5cf4fe52/go.mod h1:BNvJkPKTE28r+qRaAk2C3VoL2J9qzox3fvEeJbh7EWE=
github.com/nspcc-dev/go-ordered-json v0.0.0-20240830112754-291b000d1f3b h1:DRG4cRqIOmI/nUPggMgR92Jxt63Lxsuz40m5QpdvYXI=
github.com/nspcc-dev/go-ordered-json v0.0.0-20240830112754-291b000d1f3b/go.mod h1:d3cUseu4Asxfo9/QA/w4TtGjM0AbC9ynyab+PfH+Bso=
github.com/nspcc-dev/hrw/v2 v2.0.1 h1:CxYUkBeJvNfMEn2lHhrV6FjY8pZPceSxXUtMVq0BUOU=
Expand Down
4 changes: 2 additions & 2 deletions pkg/consensus/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/consensus/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 0 additions & 19 deletions pkg/consensus/crypto.go

This file was deleted.

9 changes: 3 additions & 6 deletions pkg/consensus/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions pkg/consensus/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions pkg/consensus/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/consensus/recovery_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down

0 comments on commit 8fe8f4f

Please sign in to comment.