Skip to content

Commit 8e13894

Browse files
gluaxhacheigriega
authored andcommitted
feat(core): port contract tests on removing data requests
Also improve tests on gas metering and payouts.
1 parent aa23a96 commit 8e13894

File tree

13 files changed

+548
-124
lines changed

13 files changed

+548
-124
lines changed

testutil/contract_msgs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ func StakeMsg(stakerPubKey, proof, memo string) []byte {
6868
}`, stakerPubKey, proof, memo))
6969
}
7070

71-
func PostDataRequestMsg(execProgHash, tallyProgHash []byte, requestMemo string, replicationFactor int) []byte {
71+
func PostDataRequestMsg(execProgHash, tallyProgHash []byte, requestMemo string, replicationFactor int, execGasLimit, tallyGasLimit uint64) []byte {
7272
return []byte(fmt.Sprintf(`{
7373
"post_data_request": {
7474
"posted_dr": {
7575
"version": "0.0.1",
7676
"exec_program_id": "%s",
7777
"exec_inputs": "ZXhlY19pbnB1dHM=",
78-
"exec_gas_limit": 100000000000000000,
78+
"exec_gas_limit": %d,
7979
"tally_program_id": "%s",
8080
"tally_inputs": "dGFsbHlfaW5wdXRz",
81-
"tally_gas_limit": 300000000000000,
81+
"tally_gas_limit": %d,
8282
"replication_factor": %d,
8383
"consensus_filter": "AA==",
8484
"gas_price": "2000",
@@ -87,5 +87,5 @@ func PostDataRequestMsg(execProgHash, tallyProgHash []byte, requestMemo string,
8787
"seda_payload": "",
8888
"payback_address": "AQID"
8989
}
90-
}`, hex.EncodeToString(execProgHash), hex.EncodeToString(tallyProgHash), replicationFactor, requestMemo))
90+
}`, hex.EncodeToString(execProgHash), execGasLimit, hex.EncodeToString(tallyProgHash), tallyGasLimit, replicationFactor, requestMemo))
9191
}

testutil/testwasms/testwasms.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ var TestWasms = [][]byte{
9696
HelloWorldWasm(),
9797
}
9898

99+
var TestWasmNames = []string{
100+
"sample_tally",
101+
"sample_tally_2",
102+
"random_string_tally",
103+
"invalid_import",
104+
"chaos_dr",
105+
"data_proxy",
106+
"http_heavy",
107+
"long_http",
108+
"max_dr",
109+
"max_result",
110+
"memory",
111+
"mock_api",
112+
"price_feed",
113+
"random_number",
114+
"big",
115+
"hello_world",
116+
}
117+
99118
// v1.0.16 Core Contract with commit/reveal refund tx call removed
100119
func CoreContractWasm() []byte {
101120
return coreContract

x/core/keeper/core_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,79 @@ func TestEndBlock(t *testing.T) {
158158
}
159159
}
160160

