Skip to content

Commit

Permalink
change nodeclient
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Mar 8, 2024
1 parent 17d2f1d commit 3015ad9
Show file tree
Hide file tree
Showing 10 changed files with 1,057 additions and 754 deletions.
1,670 changes: 968 additions & 702 deletions abi/Committer.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docs/sql/listener.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ CREATE TABLE `proposal` (
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`proposal_id` bigint NOT NULL,
`proposer` varchar(128) NOT NULL,
`state_root_hash` varchar(128) NOT NULL,
`proof_root_hash` varchar(128) NOT NULL,
`start_batch_num` bigint NOT NULL,
`end_batch_num` bigint NOT NULL,
`btc_commit_tx_hash` varchar(128) ,
`btc_reveal_tx_hash` varchar(128) ,
`block_height` bigint NOT NULL DEFAULT 0,
`winner` varchar(128),
`status` bigint NOT NULL DEFAULT 0,
`upload_details` tinyint(1) default 0,
Expand Down
25 changes: 12 additions & 13 deletions internal/handler/checkstatus.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"fmt"
"time"

"github.com/b2network/b2committer/internal/schema"
Expand Down Expand Up @@ -49,17 +48,17 @@ func CheckStatusTimeOut(ctx *svc.ServiceContext) {
log.Errorf("[Handler.CheckStatusTimeOut] QueryProposalByID err: %s\n", errors.WithStack(err))
continue
}
fmt.Println(proposal)
//if proposal.TxHash == "" && proposal.Status == schema.ProposalPendingStatus && proposal.Winner.String() != ctx.B2NodeConfig.Address {
// num := uint64(ctx.LatestBlockNumber) - proposal.BlockHight
// if num > 10000 {
// err := ctx.NodeClient.TimeoutProposal(proposal.Id)
// if err != nil {
// log.Errorf("[Handler.CheckStatusTimeOut] TimeoutProposal err: %s\n", errors.WithStack(err))
// continue
// }
// }
// time.Sleep(2 * time.Second)
//}
if proposal.TxHash == "" && proposal.Status == schema.ProposalPendingStatus && proposal.Winner.String() != ctx.B2NodeConfig.Address {
res, err := ctx.NodeClient.IsProposalTimeout(proposal.Id)
if err != nil {
log.Errorf("[Handler.CheckStatusTimeOut] TimeoutProposal err: %s\n", errors.WithStack(err))
continue
}
if res {
dbProposal.Status = schema.ProposalTimeoutStatus
ctx.DB.Save(dbProposal)
}
time.Sleep(2 * time.Second)
}
}
}
8 changes: 4 additions & 4 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func Run(ctx *svc.ServiceContext) {
go CheckStatus(ctx)
// check and inscribe
go Inscribe(ctx)
// check time out
//// check time out
go CheckStatusTimeOut(ctx)
// sync proposal
//// sync proposal
go SyncProposal(ctx)
// sequence batches
go SequenceBatches(ctx)
//// sequence batches
//go SequenceBatches(ctx)

Check failure on line 26 in internal/handler/handler.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
}
44 changes: 22 additions & 22 deletions internal/handler/syncProposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,28 @@ func SyncProposal(ctx *svc.ServiceContext) {
time.Sleep(3 * time.Second)
continue
}
if dbProposal.ProposalID != 0 {
if errors.Is(err, gorm.ErrRecordNotFound) {
dbProposal = schema.Proposal{
Base: schema.Base{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
ProposalID: proposal.Id,
StateRootHash: proposal.StateRootHash,
ProofRootHash: proposal.ProofHash,
StartBatchNum: proposal.StartIndex,
EndBatchNum: proposal.EndIndex,
BtcRevealTxHash: proposal.TxHash,
Winner: proposal.Winner.String(),
Status: uint64(proposal.Status),
}
err = ctx.DB.Create(&dbProposal).Error
if err != nil {
log.Errorf("[Handler.SyncProposal] db create error info:", errors.WithStack(err))
}
}

if dbProposal.ProposalID != 0 && dbProposal.Status != uint64(schema.ProposalVotingStatus) {
log.Infof("[Handler.SyncProposal] already voted :", ctx.B2NodeConfig.Address)
proposalID++
continue
Expand All @@ -67,27 +88,6 @@ func SyncProposal(ctx *svc.ServiceContext) {
time.Sleep(3 * time.Second)
continue
}

dbProposal := &schema.Proposal{
Base: schema.Base{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
EndBatchNum: verifyBatchInfo.endBatchNum,
ProposalID: proposal.Id,
Status: schema.ProposalVotingStatus,
StateRootHash: verifyBatchInfo.stateRootHash,
ProofRootHash: verifyBatchInfo.proofRootHash,
StartBatchNum: verifyBatchInfo.startBatchNum,
}

// store db
err = ctx.DB.Save(dbProposal).Error
if err != nil {
log.Errorf("[Handler.SyncProposal] vote proposal error info", errors.WithStack(err))
time.Sleep(3 * time.Second)
continue
}
proposalID++
continue
}
Expand Down
1 change: 0 additions & 1 deletion internal/schema/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Proposal struct {
EndBatchNum uint64 `json:"end_batch_num"`
BtcCommitTxHash string `json:"btc_commit_tx_hash"`
BtcRevealTxHash string `json:"btc_reveal_tx_hash"`
BlockHeight uint64 `json:"block_height"`
Winner string `json:"winner"`
Status uint64 `json:"status"`
}
Expand Down
12 changes: 6 additions & 6 deletions internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ type Config struct {
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
// "console","json"
LogFormat string `env:"LOG_FORMAT" envDefault:"console"`
MySQLDataSource string `env:"MYSQL_DATA_SOURCE" envDefault:"root:root@tcp(127.0.0.1:3306)/b2_committer3?charset=utf8mb4&parseTime=True&loc=Local&multiStatements=true"`
MySQLDataSource string `env:"MYSQL_DATA_SOURCE" envDefault:"root:root@tcp(127.0.0.1:3366)/b2_committer?charset=utf8mb4&parseTime=True&loc=Local&multiStatements=true"`
MySQLMaxIdleConns int `env:"MYSQL_MAX_IDLE_CONNS" envDefault:"10"`
MySQLMaxOpenConns int `env:"MYSQL_MAX_OPEN_CONNS" envDefault:"20"`
MySQLConnMaxLifetime int `env:"MYSQL_CONN_MAX_LIFETIME" envDefault:"3600"`
RPCUrl string `env:"RPC_URL" envDefault:"https://habitat-b2-nodes.bsquared.network"`
RPCUrl string `env:"RPC_URL" envDefault:"https://ethereum-goerli-rpc.publicnode.com"`
Blockchain string `env:"BLOCKCHAIN" envDefault:"b2-node"`
InitBlockNumber int64 `env:"INIT_BLOCK_NUMBER" envDefault:"70000"`
InitBlockHash string `env:"INIT_BLOCK_HASH" envDefault:"0xb2fa3c8011ce25bb1d261403107b58b6aeda8a2af3827e86ad70ee081966d99c"`
InitBlockNumber int64 `env:"INIT_BLOCK_NUMBER" envDefault:"10619156"`
InitBlockHash string `env:"INIT_BLOCK_HASH" envDefault:"0x401b2ba6be4133f37485c9af320be4ca88b2972523d7d816b463fb772444de10"`
PolygonZKEVMAddress string `env:"POLYGON_ZKEVM_ADDRESS" envDefault:"0xa997cfD539E703921fD1e3Cf25b4c241a27a4c7A"`
LimitNum int `evn:"PROPOSAL_BATCHES_LIMITNUM" envDefault:"10"`
InitProposalID uint64 `evn:"INIT_PROPOSAL_ID" envDefault:"1"`
Expand All @@ -26,8 +26,8 @@ type Config struct {
type B2NODEConfig struct {
ChainID int64 `env:"B2NODE_CHAIN_ID" envDefault:"11155111"`
RPCUrl string `env:"B2NODE_RPC_URL" envDefault:"https://eth-sepolia.g.alchemy.com/v2/lV2e-64nNnEMUA7UG0IT0uwjzlxEI512"`
CommitterAddress string `env:"B2NODE_COMMITTER_ADDRESS" envDefault:"0xBf0Fe27C50f7cf155888bf24Bf0a5104Db926661"`
Address string `env:"B2NODE_CREATOR_ADDRESS" envDefault:"0x0DD3684F0C7e6b383C7bEc2901dCDa4b5360D893"`
CommitterAddress string `env:"B2NODE_COMMITTER_ADDRESS" envDefault:"0x9bb873a9772D2356192C6D299ABcfa1F69F5b3e4"`
Address string `env:"B2NODE_CREATOR_ADDRESS" envDefault:"0xb634434CA448c39b05b460dEC51f458EaC1e2759"`
PrivateKey string `env:"B2NODE_CREATOR_PRIVATE_KEY" envDefault:"0a81baab0ca0b65d406d68c79945054b092cbe77499ca55c57b3ecfd33f1d551"`
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/b2node/b2node.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ func (n NodeClient) TimeoutProposal(id uint64) (*types.Transaction, error) {
return tx, nil
}

func (n NodeClient) IsProposalTimeout(id uint64) (bool, error) {
res, err := n.Committer.IsProposalTimeout(&bind.CallOpts{
From: common.HexToAddress(n.Address),
}, id)
if err != nil {
return false, fmt.Errorf("[IsProposalTimeout] err: %s", err)
}
return res, nil
}

//
//func (n NodeClient) GetAccountInfo(address string) (*eTypes.EthAccount, error) {

Check failure on line 138 in pkg/b2node/b2node.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
// authClient := authTypes.NewQueryClient(n.GrpcConn)
Expand Down
6 changes: 3 additions & 3 deletions pkg/b2node/b2node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const (
creatorAddress = "0xb634434CA448c39b05b460dEC51f458EaC1e2759"
creatorPrivateKey = "0a81baab0ca0b65d406d68c79945054b092cbe77499ca55c57b3ecfd33f1d551"
contractAddress = "0xb5f343da85c2FdA56811f628A537244793280F63"
contractAddress = "0x9bb873a9772D2356192C6D299ABcfa1F69F5b3e4"
URL = "https://eth-sepolia.g.alchemy.com/v2/lV2e-64nNnEMUA7UG0IT0uwjzlxEI512"
chainID = 11155111
)
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestQueryProposalByID(t *testing.T) {
require.NoError(t, err)
proposal, err := committer.Proposal(&bind.CallOpts{
From: common.HexToAddress(creatorAddress),
}, 1)
}, 3)
require.NoError(t, err)
fmt.Println(proposal)
}
Expand All @@ -143,7 +143,7 @@ func TestSubmitProof(t *testing.T) {
tx, err := committer.SubmitProof(&bind.TransactOpts{
From: common.HexToAddress(creatorAddress),
Signer: auth.Signer,
}, 1, "proofHash", "stateRoot", 1, 10)
}, 1, "proofHash", "stateRoot", 1, 718154)
require.NoError(t, err)
fmt.Println(tx.Hash())
}
Expand Down
Loading

0 comments on commit 3015ad9

Please sign in to comment.