Skip to content

Commit

Permalink
Set message bus fee in a deployment script for testnet. (#2190)
Browse files Browse the repository at this point in the history
* Setting the fee. Extended the contract addresses endpoint.

* adding script.

* Reenabled 001 and fixed 002

* Exposed fee getters.

* Testnet starting up.

* Slight refactor.

* Attempt 2.

* Change the fees.

* Set upgrader from proper place.

* Fix for linter.

* Fix for linter error.

* made it a flat fee.

* Addressed PR review.

---------

Co-authored-by: StefanIliev545 <[email protected]>
  • Loading branch information
StefanIliev545 and StefanIliev545 authored Dec 11, 2024
1 parent fec1c49 commit ff717b7
Show file tree
Hide file tree
Showing 27 changed files with 271 additions and 95 deletions.
1 change: 1 addition & 0 deletions .github/workflows/manual-deploy-testnet-l2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ jobs:
-l1_chain_id=${{ vars.L1_CHAIN_ID }} \
-l1_beacon_url=${{ vars.L1_BEACON_URL }} \
-l1_blob_archive_url=${{ vars.L1_BLOB_ARCHIVE_URL }} \
-system_contracts_upgrader=${{ vars.NETWORK_SEQUENCER_SYSTEMCONTRACTSUPGRADER }} \
-postgres_db_host=postgres://tenuser:${{ secrets.TEN_POSTGRES_USER_PWD }}@postgres-ten-${{ github.event.inputs.testnet_type }}.postgres.database.azure.com:5432/ \
start'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
networkConfig["TransactionPostProcessorAddress"],
signer
);

// TODO: add callback with the security epic when we add the EOA config and all the rules for access
// to system contracts
/*
const receipt = await transactionPostProcessor.addOnBlockEndCallback(zenTestnet.address);
if (receipt.status !== 1) {

const tx = await transactionPostProcessor.addOnBlockEndCallback(zenTestnet.address);
const receipt = await tx.wait();
if (receipt.status != 1) {
throw new Error("Failed to register Zen token as a system callback");
}
console.log(`Callback added at ${receipt.transactionHash}`); */
console.log(`Callback added at ${receipt.hash}`);
}
export default func;
func.tags = ['ZenBase', 'ZenBase_deploy'];
Expand Down
39 changes: 39 additions & 0 deletions contracts/deployment_scripts/testnet/layer2/003_set_fee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import {DeployFunction} from 'hardhat-deploy/types';
import { Receipt } from 'hardhat-deploy/dist/types';
import { network } from 'hardhat';

/*
This script sets the fee for the message bus to prevent spam.
*/
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const l2Network = hre;

const l2Accounts = await l2Network.getNamedAccounts();

const networkConfig = await l2Network.network.provider.request({
method: "net_config",
});

const signer = await l2Network.ethers.getSigner(l2Accounts.deployer);
const fees = await l2Network.ethers.getContractAt(
'Fees',
networkConfig["PublicSystemContracts"]["Fees"],
signer
);

const owner = await fees.owner();
console.log(`Owner = ${owner}`);
console.log(`Signer = ${l2Accounts.deployer}`);

const tx = await fees.setMessageFee(32*10000);
const receipt =await tx.wait();

if (receipt.status != 1) {
throw new Error("Failed to set message fee");
}
console.log(`Fee set at ${receipt.hash}`);
}
export default func;
func.tags = ['SetFees', 'SetFees_deploy'];
func.dependencies = ['ZenBase'];

Large diffs are not rendered by default.

66 changes: 64 additions & 2 deletions contracts/generated/EthereumBridge/EthereumBridge.go

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions contracts/generated/Fees/Fees.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions contracts/generated/MerkleTreeMessageBus/MerkleTreeMessageBus.go

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions contracts/generated/MessageBus/MessageBus.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/generated/ObsERC20/ObsERC20.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/generated/ObscuroBridge/ObscuroBridge.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/generated/SystemDeployer/SystemDeployer.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/generated/WrappedERC20/WrappedERC20.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions contracts/src/bridge/L2/EthereumBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ contract EthereumBridge is
return address(wrappedTokens[wrappedToken]) != address(0x0);
}

function erc20Fee() public view returns (uint256) {
return _messageBus().getPublishFee();
}

function valueTransferFee() public view returns (uint256) {
return _messageBus().getPublishFee();
}

