Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emizzle committed Oct 23, 2024
1 parent ee1c0f1 commit 4c9cfd3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
21 changes: 0 additions & 21 deletions testmodule/providers/jsonrpc/testJsonRpcSigner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,3 @@ suite "JsonRpcSigner":
transaction.chainId = 0xdeadbeef.u256.some
expect SignerError:
discard await signer.populateTransaction(transaction)

test "concurrent populate calls increment nonce":
let signer = provider.getSigner()
let count = await signer.getTransactionCount(BlockTag.pending)
var transaction1 = Transaction.example
var transaction2 = Transaction.example
var transaction3 = Transaction.example

let populated = await allFinished(
signer.populateTransaction(transaction1),
signer.populateTransaction(transaction2),
signer.populateTransaction(transaction3)
)

transaction1 = await populated[0]
transaction2 = await populated[1]
transaction3 = await populated[2]

check !transaction1.nonce == count
check !transaction2.nonce == count + 1.u256
check !transaction3.nonce == count + 2.u256
41 changes: 41 additions & 0 deletions testmodule/testContracts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,44 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
.confirm(1)

check receipt.status == TransactionStatus.Success

test "can cancel procs that execute transactions":
let signer = provider.getSigner()
let token = TestToken.new(token.address, signer)
let countBefore = await signer.getTransactionCount(BlockTag.pending)

proc executeTx {.async.} =
discard await token.mint(accounts[0], 100.u256)

await executeTx().cancelAndWait()
let countAfter = await signer.getTransactionCount(BlockTag.pending)
check countBefore == countAfter

test "concurrent transactions succeed even if one is cancelled":
let signer = provider.getSigner()
let token = TestToken.new(token.address, signer)
let balanceBefore = await token.myBalance()

proc executeTx: Future[Confirmable] {.async.} =
return await token.mint(accounts[0], 100.u256)

proc executeTxWithCancellation: Future[Confirmable] {.async.} =
let fut = token.mint(accounts[0], 100.u256)
fut.cancelSoon()
return await fut

# emulate concurrent populateTransaction/sendTransaction calls, where the
# first one fails
let futs = await allFinished(
executeTxWithCancellation(),
executeTx(),
executeTx()
)
let receipt1 = await futs[1].confirm(0)
let receipt2 = await futs[2].confirm(0)

check receipt1.status == TransactionStatus.Success
check receipt2.status == TransactionStatus.Success

let balanceAfter = await token.myBalance()
check balanceAfter == balanceBefore + 200.u256

0 comments on commit 4c9cfd3

Please sign in to comment.