Ejemplo n.º 1
0
func coreKnown(chainID, csvFile, pubKeys string) ([]byte, error) {
	var genDoc *stypes.GenesisDoc
	var err error
	// either we pass the name of a csv file or we read a priv_validator over stdin
	if csvFile != "" {
		var csvValidators, csvAccounts string
		csvFiles := strings.Split(csvFile, ",")
		csvValidators = csvFiles[0]
		if len(csvFiles) > 1 {
			csvAccounts = csvFiles[1]
		}
		pubkeys, amts, names, perms, setbits, err := parseCsv(csvValidators)
		if err != nil {
			return nil, err
		}

		if csvAccounts == "" {
			genDoc = newGenDoc(chainID, len(pubkeys), len(pubkeys))
			for i, pk := range pubkeys {
				genDocAddAccountAndValidator(genDoc, pk, amts[i], names[i], perms[i], setbits[i], i)
			}
		} else {
			pubkeysA, amtsA, namesA, permsA, setbitsA, err := parseCsv(csvAccounts)
			if err != nil {
				return nil, err
			}
			genDoc = newGenDoc(chainID, len(pubkeys), len(pubkeysA))
			for i, pk := range pubkeys {
				genDocAddValidator(genDoc, pk, amts[i], names[i], perms[i], setbits[i], i)
			}
			for i, pk := range pubkeysA {
				genDocAddAccount(genDoc, pk, amtsA[i], namesA[i], permsA[i], setbitsA[i], i)
			}
		}
	} else if pubKeys != "" {
		pubkeys := strings.Split(pubKeys, ",")
		amt := int64(1) << 50
		pubKeys := pubKeyStringsToPubKeys(pubkeys)
		genDoc = newGenDoc(chainID, len(pubkeys), len(pubkeys))

		for i, pk := range pubKeys {
			genDocAddAccountAndValidator(genDoc, pk, amt, "", ptypes.DefaultPermFlags, ptypes.DefaultPermFlags, i)
		}
	} else {
		privJSON := readStdinTimeout()
		genDoc = genesisFromPrivValBytes(chainID, privJSON)
	}

	buf, buf2, n := new(bytes.Buffer), new(bytes.Buffer), new(int64)
	wire.WriteJSON(genDoc, buf, n, &err)
	if err != nil {
		return nil, err
	}
	if err := json.Indent(buf2, buf.Bytes(), "", "\t"); err != nil {
		return nil, err
	}
	genesisBytes := buf2.Bytes()

	return genesisBytes, nil
}
Ejemplo n.º 2
0
func (tx *PermissionsTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
	wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err)
	wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"args":"`, TxTypePermissions)), w, n, err)
	wire.WriteJSON(tx.PermArgs, w, n, err)
	wire.WriteTo([]byte(`","input":`), w, n, err)
	tx.Input.WriteSignBytes(w, n, err)
	wire.WriteTo([]byte(`}]}`), w, n, err)
}
Ejemplo n.º 3
0
func WriteRPCResponse(w http.ResponseWriter, res RPCResponse) {
	buf, n, err := new(bytes.Buffer), int64(0), error(nil)
	wire.WriteJSON(res, buf, &n, &err)
	if err != nil {
		log.Error("Failed to write RPC response", "error", err, "res", res)
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	w.Write(buf.Bytes())
}
Ejemplo n.º 4
0
func binaryWriter(args ...interface{}) ([]interface{}, error) {
	list := []interface{}{}
	for _, a := range args {
		buf, n, err := new(bytes.Buffer), new(int64), new(error)
		wire.WriteJSON(a, buf, n, err)
		if *err != nil {
			return nil, *err
		}
		list = append(list, buf.Bytes())

	}
	return list, nil
}
Ejemplo n.º 5
0
func argsToJson(args ...interface{}) ([]string, error) {
	l := len(args)
	jsons := make([]string, l)
	n, err := new(int64), new(error)
	for i, a := range args {
		buf := new(bytes.Buffer)
		wire.WriteJSON(a, buf, n, err)
		if *err != nil {
			return nil, *err
		}
		jsons[i] = string(buf.Bytes())
	}
	return jsons, nil
}
Ejemplo n.º 6
0
func coreKnown(chainID, csvFile, pubKeys string) ([]byte, error) {
	var genDoc *stypes.GenesisDoc
	var err error
	// either we pass the name of a csv file or we read a priv_validator over stdin
	if csvFile != "" {
		pubkeys, amts, names, perms, setbits := parseCsv(csvFile)

		// convert amts to ints
		amt := make([]int64, len(amts))
		for i, a := range amts {
			if amt[i], err = strconv.ParseInt(a, 10, 64); err != nil {
				return nil, fmt.Errorf("Invalid amount: %v", err)
			}
		}

		// convert pubkey hex strings to struct
		pubKeys := pubKeyStringsToPubKeys(pubkeys)

		genDoc = newGenDoc(chainID, len(pubKeys), len(pubKeys))
		for i, pk := range pubKeys {
			genDocAddAccountAndValidator(genDoc, pk, amt[i], names[i], perms[i], setbits[i], i)
		}
	} else if pubKeys != "" {
		pubkeys := strings.Split(pubKeys, " ")
		amt := int64(1) << 50
		pubKeys := pubKeyStringsToPubKeys(pubkeys)

		genDoc = newGenDoc(chainID, len(pubKeys), len(pubKeys))
		for i, pk := range pubKeys {
			genDocAddAccountAndValidator(genDoc, pk, amt, "", ptypes.DefaultPermFlags, ptypes.DefaultPermFlags, i)
		}
	} else {
		privJSON := readStdinTimeout()
		genDoc = genesisFromPrivValBytes(chainID, privJSON)
	}

	buf, buf2, n := new(bytes.Buffer), new(bytes.Buffer), new(int64)
	wire.WriteJSON(genDoc, buf, n, &err)
	if err != nil {
		return nil, err
	}
	if err := json.Indent(buf2, buf.Bytes(), "", "\t"); err != nil {
		return nil, err
	}
	genesisBytes := buf2.Bytes()

	return genesisBytes, nil
}
Ejemplo n.º 7
0
func coreRandom(N int, chainID string) (genesisBytes []byte, privVals []*types.PrivValidator, err error) {
	fmt.Println("Generating accounts ...")
	genDoc, _, privVals := state.RandGenesisDoc(N, true, 100000, N, false, 1000)

	genDoc.ChainID = chainID

	// RandGenesisDoc produces random accounts and validators.
	// Give the validators accounts:
	genDoc.Accounts = make([]stypes.GenesisAccount, N)
	for i, pv := range privVals {
		genDoc.Accounts[i] = stypes.GenesisAccount{
			Address: pv.Address,
			Amount:  int64(2) << 50,
		}
	}

	buf, buf2, n := new(bytes.Buffer), new(bytes.Buffer), new(int64)
	wire.WriteJSON(genDoc, buf, n, &err)
	if err != nil {
		return
	}
	if err = json.Indent(buf2, buf.Bytes(), "", "\t"); err != nil {
		return
	}
	genesisBytes = buf2.Bytes()

	// create directory to save priv validators and genesis.json
	if DirFlag == "" {
		DirFlag = path.Join(DataContainersPath, chainID)
	}
	if _, err = os.Stat(DirFlag); err != nil {
		if err = os.MkdirAll(DirFlag, 0700); err != nil {
			return
		}
	}

	for i, v := range privVals {
		buf, n = new(bytes.Buffer), new(int64)
		wire.WriteJSON(v, buf, n, &err)
		if err != nil {
			return
		}
		valBytes := buf.Bytes()
		if len(privVals) > 1 {
			mulDir := path.Join(DirFlag, fmt.Sprintf("%s_%d", chainID, i))
			if err = os.MkdirAll(mulDir, 0700); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(mulDir, "priv_validator.json"), valBytes, 0600); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(mulDir, "genesis.json"), genesisBytes, 0644); err != nil {
				return
			}
		} else {
			if err = ioutil.WriteFile(path.Join(DirFlag, "priv_validator.json"), valBytes, 0600); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(DirFlag, "genesis.json"), genesisBytes, 0644); err != nil {
				return
			}
		}
	}
	return
}
Ejemplo n.º 8
0
// dump the latest state to json
func CoreDump(dumpval bool) []byte {
	// Get State
	stateDB := dbm.GetDB("state")
	st := sm.LoadState(stateDB)
	if st == nil {
		Exit(fmt.Errorf("Error: state loaded from %s is nil!", config.GetString("db_dir")))
	}

	stJ := new(State)

	//default is true, flag omits vals from dump
	if dumpval {
		stJ.BondedValidators = st.BondedValidators
		stJ.LastBondedValidators = st.LastBondedValidators
		stJ.UnbondingValidators = st.UnbondingValidators
	}
	// iterate through accounts tree
	// track storage roots as we go
	storageRoots := [][]byte{}
	st.GetAccounts().Iterate(func(key interface{}, value interface{}) (stopped bool) {
		acc := value.(*acm.Account)
		stJ.Accounts = append(stJ.Accounts, acc)
		storageRoots = append(storageRoots, acc.StorageRoot)
		return false
	})

	// grab all storage
	for i, root := range storageRoots {
		if len(root) == 0 {
			continue
		}
		accStorage := &AccountStorage{Address: stJ.Accounts[i].Address}

		storage := merkle.NewIAVLTree(wire.BasicCodec, wire.BasicCodec, 1024, stateDB)
		storage.Load(root)
		storage.Iterate(func(key interface{}, value interface{}) (stopped bool) {
			k, v := key.([]byte), value.([]byte)
			accStorage.Storage = append(accStorage.Storage, &Storage{k, v})
			return false
		})
		stJ.AccountsStorage = append(stJ.AccountsStorage, accStorage)
	}

	// get all validator infos
	if dumpval {
		st.GetValidatorInfos().Iterate(func(key interface{}, value interface{}) (stopped bool) {
			vi := value.(*types.ValidatorInfo)
			stJ.ValidatorInfos = append(stJ.ValidatorInfos, vi)
			return false
		})
	}
	// get all name entries
	st.GetNames().Iterate(func(key interface{}, value interface{}) (stopped bool) {
		name := value.(*types.NameRegEntry)
		stJ.NameReg = append(stJ.NameReg, name)
		return false
	})

	w, n, err := new(bytes.Buffer), new(int64), new(error)
	wire.WriteJSON(stJ, w, n, err)

	IfExit(*err)
	w2 := new(bytes.Buffer)
	json.Indent(w2, w.Bytes(), "", "\t")
	return w2.Bytes()
}
Ejemplo n.º 9
0
func coreRandom(N int, chainID, pubKeys, roots, csvFile string, noVals bool) (genesisBytes []byte, privVals []*types.PrivValidator, err error) {
	fmt.Println("Generating accounts ...")
	genDoc, _, privVals := stypes.RandGenesisDoc(N, true, 100000, N, false, 1000)

	genDoc.ChainID = chainID

	// RandGenesisDoc produces random accounts and validators.
	genDoc.Accounts = make([]stypes.GenesisAccount, 0)

	if !noVals {
		perms := ptypes.DefaultPermFlags
		// Give the validators accounts:
		for _, pv := range privVals {
			genDocAddAccount(genDoc, pv.PubKey, int64(2)<<50, "", perms, perms, -1)
		}
	}

	if pubKeys != "" {
		pubkeys := strings.Split(pubKeys, ",")
		amt := int64(1) << 50
		pubKeys := pubKeyStringsToPubKeys(pubkeys)

		for _, pk := range pubKeys {
			perms := ptypes.DefaultPermFlags
			genDocAddAccount(genDoc, pk, amt, "", perms, perms, -1)
		}
	}

	if roots != "" {
		pubkeys := strings.Split(pubKeys, ",")
		amt := int64(1) << 50
		pubKeys := pubKeyStringsToPubKeys(pubkeys)

		for _, pk := range pubKeys {
			perms := ptypes.AllPermFlags
			genDocAddAccount(genDoc, pk, amt, "", perms, perms, -1)
		}
	}

	if csvFile != "" {
		pubkeys, amts, names, perms, setbits, err := parseCsv(csvFile)
		if err != nil {
			return nil, nil, err
		}

		for i, pk := range pubkeys {
			genDocAddAccount(genDoc, pk, amts[i], names[i], perms[i], setbits[i], -1)
		}
	}

	buf, buf2, n := new(bytes.Buffer), new(bytes.Buffer), new(int64)
	wire.WriteJSON(genDoc, buf, n, &err)
	if err != nil {
		return
	}
	if err = json.Indent(buf2, buf.Bytes(), "", "\t"); err != nil {
		return
	}
	genesisBytes = buf2.Bytes()

	// create directory to save priv validators and genesis.json
	if DirFlag == "" {
		DirFlag = path.Join(BlockchainsPath, chainID)
	}
	if _, err = os.Stat(DirFlag); err != nil {
		if err = os.MkdirAll(DirFlag, 0700); err != nil {
			return
		}
	}

	for i, v := range privVals {
		buf, n = new(bytes.Buffer), new(int64)
		wire.WriteJSON(v, buf, n, &err)
		if err != nil {
			return
		}
		valBytes := buf.Bytes()
		if len(privVals) > 1 {
			mulDir := path.Join(DirFlag, fmt.Sprintf("%s_%d", chainID, i))
			if err = os.MkdirAll(mulDir, 0700); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(mulDir, "priv_validator.json"), valBytes, 0600); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(mulDir, "genesis.json"), genesisBytes, 0644); err != nil {
				return
			}
		} else {
			if err = ioutil.WriteFile(path.Join(DirFlag, "priv_validator.json"), valBytes, 0600); err != nil {
				return
			}
			if err = ioutil.WriteFile(path.Join(DirFlag, "genesis.json"), genesisBytes, 0644); err != nil {
				return
			}
		}
	}
	return
}