func TestProposerSelection(t *testing.T) {
	vset := NewValidatorSet([]*Validator{
		&Validator{
			Address:     []byte("foo"),
			PubKey:      account.PubKeyEd25519(RandBytes(64)),
			BondHeight:  RandInt(),
			VotingPower: 1000,
			Accum:       0,
		},
		&Validator{
			Address:     []byte("bar"),
			PubKey:      account.PubKeyEd25519(RandBytes(64)),
			BondHeight:  RandInt(),
			VotingPower: 300,
			Accum:       0,
		},
		&Validator{
			Address:     []byte("baz"),
			PubKey:      account.PubKeyEd25519(RandBytes(64)),
			BondHeight:  RandInt(),
			VotingPower: 330,
			Accum:       0,
		},
	})
	proposers := []string{}
	for i := 0; i < 100; i++ {
		val := vset.Proposer()
		proposers = append(proposers, string(val.Address))
		vset.IncrementAccum(1)
	}
	expected := `bar foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo foo baz bar foo foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo`
	if expected != strings.Join(proposers, " ") {
		t.Errorf("Expected sequence of proposers was\n%v\nbut got \n%v", expected, strings.Join(proposers, " "))
	}
}
func randValidator_() *Validator {
	return &Validator{
		Address:     RandBytes(20),
		PubKey:      account.PubKeyEd25519(RandBytes(64)),
		BondHeight:  RandInt(),
		VotingPower: RandInt64(),
		Accum:       RandInt64(),
	}
}
// Generates a new validator with private key.
func GenPrivValidator() *PrivValidator {
	privKeyBytes := new([64]byte)
	copy(privKeyBytes[:32], CRandBytes(32))
	pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
	pubKey := account.PubKeyEd25519(pubKeyBytes[:])
	privKey := account.PrivKeyEd25519(privKeyBytes[:])
	return &PrivValidator{
		Address:    pubKey.Address(),
		PubKey:     pubKey,
		PrivKey:    privKey,
		LastHeight: 0,
		LastRound:  0,
		LastStep:   stepNone,
		filePath:   "",
	}
}
Beispiel #4
0
// initialize config and create new node
func init() {
	chainID = config.GetString("chain_id")

	// Save new priv_validator file.
	priv := &types.PrivValidator{
		Address: user[0].Address,
		PubKey:  acm.PubKeyEd25519(user[0].PubKey.(acm.PubKeyEd25519)),
		PrivKey: acm.PrivKeyEd25519(user[0].PrivKey.(acm.PrivKeyEd25519)),
	}
	priv.SetFile(config.GetString("priv_validator_file"))
	priv.Save()

	// TODO: change consensus/state.go timeouts to be shorter

	// start a node
	ready := make(chan struct{})
	go newNode(ready)
	<-ready
}
Beispiel #5
0
func checkCommon(nodeAddr, signAddr, pubkey, addr, amtS, nonceS string) (pub account.PubKey, amt int64, nonce int64, err error) {
	if amtS == "" {
		err = fmt.Errorf("input must specify an amount with the --amt flag")
		return
	}

	var pubKeyBytes []byte
	if pubkey == "" && addr == "" {
		err = fmt.Errorf("at least one of --pubkey or --addr must be given")
		return
	} else if pubkey != "" {
		if addr != "" {
			// NOTE: if --addr given byt MINTX_PUBKEY is set, the pubkey still wins
			// TODO: fix this
			logger.Errorln("you have specified both a pubkey and an address. the pubkey takes precedent")
		}
		pubKeyBytes, err = hex.DecodeString(pubkey)
		if err != nil {
			err = fmt.Errorf("pubkey is bad hex: %v", err)
			return
		}
	} else {
		// grab the pubkey from eris-keys
		pubKeyBytes, err = Pub(addr, signAddr)
		if err != nil {
			err = fmt.Errorf("failed to fetch pubkey for address (%s): %v", addr, err)
			return
		}

	}

	if len(pubKeyBytes) == 0 {
		err = fmt.Errorf("Error resolving public key")
		return
	}

	amt, err = strconv.ParseInt(amtS, 10, 64)
	if err != nil {
		err = fmt.Errorf("amt is misformatted: %v", err)
	}

	var pubArray [32]byte
	copy(pubArray[:], pubKeyBytes)
	pub = account.PubKeyEd25519(pubArray)
	addrBytes := pub.Address()

	if nonceS == "" {
		if nodeAddr == "" {
			err = fmt.Errorf("input must specify a nonce with the --nonce flag or use --node-addr (or MINTX_NODE_ADDR) to fetch the nonce from a node")
			return
		}

		// fetch nonce from node
		client := cclient.NewClient(nodeAddr, "HTTP")
		ac, err2 := client.GetAccount(addrBytes)
		if err2 != nil {
			err = fmt.Errorf("Error connecting to node (%s) to fetch nonce: %s", nodeAddr, err2.Error())
			return
		}
		if ac == nil || ac.Account == nil {
			err = fmt.Errorf("unknown account %X", addrBytes)
			return
		}
		nonce = int64(ac.Account.Sequence) + 1
	} else {
		nonce, err = strconv.ParseInt(nonceS, 10, 64)
		if err != nil {
			err = fmt.Errorf("nonce is misformatted: %v", err)
			return
		}
	}

	return
}
Beispiel #6
0
func randPubKey() account.PubKeyEd25519 {
	var pubKey [32]byte
	copy(pubKey[:], RandBytes(32))
	return account.PubKeyEd25519(pubKey)
}