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

Revert BLS public key and signature in BlockProducerInfo #576

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
9 changes: 4 additions & 5 deletions lib/block_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package lib
import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/wire"
"github.com/tyler-smith/go-bip39"
"math"
"sync"
"sync/atomic"
"time"

"github.com/btcsuite/btcd/wire"
"github.com/tyler-smith/go-bip39"

"github.com/deso-protocol/go-deadlock"

"github.com/btcsuite/btcd/btcec"
Expand Down Expand Up @@ -577,8 +576,8 @@ func (desoBlockProducer *DeSoBlockProducer) SignBlock(blockFound *MsgDeSoBlock)
// If we get here, we now have a valid signature for the block.

// Embed the signature into the block.
blockFound.BlockProducerInfo = &MsgDeSoBlockProducerInfo{
PublicKey: NewPublicKey(desoBlockProducer.blockProducerPrivateKey.PubKey().SerializeCompressed()),
blockFound.BlockProducerInfo = &BlockProducerInfo{
PublicKey: desoBlockProducer.blockProducerPrivateKey.PubKey().SerializeCompressed(),
Signature: signature,
}

Expand Down
17 changes: 8 additions & 9 deletions lib/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"container/list"
"encoding/hex"
"fmt"
"github.com/holiman/uint256"
"math"
"math/big"
"reflect"
Expand All @@ -13,8 +14,6 @@ import (
"strings"
"time"

"github.com/holiman/uint256"

btcdchain "github.com/btcsuite/btcd/blockchain"
chainlib "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
Expand Down Expand Up @@ -1831,35 +1830,35 @@ func (bc *Blockchain) ProcessBlock(desoBlock *MsgDeSoBlock, verifySignatures boo
}

// Verify that the public key is in the allowed set.
if _, exists := bc.trustedBlockProducerPublicKeys[MakePkMapKey(publicKey.ToBytes())]; !exists {
if _, exists := bc.trustedBlockProducerPublicKeys[MakePkMapKey(publicKey)]; !exists {
return false, false, errors.Wrapf(RuleErrorBlockProducerPublicKeyNotInWhitelist,
"ProcessBlock: Block producer public key %v is not in the allowed list of "+
"--trusted_block_producer_public_keys: %v.", PkToStringBoth(publicKey.ToBytes()),
"--trusted_block_producer_public_keys: %v.", PkToStringBoth(publicKey),
bc.trustedBlockProducerPublicKeys)
}

// Verify that the public key has not been forbidden.
dbEntry := DbGetForbiddenBlockSignaturePubKey(bc.db, bc.snapshot, publicKey.ToBytes())
dbEntry := DbGetForbiddenBlockSignaturePubKey(bc.db, bc.snapshot, publicKey)
if dbEntry != nil {
return false, false, errors.Wrapf(RuleErrorForbiddenBlockProducerPublicKey,
"ProcessBlock: Block producer public key %v is forbidden", PkToStringBoth(publicKey.ToBytes()))
"ProcessBlock: Block producer public key %v is forbidden", PkToStringBoth(publicKey))
}

// At this point we are confident that we have a valid public key that is
// trusted.

signature := desoBlock.BlockProducerInfo.Signature
pkObj, err := btcec.ParsePubKey(publicKey.ToBytes(), btcec.S256())
pkObj, err := btcec.ParsePubKey(publicKey, btcec.S256())
if err != nil {
return false, false, errors.Wrapf(err,
"ProcessBlock: Error parsing block producer public key: %v.",
PkToStringBoth(publicKey.ToBytes()))
PkToStringBoth(publicKey))
}
if !signature.Verify(blockHash[:], pkObj) {
return false, false, errors.Wrapf(RuleErrorInvalidBlockProducerSIgnature,
"ProcessBlock: Error validating signature %v for public key %v: %v.",
hex.EncodeToString(signature.Serialize()),
PkToStringBoth(publicKey.ToBytes()),
PkToStringBoth(publicKey),
err)
}
}
Expand Down
13 changes: 6 additions & 7 deletions lib/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/hex"
"flag"
"fmt"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/go-pg/pg/v10"
"log"
"math/big"
"math/rand"
Expand All @@ -12,9 +14,6 @@ import (
"testing"
"time"

embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/go-pg/pg/v10"

chainlib "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/dgraph-io/badger/v3"
Expand Down Expand Up @@ -1652,20 +1651,20 @@ func TestBadBlockSignature(t *testing.T) {

// Since MineAndProcesssSingleBlock returns a valid block above, we can play with its
// signature and re-process the block to see what happens.
blockProducerInfoCopy := &MsgDeSoBlockProducerInfo{Signature: &btcec.Signature{}}
blockProducerInfoCopy.PublicKey = NewPublicKey(finalBlock1.BlockProducerInfo.PublicKey[:])
blockProducerInfoCopy := &BlockProducerInfo{Signature: &btcec.Signature{}}
blockProducerInfoCopy.PublicKey = append([]byte{}, finalBlock1.BlockProducerInfo.PublicKey...)
*blockProducerInfoCopy.Signature = *finalBlock1.BlockProducerInfo.Signature

// A bad signature with the right public key should fail.
finalBlock1.BlockProducerInfo.PublicKey = NewPublicKey(senderPkBytes)
finalBlock1.BlockProducerInfo.PublicKey = senderPkBytes
_, _, err = chain.ProcessBlock(finalBlock1, true)
require.Error(err)
require.Contains(err.Error(), RuleErrorInvalidBlockProducerSIgnature)

// A signature that's outright missing should fail
blockSignerPkBytes, _, err := Base58CheckDecode(blockSignerPk)
require.NoError(err)
finalBlock1.BlockProducerInfo.PublicKey = NewPublicKey(blockSignerPkBytes)
finalBlock1.BlockProducerInfo.PublicKey = blockSignerPkBytes
finalBlock1.BlockProducerInfo.Signature = nil
_, _, err = chain.ProcessBlock(finalBlock1, true)
require.Error(err)
Expand Down
16 changes: 0 additions & 16 deletions lib/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ const (
MsgValidatorTimeoutVersion0 MsgValidatorTimeoutVersion = 0
)

// Versioning for the BlockProducerInfo field included in MsgDeSoBlock. This type alias
// is equivalent to a uint8, and supports the same byte encoders/decoders.
type MsgDeSoBlockProducerInfoVersion = byte

const (
// This represents the original schema for the BlockProducerInfo field included in
// Proof of Work blocks. The original schema did not have versioning, so we use a default
// version value of 0 to denote this. The original schema only contains the block producer's
// ECDSA public key and ECDSA signature of the block.
MsgDeSoBlockProducerInfoVersion0 MsgDeSoBlockProducerInfoVersion = 0
// This version is introduced starting with Proof of Stake blocks. It adds versioning to the
// BlockProducerInfo schema, and adds two new fields for the block producer's BLS public key
// and BLS partial signature for the block.
MsgDeSoBlockProducerInfoVersion1 MsgDeSoBlockProducerInfoVersion = 1
)

var (
MaxUint256, _ = uint256.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")

Expand Down
Loading