Skip to content

Commit

Permalink
Merged siliev/native-transfer-emit-event
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Dec 4, 2024
2 parents ac744ae + dd2af22 commit 8a1eb6d
Show file tree
Hide file tree
Showing 19 changed files with 454 additions and 25 deletions.

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

170 changes: 168 additions & 2 deletions contracts/generated/MerkleTreeMessageBus/MerkleTreeMessageBus.go

Large diffs are not rendered by default.

170 changes: 168 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/PublicCallbacks/PublicCallbacks.go

Large diffs are not rendered by default.

46 changes: 44 additions & 2 deletions contracts/generated/PublicCallbacksTest/PublicCallbacksTest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.

5 changes: 5 additions & 0 deletions contracts/src/messaging/IMessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ interface IMessageBus {
uint64 sequence
);

event NativeDeposit(
address indexed receiver,
uint256 amount
);

// This method is called from contracts to publish messages to the other linked message bus.
// nonce - This is provided and serves as deduplication nonce. It can also be used to group a batch of messages together.
// topic - This is the topic for which the payload is published.
Expand Down
7 changes: 7 additions & 0 deletions contracts/src/messaging/MessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ contract MessageBus is IMessageBus, Initializable, OwnableUpgradeable {
);
}

function notifyDeposit(
address receiver,
uint256 amount
) external ownerOrSelf {
emit NativeDeposit(receiver, amount);
}

