Skip to content

Commit 4efc209

Browse files
committed
Fix tests for the relayer
1 parent 1e235ee commit 4efc209

39 files changed

+3324
-123
lines changed

docs/tests/NFT.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ contract NFT is NilTokenBase, NilOwnable {
3434
* @notice A 'wrapper' over mintTokenInternal(). Only one NFT can be minted.
3535
*/
3636
function mintNFT() public onlyOwner {
37-
require(totalSupply == 0, "NFT has already been minted");
37+
require(getTokenTotalSupply() == 0, "NFT has already been minted");
3838
require(!hasBeenSent, "NFT has already been sent");
3939
mintTokenInternal(1);
4040
}

docs/tests/clone-factory.test.mts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
33
import util from "node:util";
4-
import { HttpTransport, PublicClient, generateSmartAccount, CheckReceiptSuccess, ProcessedReceipt } from "@nilfoundation/niljs";
4+
import {
5+
CheckReceiptSuccess,
6+
HttpTransport,
7+
type ProcessedReceipt,
8+
PublicClient,
9+
generateSmartAccount,
10+
} from "@nilfoundation/niljs";
511
import type { Abi } from "viem";
612
import { CLONE_FACTORY_COMPILATION_COMMAND } from "./compilationCommands";
713
import { FAUCET_GLOBAL, RPC_GLOBAL } from "./globals";
@@ -70,8 +76,7 @@ function getAddressFromEvent(receipts: ProcessedReceipt[], index: number): `0x${
7076
const receipt = receipts[index];
7177
expect(receipt.logs.length).greaterThan(0);
7278
expect(receipt.logs[0].topics.length).greaterThan(1);
73-
return ("0x" + receipt.logs[0].topics[1].slice(-40)) as `0x${string}`;
74-
79+
return `0x${receipt.logs[0].topics[1].slice(-40)}` as `0x${string}`;
7580
}
7681

7782
describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
@@ -104,7 +109,7 @@ describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
104109
value: 50_000_000n * gasPrice,
105110
});
106111

107-
const factoryManagerReceipts = await factoryManagerTx.wait({waitTillMainShard: true});
112+
const factoryManagerReceipts = await factoryManagerTx.wait({ waitTillMainShard: true });
108113

109114
expect(factoryManagerReceipts.some((receipt) => !CheckReceiptSuccess(receipt))).toBe(false);
110115

@@ -116,7 +121,7 @@ describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
116121
args: [2, SALT],
117122
});
118123

119-
const createMasterChildReceipts = await createMasterChildTx.wait({waitTillMainShard: true});
124+
const createMasterChildReceipts = await createMasterChildTx.wait({ waitTillMainShard: true });
120125

121126
const masterChildAddress = getAddressFromEvent(createMasterChildReceipts, 1);
122127

@@ -130,7 +135,7 @@ describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
130135
args: [2, SALT],
131136
});
132137

133-
const createFactoryReceipts = await createFactoryTx.wait({waitTillMainShard: true});
138+
const createFactoryReceipts = await createFactoryTx.wait({ waitTillMainShard: true });
134139

135140
const factoryAddress = getAddressFromEvent(createFactoryReceipts, 1);
136141

@@ -144,7 +149,7 @@ describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
144149
args: [SALT],
145150
});
146151

147-
const createCloneReceipts = await createCloneTx.wait({waitTillMainShard: true});
152+
const createCloneReceipts = await createCloneTx.wait({ waitTillMainShard: true });
148153

149154
const cloneAddress = getAddressFromEvent(createCloneReceipts, 1);
150155

@@ -158,11 +163,11 @@ describe.sequential("Nil.js can fully tests the CloneFactory", async () => {
158163
feeCredit: 3_000_000n * gasPrice,
159164
});
160165

161-
const incrementReceipts = await incrementTx.wait({waitTillMainShard: true});
166+
const incrementReceipts = await incrementTx.wait({ waitTillMainShard: true });
162167

163168
expect(incrementReceipts.some((receipt) => !CheckReceiptSuccess(receipt))).toBe(false);
164169

165-
await new Promise(resolve => setTimeout(resolve, 5000));
170+
await new Promise((resolve) => setTimeout(resolve, 5000));
166171

167172
const result = await client.call(
168173
{

docs/tests/cookbook-deploy-smart-contract.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FAUCET_GLOBAL, RPC_GLOBAL } from "./globals";
22

33
//startImportStatements
44
import {
5+
CheckReceiptSuccess,
56
ExternalTransactionEnvelope,
67
HttpTransport,
78
PublicClient,
@@ -12,7 +13,6 @@ import {
1213
hexToBytes,
1314
topUp,
1415
waitTillCompleted,
15-
CheckReceiptSuccess,
1616
} from "@nilfoundation/niljs";
1717

1818
import { type Abi, encodeFunctionData } from "viem";

docs/tests/cookbook-english-auction.test.mts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { AUCTION_COMPILATION_COMMAND, NFT_COMPILATION_COMMAND } from "./compilat
66
import { FAUCET_GLOBAL, RPC_GLOBAL } from "./globals";
77

88
//startImportStatements
9-
import { HttpTransport, PublicClient, generateSmartAccount, CheckReceiptSuccess } from "@nilfoundation/niljs";
9+
import {
10+
CheckReceiptSuccess,
11+
HttpTransport,
12+
PublicClient,
13+
generateSmartAccount,
14+
} from "@nilfoundation/niljs";
1015
import { type Abi, encodeFunctionData } from "viem";
1116
//endImportStatements
1217

@@ -120,7 +125,7 @@ describe.sequential("Nil.js can fully interact with EnglishAuction", async () =>
120125
}),
121126
});
122127

123-
const receiptsOwnership = await changeOwnershipTx.wait({waitTillMainShard: true});
128+
const receiptsOwnership = await changeOwnershipTx.wait({ waitTillMainShard: true });
124129

125130
const startAuctionTx = await smartAccount.sendTransaction({
126131
to: addressAuction,
@@ -132,7 +137,7 @@ describe.sequential("Nil.js can fully interact with EnglishAuction", async () =>
132137
}),
133138
});
134139

135-
const receiptsStart = await startAuctionTx.wait({waitTillMainShard: true});
140+
const receiptsStart = await startAuctionTx.wait({ waitTillMainShard: true });
136141

137142
//endStartAuction
138143
expect(receiptsOwnership.some((receipt) => !CheckReceiptSuccess(receipt))).toBe(false);

docs/tests/cookbook-multisig.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createRequire } from "node:module";
44
const require = createRequire(import.meta.url);
55

66
import {
7+
CheckReceiptSuccess,
78
ExternalTransactionEnvelope,
89
type Hex,
910
HttpTransport,
@@ -22,7 +23,6 @@ import {
2223
refineAddress,
2324
refineSalt,
2425
waitTillCompleted,
25-
CheckReceiptSuccess,
2626
} from "@nilfoundation/niljs";
2727
import { secp256k1 } from "@noble/curves/secp256k1";
2828

nil/contracts/solidity/tests/BounceTest.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ contract Callee {
1616
}
1717
}
1818

19-
contract BounceTest is NilBounceable {
19+
contract BounceTest is NilBase {
2020
using Nil for address;
2121

2222
string last_bounce_err;

nil/contracts/solidity/tests/RequestResponseTest.sol

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
1010
int32 public counterValue;
1111
uint public intValue;
1212
string public strValue;
13+
uint constant private ASYNC_VALUE = 2_000_000;
1314

1415
function verifyExternal(
1516
uint256,
@@ -40,7 +41,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
4041
address counter,
4142
uint intContext,
4243
string memory strContext
43-
) public async (500_000) {
44+
) public async(ASYNC_VALUE) {
4445
bytes memory context = abi.encode(intContext, strContext);
4546
bytes memory callData = abi.encodeWithSignature("get()");
4647
sendRequest(
@@ -69,7 +70,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
6970
function nestedRequest(
7071
address callee,
7172
address counter
72-
) public async (500_000) {
73+
) public async(ASYNC_VALUE) {
7374
bytes memory callData = abi.encodeWithSelector(this.requestCounterGet.selector, counter, 123, "test");
7475
sendRequest(
7576
callee,
@@ -95,7 +96,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
9596
*/
9697
function sendRequestFromCallback(
9798
address counter
98-
) public async (500_000) {
99+
) public async(ASYNC_VALUE) {
99100
bytes memory context = abi.encode(int32(5), counter);
100101
bytes memory callData = abi.encodeWithSignature("add(int32)", 5);
101102
sendRequest(
@@ -112,7 +113,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
112113
bool success,
113114
bytes memory,
114115
bytes memory context
115-
) public async (500_000) {
116+
) public async(ASYNC_VALUE) {
116117
require(success, "Request failed");
117118
(int32 sendNext, address counter) = abi.decode(context, (int32, address));
118119
if (sendNext == 0) {
@@ -136,7 +137,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
136137
/**
137138
* Test Counter's add method. No context and empty return data.
138139
*/
139-
function requestCounterAdd(address counter, int32 valueToAdd) public async (500_000) {
140+
function requestCounterAdd(address counter, int32 valueToAdd) public async(ASYNC_VALUE) {
140141
bytes memory callData = abi.encodeWithSignature(
141142
"add(int32)",
142143
valueToAdd
@@ -164,7 +165,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
164165
/**
165166
* Test failure with value.
166167
*/
167-
function requestCheckFail(address addr, bool fail) public async (500_000) {
168+
function requestCheckFail(address addr, bool fail) public async (2_000_000) {
168169
bytes memory context = abi.encode(uint(11111));
169170
bytes memory callData = abi.encodeWithSignature(
170171
"checkFail(bool)",
@@ -193,7 +194,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
193194
/**
194195
* Test out of gas failure.
195196
*/
196-
function requestOutOfGasFailure(address counter) public async (500_000) {
197+
function requestOutOfGasFailure(address counter) public async(ASYNC_VALUE) {
197198
bytes memory context = abi.encode(uint(1234567890));
198199
bytes memory callData = abi.encodeWithSignature("outOfGasFailure()");
199200
sendRequest(
@@ -229,7 +230,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
229230
/**
230231
* Test token sending.
231232
*/
232-
function requestSendToken(address addr, uint256 amount) public async (500_000) {
233+
function requestSendToken(address addr, uint256 amount) public async(ASYNC_VALUE) {
233234
bytes memory context = abi.encode(uint(11111));
234235
bytes memory callData = abi.encodeWithSignature("get()");
235236
Nil.Token[] memory tokens = new Nil.Token[](1);
@@ -260,7 +261,7 @@ contract RequestResponseTest is NilTokenBase, NilAwaitable {
260261
/**
261262
* Fail during request sending. Context storage should not be changed.
262263
*/
263-
function failDuringRequestSending(address counter) public async (500_000) {
264+
function failDuringRequestSending(address counter) public async(ASYNC_VALUE) {
264265
bytes memory context = abi.encode(intValue, strValue);
265266
bytes memory callData = abi.encodeWithSignature("get()");
266267
sendRequest(

nil/internal/collate/proposer_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package collate
22

33
import (
4+
"fmt"
5+
"github.com/NilFoundation/nil/nil/common"
6+
"github.com/NilFoundation/nil/nil/services/txnpool"
47
"testing"
58

6-
"github.com/NilFoundation/nil/nil/common"
79
"github.com/NilFoundation/nil/nil/common/logging"
810
"github.com/NilFoundation/nil/nil/internal/config"
911
"github.com/NilFoundation/nil/nil/internal/contracts"
1012
"github.com/NilFoundation/nil/nil/internal/db"
1113
"github.com/NilFoundation/nil/nil/internal/execution"
1214
"github.com/NilFoundation/nil/nil/internal/types"
13-
"github.com/NilFoundation/nil/nil/services/txnpool"
1415
"github.com/stretchr/testify/suite"
1516
)
1617

@@ -92,8 +93,6 @@ func (s *ProposerTestSuite) TestBlockGas() {
9293
func (s *ProposerTestSuite) TestCollator() {
9394
to := contracts.CounterAddress(s.T(), s.shardId)
9495

95-
const asyncGass = 2_000_000
96-
9796
pool := &MockTxnPool{}
9897
params := s.newParams()
9998
p := newTestProposer(params, pool)
@@ -144,7 +143,10 @@ func (s *ProposerTestSuite) TestCollator() {
144143
balance = balance.
145144
Sub(txnValue).Sub(r1.GasUsed.ToValue(types.DefaultGasPrice)).Sub(r1.Forwarded).
146145
Sub(txnValue).Sub(r2.GasUsed.ToValue(types.DefaultGasPrice)).Sub(r2.Forwarded).
147-
Sub(types.GasToValue(asyncGass * 2))
146+
Sub(types.GasToValue(execution.DefaultAsyncGas.Uint64() * 2))
147+
diff, _ := balance.SubOverflow(s.getMainBalance())
148+
s.Require().Equal(balance, s.getMainBalance(),
149+
fmt.Sprintf("Balance mismatch: expected=%s, got=%s, diff=%s", balance, s.getMainBalance(), diff))
148150
s.Equal(balance, s.getMainBalance())
149151
s.Equal(types.Value{}, s.getBalance(shardId, to))
150152
})
@@ -171,7 +173,7 @@ func (s *ProposerTestSuite) TestCollator() {
171173
// Two refund transactions
172174
s.Len(proposal.InternalTxns, 2)
173175

174-
balance = balance.Add(r1.Forwarded).Add(r2.Forwarded).Add(types.GasToValue(asyncGass * 2))
176+
balance = balance.Add(r1.Forwarded).Add(r2.Forwarded).Add(types.GasToValue(execution.DefaultAsyncGas.Uint64() * 2))
175177

176178
s.Equal(balance, s.getMainBalance())
177179

nil/internal/execution/state_trace.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ func (bt *BlocksTracer) Trace(es *ExecutionState, block *types.Block, blockHash
125125
bt.Printf("gas_used: %d\n", receipt.GasUsed)
126126
bt.Printf("txn_hash: %s\n", receipt.TxnHash.Hex())
127127
bt.Printf("address: %s\n", receipt.ContractAddress.Hex())
128+
if len(receipt.Logs) > 0 {
129+
bt.Printf("logs:\n")
130+
for j, log := range receipt.Logs {
131+
bt.WithIndent(func(t *BlocksTracer) {
132+
bt.Printf("%d:\n", j)
133+
bt.WithIndent(func(t *BlocksTracer) {
134+
bt.Printf("address: %s\n", log.Address.Hex())
135+
bt.Printf("topics: %v\n", log.Topics)
136+
if len(log.Data) < 1024 {
137+
bt.Printf("data: %s\n", hexutil.Encode(log.Data))
138+
} else {
139+
bt.Printf("data_size: %d\n", len(log.Data))
140+
}
141+
})
142+
})
143+
}
144+
}
128145
})
129146

130147
outTransactions, ok := es.OutTransactions[txnHash]

nil/internal/execution/testaide.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var (
2525
)
2626

2727
const (
28-
DefaultGasLimit = 1_000_000
28+
DefaultGasLimit = 1_000_000
29+
DefaultAsyncGas types.Gas = 30_000_000
2930
)
3031

3132
func init() {

0 commit comments

Comments
 (0)