Skip to content

Commit 244e364

Browse files
authored
add global contract on startup (#5168)
1 parent 662aa31 commit 244e364

File tree

7 files changed

+122
-37
lines changed

7 files changed

+122
-37
lines changed

ironfish-cli/src/commands/evm/deploy-contract.ts renamed to ironfish-cli/src/commands/evm/shield-custom-asset.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import { LegacyTransaction } from '@ethereumjs/tx'
5-
import { Account, Address, hexToBytes } from '@ethereumjs/util'
5+
import { Account, Address } from '@ethereumjs/util'
66
import { generateKey } from '@ironfish/rust-nodejs'
7-
import { Assert, ContractArtifact, IronfishEvm } from '@ironfish/sdk'
7+
import { Assert, ContractArtifact, GLOBAL_CONTRACT_ADDRESS, IronfishEvm } from '@ironfish/sdk'
88
import { ethers } from 'ethers'
99
import { IronfishCommand } from '../../command'
1010
import { LocalFlags } from '../../flags'
@@ -32,26 +32,15 @@ export class TestEvmCommand extends IronfishCommand {
3232
await node.chain.blockchainDb.stateManager.putAccount(senderAddress, senderAccount)
3333
await node.chain.blockchainDb.stateManager.commit()
3434

35-
const globalContractAddress = Address.fromString(
36-
'0xffffffffffffffffffffffffffffffffffffffff',
37-
)
38-
39-
await node.chain.blockchainDb.stateManager.checkpoint()
40-
await node.chain.blockchainDb.stateManager.putContractCode(
41-
globalContractAddress,
42-
hexToBytes(ContractArtifact.deployedBytecode),
43-
)
44-
await node.chain.blockchainDb.stateManager.commit()
45-
4635
const contract = await node.chain.blockchainDb.stateManager.getAccount(
47-
globalContractAddress,
36+
GLOBAL_CONTRACT_ADDRESS,
4837
)
4938

5039
if (!contract) {
5140
this.error('Contract creation failed')
5241
}
5342

54-
this.log(`Contract created at: ${globalContractAddress.toString()}`)
43+
this.log(`Contract created at: ${GLOBAL_CONTRACT_ADDRESS.toString()}`)
5544

5645
const globalContract = new ethers.Interface(ContractArtifact.abi)
5746

@@ -64,7 +53,7 @@ export class TestEvmCommand extends IronfishCommand {
6453
const tx = new LegacyTransaction({
6554
nonce: 0n,
6655
gasLimit: 100_000n,
67-
to: globalContractAddress,
56+
to: GLOBAL_CONTRACT_ADDRESS,
6857
data: data,
6958
})
7059

@@ -77,17 +66,12 @@ export class TestEvmCommand extends IronfishCommand {
7766
this.log('Contract Address')
7867
this.log(Buffer.from(log[0]).toString('hex'))
7968

80-
// logging topics
81-
for (const topic of log[1]) {
82-
this.log(Buffer.from(topic).toString('ascii'))
83-
}
84-
8569
const [ironfishAddress, tokenId, caller, amount] = globalContract.decodeEventLog(
8670
'Shield',
8771
log[2],
8872
)
89-
Assert.isEqual(ironfishAddress as string, '0x' + senderKey.publicAddress)
9073

74+
Assert.isEqual(ironfishAddress as string, '0x' + senderKey.publicAddress)
9175
Assert.isEqual(tokenId as bigint, 2n)
9276
Assert.isEqual((caller as string).toUpperCase(), senderAddress.toString().toUpperCase())
9377
Assert.isEqual(amount as bigint, 100n)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
import { LegacyTransaction } from '@ethereumjs/tx'
5+
import { Account, Address } from '@ethereumjs/util'
6+
import { Asset, generateKey } from '@ironfish/rust-nodejs'
7+
import {
8+
Assert,
9+
ContractArtifact,
10+
EvmShield,
11+
GLOBAL_CONTRACT_ADDRESS,
12+
IronfishEvm,
13+
} from '@ironfish/sdk'
14+
import { ethers } from 'ethers'
15+
import { IronfishCommand } from '../../command'
16+
import { LocalFlags } from '../../flags'
17+
18+
export class TestEvmCommand extends IronfishCommand {
19+
static description = `Test adding EVM support to the Iron Fish network`
20+
21+
static flags = { ...LocalFlags }
22+
23+
async start(): Promise<void> {
24+
const node = await this.sdk.node()
25+
await node.openDB()
26+
27+
const evm = new IronfishEvm(node.chain.blockchainDb)
28+
await evm.open()
29+
30+
const senderKey = generateKey()
31+
32+
const senderPrivateKey = Uint8Array.from(Buffer.from(senderKey.spendingKey, 'hex'))
33+
const senderAddress = Address.fromPrivateKey(senderPrivateKey)
34+
35+
const senderAccount = new Account(BigInt(0), 10n)
36+
37+
await node.chain.blockchainDb.stateManager.checkpoint()
38+
await node.chain.blockchainDb.stateManager.putAccount(senderAddress, senderAccount)
39+
await node.chain.blockchainDb.stateManager.commit()
40+
41+
const contract = await node.chain.blockchainDb.stateManager.getAccount(
42+
GLOBAL_CONTRACT_ADDRESS,
43+
)
44+
45+
if (!contract) {
46+
this.error('Contract creation failed')
47+
}
48+
49+
this.log(`Contract created at: ${GLOBAL_CONTRACT_ADDRESS.toString()}`)
50+
51+
const globalContract = new ethers.Interface(ContractArtifact.abi)
52+
53+
const data = globalContract.encodeFunctionData('shield_iron', [
54+
Buffer.from(senderKey.publicAddress, 'hex'),
55+
])
56+
57+
const tx = new LegacyTransaction({
58+
nonce: 0n,
59+
gasLimit: 100_000n,
60+
value: 10n,
61+
to: GLOBAL_CONTRACT_ADDRESS,
62+
data: data,
63+
})
64+
65+
const result = await evm.runTx({ tx: tx.sign(senderPrivateKey) })
66+
67+
const logEvents = evm.decodeLogs(result.receipt.logs)
68+
69+
Assert.isEqual(logEvents.length, 1)
70+
const log = logEvents[0] as EvmShield
71+
72+
this.log('Contract Address')
73+
this.log(log.caller.toString())
74+
75+
const native = Asset.nativeId().toString('hex')
76+
Assert.isEqual(log.ironfishAddress.toString('hex'), senderKey.publicAddress)
77+
Assert.isEqual(log.assetId.toString('hex'), native)
78+
Assert.isEqual(
79+
log.caller.toString().toUpperCase(),
80+
GLOBAL_CONTRACT_ADDRESS.toString().toUpperCase(),
81+
)
82+
Assert.isEqual(log.amount, 10n)
83+
84+
await node.closeDB()
85+
}
86+
}

ironfish-contracts/artifacts/contracts/Ironfish.sol/Ironfish.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@
137137
"type": "function"
138138
}
139139
],
140-
"bytecode": "0x608060405234801561001057600080fd5b506105b9806100206000396000f3fe60806040526004361061003f5760003560e01c8063786b9bc4146100445780638bdad1011461006d578063a038e39514610089578063fc87b1af146100b2575b600080fd5b34801561005057600080fd5b5061006b60048036038101906100669190610273565b6100db565b005b610087600480360381019061008291906102e9565b61011a565b005b34801561009557600080fd5b506100b060048036038101906100ab9190610316565b61016f565b005b3480156100be57600080fd5b506100d960048036038101906100d491906103c7565b6101b1565b005b7fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc428133838360405161010e93929190610437565b60405180910390a15050565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe81600073c0ffee254729296a45a3885639ac7e10f9d549793460405161016494939291906104c2565b60405180910390a150565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe838333846040516101a49493929190610507565b60405180910390a1505050565b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156101f7573d6000803e3d6000fd5b507fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc42813060008360405161022c9392919061054c565b60405180910390a15050565b600080fd5b6000819050919050565b6102508161023d565b811461025b57600080fd5b50565b60008135905061026d81610247565b92915050565b6000806040838503121561028a57610289610238565b5b60006102988582860161025e565b92505060206102a98582860161025e565b9150509250929050565b6000819050919050565b6102c6816102b3565b81146102d157600080fd5b50565b6000813590506102e3816102bd565b92915050565b6000602082840312156102ff576102fe610238565b5b600061030d848285016102d4565b91505092915050565b60008060006060848603121561032f5761032e610238565b5b600061033d868287016102d4565b935050602061034e8682870161025e565b925050604061035f8682870161025e565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061039482610369565b9050919050565b6103a481610389565b81146103af57600080fd5b50565b6000813590506103c18161039b565b92915050565b600080604083850312156103de576103dd610238565b5b60006103ec858286016103b2565b92505060206103fd8582860161025e565b9150509250929050565b600061041282610369565b9050919050565b61042281610407565b82525050565b6104318161023d565b82525050565b600060608201905061044c6000830186610419565b6104596020830185610428565b6104666040830184610428565b949350505050565b610477816102b3565b82525050565b6000819050919050565b6000819050919050565b60006104ac6104a76104a28461047d565b610487565b61023d565b9050919050565b6104bc81610491565b82525050565b60006080820190506104d7600083018761046e565b6104e460208301866104b3565b6104f16040830185610419565b6104fe6060830184610428565b95945050505050565b600060808201905061051c600083018761046e565b6105296020830186610428565b6105366040830185610419565b6105436060830184610428565b95945050505050565b60006060820190506105616000830186610419565b61056e60208301856104b3565b61057b6040830184610428565b94935050505056fea26469706673582212207b89a915a53f48d354c68bf1f07d006d4456f8d12f4d454a6d37fb85431a1e6e64736f6c63430008180033",
141-
"deployedBytecode": "0x60806040526004361061003f5760003560e01c8063786b9bc4146100445780638bdad1011461006d578063a038e39514610089578063fc87b1af146100b2575b600080fd5b34801561005057600080fd5b5061006b60048036038101906100669190610273565b6100db565b005b610087600480360381019061008291906102e9565b61011a565b005b34801561009557600080fd5b506100b060048036038101906100ab9190610316565b61016f565b005b3480156100be57600080fd5b506100d960048036038101906100d491906103c7565b6101b1565b005b7fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc428133838360405161010e93929190610437565b60405180910390a15050565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe81600073c0ffee254729296a45a3885639ac7e10f9d549793460405161016494939291906104c2565b60405180910390a150565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe838333846040516101a49493929190610507565b60405180910390a1505050565b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156101f7573d6000803e3d6000fd5b507fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc42813060008360405161022c9392919061054c565b60405180910390a15050565b600080fd5b6000819050919050565b6102508161023d565b811461025b57600080fd5b50565b60008135905061026d81610247565b92915050565b6000806040838503121561028a57610289610238565b5b60006102988582860161025e565b92505060206102a98582860161025e565b9150509250929050565b6000819050919050565b6102c6816102b3565b81146102d157600080fd5b50565b6000813590506102e3816102bd565b92915050565b6000602082840312156102ff576102fe610238565b5b600061030d848285016102d4565b91505092915050565b60008060006060848603121561032f5761032e610238565b5b600061033d868287016102d4565b935050602061034e8682870161025e565b925050604061035f8682870161025e565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061039482610369565b9050919050565b6103a481610389565b81146103af57600080fd5b50565b6000813590506103c18161039b565b92915050565b600080604083850312156103de576103dd610238565b5b60006103ec858286016103b2565b92505060206103fd8582860161025e565b9150509250929050565b600061041282610369565b9050919050565b61042281610407565b82525050565b6104318161023d565b82525050565b600060608201905061044c6000830186610419565b6104596020830185610428565b6104666040830184610428565b949350505050565b610477816102b3565b82525050565b6000819050919050565b6000819050919050565b60006104ac6104a76104a28461047d565b610487565b61023d565b9050919050565b6104bc81610491565b82525050565b60006080820190506104d7600083018761046e565b6104e460208301866104b3565b6104f16040830185610419565b6104fe6060830184610428565b95945050505050565b600060808201905061051c600083018761046e565b6105296020830186610428565b6105366040830185610419565b6105436060830184610428565b95945050505050565b60006060820190506105616000830186610419565b61056e60208301856104b3565b61057b6040830184610428565b94935050505056fea26469706673582212207b89a915a53f48d354c68bf1f07d006d4456f8d12f4d454a6d37fb85431a1e6e64736f6c63430008180033",
140+
"bytecode": "0x608060405234801561001057600080fd5b506105a5806100206000396000f3fe60806040526004361061003f5760003560e01c8063786b9bc4146100445780638bdad1011461006d578063a038e39514610089578063fc87b1af146100b2575b600080fd5b34801561005057600080fd5b5061006b6004803603810190610066919061025f565b6100db565b005b610087600480360381019061008291906102d5565b61011a565b005b34801561009557600080fd5b506100b060048036038101906100ab9190610302565b61015b565b005b3480156100be57600080fd5b506100d960048036038101906100d491906103b3565b61019d565b005b7fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc428133838360405161010e93929190610423565b60405180910390a15050565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe816000303460405161015094939291906104ae565b60405180910390a150565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe8383338460405161019094939291906104f3565b60405180910390a1505050565b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156101e3573d6000803e3d6000fd5b507fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc42813060008360405161021893929190610538565b60405180910390a15050565b600080fd5b6000819050919050565b61023c81610229565b811461024757600080fd5b50565b60008135905061025981610233565b92915050565b6000806040838503121561027657610275610224565b5b60006102848582860161024a565b92505060206102958582860161024a565b9150509250929050565b6000819050919050565b6102b28161029f565b81146102bd57600080fd5b50565b6000813590506102cf816102a9565b92915050565b6000602082840312156102eb576102ea610224565b5b60006102f9848285016102c0565b91505092915050565b60008060006060848603121561031b5761031a610224565b5b6000610329868287016102c0565b935050602061033a8682870161024a565b925050604061034b8682870161024a565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061038082610355565b9050919050565b61039081610375565b811461039b57600080fd5b50565b6000813590506103ad81610387565b92915050565b600080604083850312156103ca576103c9610224565b5b60006103d88582860161039e565b92505060206103e98582860161024a565b9150509250929050565b60006103fe82610355565b9050919050565b61040e816103f3565b82525050565b61041d81610229565b82525050565b60006060820190506104386000830186610405565b6104456020830185610414565b6104526040830184610414565b949350505050565b6104638161029f565b82525050565b6000819050919050565b6000819050919050565b600061049861049361048e84610469565b610473565b610229565b9050919050565b6104a88161047d565b82525050565b60006080820190506104c3600083018761045a565b6104d0602083018661049f565b6104dd6040830185610405565b6104ea6060830184610414565b95945050505050565b6000608082019050610508600083018761045a565b6105156020830186610414565b6105226040830185610405565b61052f6060830184610414565b95945050505050565b600060608201905061054d6000830186610405565b61055a602083018561049f565b6105676040830184610414565b94935050505056fea26469706673582212204d1e2a4a4fa6f5d4c196f09969d8a7e49438b9004ac1df453a368705e90ccecb64736f6c63430008180033",
141+
"deployedBytecode": "0x60806040526004361061003f5760003560e01c8063786b9bc4146100445780638bdad1011461006d578063a038e39514610089578063fc87b1af146100b2575b600080fd5b34801561005057600080fd5b5061006b6004803603810190610066919061025f565b6100db565b005b610087600480360381019061008291906102d5565b61011a565b005b34801561009557600080fd5b506100b060048036038101906100ab9190610302565b61015b565b005b3480156100be57600080fd5b506100d960048036038101906100d491906103b3565b61019d565b005b7fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc428133838360405161010e93929190610423565b60405180910390a15050565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe816000303460405161015094939291906104ae565b60405180910390a150565b7f06e7e7af2833ef6acd0fa82a0c811fcae2a9743776b07004e67152c4e76e69fe8383338460405161019094939291906104f3565b60405180910390a1505050565b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156101e3573d6000803e3d6000fd5b507fac7fb4669ee6bcb4e65d1a3ed26d30037ba448f57ef727751be6c72f66fc42813060008360405161021893929190610538565b60405180910390a15050565b600080fd5b6000819050919050565b61023c81610229565b811461024757600080fd5b50565b60008135905061025981610233565b92915050565b6000806040838503121561027657610275610224565b5b60006102848582860161024a565b92505060206102958582860161024a565b9150509250929050565b6000819050919050565b6102b28161029f565b81146102bd57600080fd5b50565b6000813590506102cf816102a9565b92915050565b6000602082840312156102eb576102ea610224565b5b60006102f9848285016102c0565b91505092915050565b60008060006060848603121561031b5761031a610224565b5b6000610329868287016102c0565b935050602061033a8682870161024a565b925050604061034b8682870161024a565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061038082610355565b9050919050565b61039081610375565b811461039b57600080fd5b50565b6000813590506103ad81610387565b92915050565b600080604083850312156103ca576103c9610224565b5b60006103d88582860161039e565b92505060206103e98582860161024a565b9150509250929050565b60006103fe82610355565b9050919050565b61040e816103f3565b82525050565b61041d81610229565b82525050565b60006060820190506104386000830186610405565b6104456020830185610414565b6104526040830184610414565b949350505050565b6104638161029f565b82525050565b6000819050919050565b6000819050919050565b600061049861049361048e84610469565b610473565b610229565b9050919050565b6104a88161047d565b82525050565b60006080820190506104c3600083018761045a565b6104d0602083018661049f565b6104dd6040830185610405565b6104ea6060830184610414565b95945050505050565b6000608082019050610508600083018761045a565b6105156020830186610414565b6105226040830185610405565b61052f6060830184610414565b95945050505050565b600060608201905061054d6000830186610405565b61055a602083018561049f565b6105676040830184610414565b94935050505056fea26469706673582212204d1e2a4a4fa6f5d4c196f09969d8a7e49438b9004ac1df453a368705e90ccecb64736f6c63430008180033",
142142
"linkReferences": {},
143143
"deployedLinkReferences": {}
144144
}

ironfish-contracts/contracts/Ironfish.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ contract Ironfish {
2727
}
2828

2929
function shield_iron(bytes32 ironfishAddress) public payable {
30-
// Replace this hardcoded address with address(this) once we find a way to hardcode the
31-
// global address of the contract
32-
emit Shield(ironfishAddress, 0, 0xc0ffee254729296a45a3885639AC7E10F9d54979, msg.value);
30+
emit Shield(ironfishAddress, 0, address(this), msg.value);
3331
}
3432

3533
function unshield(uint tokenId, uint amount) public {

ironfish/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"lint:fix": "tsc -b && tsc -b tsconfig.test.json && eslint --ext .ts,.tsx,.js,.jsx src/ --fix",
6868
"start": "tsc -b -w",
6969
"test": "tsc -b && tsc -b tsconfig.test.json && cross-env NODE_OPTIONS=--experimental-vm-modules RUST_BACKTRACE=1 jest --testTimeout=${JEST_TIMEOUT:-5000}",
70+
"testd": "tsc -b && tsc -b tsconfig.test.json && cross-env RUST_BACKTRACE=1 node --experimental-vm-modules ../node_modules/jest/bin/jest.js --testTimeout=${JEST_TIMEOUT:-5000}",
7071
"test:slow": "tsc -b && tsc -b tsconfig.test.json && cross-env NODE_OPTIONS=--experimental-vm-modules TEST_INIT_RUST=true RUST_BACKTRACE=1 jest --testMatch \"**/*.test.slow.ts\" --testPathIgnorePatterns --testTimeout=${JEST_TIMEOUT:-60000}",
7172
"test:perf": "tsc -b && tsc -b tsconfig.test.json && cross-env NODE_OPTIONS=--experimental-vm-modules TEST_INIT_RUST=true jest --testMatch \"**/*.test.perf.ts\" --testPathIgnorePatterns --testTimeout=${JEST_TIMEOUT:-600000} --runInBand",
7273
"test:perf:report": "tsc -b && tsc -b tsconfig.test.json && cross-env NODE_OPTIONS=--experimental-vm-modules TEST_INIT_RUST=true GENERATE_TEST_REPORT=true jest --config jest.config.js --testMatch \"**/*.test.perf.ts\" --testPathIgnorePatterns --testTimeout=${JEST_TIMEOUT:-600000} --ci",

ironfish/src/blockchain/blockchain.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ export class Blockchain {
307307
if (this._head) {
308308
this.updateSynced()
309309
}
310+
311+
await this.evm.load()
310312
}
311313

312314
async close(): Promise<void> {

0 commit comments

Comments
 (0)