Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix phantom events #2189

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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
1 change: 1 addition & 0 deletions go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,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 @@ -200,6 +200,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 @@ -214,15 +215,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
Loading