Skip to content

Commit

Permalink
Downloading topic (#47)
Browse files Browse the repository at this point in the history
- Introducing download topic
- Update tests
  • Loading branch information
themantre authored Jan 13, 2021
1 parent afa2a83 commit 348a6dc
Show file tree
Hide file tree
Showing 98 changed files with 3,106 additions and 1,762 deletions.
11 changes: 10 additions & 1 deletion account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,17 @@ func TestMarshalingRawData(t *testing.T) {
}

func TestIncSequence(t *testing.T) {
acc, _ := GenerateTestAccount(0)
acc, _ := GenerateTestAccount(100)
seq := acc.Sequence()
acc.IncSequence()
assert.Equal(t, acc.Sequence(), seq+1)
assert.Equal(t, acc.Number(), 100)
}

func TestSubtractBalance(t *testing.T) {
acc, _ := GenerateTestAccount(100)
bal := acc.Balance()
acc.SubtractFromBalance(1)
assert.Equal(t, acc.Balance(), bal-1)

}
3 changes: 3 additions & 0 deletions block/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type commitData struct {
}

func NewCommit(blockHash crypto.Hash, round int, signed, missed []int, signature crypto.Signature) *Commit {
sort.Ints(signed)
sort.Ints(missed)

return &Commit{
data: commitData{
BlockHash: blockHash,
Expand Down
7 changes: 7 additions & 0 deletions block/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ func TestCommiters(t *testing.T) {
assert.Equal(t, c2.Committers(), expected1)
assert.Equal(t, c2.CommittersHash(), expected2)
}

func TestCommitHash(t *testing.T) {
temp := GenerateTestCommit(crypto.GenerateTestHash())
expected := temp.Hash()
c1 := NewCommit(temp.BlockHash(), temp.Round(), []int{3, 2, 1}, []int{0}, temp.Signature())
assert.Equal(t, c1.Hash(), expected)
}
14 changes: 9 additions & 5 deletions cmd/zarb/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func Init() func(c *cli.Cmd) {
}

path, _ := filepath.Abs(*workingDirOpt)
gen := makeGenesis(*workingDirOpt, *chainNameOpt)
gen, err := makeGenesis(*workingDirOpt, *chainNameOpt)
if err != nil {
cmd.PrintErrorMsg("Failed to make genesis file: %v", err)
return
}
conf := makeConfigfile()

// save genesis file to file system
Expand All @@ -65,7 +69,7 @@ func Init() func(c *cli.Cmd) {
}

// makeGenesis makes genisis file while on initialize
func makeGenesis(workingDir string, chainName string) *genesis.Genesis {
func makeGenesis(workingDir string, chainName string) (*genesis.Genesis, error) {

// create accounts for genesis
accs := make([]*account.Account, 5)
Expand All @@ -78,7 +82,7 @@ func makeGenesis(workingDir string, chainName string) *genesis.Genesis {
for i := 1; i < len(accs); i++ {
k := key.GenKey()
if err := key.EncryptKeyToFile(k, workingDir+"/keys/"+k.Address().String()+".json", "", ""); err != nil {
return nil
return nil, err
}
acc := account.NewAccount(k.Address(), i+1)
acc.AddToBalance(1000000)
Expand All @@ -89,14 +93,14 @@ func makeGenesis(workingDir string, chainName string) *genesis.Genesis {
// create validator account for genesis
k := key.GenKey()
if err := key.EncryptKeyToFile(k, workingDir+"/validator_key.json", "", ""); err != nil {
return nil
return nil, err
}
val := validator.NewValidator(k.PublicKey(), 0, 0)
vals := []*validator.Validator{val}

// create genesis
gen := genesis.MakeGenesis(chainName, util.RoundNow(60), accs, vals, 10)
return gen
return gen, nil

}

Expand Down
5 changes: 2 additions & 3 deletions consensus/commit.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package consensus

import (
"github.com/zarbchain/zarb-go/block"
"github.com/zarbchain/zarb-go/consensus/hrs"
"github.com/zarbchain/zarb-go/message"
"github.com/zarbchain/zarb-go/sync/message"
)

func (cs *consensus) enterCommit(round int) {
Expand Down Expand Up @@ -67,6 +66,6 @@ func (cs *consensus) enterCommit(round int) {
cs.scheduleNewHeight()

// Now broadcast the committed block
msg := message.NewBlocksMessage(height, []*block.Block{&commitBlock}, commit)
msg := message.NewBlockAnnounceMessage(height, &commitBlock, commit)
cs.broadcastCh <- msg
}
33 changes: 3 additions & 30 deletions consensus/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,15 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zarbchain/zarb-go/block"
"github.com/zarbchain/zarb-go/crypto"
"github.com/zarbchain/zarb-go/tx"
"github.com/zarbchain/zarb-go/vote"
)

func commitFirstBlock(t *testing.T) (b block.Block, votes [3]*vote.Vote) {
pb, err := tConsX.state.ProposeBlock(0)
require.NoError(t, err)
b = *pb

sb := block.CommitSignBytes(b.Hash(), 0)
sig1 := tSigners[0].Sign(sb)
sig2 := tSigners[1].Sign(sb)
sig3 := tSigners[2].Sign(sb)

sig := crypto.Aggregate([]*crypto.Signature{sig1, sig2, sig3})
c := block.NewCommit(b.Hash(), 0, []int{0, 1, 2}, []int{3}, sig)

require.NotNil(t, c)
err = tConsX.state.ApplyBlock(1, b, *c)
assert.NoError(t, err)
err = tConsY.state.ApplyBlock(1, b, *c)
assert.NoError(t, err)
err = tConsB.state.ApplyBlock(1, b, *c)
assert.NoError(t, err)
err = tConsP.state.ApplyBlock(1, b, *c)
assert.NoError(t, err)

return
}

func TestInvalidStepAfterBlockCommit(t *testing.T) {
setup(t)

commitFirstBlock(t)
commitBlockForAllStates(t)

tConsY.enterNewHeight()

Expand All @@ -55,7 +28,7 @@ func TestInvalidStepAfterBlockCommit(t *testing.T) {
func TestEnterCommit(t *testing.T) {
setup(t)

commitFirstBlock(t)
commitBlockForAllStates(t)

tConsY.enterNewHeight()
tConsY.enterNewRound(1)
Expand Down Expand Up @@ -109,5 +82,5 @@ func TestEnterCommit(t *testing.T) {

// Everything is good
tConsY.enterCommit(1)
shouldPublishProposalBlock(t, tConsY)
shouldPublishBlockAnnounce(t, tConsY)
}
4 changes: 2 additions & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/zarbchain/zarb-go/consensus/hrs"
"github.com/zarbchain/zarb-go/crypto"
"github.com/zarbchain/zarb-go/logger"
"github.com/zarbchain/zarb-go/message"
"github.com/zarbchain/zarb-go/state"
"github.com/zarbchain/zarb-go/sync/message"
"github.com/zarbchain/zarb-go/util"
"github.com/zarbchain/zarb-go/validator"
"github.com/zarbchain/zarb-go/vote"
Expand Down Expand Up @@ -283,6 +283,6 @@ func (cs *consensus) signAddVote(msgType vote.VoteType, hash crypto.Hash) {
}

func (cs *consensus) requestForProposal() {
msg := message.NewProposalReqMessage(cs.hrs.Height(), cs.hrs.Round())
msg := message.NewQueryProposalMessage(cs.hrs.Height(), cs.hrs.Round())
cs.broadcastCh <- msg
}
73 changes: 62 additions & 11 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/zarbchain/zarb-go/crypto"
"github.com/zarbchain/zarb-go/genesis"
"github.com/zarbchain/zarb-go/logger"
"github.com/zarbchain/zarb-go/message"
"github.com/zarbchain/zarb-go/message/payload"
"github.com/zarbchain/zarb-go/state"
"github.com/zarbchain/zarb-go/sync/message"
"github.com/zarbchain/zarb-go/sync/message/payload"
"github.com/zarbchain/zarb-go/txpool"
"github.com/zarbchain/zarb-go/util"
"github.com/zarbchain/zarb-go/validator"
Expand All @@ -40,12 +40,18 @@ const (
)

func setup(t *testing.T) {
if tConsX != nil {
tConsX.state.Close()
tConsY.state.Close()
tConsB.state.Close()
tConsP.state.Close()
}
conf := logger.TestConfig()
conf.Levels["_state"] = "debug"
logger.InitLogger(conf)

_, keys := validator.GenerateTestValidatorSet()
tTxPool = txpool.NewMockTxPool()
tTxPool = txpool.MockingTxPool()

tSigners = make([]crypto.Signer, 4)
for i, k := range keys {
Expand All @@ -62,10 +68,14 @@ func setup(t *testing.T) {
acc.AddToBalance(21000000000000)

tGenDoc = genesis.MakeGenesis("test", util.Now(), []*account.Account{acc}, vals, 1)
stX, _ := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexX], tTxPool)
stY, _ := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexY], tTxPool)
stB, _ := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexB], tTxPool)
stP, _ := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexP], tTxPool)
stX, err := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexX], tTxPool)
require.NoError(t, err)
stY, err := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexY], tTxPool)
require.NoError(t, err)
stB, err := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexB], tTxPool)
require.NoError(t, err)
stP, err := state.LoadOrNewState(state.TestConfig(), tGenDoc, tSigners[tIndexP], tTxPool)
require.NoError(t, err)

