responses_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package rpc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "regexp"
  7. "testing"
  8. "github.com/ethereum/go-ethereum/common"
  9. "github.com/ethereum/go-ethereum/core/state"
  10. "github.com/ethereum/go-ethereum/core/types"
  11. )
  12. const (
  13. reHash = `"0x[0-9a-f]{64}"` // 32 bytes
  14. reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null
  15. reAddress = `"0x[0-9a-f]{40}"` // 20 bytes
  16. reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null
  17. reNum = `"0x([1-9a-f][0-9a-f]{0,15})|0"` // must not have left-padded zeros
  18. reNumNonZero = `"0x([1-9a-f][0-9a-f]{0,15})"` // non-zero required must not have left-padded zeros
  19. reNumOpt = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null
  20. reData = `"0x[0-9a-f]*"` // can be "empty"
  21. // reListHash = `[("\w":"0x[0-9a-f]{64}",?)*]`
  22. // reListObj = `[("\w":(".+"|null),?)*]`
  23. )
  24. func TestNewBlockRes(t *testing.T) {
  25. parentHash := common.HexToHash("0x01")
  26. coinbase := common.HexToAddress("0x01")
  27. root := common.HexToHash("0x01")
  28. difficulty := common.Big1
  29. nonce := uint64(1)
  30. block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
  31. tests := map[string]string{
  32. "number": reNum,
  33. "hash": reHash,
  34. "parentHash": reHash,
  35. "nonce": reData,
  36. "sha3Uncles": reHash,
  37. "logsBloom": reData,
  38. "transactionsRoot": reHash,
  39. "stateRoot": reHash,
  40. "miner": reAddress,
  41. "difficulty": `"0x1"`,
  42. "totalDifficulty": reNum,
  43. "size": reNumNonZero,
  44. "extraData": reData,
  45. "gasLimit": reNum,
  46. // "minGasPrice": "0x",
  47. "gasUsed": reNum,
  48. "timestamp": reNum,
  49. // "transactions": reListHash,
  50. // "uncles": reListHash,
  51. }
  52. to := common.HexToAddress("0x02")
  53. amount := big.NewInt(1)
  54. gasAmount := big.NewInt(1)
  55. gasPrice := big.NewInt(1)
  56. data := []byte{1, 2, 3}
  57. tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
  58. v := NewBlockRes(block, false)
  59. v.Transactions = make([]*TransactionRes, 1)
  60. v.Transactions[0] = NewTransactionRes(tx)
  61. j, _ := json.Marshal(v)
  62. for k, re := range tests {
  63. match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
  64. if !match {
  65. t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
  66. }
  67. }
  68. }
  69. func TestNewBlockResWithTrans(t *testing.T) {
  70. parentHash := common.HexToHash("0x01")
  71. coinbase := common.HexToAddress("0x01")
  72. root := common.HexToHash("0x01")
  73. difficulty := common.Big1
  74. nonce := uint64(1)
  75. block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
  76. tests := map[string]string{
  77. "number": reNum,
  78. "hash": reHash,
  79. "parentHash": reHash,
  80. "nonce": reData,
  81. "sha3Uncles": reHash,
  82. "logsBloom": reData,
  83. "transactionsRoot": reHash,
  84. "stateRoot": reHash,
  85. "miner": reAddress,
  86. "difficulty": `"0x1"`,
  87. "totalDifficulty": reNum,
  88. "size": reNumNonZero,
  89. "extraData": reData,
  90. "gasLimit": reNum,
  91. // "minGasPrice": "0x",
  92. "gasUsed": reNum,
  93. "timestamp": reNum,
  94. // "transactions": `[{.*}]`,
  95. // "uncles": reListHash,
  96. }
  97. to := common.HexToAddress("0x02")
  98. amount := big.NewInt(1)
  99. gasAmount := big.NewInt(1)
  100. gasPrice := big.NewInt(1)
  101. data := []byte{1, 2, 3}
  102. tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
  103. v := NewBlockRes(block, true)
  104. v.Transactions = make([]*TransactionRes, 1)
  105. v.Transactions[0] = NewTransactionRes(tx)
  106. j, _ := json.Marshal(v)
  107. for k, re := range tests {
  108. match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
  109. if !match {
  110. t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
  111. }
  112. }
  113. }
  114. func TestNewTransactionRes(t *testing.T) {
  115. to := common.HexToAddress("0x02")
  116. amount := big.NewInt(1)
  117. gasAmount := big.NewInt(1)
  118. gasPrice := big.NewInt(1)
  119. data := []byte{1, 2, 3}
  120. tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
  121. tests := map[string]string{
  122. "hash": reHash,
  123. "nonce": reNum,
  124. "blockHash": reHashOpt,
  125. "blockNum": reNumOpt,
  126. "transactionIndex": reNumOpt,
  127. "from": reAddress,
  128. "to": reAddressOpt,
  129. "value": reNum,
  130. "gas": reNum,
  131. "gasPrice": reNum,
  132. "input": reData,
  133. }
  134. v := NewTransactionRes(tx)
  135. v.BlockHash = newHexData(common.HexToHash("0x030201"))
  136. v.BlockNumber = newHexNum(5)
  137. v.TxIndex = newHexNum(0)
  138. j, _ := json.Marshal(v)
  139. for k, re := range tests {
  140. match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
  141. if !match {
  142. t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
  143. }
  144. }
  145. }
  146. func TestNewLogRes(t *testing.T) {
  147. log := makeStateLog(0)
  148. tests := map[string]string{
  149. "address": reAddress,
  150. // "topics": "[.*]"
  151. "data": reData,
  152. "blockNumber": reNum,
  153. // "hash": reHash,
  154. // "logIndex": reNum,
  155. // "blockHash": reHash,
  156. // "transactionHash": reHash,
  157. "transactionIndex": reNum,
  158. }
  159. v := NewLogRes(log)
  160. j, _ := json.Marshal(v)
  161. for k, re := range tests {
  162. match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
  163. if !match {
  164. t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j))
  165. }
  166. }
  167. }
  168. func TestNewLogsRes(t *testing.T) {
  169. logs := make([]state.Log, 3)
  170. logs[0] = makeStateLog(1)
  171. logs[1] = makeStateLog(2)
  172. logs[2] = makeStateLog(3)
  173. tests := map[string]string{}
  174. v := NewLogsRes(logs)
  175. j, _ := json.Marshal(v)
  176. for k, re := range tests {
  177. match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j))
  178. if !match {
  179. t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
  180. }
  181. }
  182. }
  183. func makeStateLog(num int) state.Log {
  184. address := common.HexToAddress("0x0")
  185. data := []byte{1, 2, 3}
  186. number := uint64(num)
  187. topics := make([]common.Hash, 3)
  188. topics = append(topics, common.HexToHash("0x00"))
  189. topics = append(topics, common.HexToHash("0x10"))
  190. topics = append(topics, common.HexToHash("0x20"))
  191. log := state.NewLog(address, topics, data, number)
  192. return log
  193. }