Exemple #1
0
func simpleConsensusState(nValidators int) ([]*ConsensusState, []*types.PrivValidator) {
	// Get State
	state, privAccs, privVals := sm.RandGenesisState(10, true, 1000, nValidators, false, 10)
	_, _ = privAccs, privVals

	fmt.Println(state.BondedValidators)

	css := make([]*ConsensusState, nValidators)
	for i := 0; i < nValidators; i++ {
		// Get BlockStore
		blockDB := dbm.NewMemDB()
		blockStore := bc.NewBlockStore(blockDB)

		// Make MempoolReactor
		mempool := mempl.NewMempool(state.Copy())
		mempoolReactor := mempl.NewMempoolReactor(mempool)

		mempoolReactor.SetSwitch(p2p.NewSwitch())

		// Make ConsensusReactor
		cs := NewConsensusState(state, blockStore, mempoolReactor)
		cs.SetPrivValidator(privVals[i])

		evsw := events.NewEventSwitch()
		cs.SetFireable(evsw)

		// read off the NewHeightStep
		<-cs.NewStepCh()

		css[i] = cs
	}

	return css, privVals
}
Exemple #2
0
func randConsensusState() (*ConsensusState, []*types.PrivValidator) {
	state, _, privValidators := sm.RandGenesisState(20, false, 1000, 10, false, 1000)
	blockStore := bc.NewBlockStore(dbm.NewMemDB())
	mempool := mempl.NewMempool(state)
	mempoolReactor := mempl.NewMempoolReactor(mempool)
	cs := NewConsensusState(state, blockStore, mempoolReactor)
	return cs, privValidators
}
Exemple #3
0
func TestBroadcast(t *testing.T) {
	state, privAccs, _ := sm.RandGenesisState(6, false, 10000, 1, true, 100)
	mempool := NewMempool(state)
	reactor := NewMempoolReactor(mempool)
	reactor.Start()

	lastAcc := privAccs[5] // we save him (his tx wont become invalid)
	privAccs = privAccs[:5]

	peer := newPeer(t, state)
	newBlockChan := make(chan ResetInfo)
	tickerChan := make(chan time.Time)
	go reactor.broadcastTxRoutine(tickerChan, newBlockChan, peer)

	// we don't broadcast any before updating
	fmt.Println("dont broadcast any")
	addTxs(t, mempool, lastAcc, privAccs)
	block := makeBlock(mempool)
	ri := mempool.ResetForBlockAndState(block, state)
	newBlockChan <- ri
	peer.height = ri.Height
	tickerChan <- time.Now()
	pullTxs(t, peer, len(mempool.txs)) // should have sent whatever txs are left (3)

	toBroadcast := []int{1, 3, 7, 9, 11, 12, 18, 20, 21, 28, 29, 30, 31, 34, 35, 36, 50, 90, 99, 100}
	for _, N := range toBroadcast {
		peer = resetPeer(t, reactor, mempool, state, tickerChan, newBlockChan, peer)

		// we broadcast N txs before updating
		fmt.Println("broadcast", N)
		addTxs(t, mempool, lastAcc, privAccs)
		txsToSendPerCheck = N
		tickerChan <- time.Now()
		pullTxs(t, peer, txsToSendPerCheck) // should have sent N txs
		block = makeBlock(mempool)
		ri := mempool.ResetForBlockAndState(block, state)
		newBlockChan <- ri
		peer.height = ri.Height
		txsToSendPerCheck = 100
		tickerChan <- time.Now()
		left := len(mempool.txs)
		if N > 99 {
			left -= 3
		} else if N > 29 {
			left -= 2
		} else if N > 28 {
			left -= 1
		}
		pullTxs(t, peer, left) // should have sent whatever txs are left that havent been sent
	}
}
Exemple #4
0
// Add txs. Grab chunks to put in block. All the others become invalid because of nonce errors except those in notInvalidNotIncluded
func TestResetInfo(t *testing.T) {
	amtPerAccount := int64(100000)
	state, privAccs, _ := sm.RandGenesisState(6, false, amtPerAccount, 1, true, 100)

	mempool := NewMempool(state)

	lastAcc := privAccs[5] // we save him (his tx wont become invalid)
	privAccs = privAccs[:5]

	txs := addTxs(t, mempool, lastAcc, privAccs)

	// its actually an invalid block since we're skipping nonces
	// but all we care about is how the mempool responds after
	block := makeBlock(mempool)

	ri := mempool.ResetForBlockAndState(block, state)

	if len(ri.Included) != len(TestResetInfoData.Included) {
		t.Fatalf("invalid number of included ranges. Got %d, expected %d\n", len(ri.Included), len(TestResetInfoData.Included))
	}

	if len(ri.Invalid) != len(TestResetInfoData.Invalid) {
		t.Fatalf("invalid number of invalid ranges. Got %d, expected %d\n", len(ri.Invalid), len(TestResetInfoData.Invalid))
	}

	for i, rid := range ri.Included {
		inc := TestResetInfoData.Included[i]
		if rid.Start != inc.Start {
			t.Fatalf("Invalid start of range. Got %d, expected %d\n", inc.Start, rid.Start)
		}
		if rid.Length != inc.Length {
			t.Fatalf("Invalid length of range. Got %d, expected %d\n", inc.Length, rid.Length)
		}
	}

	txs = mempool.GetProposalTxs()
	if len(txs) != len(notInvalidNotIncluded) {
		t.Fatalf("Expected %d txs left in mempool. Got %d", len(notInvalidNotIncluded), len(txs))
	}
}