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 }
func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*account.PrivAccount, []*PrivValidator) { db := dbm.NewMemDB() genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded) s0 := MakeGenesisState(db, genDoc) s0.Save() return s0, privAccounts, privValidators }
func Status() (*ctypes.ResultStatus, error) { db := dbm.NewMemDB() if genesisState == nil { genesisState = sm.MakeGenesisState(db, genDoc) } genesisHash := genesisState.Hash() latestHeight := blockStore.Height() var ( latestBlockMeta *types.BlockMeta latestBlockHash []byte latestBlockTime int64 ) if latestHeight != 0 { latestBlockMeta = blockStore.LoadBlockMeta(latestHeight) latestBlockHash = latestBlockMeta.Hash latestBlockTime = latestBlockMeta.Header.Time.UnixNano() } return &ctypes.ResultStatus{ NodeInfo: p2pSwitch.NodeInfo(), GenesisHash: genesisHash, PubKey: privValidator.PubKey, LatestBlockHash: latestBlockHash, LatestBlockHeight: latestHeight, LatestBlockTime: latestBlockTime}, nil }
func TestPersistence(t *testing.T) { db := db.NewMemDB() // Create some random key value pairs records := make(map[string]string) for i := 0; i < 10000; i++ { records[randstr(20)] = randstr(20) } // Construct some tree and save it t1 := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, db) for key, value := range records { t1.Set(key, value) } t1.Save() hash, _ := t1.HashWithCount() // Load a tree t2 := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, db) t2.Load(hash) for key, value := range records { _, t2value := t2.Get(key) if t2value != value { t.Fatalf("Invalid value. Expected %v, got %v", value, t2value) } } }
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 }
// Call Start() after adding the listeners. func (n *Node) Start() error { n.book.Start() genState := sm.MakeGenesisState(dbm.NewMemDB(), n.genDoc) n.sw.SetNodeInfo(makeNodeInfo(n.sw, n.privKey, genState.Hash())) n.sw.SetNodePrivKey(n.privKey) _, err := n.sw.Start() return err }
func TestGenesisMakeState(t *testing.T) { genDoc := GenesisDocFromJSON([]byte(g1)) db := tdb.NewMemDB() st := MakeGenesisState(db, genDoc) acc := st.GetAccount(addr1) v, _ := acc.Permissions.Base.Get(ptypes.Send) if v != (send1 > 0) { t.Fatalf("Incorrect permission for send. Got %v, expected %v\n", v, send1 > 0) } }
func TestCompatibleNodeInfo(t *testing.T) { sw := p2p.NewSwitch() priv1, priv2 := types.GenPrivValidator(), types.GenPrivValidator() genDoc1, _, _ := stypes.RandGenesisDoc(5, true, 100, 4, true, 1000) genState1 := sm.MakeGenesisState(dbm.NewMemDB(), genDoc1) genDoc2, _, _ := stypes.RandGenesisDoc(5, true, 100, 4, true, 1000) genState2 := sm.MakeGenesisState(dbm.NewMemDB(), genDoc2) // incompatible genesis states n1 := makeNodeInfo(sw, priv1.PrivKey, genState1.Hash()) n2 := makeNodeInfo(sw, priv2.PrivKey, genState2.Hash()) if err := n1.CompatibleWith(n2); err == nil { t.Fatalf("Expected nodes to be incompatible due to genesis state") } // incompatible chain ids copy(n2.Genesis, n1.Genesis) n2.ChainID = "incryptowetrust" if err := n1.CompatibleWith(n2); err == nil { t.Fatalf("Expected nodes to be incompatible due to chain ID") } // incompatible versions n2.ChainID = n1.ChainID v := n1.Version.Tendermint spl := strings.Split(v, ".") n, err := strconv.Atoi(spl[0]) if err != nil { t.Fatalf(err.Error()) } spl[0] = strconv.Itoa(n + 1) n2.Version.Tendermint = strings.Join(spl, ".") if err := n1.CompatibleWith(n2); err == nil { t.Fatalf("Expected nodes to be incompatible due to major version") } // compatible n2.Version.Tendermint = n1.Version.Tendermint if err := n1.CompatibleWith(n2); err != nil { t.Fatalf("Expected nodes to be compatible") } }
// Get the status. func (this *blockchain) Info() (*BlockchainInfo, error) { chainId := config.GetString("chain_id") db := dbm.NewMemDB() _, genesisState := state.MakeGenesisStateFromFile(db, config.GetString("genesis_file")) genesisHash := genesisState.Hash() latestHeight := this.blockStore.Height() var latestBlockMeta *types.BlockMeta if latestHeight != 0 { latestBlockMeta = this.blockStore.LoadBlockMeta(latestHeight) } return &BlockchainInfo{ chainId, genesisHash, latestHeight, latestBlockMeta, }, nil }
func TestIAVLProof(t *testing.T) { // Convenient wrapper around binary.BasicCodec. toBytes := func(o interface{}) []byte { buf, n, err := new(bytes.Buffer), int64(0), error(nil) binary.BasicCodec.Encode(o, buf, &n, &err) if err != nil { panic(Fmt("Failed to encode thing: %v", err)) } return buf.Bytes() } // Construct some random tree db := db.NewMemDB() var tree *IAVLTree = NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 100, db) for i := 0; i < 1000; i++ { key, value := randstr(20), randstr(20) tree.Set(key, value) } // Persist the items so far tree.Save() // Add more items so it's not all persisted for i := 0; i < 100; i++ { key, value := randstr(20), randstr(20) tree.Set(key, value) } // Now for each item, construct a proof and verify tree.Iterate(func(key interface{}, value interface{}) bool { proof := tree.ConstructProof(key) if !bytes.Equal(proof.RootHash, tree.Hash()) { t.Errorf("Invalid proof. Expected root %X, got %X", tree.Hash(), proof.RootHash) } testProof(t, proof, toBytes(key), toBytes(value), tree.Hash()) return false }) }
// Get the hash of the genesis block. func (this *blockchain) GenesisHash() ([]byte, error) { db := dbm.NewMemDB() _, genesisState := state.MakeGenesisStateFromFile(db, config.GetString("genesis_file")) return genesisState.Hash(), nil }