pass chainID through sign interfaces

This commit is contained in:
Ethan Buchman
2015-05-29 18:14:19 -04:00
parent 8a2d9525f0
commit 2045aee9cd
28 changed files with 122 additions and 110 deletions
+3 -3
View File
@@ -21,9 +21,9 @@ type Block struct {
}
// Basic validation that doesn't involve state data.
func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
func (b *Block) ValidateBasic(chainID string, lastBlockHeight uint, lastBlockHash []byte,
lastBlockParts PartSetHeader, lastBlockTime time.Time) error {
if b.ChainID != config.GetString("chain_id") {
if b.ChainID != chainID {
return errors.New("Wrong Block.Header.ChainID")
}
if b.Height != lastBlockHeight+1 {
@@ -276,7 +276,7 @@ func (data *Data) Hash() []byte {
if data.hash == nil {
bs := make([]interface{}, len(data.Txs))
for i, tx := range data.Txs {
bs[i] = account.SignBytes(tx)
bs[i] = account.SignBytes(config.GetString("chain_id"), tx)
}
data.hash = merkle.HashFromBinaries(bs)
}
+14 -14
View File
@@ -42,7 +42,7 @@ Validation Txs:
- DupeoutTx Validator dupes out (equivocates)
*/
type Tx interface {
WriteSignBytes(w io.Writer, n *int64, err *error)
WriteSignBytes(chainID string, w io.Writer, n *int64, err *error)
}
// Types of Tx implementations
@@ -129,9 +129,9 @@ type SendTx struct {
Outputs []*TxOutput `json:"outputs"`
}
func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *SendTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeSend)), w, n, err)
for i, in := range tx.Inputs {
in.WriteSignBytes(w, n, err)
@@ -163,9 +163,9 @@ type CallTx struct {
Data []byte `json:"data"`
}
func (tx *CallTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *CallTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"address":"%X","data":"%X"`, TxTypeCall, tx.Address, tx.Data)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"fee":%v,"gas_limit":%v,"input":`, tx.Fee, tx.GasLimit)), w, n, err)
tx.Input.WriteSignBytes(w, n, err)
@@ -185,9 +185,9 @@ type BondTx struct {
UnbondTo []*TxOutput `json:"unbond_to"`
}
func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *BondTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeBond)), w, n, err)
for i, in := range tx.Inputs {
in.WriteSignBytes(w, n, err)
@@ -219,9 +219,9 @@ type UnbondTx struct {
Signature account.SignatureEd25519 `json:"signature"`
}
func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *UnbondTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"address":"%X","height":%v}]}`, TxTypeUnbond, tx.Address, tx.Height)), w, n, err)
}
@@ -237,9 +237,9 @@ type RebondTx struct {
Signature account.SignatureEd25519 `json:"signature"`
}
func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *RebondTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
// We hex encode the chain_id so we don't deal with escaping issues.
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"address":"%X","height":%v}]}`, TxTypeRebond, tx.Address, tx.Height)), w, n, err)
}
@@ -255,7 +255,7 @@ type DupeoutTx struct {
VoteB Vote `json:"vote_b"`
}
func (tx *DupeoutTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *DupeoutTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
panic("DupeoutTx has no sign bytes")
}
@@ -265,7 +265,7 @@ func (tx *DupeoutTx) String() string {
//-----------------------------------------------------------------------------
func TxId(tx Tx) []byte {
signBytes := account.SignBytes(tx)
func TxId(chainID string, tx Tx) []byte {
signBytes := account.SignBytes(chainID, tx)
return binary.BinaryRipemd160(signBytes)
}
+11 -5
View File
@@ -8,6 +8,12 @@ import (
_ "github.com/tendermint/tendermint/config/tendermint_test"
)
var chainID string
func init() {
chainID = config.GetString("chain_id")
}
func TestSendTxSignable(t *testing.T) {
sendTx := &SendTx{
Inputs: []*TxInput{
@@ -33,7 +39,7 @@ func TestSendTxSignable(t *testing.T) {
},
},
}
signBytes := account.SignBytes(sendTx)
signBytes := account.SignBytes(chainID, sendTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%X","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
config.GetString("chain_id"))
@@ -54,7 +60,7 @@ func TestCallTxSignable(t *testing.T) {
Fee: 222,
Data: []byte("data1"),
}
signBytes := account.SignBytes(callTx)
signBytes := account.SignBytes(chainID, callTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%X","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
config.GetString("chain_id"))
@@ -90,7 +96,7 @@ func TestBondTxSignable(t *testing.T) {
},
},
}
signBytes := account.SignBytes(bondTx)
signBytes := account.SignBytes(chainID, bondTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%X","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
config.GetString("chain_id"))
@@ -104,7 +110,7 @@ func TestUnbondTxSignable(t *testing.T) {
Address: []byte("address1"),
Height: 111,
}
signBytes := account.SignBytes(unbondTx)
signBytes := account.SignBytes(chainID, unbondTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%X","tx":[18,{"address":"6164647265737331","height":111}]}`,
config.GetString("chain_id"))
@@ -118,7 +124,7 @@ func TestRebondTxSignable(t *testing.T) {
Address: []byte("address1"),
Height: 111,
}
signBytes := account.SignBytes(rebondTx)
signBytes := account.SignBytes(chainID, rebondTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%X","tx":[19,{"address":"6164647265737331","height":111}]}`,
config.GetString("chain_id"))
+8 -8
View File
@@ -56,12 +56,12 @@ func (tx *SendTx) AddOutput(addr []byte, amt uint64) error {
return nil
}
func (tx *SendTx) SignInput(i int, privAccount *account.PrivAccount) error {
func (tx *SendTx) SignInput(chainID string, i int, privAccount *account.PrivAccount) error {
if i >= len(tx.Inputs) {
return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
}
tx.Inputs[i].PubKey = privAccount.PubKey
tx.Inputs[i].Signature = privAccount.Sign(tx)
tx.Inputs[i].Signature = privAccount.Sign(chainID, tx)
return nil
}
@@ -98,9 +98,9 @@ func NewCallTxWithNonce(from account.PubKey, to, data []byte, amt, gasLimit, fee
}
}
func (tx *CallTx) Sign(privAccount *account.PrivAccount) {
func (tx *CallTx) Sign(chainID string, privAccount *account.PrivAccount) {
tx.Input.PubKey = privAccount.PubKey
tx.Input.Signature = privAccount.Sign(tx)
tx.Input.Signature = privAccount.Sign(chainID, tx)
}
//----------------------------------------------------------------------------
@@ -155,8 +155,8 @@ func (tx *BondTx) AddOutput(addr []byte, amt uint64) error {
return nil
}
func (tx *BondTx) SignBond(privAccount *account.PrivAccount) error {
sig := privAccount.Sign(tx)
func (tx *BondTx) SignBond(chainID string, privAccount *account.PrivAccount) error {
sig := privAccount.Sign(chainID, tx)
sigEd, ok := sig.(account.SignatureEd25519)
if !ok {
return fmt.Errorf("Bond signer must be ED25519")
@@ -165,11 +165,11 @@ func (tx *BondTx) SignBond(privAccount *account.PrivAccount) error {
return nil
}
func (tx *BondTx) SignInput(i int, privAccount *account.PrivAccount) error {
func (tx *BondTx) SignInput(chainID string, i int, privAccount *account.PrivAccount) error {
if i >= len(tx.Inputs) {
return fmt.Errorf("Index %v is greater than number of inputs (%v)", i, len(tx.Inputs))
}
tx.Inputs[i].PubKey = privAccount.PubKey
tx.Inputs[i].Signature = privAccount.Sign(tx)
tx.Inputs[i].Signature = privAccount.Sign(chainID, tx)
return nil
}
+2 -2
View File
@@ -45,9 +45,9 @@ const (
VoteTypeCommit = byte(0x03)
)
func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (vote *Vote) 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"`, config.GetString("chain_id"))), w, n, err)
binary.WriteTo([]byte(Fmt(`{"chain_id":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), w, n, err)
}