OSDN Git Service

Merge pull request #15 from Bytom/dev_ci
[bytom/vapor.git] / api / api_test.go
1 package api
2
3 import (
4         "context"
5         "encoding/json"
6         "math"
7         "net/http/httptest"
8         "os"
9         "testing"
10
11         dbm "github.com/tendermint/tmlibs/db"
12
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"
18 )
19
20 func TestAPIHandler(t *testing.T) {
21         a := &API{}
22         response := &Response{}
23
24         // init httptest server
25         a.buildHandler()
26         server := httptest.NewServer(a.handler)
27         defer server.Close()
28
29         // create accessTokens
30         testDB := dbm.NewDB("testdb", "leveldb", "temp")
31         defer os.RemoveAll("temp")
32         a.accessTokens = accesstoken.NewStore(testDB)
33
34         client := &rpc.Client{
35                 BaseURL:     server.URL,
36                 AccessToken: "test-user:test-secret",
37         }
38
39         cases := []struct {
40                 path     string
41                 request  interface{}
42                 respWant *Response
43         }{
44                 {
45                         path: "/create-key",
46                         request: struct {
47                                 Alias    string `json:"alias"`
48                                 Password string `json:"password"`
49                         }{Alias: "alice", Password: "123456"},
50                         respWant: &Response{
51                                 Status: "fail",
52                                 Msg:    "wallet not found, please check that the wallet is open",
53                         },
54                 },
55                 {
56                         path:    "/error",
57                         request: nil,
58                         respWant: &Response{
59                                 Status: "fail",
60                                 Msg:    "wallet not found, please check that the wallet is open",
61                         },
62                 },
63                 {
64                         path:    "/",
65                         request: nil,
66                         respWant: &Response{
67                                 Status: "",
68                                 Msg:    "",
69                         },
70                 },
71                 {
72                         path: "/create-access-token",
73                         request: struct {
74                                 ID   string `json:"id"`
75                                 Type string `json:"type"`
76                         }{ID: "test-access-id", Type: "test-access-type"},
77                         respWant: &Response{
78                                 Status: "success",
79                                 Msg:    "",
80                                 Data:   map[string]interface{}{"id": "test-access-id", "type": "test-access-type", "token": "test-access-id:440d87ae0d625a7fcf076275b18372e09a0899e37ec86398879388de90cb0c67"},
81                         },
82                 },
83                 {
84                         path:    "/gas-rate",
85                         request: nil,
86                         respWant: &Response{
87                                 Status: "success",
88                                 Msg:    "",
89                                 Data:   map[string]interface{}{"gasRate": 1000},
90                         },
91                 },
92         }
93
94         for _, c := range cases {
95                 response = &Response{}
96                 client.Call(context.Background(), c.path, c.request, &response)
97
98                 if !testutil.DeepEqual(response.Status, c.respWant.Status) {
99                         t.Errorf(`got=%#v; want=%#v`, response.Status, c.respWant.Status)
100                 }
101         }
102 }
103
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)
108         if err != nil {
109                 t.Fatal(err)
110         }
111
112         estimateResult, err := EstimateTxGas(template)
113         if err != nil {
114                 t.Fatal(err)
115         }
116
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)
121
122         if estimateResult.TotalNeu != estimateNeu {
123                 t.Errorf(`got=%#v; want=%#v`, estimateResult.TotalNeu, estimateNeu)
124         }
125 }
126
127 func TestEstimateTxGasRange(t *testing.T) {
128
129         cases := []struct {
130                 path     string
131                 tmplStr  string
132                 respWant *EstimateTxGasResp
133         }{
134                 {
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,
139                         },
140                 },
141                 /*
142                         {
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,
147                                 },
148                         },
149                         {
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,
154                                 },
155                         },
156                 */
157         }
158         for _, c := range cases {
159                 template := txbuilder.Template{}
160                 err := json.Unmarshal([]byte(c.tmplStr), &template)
161                 if err != nil {
162                         t.Fatal(err)
163                 }
164                 estimateTxGasResp, err := EstimateTxGas(template)
165                 realTotalNeu := float64(c.respWant.TotalNeu)
166                 rate := math.Abs((float64(estimateTxGasResp.TotalNeu) - realTotalNeu) / float64(estimateTxGasResp.TotalNeu))
167                 if rate > 0.2 {
168                         t.Errorf(`the estimateNeu over realNeu 20%%`)
169                 }
170         }
171 }