Ejemplo n.º 1
0
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(wire.BasicCodec, wire.BasicCodec, 0, db)
	for key, value := range records {
		t1.Set(key, value)
	}
	t1.Save()

	hash, _ := t1.HashWithCount()

	// Load a tree
	t2 := NewIAVLTree(wire.BasicCodec, wire.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)
		}
	}
}
Ejemplo n.º 2
0
func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*acm.PrivAccount, []*types.PrivValidator) {
	db := dbm.NewMemDB()
	genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded)
	s0 := MakeGenesisState(db, genDoc)
	s0.Save()
	return s0, privAccounts, privValidators
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
0
func Status() (*ctypes.ResponseStatus, error) {
	db := dbm.NewMemDB()
	genesisState := sm.MakeGenesisStateFromFile(db, config.GetString("genesis_file"))
	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.ResponseStatus{
		Moniker:           config.GetString("moniker"),
		Network:           config.GetString("network"),
		Version:           config.GetString("version"),
		GenesisHash:       genesisHash,
		PubKey:            privValidator.PubKey,
		LatestBlockHash:   latestBlockHash,
		LatestBlockHeight: latestHeight,
		LatestBlockTime:   latestBlockTime}, nil
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
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
}
Ejemplo n.º 7
0
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)
	}
}
Ejemplo n.º 8
0
func TestIAVLProof(t *testing.T) {

	// Convenient wrapper around wire.BasicCodec.
	toBytes := func(o interface{}) []byte {
		buf, n, err := new(bytes.Buffer), int64(0), error(nil)
		wire.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(wire.BasicCodec, wire.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
	})

}
Ejemplo n.º 9
0
func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numValidators int, randBonded bool, minBonded uint64) (*State, []*account.PrivAccount, []*PrivValidator) {
	db := dbm.NewMemDB()
	accounts := make([]GenesisAccount, numAccounts)
	privAccounts := make([]*account.PrivAccount, numAccounts)
	for i := 0; i < numAccounts; i++ {
		account, privAccount := RandAccount(randBalance, minBalance)
		accounts[i] = GenesisAccount{
			Address: account.Address,
			Amount:  account.Balance,
		}
		privAccounts[i] = privAccount
	}
	validators := make([]GenesisValidator, numValidators)
	privValidators := make([]*PrivValidator, numValidators)
	for i := 0; i < numValidators; i++ {
		valInfo, _, privVal := RandValidator(randBonded, minBonded)
		validators[i] = GenesisValidator{
			PubKey: valInfo.PubKey,
			Amount: valInfo.FirstBondAmount,
			UnbondTo: []GenesisAccount{
				{
					Address: valInfo.PubKey.Address(),
					Amount:  valInfo.FirstBondAmount,
				},
			},
		}
		privValidators[i] = privVal
	}
	sort.Sort(PrivValidatorsByAddress(privValidators))
	s0 := MakeGenesisState(db, &GenesisDoc{
		GenesisTime: time.Now(),
		Accounts:    accounts,
		Validators:  validators,
	})
	s0.Save()
	return s0, privAccounts, privValidators
}