function retrieveAllFunds(
address receiver
) external onlyOwner {
Expand Down
16 changes: 13 additions & 3 deletions contracts/src/system/PublicCallbacks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ contract PublicCallbacks is Initializable {

function popCurrentCallback() internal {
delete callbacks[lastUnusedCallbackId];
}

function moveToNextCallback() internal {
lastUnusedCallbackId++;
}

Expand Down Expand Up @@ -106,13 +109,13 @@ contract PublicCallbacks is Initializable {
if (prepaidGas > gasUsed) {
gasRefundValue = (prepaidGas - gasUsed) * baseFee;
}

uint256 paymentToCoinbase = callback.value - gasRefundValue;
address target = callback.target;

if (success) {
popCurrentCallback();
}
moveToNextCallback();

internalRefund(gasRefundValue, target, callbackId);
payForCallback(paymentToCoinbase);
Expand All @@ -124,11 +127,18 @@ contract PublicCallbacks is Initializable {
// slight buffer.
(bool success, ) = to.call{value: gasRefund, gas: 35000}(abi.encodeWithSignature("handleRefund(uint256)", callbackId));
if (!success) {
block.coinbase.transfer(gasRefund); // if they dont accept the refund, we gift it to coinbase.
// if they dont accept the refund, we gift it to coinbase.
payForCallback(gasRefund);
}
}

function payForCallback(uint256 gasPayment) internal {
block.coinbase.transfer(gasPayment);
if (gasPayment == 0) {
return;
}
// We don't care about success, should always happen.
// If not, contract is upgradable and we can recover.
// solc-ignore-next-line unused-call-retval
block.coinbase.call{value: gasPayment}("");
}
}
23 changes: 19 additions & 4 deletions contracts/src/testing/PublicCallBacksTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ contract PublicCallbacksTest {
constructor(address _callbacks) payable {
callbacks = IPublicCallbacks(_callbacks);
lastCallSuccess = false;
allCallbacksRan = false;
testRegisterCallback();
}

bool lastCallSuccess = false;
bool allCallbacksRan = false;

// This function will be called back by the system
function handleCallback(uint256 expectedGas) external {
Expand All @@ -22,21 +24,34 @@ contract PublicCallbacksTest {
}
// Handle the callback here
// For testing we'll just allow it to succeed

}

function handleCallbackFail() external {
lastCallSuccess = true;
require(false, "This is a test failure");
}

function handleAllCallbacksRan() external {
allCallbacksRan = true;
}

// Test function that registers a callback
function testRegisterCallback() internal {
// Encode the callback data - calling handleCallback()
// Calculate expected gas based on value sent
uint256 expectedGas = msg.value / block.basefee;
uint256 expectedGas = (msg.value/3) / block.basefee;
bytes memory callbackData = abi.encodeWithSelector(this.handleCallback.selector, expectedGas);

bytes memory callbackDataFail = abi.encodeWithSelector(this.handleCallbackFail.selector);
bytes memory callbackDataAllCallbacksRan = abi.encodeWithSelector(this.handleAllCallbacksRan.selector);

// Register the callback, forwarding any value sent to this call
callbacks.register{value: msg.value}(callbackData);
callbacks.register{value: msg.value/3}(callbackData);
callbacks.register{value: msg.value/3}(callbackDataFail);
callbacks.register{value: msg.value/3}(callbackDataAllCallbacksRan);
}

function isLastCallSuccess() external view returns (bool) {
return lastCallSuccess;
return allCallbacksRan && lastCallSuccess;
}
}
2 changes: 1 addition & 1 deletion go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (executor *batchExecutor) ComputeBatch(ctx context.Context, context *BatchE
messages, transfers = executor.crossChainProcessors.Local.RetrieveInboundMessages(ctx, parentBlock, block, stateDB)
}

crossChainTransactions := executor.crossChainProcessors.Local.CreateSyntheticTransactions(ctx, messages, stateDB)
crossChainTransactions := executor.crossChainProcessors.Local.CreateSyntheticTransactions(ctx, messages, transfers, stateDB)
executor.crossChainProcessors.Local.ExecuteValueTransfers(ctx, transfers, stateDB)

transactionsToProcess, freeTransactions := executor.filterTransactionsWithSufficientFunds(ctx, stateDB, context)
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/crosschain/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Manager interface {

ExtractOutboundTransfers(ctx context.Context, receipts common.L2Receipts) (common.ValueTransferEvents, error)

CreateSyntheticTransactions(ctx context.Context, messages common.CrossChainMessages, rollupState *state.StateDB) common.L2Transactions
CreateSyntheticTransactions(ctx context.Context, messages common.CrossChainMessages, transfers common.ValueTransferEvents, rollupState *state.StateDB) common.L2Transactions

ExecuteValueTransfers(ctx context.Context, transfers common.ValueTransferEvents, rollupState *state.StateDB)

Expand Down
20 changes: 19 additions & 1 deletion go/enclave/crosschain/message_bus_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (m *MessageBusManager) ExecuteValueTransfers(ctx context.Context, transfers
}

// CreateSyntheticTransactions - generates transactions that the enclave should execute internally for the messages.
func (m *MessageBusManager) CreateSyntheticTransactions(ctx context.Context, messages common.CrossChainMessages, rollupState *state.StateDB) common.L2Transactions {
func (m *MessageBusManager) CreateSyntheticTransactions(ctx context.Context, messages common.CrossChainMessages, transfers common.ValueTransferEvents, rollupState *state.StateDB) common.L2Transactions {
if len(messages) == 0 {
return make(common.L2Transactions, 0)
}
Expand Down Expand Up @@ -251,5 +251,23 @@ func (m *MessageBusManager) CreateSyntheticTransactions(ctx context.Context, mes
signedTransactions = append(signedTransactions, stx)
}

startingNonce += uint64(len(messages))

for idx, transfer := range transfers {
data, err := MessageBusABI.Pack("notifyDeposit", transfer.Receiver, transfer.Amount)
if err != nil {
m.logger.Crit("Failed packing notifyDeposit message!")
return signedTransactions
}

tx := &types.LegacyTx{
Nonce: startingNonce + uint64(idx),
Value: transfer.Amount,
Data: data,
To: m.messageBusAddress,
}
signedTransactions = append(signedTransactions, types.NewTx(tx))
}

return signedTransactions
}
2 changes: 1 addition & 1 deletion tools/walletextension/rpcapi/net_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewNetAPI(we *services.Services) *NetAPI {
}

func (api *NetAPI) Version(ctx context.Context) (*string, error) {
return UnauthenticatedTenRPCCall[string](ctx, api.we, &cache.Cfg{Type: cache.LongLiving}, "net_version")
return UnauthenticatedTenRPCCall[string](ctx, api.we, &cache.Cfg{Type: cache.LongLiving}, "ten_version")
}

type ConfigResponseJson struct {
Expand Down

0 comments on commit 8a1eb6d

Please sign in to comment.