diff --git a/internal/handler/op_committer_stateroot.go b/internal/handler/op_committer_stateroot.go index c8126c1..7583702 100644 --- a/internal/handler/op_committer_stateroot.go +++ b/internal/handler/op_committer_stateroot.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "encoding/json" "fmt" + "time" + "github.com/b2network/b2committer/internal/schema" "github.com/b2network/b2committer/internal/svc" "github.com/b2network/b2committer/internal/types" @@ -17,7 +19,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" mt "github.com/txaty/go-merkletree" - "time" ) func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { @@ -120,14 +121,14 @@ func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { if err != nil { log.Errorf("[Handler.GetStateRootAndCommitStateRootProposal] constructing state root for ds is failed. err : %s", errors.WithStack(err)) } - dsJson, err := stateRoots.MarshalJson() + dsJSON, err := stateRoots.MarshalJSON() if err != nil { log.Errorf("[Handler.GetStateRootAndCommitStateRootProposal] Try to marshal ds proposal: %s", err.Error()) time.Sleep(3 * time.Second) continue } - dsTxID, err := ctx.DecentralizedStore.StoreDetailsOnChain(dsJson, ctx.B2NodeConfig.ChainID, lastProposal.ProposalID) + dsTxID, err := ctx.DecentralizedStore.StoreDetailsOnChain(dsJSON, ctx.B2NodeConfig.ChainID, lastProposal.ProposalID) if err != nil { log.Errorf("[Handler.GetStateRootAndCommitStateRootProposal] Try to store ds proposal: %s", err.Error()) continue @@ -138,7 +139,6 @@ func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { continue } log.Infof("[Handler.GetStateRootAndCommitStateRootProposal] success submit txs to ds: %s, dsHash: %s", lastProposal.ProposalID, lastProposal.DsTxHash) - } if lastProposal.Winner != common.HexToAddress(voteAddress) { outputs, err := ctx.DecentralizedStore.QueryDetailsByTxID(lastProposal.DsTxHash) @@ -176,7 +176,6 @@ func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { } log.Infof("[Handler.GetStateRootAndCommitStateRootProposal] success verify and vote submit output from ds: %s, dsHash: %s", lastProposal.ProposalID, lastProposal.DsTxHash) } - } if lastProposal.Status == schema.ProposalCommitting { @@ -236,12 +235,12 @@ func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { time.Sleep(3 * time.Second) continue } - if len(outs.Data) <= 0 { + if len(outs.Data) == 0 { log.Errorf("[Handler.GetStateRootAndCommitStateRootProposal] Try to get outputs from btc: no data") time.Sleep(3 * time.Second) continue } - if len(outs.Data[0].Inscriptions) <= 0 { + if len(outs.Data[0].Inscriptions) == 0 { log.Errorf("[Handler.GetStateRootAndCommitStateRootProposal] Try to get outputs from btc: no inscription") time.Sleep(3 * time.Second) continue @@ -276,9 +275,7 @@ func GetStateRootAndCommitStateRootProposal(ctx *svc.ServiceContext) { } log.Infof("[Handler.GetStateRootAndCommitStateRootProposal] success verify and vote submit output from btc: %s, btcTxHash: %s", lastProposal.ProposalID, lastProposal.BitcoinTxHash) } - } - } } diff --git a/internal/handler/op_committer_txs.go b/internal/handler/op_committer_txs.go index 32f83c0..8aebd7b 100644 --- a/internal/handler/op_committer_txs.go +++ b/internal/handler/op_committer_txs.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "encoding/json" "fmt" + "time" + "github.com/b2network/b2committer/internal/schema" "github.com/b2network/b2committer/internal/svc" "github.com/b2network/b2committer/internal/types" @@ -15,7 +17,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" mt "github.com/txaty/go-merkletree" - "time" ) func GetBlobsAndCommitTxsProposal(ctx *svc.ServiceContext) { @@ -99,7 +100,6 @@ func GetBlobsAndCommitTxsProposal(ctx *svc.ServiceContext) { } if lastProposal.Status == schema.ProposalPendingStatus { - phase, err := ctx.OpCommitterClient.ProposalManager.IsVotedOntxsRootDSTxPhase(&bind.CallOpts{}, lastProposal.ProposalID, common.HexToAddress(voteAddress)) if err != nil { log.Errorf("[Handler.GetBlobsAndCommitProposal][IsVotedOntxsRootDSTxPhase] is failed : %s", err) @@ -118,14 +118,19 @@ func GetBlobsAndCommitTxsProposal(ctx *svc.ServiceContext) { continue } blobMerkleRoot, err := GetBlobsMerkleRoot(blobs) + if err != nil { + log.Errorf("[Handler.GetBlobsAndCommitProposal] Try to get blobs merkle root: %s", err.Error()) + time.Sleep(3 * time.Second) + continue + } dsProposal := types.NewDsTxsProposal(ctx.B2NodeConfig.ChainID, lastProposal.ProposalID, blobMerkleRoot, blobs) - dsJson, err := dsProposal.MarshalJson() + dsJSON, err := dsProposal.MarshalJSON() if err != nil { log.Errorf("[Handler.GetBlobsAndCommitProposal] Try to marshal ds proposal: %s", err.Error()) time.Sleep(3 * time.Second) continue } - dsTxID, err := ctx.DecentralizedStore.StoreDetailsOnChain(dsJson, ctx.B2NodeConfig.ChainID, lastProposal.ProposalID) + dsTxID, err := ctx.DecentralizedStore.StoreDetailsOnChain(dsJSON, ctx.B2NodeConfig.ChainID, lastProposal.ProposalID) if err != nil { log.Errorf("[Handler.GetBlobsAndCommitProposal] Try to store ds proposal: %s", err.Error()) continue diff --git a/internal/handler/sync_blob.go b/internal/handler/sync_blob.go index 850bf26..11c4240 100644 --- a/internal/handler/sync_blob.go +++ b/internal/handler/sync_blob.go @@ -2,6 +2,9 @@ package handler import ( "context" + "math/big" + "time" + "github.com/b2network/b2committer/internal/schema" "github.com/b2network/b2committer/internal/svc" "github.com/b2network/b2committer/pkg/errcode" @@ -9,8 +12,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" "gorm.io/gorm" - "math/big" - "time" ) func QueryBlobOnChainAndStoreInLocal(ctx *svc.ServiceContext) { @@ -83,7 +84,6 @@ func QueryBlobOnChainAndStoreInLocal(ctx *svc.ServiceContext) { } ctx.SyncedBlobBlockNumber = blockOnChain.Number().Int64() ctx.SyncedBlobBlockHash = blockOnChain.Hash() - continue } else { err := ctx.DB.Delete(&dbBlob, "block_number=?", syncingBlobBlockNumber).Error if err != nil { @@ -92,6 +92,7 @@ func QueryBlobOnChainAndStoreInLocal(ctx *svc.ServiceContext) { continue } } + continue } for _, bif := range blobInfos { diff --git a/internal/svc/svc.go b/internal/svc/svc.go index 818581b..ca139da 100644 --- a/internal/svc/svc.go +++ b/internal/svc/svc.go @@ -1,6 +1,9 @@ package svc import ( + "math/big" + "time" + "github.com/b2network/b2committer/internal/types" "github.com/b2network/b2committer/pkg/b2node" "github.com/b2network/b2committer/pkg/beacon" @@ -17,8 +20,6 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" - "math/big" - "time" ) var svc *ServiceContext @@ -39,7 +40,7 @@ type ServiceContext struct { SyncedBlobBlockHash common.Hash OpCommitterClient *b2node.OpCommitterClient DecentralizedStore ds.DecentralizedStore - UnisatHTTPClient *unisat.UnisatHTTPClient + UnisatHTTPClient *unisat.UstHTTPClient } func NewServiceContext(cfg *types.Config, bitcoinCfg *types.BitcoinRPCConfig, b2nodeConfig *types.B2NODEConfig) *ServiceContext { diff --git a/internal/types/config.go b/internal/types/config.go index 5b9d79a..6663489 100644 --- a/internal/types/config.go +++ b/internal/types/config.go @@ -23,7 +23,7 @@ type Config struct { InitBlobBlockHash string `env:"INIT_BLOB_BLOCK_HASH" envDefault:"0x6218666b40fce4153e8f5349ab2f9d2590a601e5a178e4b6d4580094d5c0c2ee"` BlobIntervalTime uint64 `env:"BLOB_INTERVAL_TIME" envDefault:"1800"` OutputIntervalTime uint64 `env:"OUTPUT_INTERVAL_TIME" envDefault:"3600"` - L2OutputOracleProxyContract string `env:"L2_OUTPUT_ORACLE_PROXY_CONTRACT" envDefault:"0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F"` //sepolia + L2OutputOracleProxyContract string `env:"L2_OUTPUT_ORACLE_PROXY_CONTRACT" envDefault:"0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F"` BatcherInbox string `env:"BATCHER_INBOX" envDefault:"0xff00000000000000000000000000000011155420"` BatcherSender string `env:"BATCHER_SENDER" envDefault:"0x8F23BB38F531600e5d8FDDaAEC41F13FaB46E98c"` LimitNum int `evn:"PROPOSAL_BATCHES_LIMITNUM" envDefault:"10"` diff --git a/internal/types/ds.go b/internal/types/ds.go index 0ac68e2..156b53c 100644 --- a/internal/types/ds.go +++ b/internal/types/ds.go @@ -2,6 +2,7 @@ package types import ( "encoding/json" + "github.com/b2network/b2committer/internal/schema" ) @@ -9,7 +10,7 @@ type DsTxsProposal struct { ChainID int64 ProposalID uint64 TxsRoot string - Blobs *[]DsBlob + Blobs []*DsBlob } type DsStateRootProposal struct { @@ -46,20 +47,20 @@ func NewDsStateRootProposal(chainID int64, proposalID uint64, outputRoot string, }, nil } -func convertBlobToDsBlob(blobs []schema.BlobInfo) *[]DsBlob { - var dsBlobs []DsBlob +func convertBlobToDsBlob(blobs []schema.BlobInfo) []*DsBlob { + var dsBlobs []*DsBlob for _, blob := range blobs { - dsBlobs = append(dsBlobs, DsBlob{ + dsBlobs = append(dsBlobs, &DsBlob{ BlockID: blob.BlockNumber, Blob: blob.Blob, }) } - return &dsBlobs + return dsBlobs } func (b *DsTxsProposal) GetDBBlobInfos() ([]schema.BlobInfo, error) { var dbBlobs []schema.BlobInfo - for _, blob := range *b.Blobs { + for _, blob := range b.Blobs { dbBlobs = append(dbBlobs, schema.BlobInfo{ BlockNumber: blob.BlockID, Blob: blob.Blob, @@ -68,7 +69,7 @@ func (b *DsTxsProposal) GetDBBlobInfos() ([]schema.BlobInfo, error) { return dbBlobs, nil } -func (b *DsTxsProposal) MarshalJson() ([]byte, error) { +func (b *DsTxsProposal) MarshalJSON() ([]byte, error) { marshal, err := json.Marshal(b) if err != nil { return nil, err @@ -76,7 +77,7 @@ func (b *DsTxsProposal) MarshalJson() ([]byte, error) { return marshal, nil } -func (s *DsStateRootProposal) MarshalJson() ([]byte, error) { +func (s *DsStateRootProposal) MarshalJSON() ([]byte, error) { marshal, err := json.Marshal(s) if err != nil { return nil, err diff --git a/internal/types/proposal.go b/internal/types/proposal.go index fe6bf9e..b68f58a 100644 --- a/internal/types/proposal.go +++ b/internal/types/proposal.go @@ -2,6 +2,7 @@ package types import ( "encoding/json" + "github.com/b2network/b2committer/internal/schema" ) diff --git a/pkg/b2node/b2node.go b/pkg/b2node/b2node.go index 07643a0..774ec29 100644 --- a/pkg/b2node/b2node.go +++ b/pkg/b2node/b2node.go @@ -3,6 +3,9 @@ package b2node import ( "crypto/ecdsa" "fmt" + "math/big" + "time" + "github.com/b2network/b2committer/internal/schema" "github.com/b2network/b2committer/pkg/contract/zk" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -10,8 +13,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" - "math/big" - "time" ) type NodeClient struct { @@ -29,7 +30,13 @@ func NewNodeClient(privateKeyStr string, chainID int64, address string, contract panic(err) } auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(chainID)) + if err != nil { + panic(err) + } committer, err := zk.NewCommitter(common.HexToAddress(contractAddress), conn) + if err != nil { + panic(err) + } return &NodeClient{ PrivateKey: privateKey, Address: address, diff --git a/pkg/b2node/committer.go b/pkg/b2node/committer.go index 45e9405..d5e1f0f 100644 --- a/pkg/b2node/committer.go +++ b/pkg/b2node/committer.go @@ -1,13 +1,14 @@ package b2node import ( + "math/big" + "github.com/b2network/b2committer/internal/types" "github.com/b2network/b2committer/pkg/contract/op" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" ethTypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "math/big" ) type OpCommitterClient struct { @@ -23,6 +24,9 @@ func NewOpCommitterClient(privateKeyStr string, chainID int64, proposer *op.Prop panic(err) } auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(chainID)) + if nil != err { + panic(err) + } return &OpCommitterClient{ Proposer: proposer, Committer: committer, diff --git a/pkg/beacon/l1_beacon_client.go b/pkg/beacon/l1_beacon_client.go index 4986d4b..f47f60d 100644 --- a/pkg/beacon/l1_beacon_client.go +++ b/pkg/beacon/l1_beacon_client.go @@ -4,6 +4,8 @@ import ( "context" "errors" "fmt" + "math/big" + "github.com/b2network/b2committer/pkg/errcode" "github.com/b2network/b2committer/pkg/log" "github.com/ethereum-optimism/optimism/op-service/eth" @@ -12,7 +14,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" - "math/big" ) type BlobDataSource struct { @@ -25,7 +26,8 @@ type BlobDataSource struct { } func NewBlobDataSource(l1Signer types.Signer, batchInboxAddress common.Address, batchSenderAddress common.Address, - blobsFetcher *sources.L1BeaconClient, ethRPC *ethclient.Client) *BlobDataSource { + blobsFetcher *sources.L1BeaconClient, ethRPC *ethclient.Client, +) *BlobDataSource { return &BlobDataSource{ L1Signer: l1Signer, BatchInboxAddress: batchInboxAddress, @@ -35,7 +37,7 @@ func NewBlobDataSource(l1Signer types.Signer, batchInboxAddress common.Address, } } -func (bds *BlobDataSource) GetBlobByBlockNum(ctx context.Context, blockNum *big.Int) ([]BlockBlobInfo, error) { +func (bds *BlobDataSource) GetBlobByBlockNum(ctx context.Context, blockNum *big.Int) ([]*BlockBlobInfo, error) { block, err := bds.EthRPC.BlockByNumber(ctx, blockNum) if err != nil { return nil, fmt.Errorf("failed to get block %d: %v", blockNum, err) @@ -44,7 +46,7 @@ func (bds *BlobDataSource) GetBlobByBlockNum(ctx context.Context, blockNum *big. hashes := DataAndHashesFromTxs(txs, bds.L1Signer, bds.BatchInboxAddress, bds.BatchSenderAddress) if len(hashes) == 0 { // there are no blobs to fetch so we can return immediately - //return nil, fmt.Errorf("no blobs found in block %d", blockNum) + return nil, errcode.ErrNoBlobFoundInBlock } l1Block := eth.L1BlockRef{ @@ -53,24 +55,24 @@ func (bds *BlobDataSource) GetBlobByBlockNum(ctx context.Context, blockNum *big. ParentHash: block.ParentHash(), Time: block.Time(), } - //blobs, err := bds.BlobsFetcher.GetBlobs(context.Background(), l1Block, hashes) + blobSidecars, err := bds.BlobsFetcher.GetBlobSidecars(context.Background(), l1Block, hashes) if errors.Is(err, ethereum.NotFound) { // If the L1 block was available, then the blobs should be available too. The only // exception is if the blob retention window has expired, which we will ultimately handle // by failing over to a blob archival service. - return nil, fmt.Errorf("failed to fetch blobs: %w", err) + return nil, fmt.Errorf("failed to fetch blobs no record: %w", err) } else if err != nil { return nil, fmt.Errorf("failed to fetch blobs: %w", err) } - var data []BlockBlobInfo + var data []*BlockBlobInfo for i, hash := range hashes { - if &blobSidecars[i] == nil { + if blobSidecars[i] == nil { log.Errorf("blob %d not found in block %d", hash.Index, blockNum) continue } - data = append(data, BlockBlobInfo{ + data = append(data, &BlockBlobInfo{ BlobSidecar: blobSidecars[i], Hash: &hash, }) @@ -103,7 +105,7 @@ func DataAndHashesFromTxs(txs types.Transactions, l1Signer types.Signer, batchIn Hash: h, } hashes = append(hashes, idh) - blobIndex += 1 + blobIndex++ } } return hashes diff --git a/pkg/ds/ds.go b/pkg/ds/ds.go index 4ff74f9..e3841ec 100644 --- a/pkg/ds/ds.go +++ b/pkg/ds/ds.go @@ -1,9 +1,10 @@ package ds import ( + "strconv" + "github.com/everFinance/goar" "github.com/everFinance/goar/types" - "strconv" ) type DecentralizedStore interface { diff --git a/pkg/event/op/l2OutputOracle.go b/pkg/event/op/l2OutputOracle.go index 2f0ebb6..f75957d 100644 --- a/pkg/event/op/l2OutputOracle.go +++ b/pkg/event/op/l2OutputOracle.go @@ -40,7 +40,6 @@ func (t *OutputProposed) ToObj(data string) error { } func (*OutputProposed) Data(log types.Log) (string, error) { - transfer := &OutputProposed{ OutputRoot: event.TopicToHash(log, 1).Hex(), L2OutputIndex: big.NewInt(event.TopicToInt64(log, 2)), diff --git a/pkg/event/zkevm/sequenceBatches.go b/pkg/event/zkevm/sequenceBatches.go index 8fd831a..9e7d614 100644 --- a/pkg/event/zkevm/sequenceBatches.go +++ b/pkg/event/zkevm/sequenceBatches.go @@ -2,6 +2,7 @@ package zkevm import ( "encoding/json" + "github.com/b2network/b2committer/pkg/event" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" diff --git a/pkg/event/zkevm/verifybatches.go b/pkg/event/zkevm/verifybatches.go index ddef40f..bbd6989 100644 --- a/pkg/event/zkevm/verifybatches.go +++ b/pkg/event/zkevm/verifybatches.go @@ -40,7 +40,6 @@ func (t *VerifyBatches) ToObj(data string) error { } func (*VerifyBatches) Data(log types.Log) (string, error) { - transfer := &VerifyBatches{ RollupID: uint32(event.TopicToInt64(log, 1)), Aggregator: event.TopicToAddress(log, 2).Hex(), diff --git a/pkg/unisat/unisat.go b/pkg/unisat/unisat.go index 0630535..3fa6390 100644 --- a/pkg/unisat/unisat.go +++ b/pkg/unisat/unisat.go @@ -4,23 +4,24 @@ import ( "context" "encoding/json" "fmt" - "github.com/b2network/b2committer/internal/types" - "github.com/b2network/b2committer/pkg/client" "io" "net/http" "net/url" + + "github.com/b2network/b2committer/internal/types" + "github.com/b2network/b2committer/pkg/client" ) -type UnisatHTTPClient struct { +type UstHTTPClient struct { auth string cl client.HTTP } -func NewUnisatHTTPClient(cl client.HTTP, auth string) *UnisatHTTPClient { - return &UnisatHTTPClient{auth, cl} +func NewUnisatHTTPClient(cl client.HTTP, auth string) *UstHTTPClient { + return &UstHTTPClient{auth, cl} } -func (cl *UnisatHTTPClient) apiReq(ctx context.Context, dest any, reqPath string, reqQuery url.Values) error { +func (cl *UstHTTPClient) apiReq(ctx context.Context, dest any, reqPath string, reqQuery url.Values) error { headers := http.Header{} headers.Add("Accept", "application/json") headers.Add("Authorization", "Bearer "+cl.auth) @@ -43,7 +44,7 @@ func (cl *UnisatHTTPClient) apiReq(ctx context.Context, dest any, reqPath string return nil } -func (cl *UnisatHTTPClient) QueryAPIBTCTxOutputsByTxID(ctx context.Context, txID string) (*types.APIBTCTxOutputs, error) { +func (cl *UstHTTPClient) QueryAPIBTCTxOutputsByTxID(ctx context.Context, txID string) (*types.APIBTCTxOutputs, error) { var res types.APIBTCTxOutputs err := cl.apiReq(ctx, &res, "/v1/indexer/tx/"+txID+"/outs", nil) if err != nil { @@ -52,7 +53,7 @@ func (cl *UnisatHTTPClient) QueryAPIBTCTxOutputsByTxID(ctx context.Context, txID return &res, nil } -func (cl *UnisatHTTPClient) QueryStateRootProposalByInsID(ctx context.Context, insID string) (*types.BtcStateRootProposal, error) { +func (cl *UstHTTPClient) QueryStateRootProposalByInsID(ctx context.Context, insID string) (*types.BtcStateRootProposal, error) { var res types.BtcStateRootProposal err := cl.apiReq(ctx, &res, "/v1/indexer/inscription/content/"+insID, nil) if err != nil {