161+
// TestPayouts focuses on checking before and after balances of relevant accounts
162+
// and objects (data request poster, executors, and data proxies).
163+
func TestPayouts(t *testing.T) {
164+
f := testutil.InitFixture(t, false, nil)
165+
166+
// Set up 5 executors and 2 data proxies.
167+
f.AddStakers(t, 5)
168+
169+
proxyPubKeys := []string{"03b27f2df0cbdb5cdadff5b4be0c9fda5aa3a59557ef6d0b49b4298ef42c8ce2b0", "020173bd90e73c5f8576b3141c53aa9959b10a1daf1bc9c0ccf0a942932c703dec"}
170+
proxyPayoutAddrs := []sdk.AccAddress{sdk.MustAccAddressFromBech32("seda1zcds6ws7l0e005h3xrmg5tx0378nyg8gtmn64f"), sdk.MustAccAddressFromBech32("seda149sewl80wccuzhhukxgn2jg4kcun02d8qclwkt")}
171+
f.AddDataProxy(t, proxyPubKeys[0], proxyPayoutAddrs[0].String(), sdk.NewCoin(testutil.BondDenom, math.NewInt(4e18)))
172+
f.AddDataProxy(t, proxyPubKeys[1], proxyPayoutAddrs[1].String(), sdk.NewCoin(testutil.BondDenom, math.NewInt(6e18)))
173+
174+
tests := []struct {
175+
name string
176+
gasUsed uint64
177+
expExecutorRewards [5]math.Int
178+
expProxyRewards [2]math.Int
179+
}{
180+
{
181+
name: "replication factor of 1",
182+
gasUsed: 150e15,
183+
expExecutorRewards: [5]math.Int{math.NewInt(145e15).Mul(math.NewInt(2e3)), math.ZeroInt(), math.ZeroInt(), math.ZeroInt(), math.ZeroInt()},
184+
expProxyRewards: [2]math.Int{math.NewInt(4e18), math.NewInt(6e18)},
185+
},
186+
}
187+
for _, tt := range tests {
188+
t.Run(tt.name, func(t *testing.T) {
189+
// Check balances before
190+
proxyBalancesBefore := make([]math.Int, len(proxyPayoutAddrs))
191+
for i, addr := range proxyPayoutAddrs {
192+
balance := f.BankKeeper.GetBalance(f.Context(), addr, testutil.BondDenom)
193+
proxyBalancesBefore[i] = balance.Amount
194+
}
195+
196+
executorBalancesBefore := make([]math.Int, len(f.Stakers))
197+
for i, staker := range f.Stakers {
198+
executor, err := f.CoreKeeper.GetStaker(f.Context(), staker.PubKey)
199+
require.NoError(t, err)
200+
executorBalancesBefore[i] = executor.PendingWithdrawal
201+
}
202+
203+
// Execute data request flow and core end block
204+
dr := testutil.NewTestDRWithRandomPrograms(
205+
[]byte("reveal"), // reveal
206+
base64.StdEncoding.EncodeToString([]byte("memo")), // memo
207+
tt.gasUsed, // gas used
208+
0, // exit code
209+
proxyPubKeys,
210+
1, // replication factor
211+
f.Context().BlockTime(),
212+
)
213+
214+
dr.ExecuteDataRequestFlow(f, 1, 1, false)
215+
216+
err := f.CoreKeeper.EndBlock(f.Context())
217+
require.NoError(t, err)
218+
219+
// Check balances after
220+
for i, addr := range proxyPayoutAddrs {
221+
balance := f.BankKeeper.GetBalance(f.Context(), addr, testutil.BondDenom)
222+
require.Equal(t, proxyBalancesBefore[i].Add(tt.expProxyRewards[i]), balance.Amount)
223+
}
224+
225+
for i, staker := range f.Stakers {
226+
executor, err := f.CoreKeeper.GetStaker(f.Context(), staker.PubKey)
227+
require.NoError(t, err)
228+
require.Equal(t, executorBalancesBefore[i].Add(tt.expExecutorRewards[i]), executor.PendingWithdrawal)
229+
}
230+
})
231+
}
232+
}
233+
161234
func TestTxFeeRefund(t *testing.T) {
162235
f := testutil.InitFixture(t, false, nil)
163236
f.AddStakers(t, 5)

x/core/keeper/data_request_tests/post_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestPostWorks(t *testing.T) {
1919
_ = f.CreateStakedTestAccount("alice", 22, 10)
2020

2121
// try to get dr that doesn't exist yet
22-
_, err := bob.GetDataRequest("44f24df3aa5d1b5e809090a67b63539cd89b974a7aba0d661e1b7c6436624663")
22+
_, err := bob.GetDataRequest("b426fdf3c5aabe17ab030427965f3e1c35de064090343dc78eb7f5967b57b949")
2323
require.ErrorContains(t, err, "not found")
2424

2525
// Bob posts a data request
@@ -28,7 +28,7 @@ func TestPostWorks(t *testing.T) {
2828
require.NoError(t, err)
2929

3030
// Dr can now be found
31-
drFound, err := bob.GetDataRequest("44f24df3aa5d1b5e809090a67b63539cd89b974a7aba0d661e1b7c6436624663")
31+
drFound, err := bob.GetDataRequest("b426fdf3c5aabe17ab030427965f3e1c35de064090343dc78eb7f5967b57b949")
3232
require.NoError(t, err)
3333
require.Equal(t, postDrResult.DrID, drFound.DataRequest.ID)
3434

x/core/keeper/data_request_tests/query_by_status_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestDrsAreSortedByGasAndHeight(t *testing.T) {
138138
require.Equal(t, int64(1), drsResp.DataRequests[3].PostedHeight)
139139
}
140140

141-
func TestLastSeenIndexWorks(t *testing.T) {
141+
func TestLastSeenIndex(t *testing.T) {
142142
f := testutil.InitFixture(t, false, nil)
143143

144144
bob := f.CreateTestAccount("bob", 22)
@@ -148,34 +148,34 @@ func TestLastSeenIndexWorks(t *testing.T) {
148148
dr2 := bob.CalculateDrIDAndArgs("2", 1)
149149
dr3 := bob.CalculateDrIDAndArgs("3", 1)
150150

151-
// They all have same gas price, and height so ID will determine order
152-
_, err := bob.PostDataRequest(dr1, 1, nil)
151+
// They all have same gas price and height, so ID will determine order
152+
// Note the sorted order is descending by gas price, then height, then ID.
153+
postResult1, err := bob.PostDataRequest(dr1, 1, nil) // b426fdf3c5aabe17ab030427965f3e1c35de064090343dc78eb7f5967b57b949
153154
require.NoError(t, err)
154-
// This dr ID winds up being first
155-
postDrResult2, err := bob.PostDataRequest(dr2, 1, nil)
155+
_, err = bob.PostDataRequest(dr2, 1, nil) // 43699c923a6696aa63315589b04a8cf181aab5560239725b1454fb0f66993e27
156156
require.NoError(t, err)
157-
_, err = bob.PostDataRequest(dr3, 1, nil)
157+
_, err = bob.PostDataRequest(dr3, 1, nil) // 13bcf175ebdd0ab970bfff9e773ac15ef95ee0ac4bdbbbff8a36c8405b1f8056
158158
require.NoError(t, err)
159159

160160
firstDrResp, err := bob.GetDataRequestsByStatus(types.DATA_REQUEST_STATUS_COMMITTING, 1, nil)
161161
require.NoError(t, err)
162162
require.Len(t, firstDrResp.DataRequests, 1)
163-
require.Equal(t, postDrResult2.DrID, firstDrResp.DataRequests[0].ID)
163+
require.Equal(t, postResult1.DrID, firstDrResp.DataRequests[0].ID)
164164

165165
remainingDrResp, err := bob.GetDataRequestsByStatus(types.DATA_REQUEST_STATUS_COMMITTING, 10, &firstDrResp.LastSeenIndex)
166166
require.NoError(t, err)
167167
require.Len(t, remainingDrResp.DataRequests, 2)
168168

169-
var dr2Seen bool
169+
var dr1Seen bool
170170
for _, dr := range remainingDrResp.DataRequests {
171-
if dr.ID == postDrResult2.DrID {
172-
dr2Seen = true
171+
if dr.ID == postResult1.DrID {
172+
dr1Seen = true
173173
}
174174
}
175-
require.False(t, dr2Seen)
175+
require.False(t, dr1Seen)
176176
}
177177

178-
func TestQueryByStatusManyDrsWorks(t *testing.T) {
178+
func TestQueryByStatusManyDrs(t *testing.T) {
179179
f := testutil.InitFixture(t, false, nil)
180180

181181
bob := f.CreateTestAccount("bob", 2+25*20)
@@ -220,7 +220,7 @@ func TestQueryByStatusManyDrsWorks(t *testing.T) {
220220
require.Len(t, tallyingDrsResp.DataRequests, 3)
221221
}
222222

223-
func TestQueryByStatusManyMoreDrsWorks(t *testing.T) {
223+
func TestQueryByStatusManyMoreDrs(t *testing.T) {
224224
f := testutil.InitFixture(t, false, nil)
225225

226226
bob := f.CreateTestAccount("bob", 2+163*20)
@@ -323,7 +323,7 @@ func TestQueryByStatusManyMoreDrsWorks(t *testing.T) {
323323
require.Len(t, tallyingDrsResp.DataRequests, 0)
324324
}
325325

326-
func TestQueryStatusesWorks(t *testing.T) {
326+
func TestQueryStatuses(t *testing.T) {
327327
f := testutil.InitFixture(t, false, nil)
328328

329329
bob := f.CreateStakedTestAccount("bob", 22, 1)

0 commit comments

Comments
 (0)