consX, err := NewConsensus(TestConfig(), stX, tSigners[tIndexX], make(chan *message.Message, 100))
assert.NoError(t, err)
Expand All @@ -79,9 +89,11 @@ func setup(t *testing.T) {
tConsY = consY.(*consensus)
tConsB = consB.(*consensus)
tConsP = consP.(*consensus)

//TODO: Give a name to the loggers. Look at sync tests
}

func shouldPublishProposalBlock(t *testing.T, cons *consensus) {
func shouldPublishBlockAnnounce(t *testing.T, cons *consensus) {
timeout := time.NewTimer(1 * time.Second)

for {
Expand All @@ -90,9 +102,9 @@ func shouldPublishProposalBlock(t *testing.T, cons *consensus) {
require.NoError(t, fmt.Errorf("Timeout"))
return
case msg := <-cons.broadcastCh:
logger.Info("shouldPublishProposalBlock", "msg", msg)
logger.Info("shouldPublishBlockAnnounce", "msg", msg)

if msg.PayloadType() == payload.PayloadTypeBlocks {
if msg.PayloadType() == payload.PayloadTypeBlockAnnounce {
return
}
}
Expand All @@ -109,7 +121,7 @@ func shouldPublishProposalReqquest(t *testing.T, cons *consensus) {
case msg := <-cons.broadcastCh:
logger.Info("shouldPublishProposalReqquest", "msg", msg)

if msg.PayloadType() == payload.PayloadTypeProposalReq {
if msg.PayloadType() == payload.PayloadTypeQueryProposal {
return
}
}
Expand Down Expand Up @@ -171,6 +183,45 @@ func testAddVote(t *testing.T,
return v
}

func commitBlockForAllStates(t *testing.T) {
height := tConsX.state.LastBlockHeight()
var err error
var pb *block.Block
switch height % 4 {
case 0:
pb, err = tConsX.state.ProposeBlock(0)
require.NoError(t, err)
case 1:
pb, err = tConsY.state.ProposeBlock(0)
require.NoError(t, err)
case 2:
pb, err = tConsB.state.ProposeBlock(0)
require.NoError(t, err)
case 3:
pb, err = tConsP.state.ProposeBlock(0)
require.NoError(t, err)
}

sb := block.CommitSignBytes(pb.Hash(), 0)
sig1 := tSigners[0].Sign(sb)
sig2 := tSigners[1].Sign(sb)
sig3 := tSigners[2].Sign(sb)
sig4 := tSigners[3].Sign(sb)

sig := crypto.Aggregate([]*crypto.Signature{sig1, sig2, sig3, sig4})
c := block.NewCommit(pb.Hash(), 0, []int{0, 1, 2, 3}, []int{}, sig)

require.NotNil(t, c)
err = tConsX.state.ApplyBlock(height+1, *pb, *c)
assert.NoError(t, err)
err = tConsY.state.ApplyBlock(height+1, *pb, *c)
assert.NoError(t, err)
err = tConsB.state.ApplyBlock(height+1, *pb, *c)
assert.NoError(t, err)
err = tConsP.state.ApplyBlock(height+1, *pb, *c)
assert.NoError(t, err)
}

func TestNotInValidatorSet(t *testing.T) {
setup(t)

Expand Down
1 change: 0 additions & 1 deletion consensus/height.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (cs *consensus) enterNewHeight() {
if vs == nil {
cs.logger.Warn("NewHeight: Entering new height without last commit")
} else {
// TODO: add test for me
// Update last commit here, consensus had enough time to populate more votes
lastCommit := vs.ToCommit()
if lastCommit != nil {
Expand Down
8 changes: 4 additions & 4 deletions consensus/height_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestMoveToNewHeight(t *testing.T) {

tConsP.MoveToNewHeight()

commitFirstBlock(t)
commitBlockForAllStates(t)

tConsP.MoveToNewHeight()
checkHRSWait(t, tConsP, 2, 0, hrs.StepTypePropose)
Expand All @@ -37,8 +37,7 @@ func TestConsensusBehindState3(t *testing.T) {

// --------------------------------
// Syncer commit a block and trig consensus
commitFirstBlock(t)
tConsX.MoveToNewHeight()
commitBlockForAllStates(t)

assert.Equal(t, len(tConsX.RoundVotes(0)), 1)
assert.Equal(t, tConsX.hrs, hrs.NewHRS(1, 0, hrs.StepTypePrepare))
Expand All @@ -52,7 +51,6 @@ func TestConsensusBehindState3(t *testing.T) {
checkHRS(t, tConsX, 1, 0, hrs.StepTypePrepare)

testAddVote(t, tConsX, vote.VoteTypePrecommit, 1, 0, p.Block().Hash(), tIndexP, false)
checkHRS(t, tConsX, 1, 0, hrs.StepTypeCommit)

precommits := tConsX.pendingVotes.PrecommitVoteSet(0)
require.NotNil(t, precommits)
Expand All @@ -62,4 +60,6 @@ func TestConsensusBehindState3(t *testing.T) {

assert.NoError(t, tConsX.state.ApplyBlock(1, p.Block(), *precommits.ToCommit()))
// We don't get any error here, but the block is not committed again. Check logs.

tConsX.enterNewHeight()
}
15 changes: 10 additions & 5 deletions consensus/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
"github.com/zarbchain/zarb-go/vote"
)

type Consensus interface {
MoveToNewHeight()
Stop()
AddVote(v *vote.Vote)
type ConsensusReader interface {
RoundVotes(round int) []*vote.Vote
RoundVotesHash(round int) []crypto.Hash
SetProposal(proposal *vote.Proposal)
LastProposal() *vote.Proposal
HRS() hrs.HRS
Fingerprint() string
}

type Consensus interface {
ConsensusReader

MoveToNewHeight()
Stop()
AddVote(v *vote.Vote)
SetProposal(proposal *vote.Proposal)
}
2 changes: 1 addition & 1 deletion consensus/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MockConsensus struct {
Started bool
}

func NewMockConsensus() *MockConsensus {
func MockingConsensus() *MockConsensus {
return &MockConsensus{}
}

Expand Down
Loading

0 comments on commit 348a6dc

Please sign in to comment.