From ed8de4f2b4f2485173914a52659ae04bec4dd174 Mon Sep 17 00:00:00 2001 From: dinhln89 Date: Mon, 25 Jun 2018 17:03:05 +0700 Subject: [PATCH 1/3] Add unit test for calculate reward for signers at reward checkpoint. --- contracts/utils.go | 68 +++++++++++++++++++++++++++++++++++++++-- contracts/utils_test.go | 20 ++++++++++++ eth/backend.go | 65 ++++++--------------------------------- 3 files changed, 95 insertions(+), 58 deletions(-) diff --git a/contracts/utils.go b/contracts/utils.go index 74dbacb5d7..cb81c5b59d 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -1,6 +1,7 @@ package contracts import ( + "encoding/json" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -68,9 +69,8 @@ func CreateTxSign(blockNumber *big.Int, nonce uint64, blockSigner common.Address } // Get signers signed for blockNumber from blockSigner contract. -func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) { - addr := common.HexToAddress(common.BlockSigners) - blockSigner, err := contract.NewBlockSigner(addr, client) +func GetSignersFromContract(addrBlockSigner common.Address, client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) { + blockSigner, err := contract.NewBlockSigner(addrBlockSigner, client) if err != nil { log.Error("Fail get instance of blockSigner", "error", err) return nil, err @@ -84,3 +84,65 @@ func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([] return addrs, nil } + +// Calculate reward for reward checkpoint. +func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend) (map[common.Address]*big.Int, error) { + type rewardLog struct { + Sign uint64 `json:"sign"` + Reward *big.Int `json:"reward"` + } + + // Not reward for singer of genesis block and only calculate reward at checkpoint block. + startBlockNumber := number - (rCheckpoint * 2) + 1 + endBlockNumber := startBlockNumber + rCheckpoint - 1 + signers := make(map[common.Address]*rewardLog) + totalSigner := uint64(0) + + for i := startBlockNumber; i <= endBlockNumber; i++ { + addrs, err := GetSignersFromContract(blockSignerAddr, client, i) + if err != nil { + log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i) + return nil, err + } + // Filter duplicate address. + if len(addrs) > 0 { + addrSigners := make(map[common.Address]bool) + for _, addr := range addrs { + if _, ok := addrSigners[addr]; !ok { + addrSigners[addr] = true + } + } + for addr := range addrSigners { + _, exist := signers[addr] + if exist { + signers[addr].Sign++ + } else { + signers[addr] = &rewardLog{1, new(big.Int)} + } + totalSigner++ + } + } + } + + resultSigners := make(map[common.Address]*big.Int) + // Add reward for signer. + calcReward := new(big.Int) + // Add reward for signers. + for signer, rLog := range signers { + calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) + calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) + rLog.Reward = calcReward + + resultSigners[signer] = calcReward + } + + jsonSigners, err := json.Marshal(signers) + if err != nil { + log.Error("Fail to parse json signers", "error", err) + return nil, err + } + + log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward) + + return resultSigners, nil +} diff --git a/contracts/utils_test.go b/contracts/utils_test.go index c5bca88b09..6832dd2928 100644 --- a/contracts/utils_test.go +++ b/contracts/utils_test.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" "math/big" "math/rand" "testing" @@ -77,4 +78,23 @@ func TestSendTxSign(t *testing.T) { t.Error("Tx sign for block validators not match") } } + + // Unit test for reward checkpoint. + rCheckpoint := uint64(10) + chainReward := new(big.Int).SetUint64(15 * params.Ether) + for i := uint64(0); i < 100; i++ { + if i > 0 && i%rCheckpoint == 0 && i-rCheckpoint > 0 { + signers, err := GetRewardForCheckpoint(chainReward, blockSignerAddr, i, rCheckpoint, backend) + if err != nil { + t.Errorf("Fail to get signers for reward checkpoint: %v", err) + } + rewards := new(big.Int) + for _, reward := range signers { + rewards.Add(rewards, reward) + } + if rewards.Cmp(chainReward) != 0 { + t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards) + } + } + } } diff --git a/eth/backend.go b/eth/backend.go index 138fde4fcb..9a198f5cd7 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -25,7 +25,6 @@ import ( "sync" "sync/atomic" - "encoding/json" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -190,73 +189,29 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { c := eth.engine.(*clique.Clique) // Hook reward for clique validator. c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error { - type rewardLog struct { - Sign uint64 `json:"sign"` - Reward float64 `json:"reward"` - } - number := header.Number.Uint64() rCheckpoint := chain.Config().Clique.RewardCheckpoint - prevCheckpoint := number - rCheckpoint - - if number > 0 && prevCheckpoint > 0 { - // Not reward for singer of genesis block and only calculate reward at checkpoint block. - startBlockNumber := number - (rCheckpoint * 2) + 1 - endBlockNumber := startBlockNumber + rCheckpoint - 1 - signers := make(map[common.Address]*rewardLog) - totalSigner := uint64(0) + 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 } - - for i := startBlockNumber; i <= endBlockNumber; i++ { - addrs, err := contracts.GetSignersFromContract(client, i) - if err != nil { - log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i) - return err - } - // Filter duplicate address. - if len(addrs) > 0 { - addrSigners := make(map[common.Address]bool) - for _, addr := range addrs { - if _, ok := addrSigners[addr]; ok { - } else { - addrSigners[addr] = true - } - } - for addr := range addrSigners { - _, exist := signers[addr] - if exist { - signers[addr].Sign++ - } else { - signers[addr] = &rewardLog{1, 0} - } - totalSigner++ - } - } - } - + addr := common.HexToAddress(common.BlockSigners) chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) - // Add reward for signer. - calcReward := new(big.Int) - // Add reward for signers. - for signer, rLog := range signers { - calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) - calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) - rLog.Reward = float64(calcReward.Int64()) - state.AddBalance(signer, calcReward) - } - jsonSigners, err := json.Marshal(signers) + signers, err := contracts.GetRewardForCheckpoint(chainReward, addr, number, rCheckpoint, client) if err != nil { - log.Error("Fail to parse json signers", "error", err) - return err + log.Error("Fail to get signers for reward checkpoint", "error", err) } - log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward) + // Add reward for signers. + if len(signers) > 0 { + for signer, calcReward := range signers { + state.AddBalance(signer, calcReward) + } + } } return nil From 68697e970bc4ce05ae8bdf02495e63736bd759a3 Mon Sep 17 00:00:00 2001 From: dinhln89 Date: Tue, 26 Jun 2018 16:25:10 +0700 Subject: [PATCH 2/3] Fixed bug calculate reward and add unit test for it. --- contracts/utils.go | 72 ++++++++++++++++++++++------------------- contracts/utils_test.go | 39 +++++++++++++++------- eth/backend.go | 33 +++++++++++++------ eth/fetcher/fetcher.go | 14 ++------ eth/handler.go | 4 +-- miner/worker.go | 10 ++++++ 6 files changed, 106 insertions(+), 66 deletions(-) diff --git a/contracts/utils.go b/contracts/utils.go index cb81c5b59d..69e9c5561e 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -19,6 +19,11 @@ const ( HexSignMethod = "2fb1b25f" ) +type rewardLog struct { + Sign uint64 `json:"sign"` + Reward *big.Int `json:"reward"` +} + // Get ethClient over IPC of current node. func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) { conf := ctx.GetConfig() @@ -33,27 +38,29 @@ func GetEthClient(ctx *node.ServiceContext) (*ethclient.Client, error) { // 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 { - // Find active account. - account := accounts.Account{} - var wallet accounts.Wallet - if wallets := manager.Wallets(); len(wallets) > 0 { - wallet = wallets[0] - if accts := wallets[0].Accounts(); len(accts) > 0 { - account = accts[0] + if chainConfig.Clique != nil { + // Find active account. + account := accounts.Account{} + var wallet accounts.Wallet + if wallets := manager.Wallets(); len(wallets) > 0 { + wallet = wallets[0] + if accts := wallets[0].Accounts(); len(accts) > 0 { + account = accts[0] + } } - } - // Create and send tx to smart contract for sign validate block. - nonce := pool.State().GetNonce(account.Address) - tx := CreateTxSign(block.Number(), nonce, common.HexToAddress(common.BlockSigners)) - txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId) - if err != nil { - log.Error("Fail to create tx sign", "error", err) - return err - } + // Create and send tx to smart contract for sign validate block. + nonce := pool.State().GetNonce(account.Address) + tx := CreateTxSign(block.Number(), nonce, common.HexToAddress(common.BlockSigners)) + txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId) + if err != nil { + log.Error("Fail to create tx sign", "error", err) + return err + } - // Add tx signed to local tx pool. - pool.AddLocal(txSigned) + // Add tx signed to local tx pool. + pool.AddLocal(txSigned) + } return nil } @@ -86,17 +93,11 @@ func GetSignersFromContract(addrBlockSigner common.Address, client bind.Contract } // Calculate reward for reward checkpoint. -func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend) (map[common.Address]*big.Int, error) { - type rewardLog struct { - Sign uint64 `json:"sign"` - Reward *big.Int `json:"reward"` - } - +func GetRewardForCheckpoint(blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend, totalSigner *uint64) (map[common.Address]*rewardLog, error) { // Not reward for singer of genesis block and only calculate reward at checkpoint block. startBlockNumber := number - (rCheckpoint * 2) + 1 endBlockNumber := startBlockNumber + rCheckpoint - 1 signers := make(map[common.Address]*rewardLog) - totalSigner := uint64(0) for i := startBlockNumber; i <= endBlockNumber; i++ { addrs, err := GetSignersFromContract(blockSignerAddr, client, i) @@ -119,30 +120,35 @@ func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address } else { signers[addr] = &rewardLog{1, new(big.Int)} } - totalSigner++ + *totalSigner++ } } } + log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber) + + return signers, nil +} + +// Calculate reward for signers. +func CalculateReward(chainReward *big.Int, signers map[common.Address]*rewardLog, totalSigner uint64) (map[common.Address]*big.Int, error) { resultSigners := make(map[common.Address]*big.Int) - // Add reward for signer. - calcReward := new(big.Int) // Add reward for signers. for signer, rLog := range signers { - calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) - calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) + // Add reward for signer. + calcReward := new(big.Int) + calcReward.Div(chainReward, new(big.Int).SetUint64(totalSigner)) + calcReward.Mul(calcReward, new(big.Int).SetUint64(rLog.Sign)) rLog.Reward = calcReward resultSigners[signer] = calcReward } - jsonSigners, err := json.Marshal(signers) if err != nil { log.Error("Fail to parse json signers", "error", err) return nil, err } - - log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward) + log.Info("Signers data", "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward) return resultSigners, nil } diff --git a/contracts/utils_test.go b/contracts/utils_test.go index 6832dd2928..d9a3a8e7fd 100644 --- a/contracts/utils_test.go +++ b/contracts/utils_test.go @@ -20,11 +20,13 @@ func TestSendTxSign(t *testing.T) { acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") acc3Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + acc4Key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a") acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey) acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey) acc3Addr := crypto.PubkeyToAddress(acc3Key.PublicKey) - accounts := []common.Address{acc2Addr, acc3Addr} - keys := []*ecdsa.PrivateKey{acc2Key, acc3Key} + acc4Addr := crypto.PubkeyToAddress(acc4Key.PublicKey) + accounts := []common.Address{acc2Addr, acc3Addr, acc4Addr} + keys := []*ecdsa.PrivateKey{acc2Key, acc3Key, acc4Key} signer := types.HomesteadSigner{} genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000)}} @@ -50,16 +52,19 @@ func TestSendTxSign(t *testing.T) { } // Tx sign for signer. + signCount := uint64(0) for i := uint64(0); i < 100; i++ { randIndex := rand.Intn(len(keys)) accKey := keys[randIndex] signTx(ctx, backend, signer, nonces, accKey, i) oldBlock[i] = accounts[randIndex] + signCount++ // Tx sign for validators. for _, key := range keys { if key != accKey { signTx(ctx, backend, signer, nonces, key, i) + signCount++ } } } @@ -80,21 +85,33 @@ func TestSendTxSign(t *testing.T) { } // Unit test for reward checkpoint. - rCheckpoint := uint64(10) + rCheckpoint := uint64(5) chainReward := new(big.Int).SetUint64(15 * params.Ether) + total := new(uint64) for i := uint64(0); i < 100; i++ { if i > 0 && i%rCheckpoint == 0 && i-rCheckpoint > 0 { - signers, err := GetRewardForCheckpoint(chainReward, blockSignerAddr, i, rCheckpoint, backend) + _, err := GetRewardForCheckpoint(blockSignerAddr, i, rCheckpoint, backend, total) if err != nil { t.Errorf("Fail to get signers for reward checkpoint: %v", err) } - rewards := new(big.Int) - for _, reward := range signers { - rewards.Add(rewards, reward) - } - if rewards.Cmp(chainReward) != 0 { - t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards) - } } } + + signers := make(map[common.Address]*rewardLog) + totalSigner := uint64(17) + signers[common.HexToAddress("0x12f588d7d03bb269b382b842fc15d874e8c055a7")] = &rewardLog{5, new(big.Int).SetUint64(0)} + signers[common.HexToAddress("0x1f9e122c0921a4504fc116d967baf7a7bf2604ef")] = &rewardLog{6, new(big.Int).SetUint64(0)} + signers[common.HexToAddress("0xea489e4e673c25ff0614617ebe88efd853efe00c")] = &rewardLog{6, new(big.Int).SetUint64(0)} + rewardSigners, err := CalculateReward(chainReward, signers, totalSigner) + if err != nil { + t.Errorf("Fail to calculate reward for signers: %v", err) + } + //t.Error("Reward", rewardSigners) + rewards := new(big.Int) + for _, reward := range rewardSigners { + rewards.Add(rewards, reward) + } + if rewards.Cmp(new(big.Int).SetUint64(14999999999999999996)) != 0 { + t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards) + } } diff --git a/eth/backend.go b/eth/backend.go index 9a198f5cd7..ffaeab9df1 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -180,13 +180,25 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams) - // Inject hook for send tx sign to smartcontract after insert block into chain. - if eth.chainConfig.Clique != nil { - eth.protocolManager.fetcher.HookCreateTxSign(eth.chainConfig, eth.TxPool(), eth.AccountManager()) - } - if eth.chainConfig.Clique != nil { c := eth.engine.(*clique.Clique) + + // 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()) + if err != nil { + log.Error("Fail to get snapshot for sign tx validator.") + return + } + if _, authorized := snap.Signers[eth.etherbase]; authorized { + if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block); err != nil { + log.Error("Fail to create tx sign for imported block", "error", err) + return + } + } + } + eth.protocolManager.fetcher.SetImportedHook(importedHook) + // Hook reward for clique validator. c.HookReward = func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error { number := header.Number.Uint64() @@ -200,15 +212,18 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } addr := common.HexToAddress(common.BlockSigners) chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) - - signers, err := contracts.GetRewardForCheckpoint(chainReward, addr, number, rCheckpoint, client) + totalSigner := new(uint64) + signers, err := contracts.GetRewardForCheckpoint(addr, number, rCheckpoint, client, totalSigner) if err != nil { log.Error("Fail to get signers for reward checkpoint", "error", err) } - + rewardSigners, err := contracts.CalculateReward(chainReward, signers, *totalSigner) + if err != nil { + log.Error("Fail to calculate reward for signers", "error", err) + } // Add reward for signers. if len(signers) > 0 { - for signer, calcReward := range signers { + for signer, calcReward := range rewardSigners { state.AddBalance(signer, calcReward) } } diff --git a/eth/fetcher/fetcher.go b/eth/fetcher/fetcher.go index 732705abde..4f0c916f7b 100644 --- a/eth/fetcher/fetcher.go +++ b/eth/fetcher/fetcher.go @@ -22,14 +22,10 @@ import ( "math/rand" "time" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" - "github.com/ethereum/go-ethereum/contracts" - "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/params" "gopkg.in/karalabe/cookiejar.v2/collections/prque" ) @@ -739,11 +735,7 @@ func (f *Fetcher) forgetBlock(hash common.Hash) { } } -// Create tx for sign to smartcontract after import block into chain. -func (f *Fetcher) HookCreateTxSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager) { - f.importedHook = func(block *types.Block) { - if err := contracts.CreateTransactionSign(chainConfig, pool, manager, block); err != nil { - log.Error("Fail to create tx sign for imported block", "error", err) - } - } +// Bind import hook when block imported into chain. +func (f *Fetcher) SetImportedHook(importedHook func(*types.Block)) { + f.importedHook = importedHook } diff --git a/eth/handler.go b/eth/handler.go index 3fae0cd00d..d46e7689f3 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -332,7 +332,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Status messages should never arrive after the handshake return errResp(ErrExtraStatusMsg, "uncontrolled status message") - // Block header query, collect the requested headers and reply + // Block header query, collect the requested headers and reply case msg.Code == GetBlockHeadersMsg: // Decode the complex header query var query getBlockHeadersData @@ -742,7 +742,7 @@ func (self *ProtocolManager) txBroadcastLoop() { case event := <-self.txCh: self.BroadcastTx(event.Tx.Hash(), event.Tx) - // Err() channel will be closed when unsubscribing. + // Err() channel will be closed when unsubscribing. case <-self.txSub.Err(): return } diff --git a/miner/worker.go b/miner/worker.go index b9c588054b..7208729267 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -341,6 +341,16 @@ func (self *worker) wait() { } if self.config.Clique != nil { + c := self.engine.(*clique.Clique) + snap, err := c.GetSnapshot(self.chain, block.Header()) + if err != nil { + log.Error("Fail to get snapshot for sign tx signer.") + return + } + if _, authorized := snap.Signers[self.coinbase]; !authorized { + log.Error("Coinbase address not in snapshot signers.") + return + } // Send tx sign to smart contract blockSigners. if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block); err != nil { log.Error("Fail to create tx sign for signer", "error", "err") From 3097babcaf5f6e76740f14f52e1a7fb4c3d3f677 Mon Sep 17 00:00:00 2001 From: dinhln89 Date: Thu, 28 Jun 2018 15:35:58 +0700 Subject: [PATCH 3/3] Fixed add ipc client global variable inject into ethereum instance. --- contracts/utils.go | 14 -------------- eth/backend.go | 35 +++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/contracts/utils.go b/contracts/utils.go index 69e9c5561e..ed8a7b8b13 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -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 { diff --git a/eth/backend.go b/eth/backend.go index ffaeab9df1..c57423627d 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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) @@ -507,3 +515,18 @@ func (s *Ethereum) Stop() error { 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 +}