Skip to content

Commit

Permalink
Merge 64d7dbb into 837280a
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel authored Apr 8, 2020
2 parents 837280a + 64d7dbb commit 2d681ba
Show file tree
Hide file tree
Showing 27 changed files with 1,517 additions and 98 deletions.
2 changes: 1 addition & 1 deletion blockchain/skipchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (cosi fakeCosi) GetPublicKeyFactory() crypto.PublicKeyFactory {
return fakePublicKeyFactory{err: cosi.err}
}

func (cosi fakeCosi) GetVerifier(cosi.CollectiveAuthority) (crypto.Verifier, error) {
func (cosi fakeCosi) GetVerifier(crypto.CollectiveAuthority) (crypto.Verifier, error) {
return fakeVerifier{}, cosi.err
}

Expand Down
15 changes: 13 additions & 2 deletions blockchain/skipchain/conode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"

proto "github.com/golang/protobuf/proto"
"go.dedis.ch/fabric/cosi"
"go.dedis.ch/fabric/crypto"
"go.dedis.ch/fabric/encoding"
"go.dedis.ch/fabric/mino"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (i *publicKeyIterator) GetNext() crypto.PublicKey {
// - implements encoding.Packable
type Conodes []Conode

func newConodes(ca cosi.CollectiveAuthority) Conodes {
func newConodes(ca crypto.CollectiveAuthority) Conodes {
conodes := make([]Conode, ca.Len())
addrIter := ca.AddressIterator()
publicKeyIter := ca.PublicKeyIterator()
Expand Down Expand Up @@ -136,6 +135,18 @@ func (cc Conodes) Len() int {
return len(cc)
}

// GetPublicKey implements cosi.CollectiveAuthority. It returns the public key
// associated with the address and its index.
func (cc Conodes) GetPublicKey(addr mino.Address) (crypto.PublicKey, int) {
for i, conode := range cc {
if conode.GetAddress().Equal(addr) {
return conode.GetPublicKey(), i
}
}

return nil, -1
}

// AddressIterator implements mino.Players. It returns the address iterator.
func (cc Conodes) AddressIterator() mino.AddressIterator {
return &addressIterator{
Expand Down
12 changes: 12 additions & 0 deletions blockchain/skipchain/conode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func TestConodes_Len(t *testing.T) {
require.Equal(t, 0, conodes.Len())
}

func TestConodes_GetPublicKey(t *testing.T) {
conodes := Conodes{randomConode(), randomConode(), randomConode()}

pubkey, index := conodes.GetPublicKey(conodes[1].GetAddress())
require.Equal(t, 1, index)
require.Equal(t, conodes[1].GetPublicKey(), pubkey)

pubkey, index = conodes.GetPublicKey(fakeAddress{})
require.Equal(t, -1, index)
require.Nil(t, pubkey)
}

func TestConodes_AddressIterator(t *testing.T) {
conodes := Conodes{randomConode(), randomConode()}
iter := conodes.AddressIterator()
Expand Down
4 changes: 2 additions & 2 deletions blockchain/skipchain/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type skipchainActor struct {
// InitChain implements blockchain.Actor. It creates a genesis block if none
// exists and propagate it to the conodes.
func (a skipchainActor) InitChain(data proto.Message, players mino.Players) error {
ca, ok := players.(cosi.CollectiveAuthority)
ca, ok := players.(crypto.CollectiveAuthority)
if !ok {
return xerrors.New("players must implement cosi.CollectiveAuthority")
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func (a skipchainActor) newChain(data proto.Message, conodes Conodes) error {
func (a skipchainActor) Store(data proto.Message, players mino.Players) error {
factory := a.GetBlockFactory().(blockFactory)

ca, ok := players.(cosi.CollectiveAuthority)
ca, ok := players.(crypto.CollectiveAuthority)
if !ok {
return xerrors.Errorf("players must implement cosi.CollectiveAuthority")
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/skipchain/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
"go.dedis.ch/fabric/blockchain"
"go.dedis.ch/fabric/consensus"
"go.dedis.ch/fabric/cosi/flatcosi"
"go.dedis.ch/fabric/cosi/threshold"
"go.dedis.ch/fabric/crypto"
"go.dedis.ch/fabric/crypto/bls"
"go.dedis.ch/fabric/encoding"
Expand Down Expand Up @@ -292,7 +292,7 @@ func makeSkipchain(t *testing.T, id string, manager *minoch.Manager) (Conode, *S
publicKey: signer.GetPublicKey(),
}

cosi := flatcosi.NewFlat(mino, signer)
cosi := threshold.NewCoSi(mino, signer)
skipchain := NewSkipchain(mino, cosi)

actor, err := skipchain.Listen(testValidator{})
Expand Down
2 changes: 1 addition & 1 deletion consensus/cosipbft/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (a pbftActor) Propose(p consensus.Proposal, players mino.Players) error {
return xerrors.Errorf("couldn't create prepare request: %v", err)
}

ca, ok := players.(cosi.CollectiveAuthority)
ca, ok := players.(crypto.CollectiveAuthority)
if !ok {
return xerrors.Errorf("%T should implement cosi.CollectiveAuthority", players)
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/cosipbft/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type badCosiActor struct {
}

func (cs *badCosiActor) Sign(ctx context.Context, pb cosi.Message,
ca cosi.CollectiveAuthority) (crypto.Signature, error) {
ca crypto.CollectiveAuthority) (crypto.Signature, error) {

if cs.delay > 0 {
cs.delay--
Expand Down Expand Up @@ -454,7 +454,7 @@ func (i *fakePKIterator) GetNext() crypto.PublicKey {
}

type fakeCA struct {
cosi.CollectiveAuthority
crypto.CollectiveAuthority
addrs []mino.Address
pubkeys []crypto.PublicKey
}
Expand Down Expand Up @@ -515,7 +515,7 @@ type fakeCosiActor struct {
}

func (a *fakeCosiActor) Sign(ctx context.Context, msg cosi.Message,
ca cosi.CollectiveAuthority) (crypto.Signature, error) {
ca crypto.CollectiveAuthority) (crypto.Signature, error) {

packed, err := msg.Pack(encoding.NewProtoEncoder())
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cosi/flatcosi/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func (cosi *Flat) GetSignatureFactory() crypto.SignatureFactory {

// GetVerifier returns a verifier that can be used to verify signatures
// from this collective authority.
func (cosi *Flat) GetVerifier(ca cosi.CollectiveAuthority) (crypto.Verifier, error) {
func (cosi *Flat) GetVerifier(ca crypto.CollectiveAuthority) (crypto.Verifier, error) {
if ca == nil {
return nil, xerrors.New("collective authority is nil")
}

verifier, err := cosi.signer.GetVerifierFactory().FromIterator(ca.PublicKeyIterator())
verifier, err := cosi.signer.GetVerifierFactory().FromAuthority(ca)
if err != nil {
return nil, xerrors.Errorf("couldn't create verifier: %v", err)
}
Expand Down Expand Up @@ -87,14 +87,14 @@ type flatActor struct {

// Sign returns the collective signature of the block.
func (a flatActor) Sign(ctx context.Context, msg cosi.Message,
ca cosi.CollectiveAuthority) (crypto.Signature, error) {
ca crypto.CollectiveAuthority) (crypto.Signature, error) {

data, err := a.encoder.PackAny(msg)
if err != nil {
return nil, xerrors.Errorf("couldn't pack message: %v", err)
}

verifier, err := a.signer.GetVerifierFactory().FromIterator(ca.PublicKeyIterator())
verifier, err := a.signer.GetVerifierFactory().FromAuthority(ca)
if err != nil {
return nil, xerrors.Errorf("couldn't make verifier: %v", err)
}
Expand Down
70 changes: 26 additions & 44 deletions cosi/flatcosi/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.dedis.ch/fabric/crypto/bls"
"go.dedis.ch/fabric/encoding"
internal "go.dedis.ch/fabric/internal/testing"
"go.dedis.ch/fabric/internal/testing/fake"
"go.dedis.ch/fabric/mino"
"golang.org/x/xerrors"
)
Expand All @@ -32,31 +33,32 @@ func TestMessages(t *testing.T) {
}

func TestFlat_GetPublicKeyFactory(t *testing.T) {
signer := bls.NewSigner()
signer := fake.NewSigner()
flat := NewFlat(nil, signer)
require.NotNil(t, flat.GetPublicKeyFactory())
}

func TestFlat_GetSignatureFactory(t *testing.T) {
signer := bls.NewSigner()
signer := fake.NewSigner()
flat := NewFlat(nil, signer)
require.NotNil(t, flat.GetSignatureFactory())
}

func TestFlat_GetVerifier(t *testing.T) {
signer := bls.NewSigner()
ca := fake.NewAuthority(2, fake.NewSigner)
signer := fake.NewSigner()
flat := NewFlat(nil, signer)

_, err := flat.GetVerifier(fakeCollectiveAuthority{})
_, err := flat.GetVerifier(ca)
require.NoError(t, err)

verifier, err := flat.GetVerifier(nil)
require.Error(t, err)
require.Nil(t, verifier)

flat.signer = fakeSigner{}
_, err = flat.GetVerifier(fakeCollectiveAuthority{})
require.EqualError(t, err, "couldn't create verifier: oops")
flat.signer = fake.NewSignerWithVerifierFactory(fake.NewBadVerifierFactory())
_, err = flat.GetVerifier(ca)
require.EqualError(t, err, "couldn't create verifier: fake error")
}

func TestFlat_Listen(t *testing.T) {
Expand All @@ -75,16 +77,16 @@ func TestFlat_Listen(t *testing.T) {

func TestActor_Sign(t *testing.T) {
message := fakeMessage{}
ca := fake.NewAuthority(1, fake.NewSigner)

msgs := make(chan proto.Message, 2)
actor := flatActor{
encoder: encoding.NewProtoEncoder(),
signer: bls.NewSigner(),
signer: fake.NewSigner(),
rpc: fakeRPC{msgs: msgs},
}

sigAny := makePackedSignature(t, actor.signer, message.GetHash())
ca := fakeCollectiveAuthority{pubkey: actor.signer.GetPublicKey()}

msgs <- &SignatureResponse{Signature: sigAny}
msgs <- &SignatureResponse{Signature: sigAny}
Expand All @@ -101,42 +103,44 @@ func TestActor_Sign(t *testing.T) {
require.EqualError(t, err, "couldn't pack message: oops")

actor.encoder = encoding.NewProtoEncoder()
actor.signer = fakeSigner{}
actor.signer = fake.NewSignerWithVerifierFactory(fake.NewBadVerifierFactory())
_, err = actor.Sign(ctx, message, ca)
require.EqualError(t, err, "couldn't make verifier: oops")
require.EqualError(t, err, "couldn't make verifier: fake error")
}

func TestActor_SignWrongSignature(t *testing.T) {
message := fakeMessage{}
ca := fake.NewAuthority(1, bls.NewSigner)

msgs := make(chan proto.Message, 1)
actor := flatActor{
encoder: encoding.NewProtoEncoder(),
signer: bls.NewSigner(),
signer: ca.GetSigner(0),
rpc: fakeRPC{msgs: msgs},
}

sigAny := makePackedSignature(t, actor.signer, message.GetHash())
sigAny := makePackedSignature(t, ca.GetSigner(0), []byte{0xef})

msgs <- &SignatureResponse{Signature: sigAny}
close(msgs)

ctx := context.Background()

_, err := actor.Sign(ctx, message, fakeCollectiveAuthority{})
_, err := actor.Sign(ctx, message, ca)
require.EqualError(t, err, "couldn't verify the aggregation: bls: invalid signature")
}

func TestActor_RPCError_Sign(t *testing.T) {
buffer := new(bytes.Buffer)
message := fakeMessage{}
ca := fake.NewAuthority(1, fake.NewSigner)

msgs := make(chan proto.Message)
errs := make(chan error)
actor := flatActor{
encoder: encoding.NewProtoEncoder(),
logger: zerolog.New(buffer),
signer: bls.NewSigner(),
signer: ca.GetSigner(0),
rpc: fakeRPC{msgs: msgs, errs: errs},
}

Expand All @@ -147,7 +151,7 @@ func TestActor_RPCError_Sign(t *testing.T) {

ctx := context.Background()

sig, err := actor.Sign(ctx, message, fakeCollectiveAuthority{})
sig, err := actor.Sign(ctx, message, ca)
require.EqualError(t, err, "signature is nil")
require.Nil(t, sig)
require.Contains(t, buffer.String(), "error during collective signing")
Expand All @@ -156,20 +160,21 @@ func TestActor_RPCError_Sign(t *testing.T) {
func TestActor_Context_Sign(t *testing.T) {
buffer := new(bytes.Buffer)
message := fakeMessage{}
ca := fake.NewAuthority(1, fake.NewSigner)

msgs := make(chan proto.Message)
errs := make(chan error)
actor := flatActor{
encoder: encoding.NewProtoEncoder(),
logger: zerolog.New(buffer),
signer: bls.NewSigner(),
signer: ca.GetSigner(0),
rpc: fakeRPC{msgs: msgs, errs: errs},
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

sig, err := actor.Sign(ctx, message, fakeCollectiveAuthority{})
sig, err := actor.Sign(ctx, message, ca)
require.EqualError(t, err, "signature is nil")
require.Nil(t, sig)
require.Contains(t, buffer.String(), "error during collective signing")
Expand All @@ -178,12 +183,13 @@ func TestActor_Context_Sign(t *testing.T) {
func TestActor_SignProcessError(t *testing.T) {
buffer := new(bytes.Buffer)
message := fakeMessage{}
ca := fake.NewAuthority(1, fake.NewSigner)

msgs := make(chan proto.Message, 1)
actor := flatActor{
encoder: encoding.NewProtoEncoder(),
logger: zerolog.New(buffer),
signer: bls.NewSigner(),
signer: ca.GetSigner(0),
rpc: fakeRPC{msgs: msgs},
}

Expand All @@ -192,7 +198,7 @@ func TestActor_SignProcessError(t *testing.T) {

ctx := context.Background()

_, err := actor.Sign(ctx, message, fakeCollectiveAuthority{})
_, err := actor.Sign(ctx, message, ca)
require.EqualError(t, err, "signature is nil")
require.Contains(t, buffer.String(), "error when processing response")

Expand Down Expand Up @@ -320,30 +326,6 @@ func (m fakeMessage) Pack(encoding.ProtoMarshaler) (proto.Message, error) {
return &empty.Empty{}, m.err
}

type fakeIterator struct {
crypto.PublicKeyIterator
count int
pubkey crypto.PublicKey
}

func (i *fakeIterator) HasNext() bool {
return i.count > 0 && i.pubkey != nil
}

func (i *fakeIterator) GetNext() crypto.PublicKey {
i.count--
return i.pubkey
}

type fakeCollectiveAuthority struct {
cosi.CollectiveAuthority
pubkey crypto.PublicKey
}

func (ca fakeCollectiveAuthority) PublicKeyIterator() crypto.PublicKeyIterator {
return &fakeIterator{count: 2, pubkey: ca.pubkey}
}

type fakeSignature struct {
crypto.Signature
err error
Expand Down
Loading

0 comments on commit 2d681ba

Please sign in to comment.