Skip to content

Commit

Permalink
Merge 74003bd into 9e0351f
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel committed Apr 9, 2020
2 parents 9e0351f + 74003bd commit be5a1de
Show file tree
Hide file tree
Showing 29 changed files with 1,609 additions and 176 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
21 changes: 16 additions & 5 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 @@ -102,12 +101,12 @@ func (i *publicKeyIterator) GetNext() crypto.PublicKey {
// Conodes is a list of conodes.
//
// - implements mino.Players
// - implements cosi.CollectiveAuthority
// - implements crypto.CollectiveAuthority
// - implements io.WriterTo
// - 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 crypto.CollectiveAuthority. It returns the public key
// associated with the address and its position in the collective authority.
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 All @@ -146,8 +157,8 @@ func (cc Conodes) AddressIterator() mino.AddressIterator {
}
}

// PublicKeyIterator implements cosi.CollectiveAuthority. It returns the public
// key iterator.
// PublicKeyIterator implements crypto.CollectiveAuthority. It returns the
// public key iterator.
func (cc Conodes) PublicKeyIterator() crypto.PublicKeyIterator {
return &publicKeyIterator{
iterator: &iterator{
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
100 changes: 22 additions & 78 deletions consensus/cosipbft/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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"
"go.dedis.ch/fabric/mino/minoch"
"golang.org/x/xerrors"
Expand All @@ -41,18 +42,16 @@ func TestConsensus_Basic(t *testing.T) {
m1, err := minoch.NewMinoch(manager, "A")
require.NoError(t, err)

signer := bls.NewSigner()
cosi := flatcosi.NewFlat(m1, signer)
ca := fake.NewAuthorityFromMino(bls.NewSigner, m1)

cosi := flatcosi.NewFlat(m1, ca.GetSigner(0))

cons := NewCoSiPBFT(m1, cosi)
actor, err := cons.Listen(fakeValidator{pubkey: signer.GetPublicKey()})
actor, err := cons.Listen(fakeValidator{})
require.NoError(t, err)

prop := fakeProposal{}
err = actor.Propose(prop, fakeCA{
addrs: []mino.Address{m1.GetAddress()},
pubkeys: []crypto.PublicKey{signer.GetPublicKey()},
})
err = actor.Propose(prop, ca)
require.NoError(t, err)

ch, err := cons.GetChain(prop.GetHash())
Expand Down Expand Up @@ -153,19 +152,21 @@ func TestActor_Propose(t *testing.T) {
cosiActor: cosiActor,
}

err := actor.Propose(fakeProposal{}, fakeCA{})
ca := fake.NewAuthority(1, fake.NewSigner)

err := actor.Propose(fakeProposal{}, ca)
require.NoError(t, err)
require.Len(t, cosiActor.calls, 2)

prepare := cosiActor.calls[0]["message"].(*PrepareRequest)
require.NotNil(t, prepare)
require.IsType(t, fakeCA{}, cosiActor.calls[0]["signers"])
require.IsType(t, ca, cosiActor.calls[0]["signers"])

commit := cosiActor.calls[1]["message"].(*CommitRequest)
require.NotNil(t, commit)
require.Equal(t, []byte{0xaa}, commit.GetTo())
checkSignatureValue(t, commit.GetPrepare(), 1)
require.IsType(t, fakeCA{}, cosiActor.calls[1]["signers"])
require.IsType(t, ca, cosiActor.calls[1]["signers"])

require.Len(t, rpc.calls, 1)
propagate := rpc.calls[0]["message"].(*PropagateRequest)
Expand All @@ -175,7 +176,7 @@ func TestActor_Propose(t *testing.T) {

rpc.close = false
require.NoError(t, actor.Close())
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.NoError(t, err)
}

Expand All @@ -185,7 +186,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 All @@ -205,30 +206,32 @@ func TestConsensus_ProposeFailures(t *testing.T) {
hashFactory: sha256Factory{},
}

ca := fake.NewAuthority(1, fake.NewSigner)

err := actor.Propose(fakeProposal{}, badCA{})
require.EqualError(t, err, "cosipbft.badCA should implement cosi.CollectiveAuthority")

actor.cosiActor = &fakeCosiActor{err: xerrors.New("oops")}
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.EqualError(t, err, "couldn't sign the proposal: oops")

actor.cosiActor = &badCosiActor{}
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.EqualError(t, xerrors.Unwrap(err), "couldn't marshal prepare signature: oops")

actor.cosiActor = &fakeCosiActor{err: xerrors.New("oops"), delay: 1}
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.EqualError(t, err, "couldn't sign the commit: oops")

actor.cosiActor = &fakeCosiActor{}
actor.encoder = badPackAnyEncoder{}
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.EqualError(t, err, "couldn't pack signature: oops")

actor.encoder = encoding.NewProtoEncoder()
actor.cosiActor = &fakeCosiActor{}
actor.rpc = &fakeRPC{err: xerrors.New("oops")}
err = actor.Propose(fakeProposal{}, fakeCA{})
err = actor.Propose(fakeProposal{}, ca)
require.EqualError(t, err, "couldn't propagate the link: oops")
}

Expand Down Expand Up @@ -404,8 +407,7 @@ func (p fakeProposal) GetVerifier() crypto.Verifier {
}

type fakeValidator struct {
pubkey crypto.PublicKey
err error
err error
}

func (v fakeValidator) Validate(addr mino.Address,
Expand All @@ -419,64 +421,6 @@ func (v fakeValidator) Commit(id []byte) error {
return v.err
}

type fakeAddrIterator struct {
addrs []mino.Address
index int
}

func (i *fakeAddrIterator) HasNext() bool {
return i.index+1 < len(i.addrs)
}

func (i *fakeAddrIterator) GetNext() mino.Address {
if i.HasNext() {
i.index++
return i.addrs[i.index]
}
return nil
}

type fakePKIterator struct {
pubkeys []crypto.PublicKey
index int
}

func (i *fakePKIterator) HasNext() bool {
return i.index+1 < len(i.pubkeys)
}

func (i *fakePKIterator) GetNext() crypto.PublicKey {
if i.HasNext() {
i.index++
return i.pubkeys[i.index]
}
return nil
}

type fakeCA struct {
cosi.CollectiveAuthority
addrs []mino.Address
pubkeys []crypto.PublicKey
}

func (ca fakeCA) AddressIterator() mino.AddressIterator {
return &fakeAddrIterator{
addrs: ca.addrs,
index: -1,
}
}

func (ca fakeCA) PublicKeyIterator() crypto.PublicKeyIterator {
return &fakePKIterator{
pubkeys: ca.pubkeys,
index: -1,
}
}

func (ca fakeCA) Len() int {
return len(ca.addrs)
}

type fakeSignature struct {
crypto.Signature
value uint64
Expand Down Expand Up @@ -515,7 +459,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
Loading

0 comments on commit be5a1de

Please sign in to comment.