Skip to content

Commit

Permalink
Merge origin/main.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Dec 10, 2024
2 parents 68e4eea + e5fe097 commit 2ea5fd8
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 57 deletions.
2 changes: 1 addition & 1 deletion contracts/generated/SystemDeployer/SystemDeployer.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/src/system/TransactionPostProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract TransactionPostProcessor is Initializable, AccessControl{
revert("No transactions to convert");
}

emit TransactionsConverted(transactions.length);
// emit TransactionsConverted(transactions.length);

for (uint256 i = 0; i < onBlockEndListeners.length; ++i) {
OnBlockEndCallback callback = onBlockEndListeners[i];
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/zen/ZenBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract ZenBase is OnBlockEndCallback, ERC20, Ownable {
for (uint256 i=0; i<transactions.length; i++) {
// Process transactions
_mint(transactions[i].from, 1);
emit TransactionProcessed(transactions[i].from, 1);
// emit TransactionProcessed(transactions[i].from, 1);
}
}
}
2 changes: 1 addition & 1 deletion contracts/src/zen/ZenTestnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contract ZenTestnet is Initializable, OnBlockEndCallback, ERC20Upgradeable, Owna
for (uint256 i=0; i<transactions.length; i++) {
// Process transactions
_mint(transactions[i].from, 1);
emit TransactionProcessed(transactions[i].from, 1);
// emit TransactionProcessed(transactions[i].from, 1);
}
}
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/ethereum/go-ethereum v1.14.6 h1:ZTxnErSopkDyxdvB8zW/KcK+/AVrdil/TzoWX
github.com/ethereum/go-ethereum v1.14.6/go.mod h1:hglUZo/5pVIYXNyYjWzsAUDpT/zI+WbWo/Nih7ot+G0=
github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 h1:KrE8I4reeVvf7C1tm8elRjj4BdscTYzz/WAbYyf/JI4=
github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0/go.mod h1:D9AJLVXSyZQXJQVk8oh1EwjISE+sJTn2duYIZC0dy3w=
github.com/fjl/memsize v0.0.2 h1:27txuSD9or+NZlnOWdKUxeBzTAUkWCVh+4Gf2dWFOzA=
github.com/fjl/memsize v0.0.2/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down
22 changes: 22 additions & 0 deletions go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
gethcommon "github.com/ethereum/go-ethereum/common"

smt "github.com/FantasyJony/openzeppelin-merkle-tree-go/standard_merkle_tree"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -142,6 +143,11 @@ func (executor *batchExecutor) ComputeBatch(ctx context.Context, ec *BatchExecut
return nil, ErrNoTransactionsToProcess
}

// Step 5: burn native value on the message bus according to what has been bridged out to the L1.
if err := executor.postProcessState(ec); err != nil {
return nil, fmt.Errorf("failed to post process state. Cause: %w", err)
}

return executor.execResult(ec)
}

Expand Down Expand Up @@ -335,6 +341,21 @@ func (executor *batchExecutor) execRegisteredCallbacks(ec *BatchExecutionContext
return nil
}

// postProcessState - Function for applying post processing, which currently is removing the value from the balance of the message bus contract.
func (executor *batchExecutor) postProcessState(ec *BatchExecutionContext) error {
receipts := ec.batchTxResults.Receipts()
valueTransferMessages, err := executor.crossChainProcessors.Local.ExtractOutboundTransfers(ec.ctx, receipts)
if err != nil {
return fmt.Errorf("could not extract outbound transfers. Cause: %w", err)
}

for _, msg := range valueTransferMessages {
ec.stateDB.SubBalance(*executor.crossChainProcessors.Local.GetBusAddress(), uint256.MustFromBig(msg.Amount), tracing.BalanceChangeUnspecified)
}

return nil
}

