Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainid #81

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
// Signable is an interface for all signable things.
// It typically removes signatures before serializing.
type Signable interface {
WriteSignBytes(w io.Writer, n *int64, err *error)
WriteSignBytes(chainID string, w io.Writer, n *int64, err *error)
}

// SignBytes is a convenience method for getting the bytes to sign of a Signable.
func SignBytes(o Signable) []byte {
func SignBytes(chainID string, o Signable) []byte {
buf, n, err := new(bytes.Buffer), new(int64), new(error)
o.WriteSignBytes(buf, n, err)
o.WriteSignBytes(chainID, buf, n, err)
if *err != nil {
panic(err)
}
return buf.Bytes()
}

// HashSignBytes is a convenience method for getting the hash of the bytes of a signable
func HashSignBytes(o Signable) []byte {
return merkle.HashFromBinary(SignBytes(o))
func HashSignBytes(chainID string, o Signable) []byte {
return merkle.HashFromBinary(SignBytes(chainID, o))
}

//-----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions account/priv_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func GenPrivAccountFromKey(privKeyBytes [64]byte) *PrivAccount {
}
}

func (privAccount *PrivAccount) Sign(o Signable) Signature {
return privAccount.PrivKey.Sign(SignBytes(o))
func (privAccount *PrivAccount) Sign(chainID string, o Signable) Signature {
return privAccount.PrivKey.Sign(SignBytes(chainID, o))
}

func (privAccount *PrivAccount) String() string {
Expand Down
2 changes: 1 addition & 1 deletion alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Alert(message string) {
log.Error("<!> ALERT <!>\n" + message)
now := time.Now().Unix()
if now-lastAlertUnix > int64(config.GetInt("alert_min_interval")) {
message = fmt.Sprintf("%v:%v", config.GetString("network"), message)
message = fmt.Sprintf("%v:%v", config.GetString("chain_id"), message)
if alertCountSince > 0 {
message = fmt.Sprintf("%v (+%v more since)", message, alertCountSince)
alertCountSince = 0
Expand Down
2 changes: 1 addition & 1 deletion blockchain/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ FOR_LOOP:
firstPartsHeader := firstParts.Header()
// Finally, verify the first block using the second's validation.
err := bcR.state.BondedValidators.VerifyValidation(
first.Hash(), firstPartsHeader, first.Height, second.Validation)
bcR.state.ChainID, first.Hash(), firstPartsHeader, first.Height, second.Validation)
if err != nil {
log.Debug("error in validation", "error", err)
bcR.pool.RedoRequest(first.Height)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tendermint/gen_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ func gen_tx() {
}

// Sign
tx.Inputs[0].Signature = srcPrivKey.Sign(account.SignBytes(tx))
tx.Inputs[0].Signature = srcPrivKey.Sign(account.SignBytes(config.GetString("chain_id"), tx))
fmt.Printf("Signed tx: %X\n", binary.BinaryBytes(tx))
}
8 changes: 4 additions & 4 deletions config/tendermint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func GetConfig(rootDir string) cfg.Config {
}

// Set defaults or panic
if !mapConfig.IsSet("network") {
Exit("Must set 'network'")
if mapConfig.IsSet("chain_id") {
Exit("Cannot set 'chain_id' via config.toml")
}
if mapConfig.IsSet("version") {
Exit("Cannot set 'version' via config.toml")
}
// mapConfig.SetDefault("network", "tendermint_testnet0")
mapConfig.SetDefault("chain_id", "tendermint_testnet_5")
mapConfig.SetDefault("version", "0.3.0") // JAE: changed merkle tree persistence format for merkle proofs.
mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
mapConfig.SetDefault("moniker", "anonymous")
Expand All @@ -82,7 +82,6 @@ func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

network = "tendermint_testnet_5"
moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656"
Expand All @@ -98,6 +97,7 @@ func defaultConfig(moniker string) (defaultConfig string) {
}

var defaultGenesis = `{
"chain_id": "tendermint_testnet_5",
"accounts": [
{
"address": "F81CB9ED0A868BD961C4F5BBC0E39B763B89FCB6",
Expand Down
8 changes: 4 additions & 4 deletions config/tendermint_test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func GetConfig(rootDir string) cfg.Config {
}

// Set defaults or panic
if !mapConfig.IsSet("network") {
Exit("Must set 'network'")
if mapConfig.IsSet("chain_id") {
Exit("Cannot set 'chain_id' via config.toml")
}
if mapConfig.IsSet("version") {
Exit("Cannot set 'version' via config.toml")
}
// mapConfig.SetDefault("network", "tendermint_testnet0")
mapConfig.SetDefault("chain_id", "tendermint_test")
mapConfig.SetDefault("version", "0.3.0")
mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
mapConfig.SetDefault("moniker", "anonymous")
Expand All @@ -90,7 +90,6 @@ func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

network = "tendermint_test"
moniker = "__MONIKER__"
node_laddr = "0.0.0.0:36656"
seeds = ""
Expand All @@ -106,6 +105,7 @@ func defaultConfig(moniker string) (defaultConfig string) {
}

var defaultGenesis = `{
"chain_id" : "tendermint_test",
"accounts": [
{
"address": "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42",
Expand Down
4 changes: 2 additions & 2 deletions consensus/pol.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (pol *POL) Verify(valSet *sm.ValidatorSet) error {
}

talliedVotingPower := uint64(0)
prevoteDoc := account.SignBytes(&types.Vote{
prevoteDoc := account.SignBytes(config.GetString("chain_id"), &types.Vote{
Height: pol.Height, Round: pol.Round, Type: types.VoteTypePrevote,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
Expand All @@ -55,7 +55,7 @@ func (pol *POL) Verify(valSet *sm.ValidatorSet) error {

// Commit vote?
if vote.Round < pol.Round {
voteDoc = account.SignBytes(&types.Vote{
voteDoc = account.SignBytes(config.GetString("chain_id"), &types.Vote{
Height: pol.Height, Round: vote.Round, Type: types.VoteTypeCommit,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
Expand Down
4 changes: 2 additions & 2 deletions consensus/pol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package consensus
import (
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
sm "github.com/tendermint/tendermint/state"
_ "github.com/tendermint/tendermint/config/tendermint_test"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"

"bytes"
Expand All @@ -18,7 +18,7 @@ import (
// Returns the POLVoteSignature pointer, so you can modify it afterwards.
func signAddPOLVoteSignature(val *sm.PrivValidator, valSet *sm.ValidatorSet, vote *types.Vote, pol *POL) *POLVoteSignature {
vote = vote.Copy()
err := val.SignVote(vote)
err := val.SignVote(config.GetString("chain_id"), vote)
if err != nil {
panic(err)
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func (cs *ConsensusState) updateToState(state *sm.State, contiguous bool) {
Address: cs.privValidator.Address,
Height: cs.Height,
}
err := cs.privValidator.SignRebondTx(rebondTx)
err := cs.privValidator.SignRebondTx(cs.state.ChainID, rebondTx)
if err == nil {
err := cs.mempoolReactor.BroadcastTx(rebondTx)
if err != nil {
Expand Down Expand Up @@ -656,7 +656,7 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
txs := cs.mempoolReactor.Mempool.GetProposalTxs()
block = &types.Block{
Header: &types.Header{
Network: config.GetString("network"),
ChainID: cs.state.ChainID,
Height: cs.Height,
Time: time.Now(),
Fees: 0, // TODO fees
Expand Down Expand Up @@ -688,7 +688,7 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {

// Make proposal
proposal := NewProposal(cs.Height, cs.Round, blockParts.Header(), polParts.Header())
err := cs.privValidator.SignProposal(proposal)
err := cs.privValidator.SignProposal(cs.state.ChainID, proposal)
if err == nil {
log.Info("Signed and set proposal", "height", cs.Height, "round", cs.Round, "proposal", proposal)
log.Debug(Fmt("Signed and set proposal block: %v", block))
Expand Down Expand Up @@ -939,7 +939,7 @@ func (cs *ConsensusState) SetProposal(proposal *Proposal) error {
}

// Verify signature
if !cs.Validators.Proposer().PubKey.VerifyBytes(account.SignBytes(proposal), proposal.Signature) {
if !cs.Validators.Proposer().PubKey.VerifyBytes(account.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
return ErrInvalidProposalSignature
}

Expand Down Expand Up @@ -1111,7 +1111,7 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header types.Part
BlockHash: hash,
BlockParts: header,
}
err := cs.privValidator.SignVote(vote)
err := cs.privValidator.SignVote(cs.state.ChainID, vote)
if err == nil {
log.Info("Signed and added vote", "height", cs.Height, "round", cs.Round, "vote", vote)
cs.addVote(cs.privValidator.Address, vote)
Expand Down
8 changes: 4 additions & 4 deletions consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestSetupRound(t *testing.T) {
voteTypes := []byte{types.VoteTypePrevote, types.VoteTypePrecommit, types.VoteTypeCommit}
for _, voteType := range voteTypes {
vote := &types.Vote{Height: 1, Round: 0, Type: voteType} // nil vote
err := val0.SignVote(vote)
err := val0.SignVote(cs.state.ChainID, vote)
if err != nil {
t.Error("Error signing vote: %v", err)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(),
}
err := privValidators[i].SignVote(vote)
err := privValidators[i].SignVote(cs.state.ChainID, vote)
if err != nil {
t.Error("Error signing vote: %v", err)
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(),
}
err := privValidators[i].SignVote(vote)
err := privValidators[i].SignVote(cs.state.ChainID, vote)
if err != nil {
t.Error("Error signing vote: %v", err)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(),
}
err := privValidators[i].SignVote(vote)
err := privValidators[i].SignVote(cs.state.ChainID, vote)
if err != nil {
t.Error("Error signing vote: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (p *Proposal) String() string {
p.BlockParts, p.POLParts, p.Signature)
}

func (p *Proposal) WriteSignBytes(w io.Writer, n *int64, err *error) {
// We hex encode the network name so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"network":"%X"`, config.GetString("network"))), w, n, err)
func (p *Proposal) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id name so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(`,"proposal":{"block_parts":`), w, n, err)
p.BlockParts.WriteSignBytes(w, n, err)
binary.WriteTo([]byte(Fmt(`,"height":%v,"pol_parts":`, p.Height)), w, n, err)
Expand Down
6 changes: 3 additions & 3 deletions consensus/types/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestProposalSignable(t *testing.T) {
POLParts: types.PartSetHeader{222, []byte("polparts")},
Signature: nil,
}
signBytes := account.SignBytes(proposal)
signBytes := account.SignBytes(config.GetString("chain_id"), proposal)
signStr := string(signBytes)
expected := Fmt(`{"network":"%X","proposal":{"block_parts":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_parts":{"hash":"706F6C7061727473","total":222},"round":23456}}`,
config.GetString("network"))
expected := Fmt(`{"chain_id":"%X","proposal":{"block_parts":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_parts":{"hash":"706F6C7061727473","total":222},"round":23456}}`,
config.GetString("chain_id"))
if signStr != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/vote_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (voteSet *VoteSet) Add(address []byte, vote *types.Vote) (bool, uint, error
}

// Check signature.
if !val.PubKey.VerifyBytes(account.SignBytes(vote), vote.Signature) {
if !val.PubKey.VerifyBytes(account.SignBytes(config.GetString("chain_id"), vote), vote.Signature) {
// Bad signature.
return false, 0, types.ErrVoteInvalidSignature
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/vote_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/common/test"
sm "github.com/tendermint/tendermint/state"
_ "github.com/tendermint/tendermint/config/tendermint_test"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"

"testing"
Expand Down Expand Up @@ -50,7 +50,7 @@ func withBlockParts(vote *types.Vote, blockParts types.PartSetHeader) *types.Vot
}

func signAddVote(privVal *sm.PrivValidator, vote *types.Vote, voteSet *VoteSet) (bool, error) {
privVal.SignVoteUnsafe(vote)
privVal.SignVoteUnsafe(config.GetString("chain_id"), vote)
added, _, err := voteSet.Add(privVal.Address, vote)
return added, err
}
Expand Down
8 changes: 4 additions & 4 deletions crawler/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
//---------------------------------------------------------------------------------------
// crawler.Node

// A node is a peer on the network.
// A node is a peer on the network
type Node struct {
Host string
P2PPort uint16
Expand All @@ -32,7 +32,7 @@ type Node struct {
client *NodeClient

LastSeen time.Time
Network string
ChainID string
BlockHeight uint
BlockHistory map[uint]time.Time // when we saw each block
NetInfo *rpctypes.ResponseNetInfo
Expand All @@ -47,10 +47,10 @@ func (n *Node) Address() string {
return fmt.Sprintf("%s:%d", n.Host, n.RPCPort)
}

// Set the basic status and network info for a node from RPC responses
// Set the basic status and chain_id info for a node from RPC responses
func (n *Node) SetInfo(status *rpctypes.ResponseStatus, netinfo *rpctypes.ResponseNetInfo) {
n.LastSeen = time.Now()
n.Network = status.Network
n.ChainID = status.ChainID
n.BlockHeight = status.LatestBlockHeight
n.NetInfo = netinfo
// n.Validator
Expand Down
2 changes: 1 addition & 1 deletion node/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type PrivNodeID struct {
type NodeGreeting struct {
NodeID
Version string
Network string
ChainID string
Message string
Time time.Time
}
Expand Down
4 changes: 3 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func NewNode() *Node {
state = sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
state.Save()
}
// add the chainid to the global config
config.Set("chain_id", state.ChainID)

// Get PrivValidator
var privValidator *sm.PrivValidator
Expand Down Expand Up @@ -212,7 +214,7 @@ func (n *Node) EventSwitch() *events.EventSwitch {

func makeNodeInfo(sw *p2p.Switch) *types.NodeInfo {
nodeInfo := &types.NodeInfo{
Network: config.GetString("network"),
ChainID: config.GetString("chain_id"),
Moniker: config.GetString("moniker"),
Version: config.GetString("version"),
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ func makeSwitchPair(t testing.TB, initSwitch func(*Switch) *Switch) (*Switch, *S
s1 := initSwitch(NewSwitch())
s1.SetNodeInfo(&types.NodeInfo{
Moniker: "switch1",
Network: "testing",
ChainID: "testing",
Version: "123.123.123",
})
s2 := initSwitch(NewSwitch())
s2.SetNodeInfo(&types.NodeInfo{
Moniker: "switch2",
Network: "testing",
ChainID: "testing",
Version: "123.123.123",
})

Expand Down
2 changes: 1 addition & 1 deletion rpc/core/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) {
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
}

txHash := types.TxId(tx)
txHash := types.TxId(mempoolReactor.Mempool.GetState().ChainID, tx)
var createsContract uint8
var contractAddr []byte
// check if creates new contract
Expand Down
Loading