<s> from hydrachain . consensus . base import Vote , VoteBlock , VoteNil , LockSet , ishash , Ready <EOL> from hydrachain . consensus . base import DoubleVotingError , InvalidVoteError , MissingSignatureError <EOL> from hydrachain . consensus . base import BlockProposal , genesis_signing_lockset , InvalidProposalError <EOL> from hydrachain . consensus . base import Proposal , VotingInstruction , InvalidSignature , Signed <EOL> from ethereum import utils , tester <EOL> import rlp <EOL> import pytest <EOL> privkey = '<STR_LIT:x>' * <NUM_LIT:32> <EOL> def test_signed ( ) : <EOL> s = Signed ( v = <NUM_LIT:0> , r = <NUM_LIT:0> , s = <NUM_LIT:0> ) <EOL> assert s . sender is None <EOL> with pytest . raises ( MissingSignatureError ) : <EOL> s . hash <EOL> s . sign ( privkey ) <EOL> sender = s . sender <EOL> h = s . hash <EOL> s . v = <NUM_LIT:0> <EOL> assert s . sender == sender <EOL> assert s . hash == h <EOL> def test_vote ( ) : <EOL> h , r = <NUM_LIT:2> , <NUM_LIT:3> <EOL> bh = '<STR_LIT:0>' * <NUM_LIT:32> <EOL> sender = utils . privtoaddr ( privkey ) <EOL> v = Vote ( h , r ) <EOL> v2 = Vote ( h , r , blockhash = bh ) <EOL> assert isinstance ( v , Vote ) <EOL> assert isinstance ( v2 , Vote ) <EOL> assert isinstance ( v , VoteNil ) <EOL> assert isinstance ( v , rlp . Serializable ) <EOL> assert isinstance ( v2 , VoteBlock ) <EOL> v . sign ( privkey ) <EOL> s = v . sender <EOL> assert s == sender <EOL> v2 . sign ( privkey ) <EOL> assert v2 . sender == sender <EOL> assert len ( v . get_sedes ( ) ) == len ( v . fields ) == <NUM_LIT:6> <EOL> vs = rlp . encode ( v ) <EOL> assert isinstance ( vs , bytes ) <EOL> print rlp . decode ( vs ) <EOL> vd = rlp . decode ( vs , Vote ) <EOL> assert isinstance ( vd , VoteNil ) <EOL> assert vd . blockhash == '<STR_LIT>' <EOL> assert vd == v <EOL> v2s = rlp . encode ( v2 ) <EOL> v2d = rlp . decode ( v2s , Vote ) <EOL> assert isinstance ( v2d , VoteBlock ) <EOL> assert v2d . blockhash == bh <EOL> assert v2d == v2 <EOL> assert v != v2 <EOL> assert vd != v2d <EOL> assert len ( set ( ( v , vd ) ) ) == <NUM_LIT:1> <EOL> assert len ( set ( ( v2 , v2d ) ) ) == <NUM_LIT:1> <EOL> assert len ( set ( ( v , vd , v2 , v2d ) ) ) == <NUM_LIT:2> <EOL> privkeys = [ chr ( i ) * <NUM_LIT:32> for i in range ( <NUM_LIT:1> , <NUM_LIT:11> ) ] <EOL> validators = [ utils . privtoaddr ( p ) for p in privkeys ] <EOL> def test_ready ( ) : <EOL> ls = LockSet ( num_eligible_votes = len ( privkeys ) ) <EOL> s = Ready ( <NUM_LIT:0> , current_lockset = ls ) <EOL> assert s . current_lockset == ls <EOL> s . sign ( privkey ) <EOL> s0 = Ready ( <NUM_LIT:0> , current_lockset = ls ) <EOL> s0 . sign ( privkey ) <EOL> s1 = Ready ( <NUM_LIT:1> , current_lockset = ls ) <EOL> s1 . sign ( privkey ) <EOL> assert s == s0 <EOL> assert s != s1 <EOL> def test_LockSet ( ) : <EOL> ls = LockSet ( num_eligible_votes = len ( privkeys ) ) <EOL> assert not ls <EOL> assert len ( ls ) == <NUM_LIT:0> <EOL> bh = '<STR_LIT:0>' * <NUM_LIT:32> <EOL> r , h = <NUM_LIT:2> , <NUM_LIT:3> <EOL> v1 = VoteBlock ( h , r , bh ) <EOL> with pytest . raises ( InvalidVoteError ) : <EOL> ls . add ( v1 ) <EOL> assert not ls <EOL> assert v1 not in ls <EOL> v1 . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> ls . add ( v1 ) <EOL> assert ls <EOL> assert len ( ls ) == <NUM_LIT:1> <EOL> lsh = ls . hash <EOL> ls . add ( v1 ) <EOL> assert lsh == ls . hash <EOL> assert len ( ls ) == <NUM_LIT:1> <EOL> v2 = VoteBlock ( h , r , bh ) <EOL> v2 . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> ls . add ( v1 ) <EOL> ls . add ( v2 ) <EOL> assert lsh == ls . hash <EOL> assert len ( ls ) == <NUM_LIT:1> <EOL> v3 = VoteBlock ( h , r , bh ) <EOL> v3 . sign ( privkeys [ <NUM_LIT:1> ] ) <EOL> ls . add ( v1 ) <EOL> ls . add ( v3 ) <EOL> assert lsh != ls . hash <EOL> assert len ( ls ) == <NUM_LIT:2> <EOL> assert v3 in ls <EOL> lsh = ls . hash <EOL> v4 = VoteBlock ( h , r + <NUM_LIT:1> , bh ) <EOL> v4 . sign ( privkeys [ <NUM_LIT:2> ] ) <EOL> with pytest . raises ( InvalidVoteError ) : <EOL> ls . add ( v4 ) <EOL> assert lsh == ls . hash <EOL> assert len ( ls ) == <NUM_LIT:2> <EOL> assert v4 not in ls <EOL> v3_2 = VoteBlock ( h , r , blockhash = '<STR_LIT:1>' * <NUM_LIT:32> ) <EOL> v3_2 . sign ( privkeys [ <NUM_LIT:1> ] ) <EOL> with pytest . raises ( DoubleVotingError ) : <EOL> ls . add ( v3_2 ) <EOL> assert lsh == ls . hash <EOL> assert len ( ls ) == <NUM_LIT:2> <EOL> assert v3_2 not in ls <EOL> def test_one_vote_lockset ( ) : <EOL> ls = LockSet ( num_eligible_votes = <NUM_LIT:1> ) <EOL> bh = '<STR_LIT:0>' * <NUM_LIT:32> <EOL> r , h = <NUM_LIT:2> , <NUM_LIT:3> <EOL> v1 = VoteBlock ( h , r , bh ) <EOL> v1 . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> ls . add ( v1 ) <EOL> assert ls . has_quorum <EOL> def test_LockSet_isvalid ( ) : <EOL> ls = LockSet ( num_eligible_votes = len ( privkeys ) ) <EOL> bh = '<STR_LIT:0>' * <NUM_LIT:32> <EOL> r , h = <NUM_LIT:2> , <NUM_LIT:3> <EOL> votes = [ VoteBlock ( h , r , bh ) for i in range ( len ( privkeys ) ) ] <EOL> for i , v in enumerate ( votes ) : <EOL> v . sign ( privkeys [ i ] ) <EOL> ls . add ( v ) <EOL> assert len ( ls ) == i + <NUM_LIT:1> <EOL> if len ( ls ) < ls . num_eligible_votes * <NUM_LIT:2> / <NUM_LIT> : <EOL> assert not ls . is_valid <EOL> else : <EOL> assert ls . is_valid <EOL> assert ls . has_quorum <EOL> ls . check ( ) <EOL> def test_LockSet_3_quorums ( ) : <EOL> ls = LockSet ( <NUM_LIT:3> ) <EOL> v = VoteBlock ( <NUM_LIT:0> , <NUM_LIT:0> , '<STR_LIT:0>' * <NUM_LIT:32> ) <EOL> v . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> ls . add ( v ) <EOL> v = VoteNil ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> v . sign ( privkeys [ <NUM_LIT:1> ] ) <EOL> ls . add ( v ) <EOL> assert len ( ls ) == <NUM_LIT:2> <EOL> assert not ls . is_valid <EOL> v = VoteNil ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> v . sign ( privkeys [ <NUM_LIT:2> ] ) <EOL> ls . add ( v ) <EOL> assert ls . is_valid <EOL> assert ls . has_noquorum <EOL> assert not ls . has_quorum <EOL> assert not ls . has_quorum_possible <EOL> assert ls . check ( ) <EOL> def test_LockSet_quorums ( ) : <EOL> combinations = dict ( has_quorum = [ <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:7> , <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:7> + [ <NUM_LIT:2> ] * <NUM_LIT:3> , <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:7> + [ None ] * <NUM_LIT:3> , <EOL> ] , <EOL> has_noquorum = [ <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:3> + [ <NUM_LIT:2> ] * <NUM_LIT:3> + [ None ] , <EOL> [ None ] * <NUM_LIT:7> , <EOL> [ None ] * <NUM_LIT:10> , <EOL> range ( <NUM_LIT:10> ) , <EOL> range ( <NUM_LIT:7> ) <EOL> ] , <EOL> has_quorum_possible = [ <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:4> + [ None ] * <NUM_LIT:3> , <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:4> + [ <NUM_LIT:2> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:4> + [ <NUM_LIT:2> ] * <NUM_LIT:3> + [ <NUM_LIT:3> ] * <NUM_LIT:3> , <EOL> [ <NUM_LIT:1> ] * <NUM_LIT:6> + [ <NUM_LIT:2> ] <EOL> ] ) <EOL> r , h = <NUM_LIT:1> , <NUM_LIT:2> <EOL> for method , permutations in combinations . items ( ) : <EOL> for set_ in permutations : <EOL> assert len ( set_ ) >= <NUM_LIT:7> <EOL> ls = LockSet ( len ( privkeys ) ) <EOL> for i , p in enumerate ( set_ ) : <EOL> if p is not None : <EOL> bh = chr ( p ) * <NUM_LIT:32> <EOL> v = VoteBlock ( h , r , bh ) <EOL> else : <EOL> v = VoteNil ( h , r ) <EOL> v . sign ( privkeys [ i ] ) <EOL> ls . add ( v ) <EOL> assert len ( ls ) >= <NUM_LIT:7> <EOL> assert getattr ( ls , method ) <EOL> ls . check ( ) <EOL> bhs = ls . blockhashes ( ) <EOL> if len ( bhs ) > <NUM_LIT:1> : <EOL> assert ishash ( bhs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> assert isinstance ( bhs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , int ) <EOL> if bhs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == bhs [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> assert bhs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] > bhs [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> assert bhs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] > bhs [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> s = rlp . encode ( ls ) <EOL> d = rlp . decode ( s , LockSet ) <EOL> assert ls == d <EOL> assert id ( ls ) != id ( d ) <EOL> assert getattr ( ls , method ) == getattr ( d , method ) <EOL> def test_blockproposal ( ) : <EOL> s = tester . state ( ) <EOL> s . mine ( n = <NUM_LIT:1> ) <EOL> genesis = s . blocks [ <NUM_LIT:0> ] <EOL> assert genesis . header . number == <NUM_LIT:0> <EOL> blk1 = s . blocks [ <NUM_LIT:1> ] <EOL> assert blk1 . header . number == <NUM_LIT:1> <EOL> gls = genesis_signing_lockset ( genesis , privkeys [ <NUM_LIT:0> ] ) <EOL> bp = BlockProposal ( height = <NUM_LIT:1> , round = <NUM_LIT:0> , block = blk1 , signing_lockset = gls , round_lockset = None ) <EOL> assert bp . lockset == gls <EOL> assert isinstance ( bp , Proposal ) <EOL> bp . sign ( tester . k0 ) <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp = BlockProposal ( height = <NUM_LIT:1> , round = <NUM_LIT:1> , block = blk1 , signing_lockset = gls , round_lockset = None ) <EOL> bp . validate_votes ( validators , validators [ : <NUM_LIT:1> ] ) <EOL> s . mine ( n = <NUM_LIT:1> ) <EOL> blk2 = s . blocks [ <NUM_LIT:2> ] <EOL> assert blk2 . header . number == <NUM_LIT:2> <EOL> ls = LockSet ( len ( validators ) ) <EOL> for privkey in privkeys : <EOL> v = VoteBlock ( height = <NUM_LIT:1> , round = <NUM_LIT:0> , blockhash = blk1 . hash ) <EOL> v . sign ( privkey ) <EOL> ls . add ( v ) <EOL> bp = BlockProposal ( height = <NUM_LIT:2> , round = <NUM_LIT:0> , block = blk2 , signing_lockset = ls , round_lockset = None ) <EOL> assert bp . lockset == ls <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp . validate_votes ( validators , validators ) <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> bp . validate_votes ( validators , validators ) <EOL> with pytest . raises ( InvalidSignature ) : <EOL> bp . sign ( tester . k0 ) <EOL> bp . v = <NUM_LIT:0> <EOL> bp . sign ( tester . k0 ) <EOL> bp . validate_votes ( validators , validators ) <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp = BlockProposal ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , block = blk2 , signing_lockset = gls , round_lockset = None ) <EOL> rls = LockSet ( len ( validators ) ) <EOL> for privkey in privkeys : <EOL> v = VoteNil ( height = <NUM_LIT:2> , round = <NUM_LIT:0> ) <EOL> v . sign ( privkey ) <EOL> rls . add ( v ) <EOL> bp = BlockProposal ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , block = blk2 , signing_lockset = ls , round_lockset = rls ) <EOL> assert bp . lockset == rls <EOL> bp . sign ( tester . k0 ) <EOL> bp . validate_votes ( validators , validators ) <EOL> s = rlp . encode ( bp ) <EOL> dbp = rlp . decode ( s , BlockProposal ) <EOL> assert dbp . block == blk2 <EOL> dbp . validate_votes ( validators , validators ) <EOL> rls = LockSet ( len ( validators ) ) <EOL> for i , privkey in enumerate ( privkeys ) : <EOL> if i < <NUM_LIT:4> : <EOL> v = VoteBlock ( height = <NUM_LIT:2> , round = <NUM_LIT:0> , blockhash = '<STR_LIT:0>' * <NUM_LIT:32> ) <EOL> else : <EOL> v = VoteNil ( height = <NUM_LIT:2> , round = <NUM_LIT:0> ) <EOL> v . sign ( privkey ) <EOL> rls . add ( v ) <EOL> assert not rls . has_noquorum <EOL> assert rls . has_quorum_possible <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp = BlockProposal ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , block = blk2 , signing_lockset = ls , round_lockset = rls ) <EOL> def test_VotingInstruction ( ) : <EOL> rls = LockSet ( len ( validators ) ) <EOL> bh = '<STR_LIT:1>' * <NUM_LIT:32> <EOL> for i , privkey in enumerate ( privkeys ) : <EOL> if i < <NUM_LIT:4> : <EOL> v = VoteBlock ( height = <NUM_LIT:2> , round = <NUM_LIT:0> , blockhash = bh ) <EOL> else : <EOL> v = VoteNil ( height = <NUM_LIT:2> , round = <NUM_LIT:0> ) <EOL> v . sign ( privkey ) <EOL> rls . add ( v ) <EOL> assert rls . has_quorum_possible <EOL> bp = VotingInstruction ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , round_lockset = rls ) <EOL> bp . sign ( privkeys [ <NUM_LIT:0> ] ) <EOL> assert bh == bp . blockhash <EOL> rls = LockSet ( len ( validators ) ) <EOL> for i , privkey in enumerate ( privkeys ) : <EOL> if i < <NUM_LIT:3> : <EOL> v = VoteBlock ( height = <NUM_LIT:2> , round = <NUM_LIT:0> , blockhash = bh ) <EOL> else : <EOL> v = VoteNil ( height = <NUM_LIT:2> , round = <NUM_LIT:0> ) <EOL> v . sign ( privkey ) <EOL> rls . add ( v ) <EOL> assert not rls . has_quorum_possible <EOL> assert rls . has_noquorum <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp = VotingInstruction ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , round_lockset = rls ) <EOL> rls = LockSet ( len ( validators ) ) <EOL> for i , privkey in enumerate ( privkeys ) : <EOL> if i < <NUM_LIT:3> : <EOL> v = VoteBlock ( height = <NUM_LIT:2> , round = <NUM_LIT:0> , blockhash = bh ) <EOL> else : <EOL> v = VoteNil ( height = <NUM_LIT:2> , round = <NUM_LIT:0> ) <EOL> v . sign ( privkey ) <EOL> rls . add ( v ) <EOL> assert not rls . has_quorum_possible <EOL> assert rls . has_noquorum <EOL> with pytest . raises ( InvalidProposalError ) : <EOL> bp = VotingInstruction ( height = <NUM_LIT:2> , round = <NUM_LIT:1> , round_lockset = rls ) </s>
