Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions node/derivation/batch_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type BatchInfo struct {
firstBlockNumber uint64

root common.Hash
withdrawalRoot common.Hash
skippedL1MessageBitmap *big.Int
}

Expand All @@ -83,6 +84,7 @@ func (bi *BatchInfo) TxNum() uint64 {
func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error {
//var rollupData BatchInfo
bi.root = batch.PostStateRoot
bi.withdrawalRoot = batch.WithdrawRoot
bi.skippedL1MessageBitmap = new(big.Int).SetBytes(batch.SkippedL1MessageBitmap[:])
bi.version = uint64(batch.Version)
var txPayload []byte
Expand Down
23 changes: 21 additions & 2 deletions node/derivation/derivation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/morph-l2/bindings/bindings"
"github.com/morph-l2/bindings/predeploys"
"github.com/morph-l2/node/sync"
"github.com/morph-l2/node/types"
"github.com/morph-l2/node/validator"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Derivation struct {
rollup *bindings.Rollup
metrics *Metrics
l1BeaconClient *L1BeaconClient
L2ToL1MessagePasser *bindings.L2ToL1MessagePasser

latestDerivation uint64
db Database
Expand Down Expand Up @@ -76,6 +78,10 @@ func NewDerivationClient(ctx context.Context, cfg *Config, syncer *sync.Syncer,
if err != nil {
return nil, err
}
msgPasser, err := bindings.NewL2ToL1MessagePasser(predeploys.L2ToL1MessagePasserAddr, eClient)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
logger = logger.With("module", "derivation")
metrics := PrometheusMetrics("morphnode")
Expand Down Expand Up @@ -108,6 +114,7 @@ func NewDerivationClient(ctx context.Context, cfg *Config, syncer *sync.Syncer,
logProgressInterval: cfg.LogProgressInterval,
metrics: metrics,
l1BeaconClient: l1BeaconClient,
L2ToL1MessagePasser: msgPasser,
}, nil
}

Expand Down Expand Up @@ -201,7 +208,14 @@ func (d *Derivation) derivationBlock(ctx context.Context) {
// only last block of batch
d.logger.Info("batch derivation complete", "batch_index", batchInfo.batchIndex, "currentBatchEndBlock", lastHeader.Number.Uint64())
d.metrics.SetL2DeriveHeight(lastHeader.Number.Uint64())
if !bytes.Equal(lastHeader.Root.Bytes(), batchInfo.root.Bytes()) {
withdrawalRoot, err := d.L2ToL1MessagePasser.MessageRoot(&bind.CallOpts{
BlockNumber: lastHeader.Number,
})
if err != nil {
d.logger.Error("get withdrawal root failed", "error", err)
return
}
if !bytes.Equal(lastHeader.Root.Bytes(), batchInfo.root.Bytes()) || !bytes.Equal(withdrawalRoot[:], batchInfo.withdrawalRoot.Bytes()) {
d.metrics.SetBatchStatus(stateException)
// TODO The challenge switch is currently on and will be turned on in the future
if d.validator != nil && d.validator.ChallengeEnable() {
Expand All @@ -210,7 +224,12 @@ func (d *Derivation) derivationBlock(ctx context.Context) {
return
}
}
d.logger.Info("root hash is not equal", "originStateRootHash", batchInfo.root, "deriveStateRootHash", lastHeader.Root.Hex())
d.logger.Info("root hash or withdrawal hash is not equal",
"originStateRootHash", batchInfo.root,
"deriveStateRootHash", lastHeader.Root.Hex(),
"batchWithdrawalRoot", batchInfo.withdrawalRoot.Hex(),
"deriveWithdrawalRoot", common.BytesToHash(withdrawalRoot[:]).Hex(),
)
return
} else {
d.metrics.SetBatchStatus(stateNormal)
Expand Down
14 changes: 14 additions & 0 deletions node/derivation/derivation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/morph-l2/bindings/bindings"
"github.com/morph-l2/bindings/predeploys"
"github.com/morph-l2/node/types"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind/backends"
Expand Down Expand Up @@ -53,6 +54,8 @@ func testNewDerivationClient(t *testing.T) *Derivation {
require.NoError(t, err)
eClient, err := ethclient.Dial("http://localhost:7545")
require.NoError(t, err)
msgPasser, err := bindings.NewL2ToL1MessagePasser(predeploys.L2ToL1MessagePasserAddr, eClient)
require.NoError(t, err)
logger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout))
baseHttp := NewBasicHTTPClient("http://localhost:3500", logger)
l1BeaconClient := NewL1BeaconClient(baseHttp)
Expand All @@ -67,6 +70,7 @@ func testNewDerivationClient(t *testing.T) *Derivation {
fetchBlockRange: 100,
pollInterval: 1,
l1BeaconClient: l1BeaconClient,
L2ToL1MessagePasser: msgPasser,
}
return &d
}
Expand All @@ -77,6 +81,16 @@ func TestFetchRollupData(t *testing.T) {
require.NoError(t, err)
}

func TestFetchWithdrawalRoot(t *testing.T) {
d := testNewDerivationClient(t)
bn, err := d.l2Client.BlockNumber(context.Background())
require.NoError(t, err)
_, err = d.L2ToL1MessagePasser.MessageRoot(&bind.CallOpts{
BlockNumber: big.NewInt(int64(bn)),
})
require.NoError(t, err)
}

func newSimulatedBackend(key *ecdsa.PrivateKey) (*backends.SimulatedBackend, ethdb.Database) {
var gasLimit uint64 = 9_000_000
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
Expand Down