function sendNative(address receiver) external payable {
require(msg.value > 0, "Nothing sent.");
require(msg.value >= _messageBus().getPublishFee(), "Insufficient funds to publish value transfer");
_messageBus().sendValueToL2{value: msg.value}(receiver, msg.value);
}

Expand All @@ -77,6 +86,8 @@ contract EthereumBridge is
amount,
receiver
);

require(msg.value >= _messageBus().getPublishFee(), "Insufficient funds to publish message");
queueMessage(remoteBridgeAddress, data, uint32(Topics.TRANSFER), 0, 0, msg.value);
}

Expand Down
3 changes: 3 additions & 0 deletions contracts/src/messaging/IMessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ interface IMessageBus {

// This is a testnet function which allows the bridge owner to retrieve all funds from the message bus.
function retrieveAllFunds(address receiver) external;

// the fee needed to be paid in msg.value to publish the value transfer
function getPublishFee() external view returns (uint256);
}
32 changes: 11 additions & 21 deletions contracts/src/messaging/MessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ contract MessageBus is IMessageBus, Initializable, OwnableUpgradeable {
) external payable {
require(msg.value > 0 && msg.value == amount, "Attempting to send value without providing Ether");

uint256 amountToTransfer = msg.value;
uint256 amountToBridge = msg.value;
if (address(fees) != address(0)) {
uint256 feesToTransfer = getValueTransferFee();
require(msg.value >= feesToTransfer, "Insufficient funds to send value");
amountToTransfer = msg.value - feesToTransfer;
(bool ok, ) = address(fees).call{value: feesToTransfer}("");
uint256 fee = getPublishFee();
require(msg.value >= fee, "Insufficient funds to send value");
amountToBridge = msg.value - fee;
(bool ok, ) = address(fees).call{value: fee}("");
require(ok, "Failed to send fees to fees contract");
}

uint64 sequence = incrementSequence(msg.sender);
emit ValueTransfer(msg.sender, receiver, amountToTransfer, sequence);
emit ValueTransfer(msg.sender, receiver, amountToBridge, sequence);
}

