mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Fix eth events query and sync inconsistent state
- kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
This commit is contained in:
@@ -1116,15 +1116,20 @@ func (c *Client) RollupConstants() (*common.RollupConstants, error) {
|
||||
}
|
||||
|
||||
// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
|
||||
func (c *Client) RollupEventsByBlock(blockNum int64) (*eth.RollupEvents, *ethCommon.Hash, error) {
|
||||
func (c *Client) RollupEventsByBlock(blockNum int64,
|
||||
blockHash *ethCommon.Hash) (*eth.RollupEvents, error) {
|
||||
c.rw.RLock()
|
||||
defer c.rw.RUnlock()
|
||||
|
||||
block, ok := c.blocks[blockNum]
|
||||
if !ok {
|
||||
return nil, nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
}
|
||||
return &block.Rollup.Events, &block.Eth.Hash, nil
|
||||
if blockHash != nil && *blockHash != block.Eth.Hash {
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Hash mismatch, requested %v got %v",
|
||||
blockHash, block.Eth.Hash))
|
||||
}
|
||||
return &block.Rollup.Events, nil
|
||||
}
|
||||
|
||||
// RollupEventInit returns the initialize event with its corresponding block number
|
||||
@@ -1573,15 +1578,20 @@ func (c *Client) AuctionConstants() (*common.AuctionConstants, error) {
|
||||
}
|
||||
|
||||
// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
|
||||
func (c *Client) AuctionEventsByBlock(blockNum int64) (*eth.AuctionEvents, *ethCommon.Hash, error) {
|
||||
func (c *Client) AuctionEventsByBlock(blockNum int64,
|
||||
blockHash *ethCommon.Hash) (*eth.AuctionEvents, error) {
|
||||
c.rw.RLock()
|
||||
defer c.rw.RUnlock()
|
||||
|
||||
block, ok := c.blocks[blockNum]
|
||||
if !ok {
|
||||
return nil, nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
}
|
||||
return &block.Auction.Events, &block.Eth.Hash, nil
|
||||
if blockHash != nil && *blockHash != block.Eth.Hash {
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Hash mismatch, requested %v got %v",
|
||||
blockHash, block.Eth.Hash))
|
||||
}
|
||||
return &block.Auction.Events, nil
|
||||
}
|
||||
|
||||
// AuctionEventInit returns the initialize event with its corresponding block number
|
||||
@@ -1789,15 +1799,20 @@ func (c *Client) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address, amou
|
||||
}
|
||||
|
||||
// WDelayerEventsByBlock returns the events in a block that happened in the WDelayer Contract
|
||||
func (c *Client) WDelayerEventsByBlock(blockNum int64) (*eth.WDelayerEvents, *ethCommon.Hash, error) {
|
||||
func (c *Client) WDelayerEventsByBlock(blockNum int64,
|
||||
blockHash *ethCommon.Hash) (*eth.WDelayerEvents, error) {
|
||||
c.rw.RLock()
|
||||
defer c.rw.RUnlock()
|
||||
|
||||
block, ok := c.blocks[blockNum]
|
||||
if !ok {
|
||||
return nil, nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Block %v doesn't exist", blockNum))
|
||||
}
|
||||
return &block.WDelayer.Events, &block.Eth.Hash, nil
|
||||
if blockHash != nil && *blockHash != block.Eth.Hash {
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Hash mismatch, requested %v got %v",
|
||||
blockHash, block.Eth.Hash))
|
||||
}
|
||||
return &block.WDelayer.Events, nil
|
||||
}
|
||||
|
||||
// WDelayerConstants returns the Constants of the WDelayer Contract
|
||||
|
||||
@@ -130,7 +130,7 @@ func TestClientAuction(t *testing.T) {
|
||||
blockNum, err := c.EthLastBlock()
|
||||
require.Nil(t, err)
|
||||
|
||||
auctionEvents, _, err := c.AuctionEventsByBlock(blockNum)
|
||||
auctionEvents, err := c.AuctionEventsByBlock(blockNum, nil)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 2, len(auctionEvents.NewBid))
|
||||
}
|
||||
@@ -171,7 +171,7 @@ func TestClientRollup(t *testing.T) {
|
||||
|
||||
blockNum, err := c.EthLastBlock()
|
||||
require.Nil(t, err)
|
||||
rollupEvents, _, err := c.RollupEventsByBlock(blockNum)
|
||||
rollupEvents, err := c.RollupEventsByBlock(blockNum, nil)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, N, len(rollupEvents.L1UserTx))
|
||||
assert.Equal(t, 1, len(rollupEvents.AddToken))
|
||||
@@ -192,7 +192,7 @@ func TestClientRollup(t *testing.T) {
|
||||
|
||||
blockNumA, err := c.EthLastBlock()
|
||||
require.Nil(t, err)
|
||||
rollupEvents, hashA, err := c.RollupEventsByBlock(blockNumA)
|
||||
rollupEvents, err = c.RollupEventsByBlock(blockNumA, nil)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, len(rollupEvents.L1UserTx))
|
||||
assert.Equal(t, 0, len(rollupEvents.AddToken))
|
||||
@@ -205,14 +205,14 @@ func TestClientRollup(t *testing.T) {
|
||||
|
||||
blockNumB, err := c.EthLastBlock()
|
||||
require.Nil(t, err)
|
||||
rollupEvents, hashB, err := c.RollupEventsByBlock(blockNumA)
|
||||
rollupEventsB, err := c.RollupEventsByBlock(blockNumA, nil)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, len(rollupEvents.L1UserTx))
|
||||
assert.Equal(t, 0, len(rollupEvents.AddToken))
|
||||
assert.Equal(t, 0, len(rollupEvents.ForgeBatch))
|
||||
assert.Equal(t, 0, len(rollupEventsB.L1UserTx))
|
||||
assert.Equal(t, 0, len(rollupEventsB.AddToken))
|
||||
assert.Equal(t, 0, len(rollupEventsB.ForgeBatch))
|
||||
|
||||
assert.Equal(t, blockNumA, blockNumB)
|
||||
assert.NotEqual(t, hashA, hashB)
|
||||
assert.NotEqual(t, rollupEvents, rollupEventsB)
|
||||
|
||||
// Forge again
|
||||
rollupForgeBatchArgs0 := ð.RollupForgeBatchArgs{
|
||||
@@ -232,7 +232,7 @@ func TestClientRollup(t *testing.T) {
|
||||
|
||||
blockNum, err = c.EthLastBlock()
|
||||
require.Nil(t, err)
|
||||
rollupEvents, _, err = c.RollupEventsByBlock(blockNum)
|
||||
rollupEvents, err = c.RollupEventsByBlock(blockNum, nil)
|
||||
require.Nil(t, err)
|
||||
|
||||
rollupForgeBatchArgs1, sender, err := c.RollupForgeBatchArgs(rollupEvents.ForgeBatch[0].EthTxHash,
|
||||
|
||||
Reference in New Issue
Block a user