11 dbm "github.com/tendermint/tmlibs/db"
13 "github.com/vapor/accesstoken"
14 "github.com/vapor/blockchain/rpc"
15 "github.com/vapor/blockchain/txbuilder"
16 "github.com/vapor/consensus"
17 "github.com/vapor/testutil"
20 func TestAPIHandler(t *testing.T) {
22 response := &Response{}
24 // init httptest server
26 server := httptest.NewServer(a.handler)
29 // create accessTokens
30 testDB := dbm.NewDB("testdb", "leveldb", "temp")
31 defer os.RemoveAll("temp")
32 a.accessTokens = accesstoken.NewStore(testDB)
34 client := &rpc.Client{
36 AccessToken: "test-user:test-secret",
47 Alias string `json:"alias"`
48 Password string `json:"password"`
49 }{Alias: "alice", Password: "123456"},
52 Msg: "wallet not found, please check that the wallet is open",
60 Msg: "wallet not found, please check that the wallet is open",
72 path: "/create-access-token",
75 Type string `json:"type"`
76 }{ID: "test-access-id", Type: "test-access-type"},
80 Data: map[string]interface{}{"id": "test-access-id", "type": "test-access-type", "token": "test-access-id:440d87ae0d625a7fcf076275b18372e09a0899e37ec86398879388de90cb0c67"},
89 Data: map[string]interface{}{"gasRate": 1000},
94 for _, c := range cases {
95 response = &Response{}
96 client.Call(context.Background(), c.path, c.request, &response)
98 if !testutil.DeepEqual(response.Status, c.respWant.Status) {
99 t.Errorf(`got=%#v; want=%#v`, response.Status, c.respWant.Status)
104 func TestEstimateTxGas(t *testing.T) {
105 tmplStr := `{"allow_additional_actions":false,"raw_transaction":"0701a8d30201010060015ef39ac9f5a6b0db3eb4b2a54a8d012ef5626c1da5462bc97c7a0a1c11bd8e39bdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0d6e0ce120001160014b29b9e1b31018d5161e33d0c465bbb6dd1df1556010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffacfc9bc512011600142a38b1e022d42414b76a11b7b63075d08fe90b77000139ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff14011600145427f2318811979c908eb2f06d439d134aa156fe00","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"6c420aa025610d323a55c29a8692e2f909b176e88c3bfc8b78cb64ead1bd5db2c6d8492346d12acea177ed0fa4a4579c4bdf020c8cf10fa99cad72f3d5b7de04","derivation_path":["010100000000000000","0e00000000000000"]}],"signatures":null},{"type":"data","value":"512d84b99c93d51729215de3d796390f762f74692306863e3f3bcb0090b399f4"}]}]}`
106 template := txbuilder.Template{}
107 err := json.Unmarshal([]byte(tmplStr), &template)
112 estimateResult, err := EstimateTxGas(template)
117 baseRate := float64(100000)
118 totalNeu := float64(estimateResult.StorageNeu+estimateResult.VMNeu+flexibleGas*consensus.VMGasRate) / baseRate
119 roundingNeu := math.Ceil(totalNeu)
120 estimateNeu := int64(roundingNeu) * int64(baseRate)
122 if estimateResult.TotalNeu != estimateNeu {
123 t.Errorf(`got=%#v; want=%#v`, estimateResult.TotalNeu, estimateNeu)
127 func TestEstimateTxGasRange(t *testing.T) {
132 respWant *EstimateTxGasResp
135 path: "/estimate-transaction-gas",
136 tmplStr: `{"allow_additional_actions":false,"raw_transaction":"0701a8d30201010060015ef39ac9f5a6b0db3eb4b2a54a8d012ef5626c1da5462bc97c7a0a1c11bd8e39bdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0d6e0ce120001160014b29b9e1b31018d5161e33d0c465bbb6dd1df1556010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffacfc9bc512011600142a38b1e022d42414b76a11b7b63075d08fe90b77000139ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff14011600145427f2318811979c908eb2f06d439d134aa156fe00","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"6c420aa025610d323a55c29a8692e2f909b176e88c3bfc8b78cb64ead1bd5db2c6d8492346d12acea177ed0fa4a4579c4bdf020c8cf10fa99cad72f3d5b7de04","derivation_path":["010100000000000000","0e00000000000000"]}],"signatures":null},{"type":"data","value":"512d84b99c93d51729215de3d796390f762f74692306863e3f3bcb0090b399f4"}]}]}`,
137 respWant: &EstimateTxGasResp{
138 TotalNeu: (flexibleGas + 2095) * consensus.VMGasRate,
143 path: "/estimate-transaction-gas",
144 tmplStr: `{"raw_transaction":"0701a8d30201010060015ef39ac9f5a6b0db3eb4b2a54a8d012ef5626c1da5462bc97c7a0a1c11bd8e39bdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0d6e0ce120001160014b29b9e1b31018d5161e33d0c465bbb6dd1df1556010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc092ebc4120116001468c873fd87f05dc1e6ac5d43cc6889a0338d9ad200013bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ea30011600145427f2318811979c908eb2f06d439d134aa156fe00","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"6c420aa025610d323a55c29a8692e2f909b176e88c3bfc8b78cb64ead1bd5db2c6d8492346d12acea177ed0fa4a4579c4bdf020c8cf10fa99cad72f3d5b7de04","derivation_path":["010100000000000000","0e00000000000000"]}],"signatures":null},{"type":"data","value":"512d84b99c93d51729215de3d796390f762f74692306863e3f3bcb0090b399f4"}]}],"allow_additional_actions":false}`,
145 respWant: &EstimateTxGasResp{
146 TotalNeu: (flexibleGas + 3305) * consensus.VMGasRate,
150 path: "/estimate-transaction-gas",
151 tmplStr: `{"raw_transaction":"0701a8d30201010060015ef39ac9f5a6b0db3eb4b2a54a8d012ef5626c1da5462bc97c7a0a1c11bd8e39bdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0d6e0ce120001160014b29b9e1b31018d5161e33d0c465bbb6dd1df1556010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0cfb9c01201160014e75b5b89f8398b214c64d0621a19f25d716c2f4700013cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ade204011600145427f2318811979c908eb2f06d439d134aa156fe00","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"6c420aa025610d323a55c29a8692e2f909b176e88c3bfc8b78cb64ead1bd5db2c6d8492346d12acea177ed0fa4a4579c4bdf020c8cf10fa99cad72f3d5b7de04","derivation_path":["010100000000000000","0e00000000000000"]}],"signatures":null},{"type":"data","value":"512d84b99c93d51729215de3d796390f762f74692306863e3f3bcb0090b399f4"}]}],"allow_additional_actions":false}`,
152 respWant: &EstimateTxGasResp{
153 TotalNeu: (flexibleGas + 13556) * consensus.VMGasRate,
158 for _, c := range cases {
159 template := txbuilder.Template{}
160 err := json.Unmarshal([]byte(c.tmplStr), &template)
164 estimateTxGasResp, err := EstimateTxGas(template)
165 realTotalNeu := float64(c.respWant.TotalNeu)
166 rate := math.Abs((float64(estimateTxGasResp.TotalNeu) - realTotalNeu) / float64(estimateTxGasResp.TotalNeu))
168 t.Errorf(`the estimateNeu over realNeu 20%%`)