Skip to content

Commit

Permalink
core: add transaction JSON string for explorer to use
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengOrangeJu committed May 10, 2018
1 parent 8029ed7 commit 88059ad
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
18 changes: 18 additions & 0 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions core/transaction_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions core/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
2 changes: 1 addition & 1 deletion storage/rocks_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 88059ad

Please sign in to comment.