// Get a connection to the proxyAppConn addr. // Check the current hash, and panic if it doesn't match. func getProxyApp(addr string, hash []byte) (proxyAppConn proxy.AppConn) { // use local app (for testing) if addr == "local" { app := example.NewCounterApplication(true) mtx := new(sync.Mutex) proxyAppConn = proxy.NewLocalAppConn(mtx, app) } else { proxyConn, err := Connect(addr) if err != nil { Exit(Fmt("Failed to connect to proxy for mempool: %v", err)) } remoteApp := proxy.NewRemoteAppConn(proxyConn, 1024) remoteApp.Start() proxyAppConn = remoteApp } // Check the hash currentHash, err := proxyAppConn.GetHashSync() if err != nil { PanicCrisis(Fmt("Error in getting proxyAppConn hash: %v", err)) } if !bytes.Equal(hash, currentHash) { PanicCrisis(Fmt("ProxyApp hash does not match. Expected %X, got %X", hash, currentHash)) } return proxyAppConn }
func simpleConsensusState(nValidators int) (*ConsensusState, []*validatorStub) { // Get State state, privVals := randGenesisState(nValidators, false, 10) // fmt.Println(state.Validators) vss := make([]*validatorStub, nValidators) // make consensus state for lead validator // Get BlockStore blockDB := dbm.NewMemDB() blockStore := bc.NewBlockStore(blockDB) // one for mempool, one for consensus mtx, app := new(sync.Mutex), example.NewCounterApplication(false) proxyAppConnMem := proxy.NewLocalAppConn(mtx, app) proxyAppConnCon := proxy.NewLocalAppConn(mtx, app) // Make Mempool mempool := mempl.NewMempool(proxyAppConnMem) // Make ConsensusReactor cs := NewConsensusState(state, proxyAppConnCon, blockStore, mempool) cs.SetPrivValidator(privVals[0]) evsw := events.NewEventSwitch() cs.SetEventSwitch(evsw) evsw.Start() // start the transition routines // cs.startRoutines() for i := 0; i < nValidators; i++ { vss[i] = NewValidatorStub(privVals[i]) } // since cs1 starts at 1 incrementHeight(vss[1:]...) return cs, vss }
func TestSerialReap(t *testing.T) { app := counter.NewCounterApplication(true) app.SetOption("serial", "on") mtx := new(sync.Mutex) appConnMem := proxy.NewLocalAppConn(mtx, app) appConnCon := proxy.NewLocalAppConn(mtx, app) mempool := NewMempool(appConnMem) appendTxsRange := func(start, end int) { // Append some txs. for i := start; i < end; i++ { // This will succeed txBytes := make([]byte, 8) binary.BigEndian.PutUint64(txBytes, uint64(i)) err := mempool.CheckTx(txBytes, nil) if err != nil { t.Fatal("Error after CheckTx: %v", err) } // This will fail because not serial (incrementing) // However, error should still be nil. // It just won't show up on Reap(). err = mempool.CheckTx(txBytes, nil) if err != nil { t.Fatal("Error after CheckTx: %v", err) } } } reapCheck := func(exp int) { txs := mempool.Reap() if len(txs) != exp { t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs)) } } updateRange := func(start, end int) { txs := make([]types.Tx, 0) for i := start; i < end; i++ { txBytes := make([]byte, 8) binary.BigEndian.PutUint64(txBytes, uint64(i)) txs = append(txs, txBytes) } mempool.Update(0, txs) } commitRange := func(start, end int) { // Append some txs. for i := start; i < end; i++ { txBytes := make([]byte, 8) binary.BigEndian.PutUint64(txBytes, uint64(i)) code, result, logStr := appConnCon.AppendTx(txBytes) if code != tmsp.CodeType_OK { t.Errorf("Error committing tx. Code:%v result:%X log:%v", code, result, logStr) } } hash, log := appConnCon.Commit() if len(hash) != 8 { t.Errorf("Error committing. Hash:%X log:%v", hash, log) } } //---------------------------------------- // Append some txs. appendTxsRange(0, 100) // Reap the txs. reapCheck(100) // Reap again. We should get the same amount reapCheck(100) // Append 0 to 999, we should reap 900 new txs // because 100 were already counted. appendTxsRange(0, 1000) // Reap the txs. reapCheck(1000) // Reap again. We should get the same amount reapCheck(1000) // Commit from the conensus AppConn commitRange(0, 500) updateRange(0, 500) // We should have 500 left. reapCheck(500) // Append 100 invalid txs and 100 valid txs appendTxsRange(900, 1100) // We should have 600 now. reapCheck(600) }