From 9551a39a97328f015c06c32f5c03051ed8500ace Mon Sep 17 00:00:00 2001 From: parmarrushabh Date: Fri, 31 Aug 2018 16:37:52 +0530 Subject: [PATCH] =?UTF-8?q?Fixed=20move=20method=20getBlockSingers=20by=20?= =?UTF-8?q?blockNumber=20to=20utils=20of=20contact=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- contracts/XDC.go | 42 ------------------------- contracts/utils.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++ eth/backend.go | 23 ++------------ 4 files changed, 82 insertions(+), 63 deletions(-) delete mode 100644 contracts/XDC.go create mode 100644 contracts/utils.go diff --git a/Dockerfile b/Dockerfile index df296a9bb6..938cfc8515 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ FROM alpine:latest LABEL maintainer="admin@xinfin.org" -COPY --from=builder /tomochain/build/bin/XDC /usr/local/bin/XDC +COPY --from=builder /XDCchain/build/bin/XDC /usr/local/bin/XDC RUN chmod +x /usr/local/bin/XDC diff --git a/contracts/XDC.go b/contracts/XDC.go deleted file mode 100644 index ebbbc24272..0000000000 --- a/contracts/XDC.go +++ /dev/null @@ -1,42 +0,0 @@ -package contracts - - import ( - "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/common" - "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" - "math/big" -) - - const ( - HexSignMethod = "2fb1b25f" -) - - // 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. - 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. - blockHex := common.LeftPadBytes(block.Number().Bytes(), 32) - data := common.Hex2Bytes(HexSignMethod) - inputData := append(data, blockHex...) - nonce := pool.State().GetNonce(account.Address) - tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), inputData) - txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId) - if err != nil { - log.Error("Fail to create tx sign", "error", err) - return - } - - // Add tx signed to local tx pool. - pool.AddLocal(txSigned) \ No newline at end of file diff --git a/contracts/utils.go b/contracts/utils.go new file mode 100644 index 0000000000..e40e1da0be --- /dev/null +++ b/contracts/utils.go @@ -0,0 +1,78 @@ +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" +) + +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. + 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. + blockHex := common.LeftPadBytes(block.Number().Bytes(), 32) + data := common.Hex2Bytes(HexSignMethod) + inputData := append(data, blockHex...) + nonce := pool.State().GetNonce(account.Address) + tx := types.NewTransaction(nonce, common.HexToAddress(common.BlockSigners), big.NewInt(0), 100000, big.NewInt(0), inputData) + txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId) + if err != nil { + log.Error("Fail to create tx sign", "error", err) + return + } + + // 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 +} diff --git a/eth/backend.go b/eth/backend.go index 5306a7b65c..d4a143ce83 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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.