Fixed move method getBlockSingers by blockNumber to utils of contact package.

This commit is contained in:
dinhln89 2018-06-21 11:24:06 +07:00
parent 124f587abf
commit d502c6b2e4
2 changed files with 38 additions and 20 deletions

View file

@ -2,10 +2,14 @@ package contracts
import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"math/big"
)
@ -14,6 +18,18 @@ const (
HexSignMethod = "2fb1b25f"
)
// Get ethClient over IPC of current node.
func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) {
conf := ctx.GetConfig()
client, err := ethclient.Dial(conf.IPCEndpoint())
if err != nil {
log.Error("Fail to connect RPC", "error", err)
return nil, err
}
return client, nil
}
// Send tx sign for block number to smart contract blockSigner.
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block) {
// Find active account.
@ -41,3 +57,22 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
// Add tx signed to local tx pool.
pool.AddLocal(txSigned)
}
// Get signers signed for blockNumber from blockSigner contract.
func GetSignersFromContract(ctx *node.ServiceContext, blockNumber uint64) ([]common.Address, error) {
client, err := GetEthClient(ctx)
if err != nil {
log.Error("Fail to connect IPC from blockSigner", "error", err)
return nil, err
}
addr := common.HexToAddress(common.BlockSigners)
blockSigner, err := contract.NewBlockSigner(addr, client)
if err != nil {
log.Error("Fail get block signers", "error", err)
return nil, err
}
opts := new(bind.CallOpts)
addrs, err := blockSigner.GetSigners(opts, new(big.Int).SetUint64(blockNumber))
return addrs, nil
}

View file

@ -27,13 +27,12 @@ import (
"encoding/json"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/state"
@ -42,7 +41,6 @@ import (
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi"
@ -199,21 +197,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
number := header.Number.Uint64()
rCheckpoint := chain.Config().Clique.RewardCheckpoint
// Call to smart contract signer.
config := ctx.GetConfig()
client, err := ethclient.Dial(config.IPCEndpoint())
if err != nil {
log.Error("Fail to connect RPC", "error", err)
return err
}
addr := common.HexToAddress(common.BlockSigners)
blockSigner, err := contract.NewBlockSigner(addr, client)
if err != nil {
log.Error("Fail get block signer", "error", err)
return err
}
opts := new(bind.CallOpts)
prevCheckpoint := number - rCheckpoint
if number > 0 && prevCheckpoint > 0 {
@ -225,9 +208,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
for i := startBlockNumber; i <= endBlockNumber; i++ {
// Get signers in blockSigner smartcontract.
addrs, err := blockSigner.GetSigners(opts, new(big.Int).SetUint64(i))
addrs, err := contracts.GetSignersFromContract(ctx, i)
if err != nil {
log.Error("Fail to get signers from smartcontract.", "error", err)
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
return err
}
// Filter duplicate address.