From 88059ada87b6270bffd50565ce0579142f76b7c6 Mon Sep 17 00:00:00 2001 From: ChengOrangeJu Date: Thu, 10 May 2018 18:12:04 +0800 Subject: [PATCH] core: add transaction JSON string for explorer to use --- core/transaction.go | 18 ++++++++++++++++++ core/transaction_pool.go | 4 ++-- core/transaction_test.go | 22 ++++++++++++++++++++++ storage/rocks_storage_test.go | 2 +- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/core/transaction.go b/core/transaction.go index 7ac6ed326..d8000539d 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -265,6 +265,24 @@ func (tx *Transaction) String() string { ) } +// JSONString of transaction +func (tx *Transaction) JSONString() string { + txJSONObj := make(map[string]interface{}) + txJSONObj["chainID"] = tx.chainID + txJSONObj["hash"] = tx.hash.String() + txJSONObj["from"] = tx.from.String() + txJSONObj["to"] = tx.to.String() + txJSONObj["nonce"] = tx.nonce + txJSONObj["value"] = tx.value.String() + txJSONObj["timestamp"] = tx.timestamp + txJSONObj["gasprice"] = tx.gasPrice.String() + txJSONObj["gaslimit"] = tx.gasLimit.String() + txJSONObj["data"] = string(tx.Data()) + txJSONObj["type"] = tx.Type() + txJSON, _ := json.Marshal(txJSONObj) + return string(txJSON) +} + // Transactions is an alias of Transaction array. type Transactions []*Transaction diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 0964bd4a6..6fe0ea3b0 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -324,7 +324,7 @@ func (pool *TransactionPool) Push(tx *Transaction) error { // trigger pending transaction event := &state.Event{ Topic: TopicPendingTransaction, - Data: tx.String(), + Data: tx.JSONString(), } pool.eventEmitter.Trigger(event) @@ -526,7 +526,7 @@ func (pool *TransactionPool) evictExpiredTransactions() { // trigger pending transaction event := &state.Event{ Topic: TopicDropTransaction, - Data: tx.String(), + Data: tx.JSONString(), } pool.eventEmitter.Trigger(event) } diff --git a/core/transaction_test.go b/core/transaction_test.go index 6295602ec..f4327bd0d 100644 --- a/core/transaction_test.go +++ b/core/transaction_test.go @@ -767,3 +767,25 @@ func TestDeployAndCall(t *testing.T) { func Test1(t *testing.T) { fmt.Println(len(hash.Sha3256([]byte("abc")))) } +func TestTransactionString(t *testing.T) { + neb := testNeb(t) + bc := neb.chain + + a := mockAddress() + b := mockAddress() + + v, _ := util.NewUint128FromInt(1) + data := `{"Function":"donation","Args":"[\"d\"]"}", "type":"call"}` + tx1, _ := NewTransaction(bc.chainID, a, b, v, uint64(1), TxPayloadDeployType, []byte(data), TransactionGasPrice, TransactionMaxGas) + expectedOut := fmt.Sprintf(`{"chainID":100,"data":"{\"Function\":\"donation\",\"Args\":\"[\\\"d\\\"]\"}\", \"type\":\"call\"}","from":"%s","gaslimit":"50000000000","gasprice":"1000000","hash":"","nonce":1,"timestamp":%d,"to":"%s","type":"deploy","value":"1"}`, a, tx1.timestamp, b) + + if tx1.String() == tx1.JSONString() { + t.Errorf("tx String() != tx.JsonString") + } + + if tx1.JSONString() != expectedOut { + fmt.Println(tx1.JSONString()) + fmt.Println(expectedOut) + t.Errorf("tx JsonString() is not working as xpected") + } +} diff --git a/storage/rocks_storage_test.go b/storage/rocks_storage_test.go index 20070222f..ecbf89c21 100644 --- a/storage/rocks_storage_test.go +++ b/storage/rocks_storage_test.go @@ -17,7 +17,7 @@ func TestNewRocksStorage(t *testing.T) { want *RocksStorage wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {