Browse Source

fixes for chain id in nametx sign functions

pull/83/head
Ethan Buchman 10 years ago
parent
commit
6eb8386c7c
6 changed files with 26 additions and 24 deletions
  1. +2
    -2
      rpc/test/helpers.go
  2. +1
    -1
      rpc/test/tests.go
  3. +1
    -1
      state/execution.go
  4. +18
    -16
      state/state_test.go
  5. +2
    -2
      types/tx.go
  6. +2
    -2
      types/tx_utils.go

+ 2
- 2
rpc/test/helpers.go View File

@ -107,7 +107,7 @@ func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt uint64)
func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee uint64) *types.CallTx {
nonce := getNonce(t, typ, user[0].Address)
tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce)
tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce+1)
tx.Sign(chainID, user[0])
return tx
}
@ -115,7 +115,7 @@ func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim,
func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, fee uint64) *types.NameTx {
nonce := getNonce(t, typ, user[0].Address)
tx := types.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce+1)
tx.Sign(user[0])
tx.Sign(chainID, user[0])
return tx
}


+ 1
- 1
rpc/test/tests.go View File

@ -238,7 +238,7 @@ func testNameReg(t *testing.T, typ string) {
nonce := getNonce(t, typ, user[1].Address)
data2 := "this is not my beautiful house"
tx = types.NewNameTxWithNonce(user[1].PubKey, name, data2, amt, fee, nonce+1)
tx.Sign(user[1])
tx.Sign(chainID, user[1])
_, err := client.BroadcastTx(tx)
if err == nil {
t.Fatal("Expected error on NameTx")


+ 1
- 1
state/execution.go View File

@ -492,7 +492,7 @@ func ExecTx(blockCache *BlockCache, tx_ types.Tx, runCall bool, evc events.Firea
log.Debug(Fmt("Can't find pubkey for %X", tx.Input.Address))
return err
}
signBytes := account.SignBytes(tx)
signBytes := account.SignBytes(_s.ChainID, tx)
err := validateInput(inAcc, signBytes, tx.Input)
if err != nil {
log.Debug(Fmt("validateInput failed on %X:", tx.Input.Address))


+ 18
- 16
state/state_test.go View File

@ -179,7 +179,9 @@ func TestTxSequence(t *testing.T) {
// The tx should only pass when i == 1.
for i := -1; i < 3; i++ {
sequence := acc0.Sequence + uint(i)
tx := makeSendTx(sequence)
tx := types.NewSendTx()
tx.AddInputWithNonce(acc0PubKey, 1, sequence)
tx.AddOutput(acc1.Address, 1)
tx.Inputs[0].Signature = privAccounts[0].Sign(state.ChainID, tx)
stateCopy := state.Copy()
err := execTxWithState(stateCopy, tx, true)
@ -223,7 +225,7 @@ func TestNameTxs(t *testing.T) {
for _, name := range names {
amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithState(state, tx, true); err == nil {
t.Fatalf("Expected invalid name error from %s", name)
@ -236,7 +238,7 @@ func TestNameTxs(t *testing.T) {
for _, data := range datas {
amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithState(state, tx, true); err == nil {
t.Fatalf("Expected invalid data error from %s", data)
@ -267,7 +269,7 @@ func TestNameTxs(t *testing.T) {
data = "on this side of neptune there are 1234567890 people: first is OMNIVORE. Or is it. Ok this is pretty restrictive. No exclamations :(. Faces tho :')"
amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
tx, _ := types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithState(state, tx, true); err != nil {
t.Fatal(err)
}
@ -276,7 +278,7 @@ func TestNameTxs(t *testing.T) {
// fail to update it as non-owner, in same block
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithState(state, tx, true); err == nil {
t.Fatal("Expected error")
}
@ -284,7 +286,7 @@ func TestNameTxs(t *testing.T) {
// update it as owner, just to increase expiry, in same block
// NOTE: we have to resend the data or it will clear it (is this what we want?)
tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithStateNewBlock(state, tx, true); err != nil {
t.Fatal(err)
}
@ -293,7 +295,7 @@ func TestNameTxs(t *testing.T) {
// update it as owner, just to increase expiry, in next block
tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithStateNewBlock(state, tx, true); err != nil {
t.Fatal(err)
}
@ -303,7 +305,7 @@ func TestNameTxs(t *testing.T) {
// fail to update it as non-owner
state.LastBlockHeight = uint(entry.Expires - 1)
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithState(state, tx, true); err == nil {
t.Fatal("Expected error")
}
@ -311,7 +313,7 @@ func TestNameTxs(t *testing.T) {
// once expires, non-owner succeeds
state.LastBlockHeight = uint(entry.Expires)
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithState(state, tx, true); err != nil {
t.Fatal(err)
}
@ -324,7 +326,7 @@ func TestNameTxs(t *testing.T) {
numDesiredBlocks = 10
amt = fee + (numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data) - oldCredit)
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithState(state, tx, true); err != nil {
t.Fatal(err)
}
@ -335,7 +337,7 @@ func TestNameTxs(t *testing.T) {
amt = fee
data = ""
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithStateNewBlock(state, tx, true); err != nil {
t.Fatal(err)
}
@ -350,7 +352,7 @@ func TestNameTxs(t *testing.T) {
data = "some data"
amt = fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
tx, _ = types.NewNameTx(state, privAccounts[0].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[0])
tx.Sign(state.ChainID, privAccounts[0])
if err := execTxWithState(state, tx, true); err != nil {
t.Fatal(err)
}
@ -361,7 +363,7 @@ func TestNameTxs(t *testing.T) {
amt = fee
data = ""
tx, _ = types.NewNameTx(state, privAccounts[1].PubKey, name, data, amt, fee)
tx.Sign(privAccounts[1])
tx.Sign(state.ChainID, privAccounts[1])
if err := execTxWithStateNewBlock(state, tx, true); err != nil {
t.Fatal(err)
}
@ -436,7 +438,7 @@ func TestTxs(t *testing.T) {
GasLimit: 10,
}
tx.Input.Signature = privAccounts[0].Sign(tx)
tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx)
err := execTxWithState(state, tx, true)
if err != nil {
t.Errorf("Got error in executing call transaction, %v", err)
@ -485,7 +487,7 @@ proof-of-work chain as proof of what happened while they were gone `
Data: entryData,
}
tx.Input.Signature = privAccounts[0].Sign(tx)
tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx)
err := execTxWithState(state, tx, true)
if err != nil {
t.Errorf("Got error in executing call transaction, %v", err)
@ -506,7 +508,7 @@ proof-of-work chain as proof of what happened while they were gone `
// test a bad string
tx.Data = string([]byte{0, 1, 2, 3, 127, 128, 129, 200, 251})
tx.Input.Sequence += 1
tx.Input.Signature = privAccounts[0].Sign(tx)
tx.Input.Signature = privAccounts[0].Sign(state.ChainID, tx)
err = execTxWithState(state, tx, true)
if err != types.ErrTxInvalidString {
t.Errorf("Expected invalid string error. Got: %s", err.Error())


+ 2
- 2
types/tx.go View File

@ -190,9 +190,9 @@ type NameTx struct {
Fee uint64 `json:"fee"`
}
func (tx *NameTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
func (tx *NameTx) WriteSignBytes(chainID string, 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)
binary.WriteTo([]byte(Fmt(`{"network":"%X"`, chainID)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"name":"%s","data":"%s"`, TxTypeName, tx.Name, tx.Data)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"fee":%v,"input":`, tx.Fee)), w, n, err)
tx.Input.WriteSignBytes(w, n, err)


+ 2
- 2
types/tx_utils.go View File

@ -127,9 +127,9 @@ func NewNameTxWithNonce(from account.PubKey, name, data string, amt, fee uint64,
}
}
func (tx *NameTx) Sign(privAccount *account.PrivAccount) {
func (tx *NameTx) Sign(chainID string, privAccount *account.PrivAccount) {
tx.Input.PubKey = privAccount.PubKey
tx.Input.Signature = privAccount.Sign(tx)
tx.Input.Signature = privAccount.Sign(chainID, tx)
}
//----------------------------------------------------------------------------


Loading…
Cancel
Save