From 5cb57d3eaa132fa44f00542f6440fe1cc8661d43 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 29 Mar 2015 18:43:27 -0700 Subject: [PATCH] Use BlockCache for RPC/mempool and added TxId() TxId() uses signbytes --- .gitignore | 1 + mempool/mempool.go | 4 ++++ rpc/core/accounts.go | 4 ++-- rpc/core/mempool.go | 3 +-- rpc/test/json_rpc_test.go | 6 +++--- types/tx.go | 7 +++++++ 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index c4760c4f..eba2cac3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .bak tendermint .DS_Store +rpc/test/.tendermint diff --git a/mempool/mempool.go b/mempool/mempool.go index 28e3ab70..81919877 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -34,6 +34,10 @@ func (mem *Mempool) GetState() *sm.State { return mem.state } +func (mem *Mempool) GetCache() *sm.BlockCache { + return mem.cache +} + // Apply tx to the state and remember it. func (mem *Mempool) AddTx(tx types.Tx) (err error) { mem.mtx.Lock() diff --git a/rpc/core/accounts.go b/rpc/core/accounts.go index 84ffc2ee..6949ab86 100644 --- a/rpc/core/accounts.go +++ b/rpc/core/accounts.go @@ -13,8 +13,8 @@ func GenPrivAccount() (*ResponseGenPrivAccount, error) { //----------------------------------------------------------------------------- func GetAccount(address []byte) (*ResponseGetAccount, error) { - state := consensusState.GetState() - return &ResponseGetAccount{state.GetAccount(address)}, nil + cache := mempoolReactor.Mempool.GetCache() + return &ResponseGetAccount{cache.GetAccount(address)}, nil } //----------------------------------------------------------------------------- diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 3ff1a85e..070d6e40 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -3,7 +3,6 @@ package core import ( "fmt" . "github.com/tendermint/tendermint/common" - "github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" ) @@ -24,7 +23,7 @@ func BroadcastTx(tx types.Tx) (*ResponseBroadcastTx, error) { return nil, fmt.Errorf("Error broadcasting transaction: %v", err) } - txHash := merkle.HashFromBinary(tx) + txHash := types.TxId(tx) var createsContract uint8 var contractAddr []byte // check if creates new contract diff --git a/rpc/test/json_rpc_test.go b/rpc/test/json_rpc_test.go index 62f9c60b..3b857f24 100644 --- a/rpc/test/json_rpc_test.go +++ b/rpc/test/json_rpc_test.go @@ -6,8 +6,8 @@ import ( "encoding/json" "fmt" "github.com/tendermint/tendermint/binary" + . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/rpc" "github.com/tendermint/tendermint/rpc/core" "github.com/tendermint/tendermint/types" @@ -161,8 +161,8 @@ func TestJSONBroadcastTx(t *testing.T) { } tx2 := txs[mempoolCount].(*types.SendTx) mempoolCount += 1 - if bytes.Compare(merkle.HashFromBinary(tx), merkle.HashFromBinary(tx2)) != 0 { - t.Fatal("inconsistent hashes for mempool tx and sent tx") + if bytes.Compare(types.TxId(tx), types.TxId(tx2)) != 0 { + t.Fatal(Fmt("inconsistent hashes for mempool tx and sent tx: %v vs %v", tx, tx2)) } } diff --git a/types/tx.go b/types/tx.go index 1df6f6be..31af05b7 100644 --- a/types/tx.go +++ b/types/tx.go @@ -254,3 +254,10 @@ func (tx *DupeoutTx) WriteSignBytes(w io.Writer, n *int64, err *error) { func (tx *DupeoutTx) String() string { return Fmt("DupeoutTx{%X,%v,%v}", tx.Address, tx.VoteA, tx.VoteB) } + +//----------------------------------------------------------------------------- + +func TxId(tx Tx) []byte { + signBytes := account.SignBytes(tx) + return binary.BinaryRipemd160(signBytes) +}