Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consensus block propagation via IPFS #293

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blockchain/v0/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func makeTxs(height int64) (txs []types.Tx) {
}

func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
block, _ := state.MakeBlock(height, makeTxs(height), nil,
block := state.MakeBlock(height, makeTxs(height), nil,
nil, types.Messages{}, lastCommit, state.Validators.GetProposer().Address)
return block
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/v2/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func makeTxs(height int64) (txs []types.Tx) {
}

func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
block, _ := state.MakeBlock(height, makeTxs(height), nil,
block := state.MakeBlock(height, makeTxs(height), nil,
nil, types.Messages{}, lastCommit, state.Validators.GetProposer().Address)
return block
}
Expand Down
8 changes: 6 additions & 2 deletions consensus/byzantine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
// Avoid sending on internalMsgQueue and running consensus state.

// Create a new proposal block from state/txs from the mempool.
block1, blockParts1 := cs.createProposalBlock()
block1 := cs.createProposalBlock()
blockParts1 := block1.MakePartSet(types.BlockPartSizeBytes)

polRound, propBlockID := cs.ValidRound, types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()}
proposal1 := types.NewProposal(height, round, polRound, propBlockID, &block1.DataAvailabilityHeader)
p1, err := proposal1.ToProto()
Expand All @@ -388,7 +390,9 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
deliverTxsRange(cs, 0, 1)

// Create a new proposal block from state/txs from the mempool.
block2, blockParts2 := cs.createProposalBlock()
block2 := cs.createProposalBlock()
blockParts2 := block2.MakePartSet(types.BlockPartSizeBytes)

polRound, propBlockID = cs.ValidRound, types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()}
proposal2 := types.NewProposal(height, round, polRound, propBlockID, &block2.DataAvailabilityHeader)
p2, err := proposal2.ToProto()
Expand Down
51 changes: 45 additions & 6 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"sync"
"testing"
"time"

"github.com/go-kit/kit/log/term"
"github.com/ipfs/go-ipfs/core/coreapi"
coremock "github.com/ipfs/go-ipfs/core/mock"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"

"path"

abcicli "github.com/lazyledger/lazyledger-core/abci/client"
"github.com/lazyledger/lazyledger-core/abci/example/counter"
"github.com/lazyledger/lazyledger-core/abci/example/kvstore"
Expand Down Expand Up @@ -191,7 +193,8 @@ func decideProposal(
round int32,
) (proposal *types.Proposal, block *types.Block) {
cs1.mtx.Lock()
block, blockParts := cs1.createProposalBlock()
block = cs1.createProposalBlock()
cs1.shareProposalBlock(block)
validRound := cs1.ValidRound
chainID := cs1.state.ChainID
cs1.mtx.Unlock()
Expand All @@ -200,7 +203,7 @@ func decideProposal(
}

// Make proposal
polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()}
polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash()}
proposal = types.NewProposal(height, round, polRound, propBlockID, &block.DataAvailabilityHeader)
p, err := proposal.ToProto()
if err != nil {
Expand Down Expand Up @@ -425,7 +428,7 @@ func loadPrivValidator(config *cfg.Config) *privval.FilePV {
return privValidator
}

func randState(nValidators int) (*State, []*validatorStub) {
func randState(t *testing.T, nValidators int) (*State, []*validatorStub) {
// Get State
state, privVals := randGenesisState(nValidators, false, 10)

Expand All @@ -439,7 +442,43 @@ func randState(nValidators int) (*State, []*validatorStub) {
// since cs1 starts at 1
incrementHeight(vss[1:]...)

return cs, vss
return withIPFS(t, cs), vss
}

func withIPFS(t *testing.T, s *State) *State {
ipfsNode, err := coremock.NewMockNode()
if err != nil {
t.Fatal(err)
}

s.IpfsAPI, err = coreapi.NewCoreAPI(ipfsNode)
if err != nil {
t.Fatal(err)
}

return s
}

func withIPFSNet(t *testing.T, ss []*State) []*State {
ctx := context.TODO()
net, err := mocknet.FullMeshConnected(ctx, len(ss))
if err != nil {
t.Fatal(err)
}

for _, s := range ss {
nd, err := coremock.MockPublicNode(ctx, net)
if err != nil {
t.Fatal(err)
}

s.IpfsAPI, err = coreapi.NewCoreAPI(nd)
if err != nil {
t.Fatal(err)
}
}

return ss
}

//-------------------------------------------------------------------------------
Expand Down
22 changes: 11 additions & 11 deletions consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil, mempl.TxInfo{})
assert.Nil(t, err)
propBlock, _ := css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlock := css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlockParts := propBlock.MakePartSet(partSize)
blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}

Expand All @@ -377,7 +377,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
proposal.Signature = p.Signature

// set the proposal block
if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
if err := css[0].SetProposal(proposal, "some peer"); err != nil {
t.Fatal(err)
}
ensureNewProposal(proposalCh, height, round)
Expand All @@ -395,7 +395,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil, mempl.TxInfo{})
assert.Nil(t, err)
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlockParts = propBlock.MakePartSet(partSize)
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}

Expand All @@ -408,7 +408,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
proposal.Signature = p.Signature

// set the proposal block
if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
if err := css[0].SetProposal(proposal, "some peer"); err != nil {
t.Fatal(err)
}
ensureNewProposal(proposalCh, height, round)
Expand All @@ -433,7 +433,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)
err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil, mempl.TxInfo{})
assert.Nil(t, err)
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlockParts = propBlock.MakePartSet(partSize)
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
newVss := make([]*validatorStub, nVals+1)
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
proposal.Signature = p.Signature

// set the proposal block
if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
if err := css[0].SetProposal(proposal, "some peer"); err != nil {
t.Fatal(err)
}
ensureNewProposal(proposalCh, height, round)
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil, mempl.TxInfo{})
assert.Nil(t, err)
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
propBlockParts = propBlock.MakePartSet(partSize)
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
newVss = make([]*validatorStub, nVals+3)
Expand All @@ -526,7 +526,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
proposal.Signature = p.Signature

// set the proposal block
if err := css[0].SetProposalAndBlock(proposal, propBlock, propBlockParts, "some peer"); err != nil {
if err := css[0].SetProposal(proposal, "some peer"); err != nil {
t.Fatal(err)
}
ensureNewProposal(proposalCh, height, round)
Expand Down Expand Up @@ -965,11 +965,11 @@ func makeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Bl
for i := 0; i < n; i++ {
height := int64(i + 1)

block, parts := makeBlock(*state, prevBlock, prevBlockMeta, privVal, height)
block := makeBlock(*state, prevBlock, prevBlockMeta, privVal, height)
blocks = append(blocks, block)

prevBlock = block
prevBlockMeta = types.NewBlockMeta(block, parts)
prevBlockMeta = types.NewBlockMeta(block, nil)

// update state
state.AppHash = []byte{appHeight}
Expand All @@ -981,7 +981,7 @@ func makeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Bl
}

func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta,
privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) {
privVal types.PrivValidator, height int64) *types.Block {

lastCommit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
if height > 1 {
Expand Down
Loading