Skip to content

Commit

Permalink
opnode: Parameterize deposit contract address (ethereum-optimism#361)
Browse files Browse the repository at this point in the history
The contract can be deployed to any address. It is set in the
rollup config file.
  • Loading branch information
trianglesphere authored Apr 9, 2022
1 parent 0874174 commit da8b3e2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
4 changes: 3 additions & 1 deletion opnode/rollup/derive/invert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ type infoTest struct {
mkInfo func(rng *rand.Rand) *l1MockInfo
}

var MockDepositContractAddr = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001")

func TestParseL1InfoDepositTxData(t *testing.T) {
// Go 1.18 will have native fuzzing for us to use, until then, we cover just the below cases
cases := []infoTest{
Expand All @@ -123,7 +125,7 @@ func TestParseL1InfoDepositTxData(t *testing.T) {
for i, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
info := testCase.mkInfo(rand.New(rand.NewSource(int64(1234 + i))))
depTx := L1InfoDeposit(123, info)
depTx := L1InfoDeposit(123, info, MockDepositContractAddr)
nr, time, baseFee, h, err := L1InfoDepositTxData(depTx.Data)
assert.NoError(t, err, "expected valid deposit info")
assert.Equal(t, nr, info.num)
Expand Down
17 changes: 8 additions & 9 deletions opnode/rollup/derive/payload_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
var (
DepositEventABI = "TransactionDeposited(address,address,uint256,uint256,uint256,bool,bytes)"
DepositEventABIHash = crypto.Keccak256Hash([]byte(DepositEventABI))
DepositContractAddr = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001")
L1InfoFuncSignature = "setL1BlockValues(uint256,uint256,uint256,bytes32)"
L1InfoFuncBytes4 = crypto.Keccak256([]byte(L1InfoFuncSignature))[:4]
L1InfoPredeployAddr = common.HexToAddress("0x4200000000000000000000000000000000000015")
Expand Down Expand Up @@ -169,7 +168,7 @@ type L1Info interface {

// L1InfoDeposit creats a L1 Info deposit transaction based on the L1 block,
// and the L2 block-height difference with the start of the epoch.
func L1InfoDeposit(seqNumber uint64, block L1Info) *types.DepositTx {
func L1InfoDeposit(seqNumber uint64, block L1Info, depositContractAddr common.Address) *types.DepositTx {
data := make([]byte, 4+8+8+32+32)
offset := 0
copy(data[offset:4], L1InfoFuncBytes4)
Expand All @@ -189,7 +188,7 @@ func L1InfoDeposit(seqNumber uint64, block L1Info) *types.DepositTx {

return &types.DepositTx{
SourceHash: source.SourceHash(),
From: DepositContractAddr,
From: depositContractAddr,
To: &L1InfoPredeployAddr,
Mint: nil,
Value: big.NewInt(0),
Expand All @@ -199,15 +198,15 @@ func L1InfoDeposit(seqNumber uint64, block L1Info) *types.DepositTx {
}

// UserDeposits transforms the L2 block-height and L1 receipts into the transaction inputs for a full L2 block
func UserDeposits(receipts []*types.Receipt) ([]*types.DepositTx, error) {
func UserDeposits(receipts []*types.Receipt, depositContractAddr common.Address) ([]*types.DepositTx, error) {
var out []*types.DepositTx

for _, rec := range receipts {
if rec.Status != types.ReceiptStatusSuccessful {
continue
}
for _, log := range rec.Logs {
if log.Address == DepositContractAddr {
if log.Address == depositContractAddr {
dep, err := UnmarshalLogEvent(log)
if err != nil {
return nil, fmt.Errorf("malformatted L1 deposit log: %v", err)
Expand Down Expand Up @@ -330,17 +329,17 @@ func FillMissingBatches(batches []*BatchData, epoch, blockTime, minL2Time, nextL
}

// L1InfoDepositBytes returns a serialized L1-info attributes transaction.
func L1InfoDepositBytes(seqNumber uint64, l1Info L1Info) (hexutil.Bytes, error) {
l1Tx := types.NewTx(L1InfoDeposit(seqNumber, l1Info))
func L1InfoDepositBytes(seqNumber uint64, l1Info L1Info, depositContractAddress common.Address) (hexutil.Bytes, error) {
l1Tx := types.NewTx(L1InfoDeposit(seqNumber, l1Info, depositContractAddress))
opaqueL1Tx, err := l1Tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to encode L1 info tx")
}
return opaqueL1Tx, nil
}

func DeriveDeposits(receipts []*types.Receipt) ([]hexutil.Bytes, error) {
userDeposits, err := UserDeposits(receipts)
func DeriveDeposits(receipts []*types.Receipt, depositContractAddr common.Address) ([]hexutil.Bytes, error) {
userDeposits, err := UserDeposits(receipts, depositContractAddr)
if err != nil {
return nil, fmt.Errorf("failed to derive user deposits: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions opnode/rollup/derive/payload_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func GenerateDepositLog(deposit *types.DepositTx) *types.Log {
data = append(data, make([]byte, 32-(len(data)%32))...)
}

return GenerateLog(DepositContractAddr, topics, data)
return GenerateLog(MockDepositContractAddr, topics, data)
}

// Generates an EVM log entry with the given topics and data.
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestDeriveUserDeposits(t *testing.T) {
TransactionIndex: uint(txIndex),
})
}
got, err := UserDeposits(receipts)
got, err := UserDeposits(receipts, MockDepositContractAddr)
assert.NoError(t, err)
assert.Equal(t, len(got), len(expectedDeposits))
for d, depTx := range got {
Expand Down
8 changes: 4 additions & 4 deletions opnode/rollup/driver/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ func (d *outputImpl) createNewBlock(ctx context.Context, l2Head eth.L2BlockRef,

// First transaction in every block is always the L1 info transaction.
seqNumber := l2Head.Number + 1 - l2SafeHead.Number
l1InfoTx, err := derive.L1InfoDepositBytes(seqNumber, l1Info)
l1InfoTx, err := derive.L1InfoDepositBytes(seqNumber, l1Info, d.Config.DepositContractAddress)
if err != nil {
return l2Head, nil, err
}
txns = append(txns, l1InfoTx)

// Next we append user deposits. If we're not the first block in an epoch, then receipts will
// be empty and no deposits will be derived.
deposits, err := derive.DeriveDeposits(receipts)
deposits, err := derive.DeriveDeposits(receipts, d.Config.DepositContractAddress)
d.log.Info("Derived deposits", "deposits", deposits, "l2Parent", l2Head, "l1Origin", l1Origin)
if err != nil {
return l2Head, nil, fmt.Errorf("failed to derive deposits: %v", err)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (d *outputImpl) insertEpoch(ctx context.Context, l2Head eth.L2BlockRef, l2S
if err != nil {
return l2Head, l2SafeHead, false, fmt.Errorf("failed to get L1 timestamp of next L1 block: %v", err)
}
deposits, err := derive.DeriveDeposits(receipts)
deposits, err := derive.DeriveDeposits(receipts, d.Config.DepositContractAddress)
if err != nil {
return l2Head, l2SafeHead, false, fmt.Errorf("failed to derive deposits: %w", err)
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (d *outputImpl) insertEpoch(ctx context.Context, l2Head eth.L2BlockRef, l2S
var reorg bool
for i, batch := range batches {
var txns []l2.Data
l1InfoTx, err := derive.L1InfoDepositBytes(uint64(i), l1Info)
l1InfoTx, err := derive.L1InfoDepositBytes(uint64(i), l1Info, d.Config.DepositContractAddress)
if err != nil {
return l2Head, l2SafeHead, false, fmt.Errorf("failed to create l1InfoTx: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions opnode/rollup/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Config struct {
BatchInboxAddress common.Address `json:"batch_inbox_address"`
// Acceptable batch-sender address
BatchSenderAddress common.Address `json:"batch_sender_address"`
// L1 Deposit Contract Address
DepositContractAddress common.Address `json:"deposit_contract_address"`
}

// Check verifies that the given configuration makes sense
Expand All @@ -63,6 +65,9 @@ func (cfg *Config) Check() error {
if cfg.Genesis.L2.Hash == cfg.Genesis.L1.Hash {
return errors.New("achievement get! rollup inception: L1 and L2 genesis cannot be the same")
}
if cfg.DepositContractAddress == (common.Address{}) {
return errors.New("did not provide deposit contract address ")
}
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion opnode/test/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"github.com/stretchr/testify/require"
)

// Temporary until the contract is deployed properly instead of as a pre-deploy to a specific address
var MockDepositContractAddr = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001")

const (
cliqueSignerHDPath = "m/44'/60'/0'/0/0"
transactorHDPath = "m/44'/60'/0'/0/1"
Expand All @@ -49,7 +52,7 @@ func defaultSystemConfig(t *testing.T) SystemConfig {
},
BatchSubmitterHDPath: bssHDPath,
CliqueSignerDerivationPath: cliqueSignerHDPath,
DepositContractAddress: derive.DepositContractAddr,
DepositContractAddress: MockDepositContractAddr,
L1InfoPredeployAddress: derive.L1InfoPredeployAddr,
L1WsAddr: "127.0.0.1",
L1WsPort: 9090,
Expand Down Expand Up @@ -86,6 +89,7 @@ func defaultSystemConfig(t *testing.T) SystemConfig {
FeeRecipientAddress: common.Address{0xff, 0x01},
BatchInboxAddress: common.Address{0xff, 0x02},
// Batch Sender address is filled out in system start
DepositContractAddress: MockDepositContractAddr,
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion ops/rollup.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@

"batch_inbox_address": "0xff00000000000000000000000000000000000002",

"batch_sender_address": "0xde3829a23df1479438622a08a116e8eb3f620bb5"
"batch_sender_address": "0xde3829a23df1479438622a08a116e8eb3f620bb5",

"deposit_contract_address": "0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001"
}

0 comments on commit da8b3e2

Please sign in to comment.