func (executor *batchExecutor) execOnBlockEndTx(ec *BatchExecutionContext) error {
onBlockTx, err := executor.systemContracts.CreateOnBatchEndTransaction(ec.ctx, ec.stateDB, ec.batchTxResults)
if err != nil && !errors.Is(err, system.ErrNoTransactions) {
Expand Down Expand Up @@ -624,6 +645,7 @@ func (executor *batchExecutor) executeTxs(ec *BatchExecutionContext, offset int,
panic("Should not happen. Tx receipts and tx results do not match")
}
txResult.Receipt = txReceipts[i]
txResult.Receipt.TransactionIndex += uint(offset)
}

sort.Sort(sortByTxIndex(txResults))
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func LogMethodDuration(logger gethlog.Logger, stopWatch *measure.Stopwatch, msg
f(fmt.Sprintf("LogMethodDuration::%s", msg), newArgs...)
}

// GetTxSigner returns the address that signed a transaction
// GetExternalTxSigner returns the address that signed a transaction
func GetExternalTxSigner(tx *types.Transaction) (gethcommon.Address, error) {
from, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
Expand Down
44 changes: 23 additions & 21 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,33 @@ func ExistsBatchAtHeight(ctx context.Context, dbTx *sql.Tx, height *big.Int) (bo

// WriteTransactions - persists the batch and the transactions
func WriteTransactions(ctx context.Context, dbtx *sql.Tx, transactions []*core.TxWithSender, height uint64, isSynthetic bool, senderIds []uint64, toContractIds []*uint64, fromIdx int) error {
if len(transactions) == 0 {
return nil
}
// creates a batch insert statement for all entries
if len(transactions) > 0 {
insert := "insert into tx (hash, content, to_address, type, sender_address, idx, batch_height, is_synthetic) values " + repeat("(?,?,?,?,?,?,?,?)", ",", len(transactions))

args := make([]any, 0)
for i, transaction := range transactions {
txBytes, err := rlp.EncodeToBytes(transaction.Tx)
if err != nil {
return fmt.Errorf("failed to encode block receipts. Cause: %w", err)
}

args = append(args, transaction.Tx.Hash()) // tx_hash
args = append(args, txBytes) // content
args = append(args, toContractIds[i]) // To
args = append(args, transaction.Tx.Type()) // Type
args = append(args, senderIds[i]) // sender_address
args = append(args, fromIdx+i) // idx
args = append(args, height) // the batch height which contained it
args = append(args, isSynthetic) // is_synthetic if the transaction is a synthetic (internally derived transaction)
}
_, err := dbtx.ExecContext(ctx, insert, args...)
insert := "insert into tx (hash, content, to_address, type, sender_address, idx, batch_height, is_synthetic) values " + repeat("(?,?,?,?,?,?,?,?)", ",", len(transactions))

args := make([]any, 0)
for i, transaction := range transactions {
txBytes, err := rlp.EncodeToBytes(transaction.Tx)
if err != nil {
return err
return fmt.Errorf("failed to encode block receipts. Cause: %w", err)
}

args = append(args, transaction.Tx.Hash().Bytes()) // tx_hash
args = append(args, txBytes) // content
args = append(args, toContractIds[i]) // To
args = append(args, transaction.Tx.Type()) // Type
args = append(args, senderIds[i]) // sender_address
args = append(args, fromIdx+i) // idx
args = append(args, height) // the batch height which contained it
args = append(args, isSynthetic) // is_synthetic if the transaction is a synthetic (internally derived transaction)
}
_, err := dbtx.ExecContext(ctx, insert, args...)
if err != nil {
return err
}

return nil
}

Expand Down
14 changes: 9 additions & 5 deletions go/enclave/storage/enclavedb/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
" left join externally_owned_account eoa1 on t1.rel_address=eoa1.id " +
"left join event_topic t2 on e.topic2=t2.id and et.id=t2.event_type " +
" left join externally_owned_account eoa2 on t2.rel_address=eoa2.id " +
"left join event_topic t3 on e.topic3=t3.id and et.id=t1.event_type " +
"left join event_topic t3 on e.topic3=t3.id and et.id=t3.event_type " +
" left join externally_owned_account eoa3 on t3.rel_address=eoa3.id " +
"where b.is_canonical=true "
)
Expand Down Expand Up @@ -319,7 +319,7 @@ func loadReceiptsAndEventLogs(ctx context.Context, db *sql.DB, requestingAccount

if requestingAccount != nil {
// Add log visibility rules
logsVisibQuery, logsVisibParams := logsVisibilityQuery(requestingAccount)
logsVisibQuery, logsVisibParams := logsVisibilityQuery(requestingAccount, withReceipts)
query += logsVisibQuery
queryParams = append(queryParams, logsVisibParams...)

Expand Down Expand Up @@ -461,15 +461,19 @@ func receiptsVisibilityQuery(requestingAccount *gethcommon.Address) (string, []a
}

// this function encodes the event log visibility rules
func logsVisibilityQuery(requestingAccount *gethcommon.Address) (string, []any) {
func logsVisibilityQuery(requestingAccount *gethcommon.Address, withReceipts bool) (string, []any) {
acc := requestingAccount.Bytes()

visibParams := make([]any, 0)

visibQuery := "AND ("

// this condition only affects queries that return receipts that have no events logs
visibQuery += " (e.id is NULL) "
if withReceipts {
// this condition only affects queries that return receipts that have no events logs
visibQuery += " (e.id is NULL) "
} else {
visibQuery += " (1=0) "
}

// everyone can query config_public events
visibQuery += " OR (et.config_public=true) "
Expand Down
3 changes: 1 addition & 2 deletions go/enclave/storage/events_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ func (es *eventsStorage) storeReceipt(ctx context.Context, dbTX *sql.Tx, batch *
}

func (es *eventsStorage) storeEventLog(ctx context.Context, dbTX *sql.Tx, receiptId uint64, l *types.Log) error {
eventSig := l.Topics[0]

contract, err := es.readContract(ctx, dbTX, l.Address)
if err != nil {
// the contract should already have been stored when it was created
return fmt.Errorf("could not read contract address. %s. Cause: %w", l.Address, err)
}

eventSig := l.Topics[0]
eventType, err := es.readEventType(ctx, dbTX, l.Address, eventSig)
if errors.Is(err, errutil.ErrNotFound) {
// this is the first type an event of this type is emitted, so we must store it
Expand Down
6 changes: 3 additions & 3 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,14 @@ func (s *storageImpl) StoreExecutedBatch(ctx context.Context, batch *core.Batch,
}

// store the synthetic transactions
transactionsWithSenders := results.SyntheticTransactions().ToTransactionsWithSenders()
syntheticTxs := results.SyntheticTransactions().ToTransactionsWithSenders()

senders, toContracts, err := s.handleTxSendersAndReceivers(ctx, transactionsWithSenders, dbTx)
senders, toContracts, err := s.handleTxSendersAndReceivers(ctx, syntheticTxs, dbTx)
if err != nil {
return fmt.Errorf("could not handle synthetic txs senders and receivers. Cause: %w", err)
}

if err := enclavedb.WriteTransactions(ctx, dbTx, transactionsWithSenders, batch.Header.Number.Uint64(), true, senders, toContracts, len(batch.Transactions)); err != nil {
if err := enclavedb.WriteTransactions(ctx, dbTx, syntheticTxs, batch.Header.Number.Uint64(), true, senders, toContracts, len(batch.Transactions)); err != nil {
return fmt.Errorf("could not write synthetic txs. Cause: %w", err)
}

Expand Down
10 changes: 2 additions & 8 deletions go/enclave/system/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (s *systemContractCallbacks) CreateOnBatchEndTransaction(_ context.Context,
tx := txExecResult.TxWithSender.Tx
receipt := txExecResult.Receipt
synTx := TransactionPostProcessor.StructsTransaction{
From: *txExecResult.TxWithSender.Sender,
Nonce: big.NewInt(int64(txExecResult.TxWithSender.Tx.Nonce())),
GasPrice: tx.GasPrice(),
GasLimit: big.NewInt(int64(tx.Gas())),
Expand All @@ -218,15 +219,8 @@ func (s *systemContractCallbacks) CreateOnBatchEndTransaction(_ context.Context,
synTx.To = gethcommon.Address{} // Zero address - contract deployment
}

sender, err := core.GetExternalTxSigner(tx)
if err != nil {
s.logger.Error("CreateOnBatchEndTransaction: Failed to recover sender address", "error", err, "transactionHash", tx.Hash().Hex())
return nil, fmt.Errorf("failed to recover sender address: %w", err)
}
synTx.From = sender

synTxs = append(synTxs, synTx)
s.logger.Debug("CreateOnBatchEndTransaction: Encoded transaction", log.TxKey, tx.Hash(), "sender", sender.Hex())
s.logger.Debug("CreateOnBatchEndTransaction: Encoded transaction", log.TxKey, tx.Hash(), "sender", synTx.From)
}

data, err := transactionPostProcessorABI.Pack("onBlock", synTxs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export const WalletConnectionProvider = ({
setVersion(await fetchVersion());
} catch (error) {
showToast(
ToastType.DESTRUCTIVE,
error instanceof Error ? error.message : "Error initializing wallet connection. Please refresh the page."
);
ToastType.DESTRUCTIVE,
error instanceof Error ? error.message : "Error initializing wallet connection. Please refresh the page."
);
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -189,16 +189,26 @@ export const WalletConnectionProvider = ({
setProvider(providerInstance);
initialize(providerInstance);

ethereum.on("accountsChanged", fetchUserAccounts);
const handleAccountsChanged = async (accounts: string[]) => {
if (accounts.length === 0) {
setAccounts(null);
setWalletConnected(false);
setToken("");
} else {
window.location.reload();
}
};

ethereum.on("accountsChanged", handleAccountsChanged);

return () => {
if (ethereum && ethereum.removeListener) {
ethereum.removeListener("accountsChanged", handleAccountsChanged);
}
};
} else {
setLoading(false);
}

return () => {
if (ethereum && ethereum.removeListener) {
ethereum.removeListener("accountsChanged", fetchUserAccounts);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down

0 comments on commit 2ea5fd8

Please sign in to comment.