-
Notifications
You must be signed in to change notification settings - Fork 20
/
apis_test.go
192 lines (153 loc) · 5.63 KB
/
apis_test.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"io"
"log"
"testing"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/RiemaLabs/modular-indexer-committee/apis"
"github.com/RiemaLabs/modular-indexer-committee/ord/stateless"
"github.com/gin-gonic/gin"
)
func TestAPI_GetLatestStateProof(t *testing.T) {
loadGetLatestStateProof(uint(779980), t)
}
func TestAPI_GetLatestStateProof_ZeroTransfers(t *testing.T) {
// There is no transaction at block 779940.
loadGetLatestStateProof(uint(779940), t)
}
func loadGetLatestStateProof(catchupHeight uint, t *testing.T) {
ordGetterTest, arguments := loadMain(782000)
queue, _ := CatchupStage(ordGetterTest, &arguments, stateless.BRC20StartHeight-1, catchupHeight)
// Set gin as test mode
gin.SetMode(gin.TestMode)
r := gin.Default()
r.GET("/v1/brc20_verifiable/latest_state_proof", func(c *gin.Context) {
apis.GetLatestStateProof(c, queue)
})
// create test server
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL+"/v1/brc20_verifiable/latest_state_proof", nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// check status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("[TestGetLatestStateProof]", err)
}
// Get result
var res apis.Brc20VerifiableLatestStateProofResponse
if err := json.Unmarshal(body, &res); err != nil {
log.Fatal("[TestGetLatestStateProof]", err)
}
lastIndex := len(queue.History) - 1
preState, _ := stateless.Rollingback(queue.Header, &queue.History[lastIndex])
_, err = apis.GeneratePostRoot(preState.Commit(), queue.LatestHeight(), &res)
if err != nil {
log.Fatal("With error: ", err)
}
}
func TestAPI_VerifyCurrentBalanceOfPkscript(t *testing.T) {
loadVerifyCurrentBalanceOfPkscript("ordi", "5120409943cab2dee3c71940969a612c6ee65c57cad1f064ca8db4508dab49260ca3", uint(779960), t)
}
func loadVerifyCurrentBalanceOfPkscript(tick string, pkScript string, catchupHeight uint, t *testing.T) {
ordGetterTest, arguments := loadMain(782000)
queue, _ := CatchupStage(ordGetterTest, &arguments, stateless.BRC20StartHeight-1, catchupHeight)
// Get current balance from api
// Set gin as test mode
gin.SetMode(gin.TestMode)
// register route
r := gin.Default()
r.GET("/v1/brc20_verifiable/current_balance_of_pkscript", func(c *gin.Context) {
apis.GetCurrentBalanceOfPkscript(c, queue)
})
// create test server
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL+"/v1/brc20_verifiable/current_balance_of_pkscript?tick="+tick+"&pkscript="+pkScript, nil)
if err != nil {
t.Fatal("[TestVerifyCurrentBalanceOfPkscript]", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal("[TestVerifyCurrentBalanceOfPkscript]", err)
}
defer resp.Body.Close()
// check status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("[TestVerifyCurrentBalanceOfPkscript]", err)
}
// Get result
var res apis.Brc20VerifiableCurrentBalanceOfPkscriptResponse
if err := json.Unmarshal(body, &res); err != nil {
log.Fatal("[TestVerifyCurrentBalanceOfPkscript]", err)
}
log.Println("[res]: ", res.Result.AvailableBalance)
_, err = apis.VerifyCurrentBalanceOfPkscript(queue.Header.Root.Commit(), tick, pkScript, &res)
if err != nil {
log.Fatalf("[TestVerifyCurrentBalanceOfPkscript] verify not right. At tick %s, pkScript %s, height %d", tick, pkScript, catchupHeight)
log.Fatal("With error: ", err)
}
}
func TestAPI_VerifyCurrentBalanceOfWallet(t *testing.T) {
loadVerifyCurrentBalanceOfWallet("meme", "bc1prvqdfjku8359hk9uc2tdgg0xlwvsel2fjr9ysydmaas9x3kyzuvskuwmlq", uint(779980), t, 782000)
}
func loadVerifyCurrentBalanceOfWallet(tick string, wallet string, catchupHeight uint, t *testing.T, loadHeight uint) {
ordGetterTest, arguments := loadMain(loadHeight)
queue, _ := CatchupStage(ordGetterTest, &arguments, stateless.BRC20StartHeight-1, catchupHeight)
// Get current balance from api
// Set gin as test mode
gin.SetMode(gin.TestMode)
// register route
r := gin.Default()
r.GET("/v1/brc20_verifiable/current_balance_of_wallet", func(c *gin.Context) {
apis.GetCurrentBalanceOfWallet(c, queue)
})
// create test server
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL+"/v1/brc20_verifiable/current_balance_of_wallet?tick="+tick+"&wallet="+wallet, nil)
if err != nil {
t.Fatal("[TestVerifyCurrentBalanceOfWallet]", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal("[TestVerifyCurrentBalanceOfWallet]", err)
}
defer resp.Body.Close()
// check status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("[TestVerifyCurrentBalanceOfWallet]", err)
}
// Get result
var res apis.Brc20VerifiableCurrentBalanceOfWalletResponse
if err := json.Unmarshal(body, &res); err != nil {
log.Fatal("[TestVerifyCurrentBalanceOfWallet]", err)
}
log.Println("[OverallBalance res]: ", res.Result.OverallBalance)
log.Println("[AvailableBalance res]: ", res.Result.AvailableBalance)
_, err = apis.VerifyCurrentBalanceOfWallet(queue.Header.Root.Commit(), tick, wallet, &res)
if err != nil {
// log.Fatalf("[TestVerifyCurrentBalanceOfWallet] verify not right. At tick %s, wallet %s, height %d", tick, wallet, catchupHeight)
log.Fatal("With error: ", err)
}
}