Fixed move method getBlockSingers by blockNumber to utils of contact …

This commit is contained in:
parmarrushabh 2018-08-31 16:37:52 +05:30
parent f0b6c70a8e
commit 9551a39a97
4 changed files with 82 additions and 63 deletions

View file

@ -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

View file

@ -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)

78
contracts/utils.go Normal file
View file

@ -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
}

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.