/
call.go
138 lines (114 loc) · 3.08 KB
/
call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"strconv"
"github.com/PlatONEnetwork/PlatONE-Go/cmd/ctool/core"
"github.com/PlatONEnetwork/PlatONE-Go/log"
)
// 判断合约是否成功上链
func getContractByAddress(addr string) bool {
params := []string{addr, "latest"}
r, err := Send(params, "eth_getCode")
if err != nil {
fmt.Printf("send http post to get contract address error ")
return false
}
var resp = core.Response{}
err = json.Unmarshal([]byte(r), &resp)
if err != nil {
fmt.Printf("parse eth_getCode result error ! \n %s", err.Error())
return false
}
if resp.Error.Code != 0 {
fmt.Printf("eth_getCode error ,error:%v", resp.Error.Message)
return false
}
//fmt.Printf("trasaction hash: %s\n", resp.Result)
if resp.Result != "" && len(resp.Result) > 2 {
return true
} else {
return false
}
}
func getTxByHash(hash string) bool {
params := []string{hash}
r, err := Send(params, "eth_getTransactionByHash")
if err != nil {
fmt.Printf("send http post to get contract address error ")
return false
}
var resp map[string]interface{}
err = json.Unmarshal([]byte(r), &resp)
if err != nil {
panic(err)
}
if resp["result"].(map[string]interface{})["blockNumber"] != nil {
return true
} else {
return false
}
}
func getCurrentBlockNum() int64 {
params := []string{}
r, err := Send(params, "eth_blockNumber")
if err != nil {
fmt.Printf("send http post to get contract address error ")
return -1
}
var resp map[string]interface{}
err = json.Unmarshal([]byte(r), &resp)
if err != nil {
panic(err)
}
num, _ := strconv.ParseInt(resp["result"].(string), 0, 64)
return num
}
func getBlockTxNum(h int64) (num, timestamp int64) {
var height interface{}
height = "0x" + strconv.FormatInt(h, 16)
params := []interface{}{height, true}
r, err := Send(params, "eth_getBlockByNumber")
if err != nil {
fmt.Printf("send http post to get contract address error ")
return -1, -1
}
var resp map[string]interface{}
err = json.Unmarshal([]byte(r), &resp)
if err != nil {
panic(err)
}
txList := resp["result"].(map[string]interface{})["transactions"].([]interface{})
timestamp, _ = strconv.ParseInt(resp["result"].(map[string]interface{})["timestamp"].(string), 0, 64)
num = int64(len(txList))
return
}
func getPerfResults() (interface{}) {
var ret interface{}
var err error
if *stressTest == 1 {
err, ret = invoke(*contractAddress, *abiPath, "getName()", *txType)
if err != nil {
panic(err)
}
return ret
} else {
var records map[string]interface{}
err, ret = invoke(*contractAddress, *abiPath, "getRegisteredContracts(0,10000)", *txType)
if err != nil {
panic(err)
}
trimRet := []byte(ret.(string))
l := len(trimRet) - 1
for trimRet[l] == byte(0) {
l--
}
if err := json.Unmarshal(trimRet[:l+1], &records); err != nil {
panic(err)
} else if records["code"] != 0 || records["msg"].(string) != "ok" {
log.Error("contract inner error", "code", records["code"], "msg", records["msg"].(string))
}
registerContracts := int64(records["data"].(map[string]interface{})["total"].(float64)) - 1
return registerContracts
}
}