Skip to main content

Interacting with the Blockchain in Tests

The OP_NET Unit Test Framework provides a Blockchain class that allows developers to simulate and manipulate the blockchain environment for testing purposes. This guide covers key functionalities, including setting block numbers, simulating transactions, and working with accounts.


Using the Blockchain Class in Tests

The Blockchain class is central to managing blockchain interactions during tests. It enables you to set up blocks, transactions, and accounts dynamically.

Setting and Manipulating Blockchain.blockNumber

You can directly manipulate the block number to simulate changes in blockchain state.

import { Blockchain } from "@btc-vision/unit-test-framework";

Blockchain.blockNumber = 1234n; // Set the block number
Blockchain.log(`Current Block Number: ${Blockchain.blockNumber}`);

Manipulating Transactions and Blocks

Simulating Transactions

You can assign a custom transaction object to Blockchain.transaction to simulate a transaction.

import { Transaction } from "@btc-vision/transaction";
import { Blockchain } from "@btc-vision/unit-test-framework";


const customTransaction = new Transaction(
..., // uint8Array format (txId)
[ // inputs
{
outputIndex: ..., // number format (index)
scriptSig: ..., // Uint8Array format (script)
txId: ..., // Uint8Array format (txId)
},
],
[ // outputs
{
index: ..., // number format (index)
to: ..., // string format (address)
value: ..., // bigint format (value)
},
]
);

Blockchain.transaction = customTransaction;
Blockchain.log(`Transaction ID: ${Blockchain.transaction.id}`);

Simulating Accounts

Generating Random Addresses

Use the Blockchain.generateRandomAddress method to create random addresses for testing purposes.

const randomAddress = Blockchain.generateRandomAddress();
Blockchain.log(`Generated Random Address: ${randomAddress}`);

Setting Blockchain.msgSender and Blockchain.txOrigin

You can define the sender (msgSender) and origin (txOrigin) of a transaction to simulate contract calls.

Blockchain.msgSender = Blockchain.generateRandomAddress();
Blockchain.txOrigin = Blockchain.generateRandomAddress();

Blockchain.log(`Message Sender: ${Blockchain.msgSender}`);
Blockchain.log(`Transaction Origin: ${Blockchain.txOrigin}`);

Example Test: Simulating Blockchain Interactions

import {
Assert,
Blockchain,
opnet,
OPNetUnit,
} from "@btc-vision/unit-test-framework";

await opnet("Blockchain Simulation Test Suite", async (vm: OPNetUnit) => {
await vm.beforeAll(() => {
Blockchain.blockNumber = 1n; // Initialize block number
});

await vm.it("should simulate transaction and account states", () => {
// Simulate sender and origin
Blockchain.msgSender = Blockchain.generateRandomAddress();
Blockchain.txOrigin = Blockchain.generateRandomAddress();

// Set and verify block number
Blockchain.blockNumber = 1234n;
Assert.expect(Blockchain.blockNumber).toEqual(1234n);

// Generate and verify random address
const randomAddress = Blockchain.generateRandomAddress();
Assert.expect(randomAddress).toNotEqual(Blockchain.msgSender);

Blockchain.log("Blockchain interactions simulated successfully.");
});
});

Best Practices

  • Use vm.beforeEach and vm.afterEach to ensure blockchain states are reset between tests.
  • Leverage Blockchain.log to debug interactions during test execution.
  • Use assertions to confirm the expected behavior of simulated blockchain interactions.