-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
View change improvement and unit tests
- Loading branch information
1 parent
b2cbf2c
commit f1414fb
Showing
11 changed files
with
234 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package viewchange | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.dedis.ch/fabric/blockchain" | ||
"go.dedis.ch/fabric/mino" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func TestConstantViewChange_Wait(t *testing.T) { | ||
vc := NewConstant(fakeAddress{id: "unknown"}, fakeBlockchain{len: 1, id: "unknown"}) | ||
|
||
players, err := vc.Wait(fakeBlock{}) | ||
require.NoError(t, err) | ||
require.NotNil(t, players) | ||
|
||
vc.bc = fakeBlockchain{err: xerrors.New("oops")} | ||
_, err = vc.Wait(fakeBlock{}) | ||
require.EqualError(t, err, "couldn't read latest block: oops") | ||
|
||
vc.bc = fakeBlockchain{len: 0} | ||
_, err = vc.Wait(fakeBlock{}) | ||
require.EqualError(t, err, "players is empty") | ||
|
||
vc.bc = fakeBlockchain{len: 1, id: "unknown"} | ||
vc.addr = fakeAddress{id: "deadbeef"} | ||
_, err = vc.Wait(fakeBlock{}) | ||
require.EqualError(t, err, "mismatching leader: unknown != deadbeef") | ||
} | ||
|
||
func TestConstantViewChange_Verify(t *testing.T) { | ||
vc := NewConstant(fakeAddress{}, fakeBlockchain{}) | ||
|
||
err := vc.Verify(fakeBlock{}) | ||
require.NoError(t, err) | ||
|
||
vc.bc = fakeBlockchain{err: xerrors.New("oops")} | ||
err = vc.Verify(fakeBlock{}) | ||
require.EqualError(t, err, "couldn't read latest block: oops") | ||
|
||
vc.bc = fakeBlockchain{id: "B"} | ||
err = vc.Verify(fakeBlock{id: "A"}) | ||
require.EqualError(t, err, "mismatching leader: A != B") | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Utility functions | ||
|
||
type fakeAddress struct { | ||
mino.Address | ||
id string | ||
} | ||
|
||
func (a fakeAddress) Equal(other mino.Address) bool { | ||
return a.id == other.(fakeAddress).id | ||
} | ||
|
||
func (a fakeAddress) String() string { | ||
return a.id | ||
} | ||
|
||
type fakeIterator struct { | ||
mino.AddressIterator | ||
id string | ||
} | ||
|
||
func (i fakeIterator) GetNext() mino.Address { | ||
return fakeAddress{id: i.id} | ||
} | ||
|
||
type fakePlayers struct { | ||
mino.Players | ||
len int | ||
id string | ||
} | ||
|
||
func (p fakePlayers) Len() int { | ||
return p.len | ||
} | ||
|
||
func (p fakePlayers) AddressIterator() mino.AddressIterator { | ||
return fakeIterator{id: p.id} | ||
} | ||
|
||
type fakeBlock struct { | ||
blockchain.Block | ||
len int | ||
id string | ||
} | ||
|
||
func (b fakeBlock) GetPlayers() mino.Players { | ||
return fakePlayers{len: b.len, id: b.id} | ||
} | ||
|
||
type fakeBlockchain struct { | ||
blockchain.Blockchain | ||
err error | ||
len int | ||
id string | ||
} | ||
|
||
func (bc fakeBlockchain) GetBlock() (blockchain.Block, error) { | ||
return fakeBlock{len: bc.len, id: bc.id}, bc.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package viewchange | ||
|
||
import ( | ||
"go.dedis.ch/fabric/blockchain" | ||
"go.dedis.ch/fabric/mino" | ||
) | ||
|
||
// ViewChange provides primitives to verify if a participant is allowed to | ||
// propose a block as the leader. It is also responsible for verifying the | ||
// integrity of the players of the chain. | ||
type ViewChange interface { | ||
// Wait returns a non-nil error when the node is allowed to make the | ||
// proposal. It will also return the authorized list of players that must be | ||
// used so that the Verify function returns nil. | ||
// | ||
// Note: the implementation of the returned mino.Players interface must be | ||
// preserved. | ||
Wait(block blockchain.Block) (mino.Players, error) | ||
|
||
// Verify makes sure that the players for the given are authorized and in | ||
// the right order if necessary. | ||
Verify(block blockchain.Block) error | ||
} |
Oops, something went wrong.