Fixed add ipc client global variable inject into ethereum instance.

This commit is contained in:
parmarrushabh 2018-09-27 11:49:51 +05:30
parent c69251ddd2
commit 3d11656c45
2 changed files with 29 additions and 20 deletions

View file

@ -8,9 +8,7 @@ import (
"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"
)
@ -24,18 +22,6 @@ type rewardLog struct {
Reward *big.Int `json:"reward"`
}
// 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) error {
if chainConfig.Clique != nil {

View file

@ -40,6 +40,7 @@ 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"
@ -93,7 +94,9 @@ type Ethereum struct {
networkId uint64
netRPCService *ethapi.PublicNetAPI
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
IPCEndpoint string
Client *ethclient.Client // Global ipc client instance.
}
func (s *Ethereum) AddLesServer(ls LesServer) {
@ -183,6 +186,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
if eth.chainConfig.Clique != nil {
c := eth.engine.(*clique.Clique)
// Set global ipc endpoint.
eth.IPCEndpoint = ctx.GetConfig().IPCEndpoint()
// Inject hook for send tx sign to smartcontract after insert block into chain.
importedHook := func(block *types.Block) {
snap, err := c.GetSnapshot(eth.blockchain, block.Header())
@ -201,15 +207,17 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
// Hook reward for clique validator.
c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error {
client, err := eth.GetClient()
if err != nil {
log.Error("Fail to connect IPC client for blockSigner", "error", err)
return err
}
number := header.Number.Uint64()
rCheckpoint := chain.Config().Clique.RewardCheckpoint
if number > 0 && number-rCheckpoint > 0 {
// Get signers in blockSigner smartcontract.
client, err := contracts.GetEthClient(ctx)
if err != nil {
log.Error("Fail to connect IPC from blockSigner", "error", err)
return err
}
addr := common.HexToAddress(common.BlockSigners)
chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether)
totalSigner := new(uint64)
@ -506,4 +514,19 @@ func (s *Ethereum) Stop() error {
close(s.shutdownChan)
return nil
}
// Get current IPC Client.
func (s *Ethereum) GetClient() (*ethclient.Client, error) {
if s.Client == nil {
// Inject ipc client global instance.
client, err := ethclient.Dial(s.IPCEndpoint)
if err != nil {
log.Error("Fail to connect RPC", "error", err)
return nil, err
}
s.Client = client
}
return s.Client, nil
}