Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Apr 29, 2024
1 parent 56ee238 commit 0f70d0c
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 52 deletions.
15 changes: 6 additions & 9 deletions internal/handler/op_committer_stateroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}

}
}

Expand Down
13 changes: 9 additions & 4 deletions internal/handler/op_committer_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/sync_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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"
"github.com/b2network/b2committer/pkg/log"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"gorm.io/gorm"
"math/big"
"time"
)

func QueryBlobOnChainAndStoreInLocal(ctx *svc.ServiceContext) {
Expand Down Expand Up @@ -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 {
Expand All @@ -92,6 +92,7 @@ func QueryBlobOnChainAndStoreInLocal(ctx *svc.ServiceContext) {
continue
}
}
continue
}

for _, bif := range blobInfos {
Expand Down
7 changes: 4 additions & 3 deletions internal/svc/svc.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -17,8 +20,6 @@ import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"math/big"
"time"
)

var svc *ServiceContext
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 9 additions & 8 deletions internal/types/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package types

import (
"encoding/json"

"github.com/b2network/b2committer/internal/schema"
)

type DsTxsProposal struct {
ChainID int64
ProposalID uint64
TxsRoot string
Blobs *[]DsBlob
Blobs []*DsBlob
}

type DsStateRootProposal struct {
Expand Down Expand Up @@ -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

Check failure on line 51 in internal/types/ds.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

Consider pre-allocating `dsBlobs` (prealloc)
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

Check failure on line 62 in internal/types/ds.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

Consider pre-allocating `dbBlobs` (prealloc)
for _, blob := range *b.Blobs {
for _, blob := range b.Blobs {
dbBlobs = append(dbBlobs, schema.BlobInfo{
BlockNumber: blob.BlockID,
Blob: blob.Blob,
Expand All @@ -68,15 +69,15 @@ 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
}
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
Expand Down
1 change: 1 addition & 0 deletions internal/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"

"github.com/b2network/b2committer/internal/schema"
)

Expand Down
11 changes: 9 additions & 2 deletions pkg/b2node/b2node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ 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"
"github.com/ethereum/go-ethereum/common"
"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 {
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion pkg/b2node/committer.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 0f70d0c

Please sign in to comment.