function receiveValueFromL2(
Expand All @@ -75,19 +75,8 @@ contract MessageBus is IMessageBus, Initializable, OwnableUpgradeable {
require(ok, "failed sending value");
}

function getFixedDataLength() internal pure returns (uint256) {
return 4 + // nonce (uint32)
4 + // topic (uint32)
1 + // consistencyLevel (uint8)
8; // sequence (uint64)
}

function getValueTransferFee() internal view returns (uint256) {
return fees.messageFee(32); //just a hash
}

function getMessageFee(uint256 payloadLength) internal view returns (uint256) {
return fees.messageFee(payloadLength + getFixedDataLength());
function getPublishFee() public view returns (uint256) {
return fees.messageFee();
}

// This method is called from contracts to publish messages to the other linked message bus.
Expand All @@ -105,8 +94,9 @@ contract MessageBus is IMessageBus, Initializable, OwnableUpgradeable {
uint8 consistencyLevel
) external payable override returns (uint64 sequence) {
if (address(fees) != address(0)) { // No fee required for L1 to L2 messages.
require(msg.value >= getMessageFee(payload.length), "Insufficient funds to publish message");
(bool ok, ) = address(fees).call{value: msg.value}("");
uint256 fee = getPublishFee();
require(msg.value >= fee, "Insufficient funds to publish message");
(bool ok, ) = address(fees).call{value: fee}("");
require(ok, "Failed to send fees to fees contract");
}

Expand Down
18 changes: 9 additions & 9 deletions contracts/src/system/Fees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

interface IFees {
function messageFee(uint256 messageSize) external view returns (uint256);
function messageFee() external view returns (uint256);
}

// Contract that will contain fees for contracts that need to apply them
contract Fees is Initializable, OwnableUpgradeable {
contract Fees is Initializable, OwnableUpgradeable, IFees {

uint256 private _messageFeePerByte;
uint256 private _messageFee;

// Constructor disables initializer;
// Only owner functions will not be callable on implementation
Expand All @@ -20,20 +20,20 @@ contract Fees is Initializable, OwnableUpgradeable {
}

// initialization function to be used by the proxy.
function initialize(uint256 initialMessageFeePerByte, address eoaOwner) public initializer {
function initialize(uint256 flatFee, address eoaOwner) public initializer {
__Ownable_init(eoaOwner);
_messageFeePerByte = initialMessageFeePerByte;
_messageFee = flatFee;
}

// Helper function to calculate the fee for a message
function messageFee(uint256 messageSize) external view returns (uint256) {
return _messageFeePerByte * messageSize;
function messageFee() external view returns (uint256) {
return _messageFee;
}

// The EOA owner can set the message fee to ensure sequencer is not publishing
// at a loss
function setMessageFee(uint256 newMessageFeePerByte) external onlyOwner{
_messageFeePerByte = newMessageFeePerByte;
function setMessageFee(uint256 newFeeForMessage) external onlyOwner{
_messageFee = newFeeForMessage;
}

// The EOA owner can collect the fees
Expand Down
2 changes: 1 addition & 1 deletion go/config/defaults/0-base-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ network:
bridge: 0x0 # L1 address of the Ten bridge contract
sequencer:
p2pAddress: 0x0 # address of the sequencer's p2p server
systemContractsUpgrader: 0x1 # L2 address of the EOA allowed to upgrade the system contract proxies
systemContractsUpgrader: 0x2 # L2 address of the EOA allowed to upgrade the system contract proxies
crossChain:
interval: 6s

Expand Down
5 changes: 3 additions & 2 deletions go/config/defaults/sim/1-env-sim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ enclave:
log:
level: 1
path: ""
sequencer:
systemContractsUpgrader: 0x5
network:
sequencer:
systemContractsUpgrader: 0x5
3 changes: 1 addition & 2 deletions go/config/defaults/testnet-launcher/1-testnet-launcher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ network:
minGasPrice: +1
sequencer:
p2pAddress: sequencer-host:15000
systemContractsUpgrader: 0xA58C60cc047592DE97BF1E8d2f225Fc5D959De77
host:
debug:
enableDebugNamespace: true
Expand All @@ -20,5 +21,3 @@ enclave:
enableDebugNamespace: true
log:
level: 4
sequencer:
systemContractsUpgrader: 0xDEe530E22045939e6f6a0A593F829e35A140D3F1
11 changes: 5 additions & 6 deletions go/enclave/enclave_rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ func (e *enclaveRPCService) EnclavePublicConfig(context.Context) (*common.Enclav
if analyzerAddress == nil {
analyzerAddress = &gethcommon.Address{}
}
publicCallbacksAddress := e.scb.PublicCallbackHandler()
if publicCallbacksAddress == nil {
publicCallbacksAddress = &gethcommon.Address{}

publicContractsMap := make(map[string]gethcommon.Address)
for name, address := range e.scb.PublicSystemContracts() {
publicContractsMap[name] = *address
}

return &common.EnclavePublicConfig{
L2MessageBusAddress: address,
TransactionPostProcessorAddress: *analyzerAddress,
PublicSystemContracts: map[string]gethcommon.Address{
"PublicCallbacks": *publicCallbacksAddress,
},
PublicSystemContracts: publicContractsMap,
}, nil
}
1 change: 1 addition & 0 deletions go/enclave/main/enclave.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
{ "fromHost": true, "name": "NETWORK_ROLLUP_MAXINTERVAL" },
{ "fromHost": true, "name": "NETWORK_ROLLUP_MAXSIZE" },
{ "fromHost": true, "name": "NETWORK_SEQUENCER_P2PADDRESS" },
{ "fromHost": true, "name": "NETWORK_SEQUENCER_SYSTEMCONTRACTSUPGRADER" },
{ "fromHost": true, "name": "NODE_HOSTADDRESS" },
{ "fromHost": true, "name": "NODE_ID" },
{ "fromHost": true, "name": "NODE_ISGENESIS" },
Expand Down
6 changes: 5 additions & 1 deletion go/enclave/system/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type SystemContractCallbacks interface {
PublicCallbackHandler() *gethcommon.Address
TransactionPostProcessor() *gethcommon.Address
SystemContractsUpgrader() *gethcommon.Address

PublicSystemContracts() map[string]*gethcommon.Address
// Initialization
Initialize(batch *core.Batch, receipts types.Receipt, msgBusManager SystemContractsInitializable) error
Load() error
Expand Down Expand Up @@ -81,6 +81,10 @@ func (s *systemContractCallbacks) PublicCallbackHandler() *gethcommon.Address {
return s.systemAddresses["PublicCallbacks"]
}

func (s *systemContractCallbacks) PublicSystemContracts() map[string]*gethcommon.Address {
return s.systemAddresses
}

func (s *systemContractCallbacks) Load() error {
s.logger.Info("Load: Initializing system contracts")

Expand Down
5 changes: 4 additions & 1 deletion go/node/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type NodeConfigCLI struct {
privateKey string
hostID string
sequencerP2PAddr string
sequencerUpgraderAddr string
managementContractAddr string
messageBusContractAddr string
l1Start string
Expand Down Expand Up @@ -92,7 +93,7 @@ func ParseConfigCLI() *NodeConfigCLI {
postgresDBHost := flag.String(postgresDBHostFlag, "dd", flagUsageMap[postgresDBHostFlag])
l1BeaconUrl := flag.String(l1BeaconUrlFlag, "eth2network:126000", flagUsageMap[l1BeaconUrlFlag])
l1BlobArchiveUrl := flag.String(l1BlobArchiveUrlFlag, "", flagUsageMap[l1BlobArchiveUrlFlag])

systemContractsUpgrader := flag.String(systemContractsUpgraderFlag, "", flagUsageMap[systemContractsUpgraderFlag])
flag.Parse()
cfg.nodeName = *nodeName
cfg.nodeType = *nodeType
Expand Down Expand Up @@ -126,6 +127,7 @@ func ParseConfigCLI() *NodeConfigCLI {
cfg.postgresDBHost = *postgresDBHost
cfg.l1BeaconUrl = *l1BeaconUrl
cfg.l1BlobArchiveUrl = *l1BlobArchiveUrl
cfg.sequencerUpgraderAddr = *systemContractsUpgrader

cfg.nodeAction = flag.Arg(0)
if !validateNodeAction(cfg.nodeAction) {
Expand Down Expand Up @@ -185,6 +187,7 @@ func NodeCLIConfigToTenConfig(cliCfg *NodeConfigCLI) *config.TenConfig {
os.Exit(1)
}
tenCfg.Network.Sequencer.P2PAddress = cliCfg.sequencerP2PAddr
tenCfg.Network.Sequencer.SystemContractsUpgrader = gethcommon.HexToAddress(cliCfg.sequencerUpgraderAddr)

tenCfg.Node.ID = gethcommon.HexToAddress(cliCfg.hostID)
tenCfg.Node.Name = cliCfg.nodeName
Expand Down
2 changes: 2 additions & 0 deletions go/node/cmd/cli_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
postgresDBHostFlag = "postgres_db_host"
l1BeaconUrlFlag = "l1_beacon_url"
l1BlobArchiveUrlFlag = "l1_blob_archive_url"
systemContractsUpgraderFlag = "system_contracts_upgrader"
)

// Returns a map of the flag usages.
Expand Down Expand Up @@ -72,5 +73,6 @@ func getFlagUsageMap() map[string]string {
postgresDBHostFlag: "Host connection details for Postgres DB",
l1BeaconUrlFlag: "Url for the beacon chain API",
l1BlobArchiveUrlFlag: "Url for the blob archive endpoint",
systemContractsUpgraderFlag: "Address of the system contracts upgrader",
}
}
2 changes: 1 addition & 1 deletion integration/simulation/devnetwork/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (n *InMemNodeOperator) createEnclaveContainer(idx int) *enclavecontainer.En
GasLocalExecutionCapFlag: defaultCfg.GasLocalExecutionCapFlag,
GasPaymentAddress: defaultCfg.GasPaymentAddress,
RPCTimeout: 5 * time.Second,
SystemContractOwner: gethcommon.HexToAddress("0xDEe530E22045939e6f6a0A593F829e35A140D3F1"),
SystemContractOwner: gethcommon.HexToAddress("0xA58C60cc047592DE97BF1E8d2f225Fc5D959De77"),
}
return enclavecontainer.NewEnclaveContainerWithLogger(enclaveConfig, enclaveLogger)
}
Expand Down
1 change: 1 addition & 0 deletions tools/walletextension/rpcapi/net_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ConfigResponseJson struct {
MessageBusAddress string
L2MessageBusAddress string
TransactionPostProcessorAddress string
PublicSystemContracts map[string]string
}

func (api *NetAPI) Config(ctx context.Context) (*ConfigResponseJson, error) {
Expand Down

0 comments on commit ff717b7

Please sign in to comment.