Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel committed Apr 8, 2020
1 parent 64d7dbb commit f2c4260
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 85 deletions.
8 changes: 4 additions & 4 deletions blockchain/skipchain/conode.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ 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
Expand Down Expand Up @@ -135,7 +135,7 @@ func (cc Conodes) Len() int {
return len(cc)
}

// GetPublicKey implements cosi.CollectiveAuthority. It returns the public key
// GetPublicKey implements crypto.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 {
Expand All @@ -157,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
96 changes: 20 additions & 76 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 Down Expand Up @@ -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 {
crypto.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
4 changes: 3 additions & 1 deletion cosi/threshold/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func TestSignatureFactory_FromProto(t *testing.T) {
func TestVerifier_Verify(t *testing.T) {
call := &fake.Call{}

verifier := newVerifier(fake.NewAuthority(3, fake.NewSigner), fake.NewVerifier(call))
verifier := newVerifier(
fake.NewAuthority(3, fake.NewSigner),
fake.NewVerifierFactoryWithCalls(call))

err := verifier.Verify([]byte{0xff}, &Signature{mask: []byte{0x3}})
require.NoError(t, err)
Expand Down
19 changes: 15 additions & 4 deletions internal/testing/fake/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import (
"golang.org/x/xerrors"
)

// Call is a tool to keep track of a function calls.
type Call struct {
calls [][]interface{}
}

// Get returns the nth call ith parameter.
func (c *Call) Get(n, i int) interface{} {
return c.calls[n][i]
}

// Len returns the number of calls.
func (c *Call) Len() int {
return len(c.calls)
}

// Add adds a call to the list.
func (c *Call) Add(args ...interface{}) {
c.calls = append(c.calls, args)
}
Expand Down Expand Up @@ -89,6 +93,7 @@ type CollectiveAuthority struct {
signers []crypto.AggregateSigner
}

// GenSigner is a function to generate a signer.
type GenSigner func() crypto.AggregateSigner

// NewAuthority returns a new fake collective authority of size n.
Expand Down Expand Up @@ -229,6 +234,7 @@ func (f SignatureFactory) FromProto(proto.Message) (crypto.Signature, error) {
return Signature{}, f.err
}

// PublicKey is a fake implementation of crypto.PublicKey.
type PublicKey struct {
crypto.PublicKey
}
Expand All @@ -252,6 +258,8 @@ func NewSignerWithSignatureFactory(f SignatureFactory) Signer {
return Signer{signatureFactory: f}
}

// NewSignerWithVerifierFactory returns a new fake signer with the specific
// verifier factory.
func NewSignerWithVerifierFactory(f VerifierFactory) Signer {
return Signer{verifierFactory: f}
}
Expand All @@ -277,6 +285,7 @@ func (s Signer) GetVerifierFactory() crypto.VerifierFactory {
return s.verifierFactory
}

// GetPublicKey implements crypto.Signer.
func (s Signer) GetPublicKey() crypto.PublicKey {
return PublicKey{}
}
Expand Down Expand Up @@ -315,15 +324,17 @@ type VerifierFactory struct {
call *Call
}

func NewVerifier(c *Call) VerifierFactory {
return VerifierFactory{call: c}
}

// NewVerifierFactory returns a new fake verifier factory.
func NewVerifierFactory(v Verifier) VerifierFactory {
return VerifierFactory{verifier: v}
}

// NewVerifierFactoryWithCalls returns a new verifier factory that will register
// the calls.
func NewVerifierFactoryWithCalls(c *Call) VerifierFactory {
return VerifierFactory{call: c}
}

// NewBadVerifierFactory returns a fake verifier factory that returns an error
// when appropriate.
func NewBadVerifierFactory() VerifierFactory {
Expand Down
7 changes: 7 additions & 0 deletions mino/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ func TestFilter_RangeFilter(t *testing.T) {
RangeFilter(1, 7)(filters)
require.Equal(t, []int{0, 1, 2, 3, 4, 5, 6}, filters.Indices)
}

func TestFilter_ListFilter(t *testing.T) {
filters := &Filter{Indices: []int{1, 2, 3}}

ListFilter([]int{3, 4, 7})(filters)
require.Equal(t, []int{3, 4, 7}, filters.Indices)
}

0 comments on commit f2c4260

Please sign in to comment.