Skip to content

Commit

Permalink
[WIP]L2->L1 debug for L1-L2 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shepf committed Aug 13, 2024
1 parent 6fe0e50 commit 9fc32fe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
56 changes: 29 additions & 27 deletions cairo/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/NethermindEth/juno/vm"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/sirupsen/logrus"
"github.com/yu-org/yu/core/context"
"github.com/yu-org/yu/core/tripod"
Expand All @@ -23,10 +24,10 @@ import (
"itachi/cairo/adapters"
"itachi/cairo/config"
"itachi/cairo/l1/contract"

snos_ouput "itachi/cairo/snos-ouput"
"math/big"
"net/http"
"os"
)

type Cairo struct {
Expand All @@ -38,20 +39,13 @@ type Cairo struct {
network utils.Network
}

func (c *Cairo) StartBlock(block *Block) {

logrus.Infof("Cairo TriPod StartBlock for height (%d)! ", block.Height)

}

func (c *Cairo) EndBlock(block *Block) {

logrus.Infof("Cairo TriPod EndBlock for height (%d)! ", block.Height)
func (c *Cairo) FinalizeBlock(block *Block) {
logrus.SetOutput(os.Stdout)

}
if !c.cfg.EnableL2UpdateState {

func (c *Cairo) FinalizeBlock(block *Block) {
logrus.Infof("Cairo TriPod finalize block for height (%d)! ", block.Height)
return
}

// for PrevStateRoot, get last finalized block
compactBlock, err := c.Chain.LastFinalized()
Expand Down Expand Up @@ -86,6 +80,10 @@ func (c *Cairo) FinalizeBlock(block *Block) {

// todo messagesToL2 := make([]*rpc.MsgFromL1, 0)
messagesToL2 := make([]*adapters.MessageL1ToL2, 0)
//for t := 0; t < len(txns); t++ {
// txn := txns[t]
//
//}

num := uint64(block.Height)
// init StarknetOsOutput by block
Expand All @@ -100,62 +98,66 @@ func (c *Cairo) FinalizeBlock(block *Block) {
MessagesToL2: messagesToL2,
}
// cairoState.UpdateStarknetOsOutput(snOsOutput)
logrus.Infof("snOsOutput: %+v", snOsOutput)
fmt.Printf("snOsOutput:\n%+v\n", snOsOutput)

// send snOsOutput to L1 chain
c.ethCallUpdateState(c.cairoState, snOsOutput)
// 新旧状态根对比
if snOsOutput.PrevStateRoot.String() != snOsOutput.NewStateRoot.String() {
// send snOsOutput to L1 chain
c.ethCallUpdateState(c.cairoState, snOsOutput)
}

log.DoubleLineConsole.Info(fmt.Sprintf("Cairo Tripod finalize block, height=%d, hash=%s", block.Height, block.Hash.String()))

}

func (c *Cairo) ethCallUpdateState(cairoState *CairoState, snOsOutput *snos_ouput.StarknetOsOutput) {

ethClient, err := NewEthClient(c.cfg.EthClientAddress)
client, err := ethclient.Dial(c.cfg.EthRpcUrl)
if err != nil {
logrus.Errorf("init ethClient failed: %s", err)
fmt.Println("init client failed: ", err)
}

starknetCore, err := contract.NewStarknetCore(common.HexToAddress(c.cfg.EthContractAddress), ethClient)
starknetCore, err := contract.NewStarknetCore(common.HexToAddress(c.cfg.EthCoreContractAddress), client)
if err != nil {
fmt.Println("init starknetCore failed: ", err)
return
}

// encode snOsOutput to []*big.Int
programOutput, err := snOsOutput.EncodeTo()
if err != nil {
logrus.Errorf("encode snOsOutput failed: %s", err)
fmt.Println("encode snOsOutput failed: ", err)
return
}

// compute onchainDataHash and onchainDataSize
onchainDataHash, onchainDataSize, err := calculateOnchainData(programOutput)
if err != nil {
logrus.Errorf("calculate onchain data failed: %s", err)
fmt.Println("calculate onchain data failed: ", err)
return
}

chainID := big.NewInt(c.cfg.ChainID)
privateKeyHex := c.cfg.EthPrivateKey
address := c.cfg.EthClientAddress
address := c.cfg.EthWalletAddress
gasLimit := c.cfg.GasLimit
auth, err := CreateAuth(ethClient, privateKeyHex, address, gasLimit, chainID)
auth, err := CreateAuth(client, privateKeyHex, address, gasLimit, chainID)
if err != nil {
logrus.Errorf("create auth failed: %s", err)
fmt.Println("create auth failed: ", err)
return
}

// call updateState
tx, err := starknetCore.UpdateState(auth, programOutput, onchainDataHash, onchainDataSize)
if err != nil {
logrus.Errorf("call updateState failed: %s", err)
fmt.Println("call updateState failed: %s", err)
return
}

// retrieve transaction hash and print.
logrus.Infof("update state, tx size: %d bytes", tx.Size())
txHash := tx.Hash()
logrus.Infof("update state, tx hash: %s", txHash.Hex())
log.DoubleLineConsole.Info("update state, tx hash: %s", txHash.Hex())
fmt.Println("https://sepolia.etherscan.io/tx/" + tx.Hash().Hex())

}

Expand Down
11 changes: 7 additions & 4 deletions cairo/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ type Config struct {
EthContractAddress string `toml:"eth_contract_address"`

// L2 eth client configs
EnableL2 bool `toml:"enable_l2"`
ChainID int64 `toml:"chain_id"`
GasLimit uint64 `toml:"gas_limit"`
EthPrivateKey string `toml:"eth_private_key"`
EnableL2UpdateState bool `toml:"enable_l2_update_state"`
EthRpcUrl string `toml:"eth_rpc_url"`
ChainID int64 `toml:"chain_id"`
GasLimit uint64 `toml:"gas_limit"`
EthCoreContractAddress string `toml:"eth_core_contract_address"`
EthWalletAddress string `toml:"eth_wallet_address"`
EthPrivateKey string `toml:"eth_private_key"`

EnablePprof bool `toml:"enable_pprof"`
PprofAddr string `toml:"pprof_addr"`
Expand Down
23 changes: 0 additions & 23 deletions cairo/eth_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,9 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"itachi/cairo/config"
"itachi/cairo/l1/contract"
"math/big"
"time"
)

type MyEthClient struct {
cfg *config.Config
ethClient *ethclient.Client
client *rpc.Client
filterer *contract.StarknetCoreFilterer
}

func NewEthClient(ethClientAddress string) (*ethclient.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// TODO replace with our own client once we have one.
// Geth pulls in a lot of dependencies that we don't use.
client, err := rpc.DialContext(ctx, ethClientAddress)
if err != nil {
return nil, err
}
return ethclient.NewClient(client), nil
}

func CreateAuth(client *ethclient.Client, privateKeyHex string, address string, gasLimit uint64, chainID *big.Int) (*bind.TransactOpts, error) {
// Get nonce
nonce, err := client.PendingNonceAt(context.Background(), common.HexToAddress(address))
Expand Down
12 changes: 6 additions & 6 deletions cairo/l1/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package l1
import (
"context"
"fmt"
"itachi/cairo/config"
"itachi/cairo/l1/contract"
"itachi/cairo/starknetrpc"
"math/big"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/sirupsen/logrus"
"github.com/yu-org/yu/core/kernel"
"itachi/cairo/config"
"itachi/cairo/l1/contract"
"itachi/cairo/starknetrpc"
"math/big"
"os"
)

type L1 struct {
Expand All @@ -37,6 +37,7 @@ func StartupL1(itachi *kernel.Kernel, cfg *config.Config, s *starknetrpc.Starkne
if cfg.EnableL1 {
l1, err := NewL1(itachi, cfg, s)
if err != nil {

logrus.Fatal("init L1 client failed: ", err)
}
err = l1.Run(context.Background())
Expand Down Expand Up @@ -107,7 +108,6 @@ func convertL1TxnToBroadcastedTxn(event *contract.StarknetLogMessageToL2) (*rpc.
ContractAddress: new(felt.Felt).SetBigInt(event.ToAddress),
Nonce: new(felt.Felt).SetBigInt(event.Nonce),
CallData: &callData,
Version: new(felt.Felt).SetUint64(0),
},
PaidFeeOnL1: new(felt.Felt).SetBigInt(event.Fee),
}, nil
Expand Down
7 changes: 5 additions & 2 deletions conf/cairo_cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ skip_validate = false
err_on_revert = true

# L2 eth client configs
enable_l2 = false
enable_l2_update_state = true
eth_rpc_url = "https://sepolia.infura.io/v3/xxx"
chain_id = 11155111
gas_limit = 300000
eth_private_key = ""
eth_wallet_address = "xxx"
eth_private_key = "xxx"
eth_core_contract_address = "xxx"

# starknet RPC
enable_starknet_rpc = true
Expand Down

0 comments on commit 9fc32fe

Please sign in to comment.