common, k8s: implement ether sending for k8s ethereum

This commit is contained in:
Alan Chen 2017-09-13 11:05:13 +08:00
parent 99e2bbd192
commit f33dfd9749
4 changed files with 108 additions and 1 deletions

45
common/transactions.go Normal file
View File

@ -0,0 +1,45 @@
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package common
import (
"context"
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/getamis/istanbul-tools/client"
)
func SendEther(client *client.Client, from *ecdsa.PrivateKey, to common.Address, amount *big.Int, nonce uint64) error {
tx := types.NewTransaction(nonce, to, amount, nil, nil, []byte{})
signedTx, err := types.SignTx(tx, types.EIP155Signer{}, from)
if err != nil {
log.Error("Failed to sign transaction", "tx", tx, "err", err)
return err
}
err = client.SendRawTransaction(context.Background(), tx)
if err != nil {
log.Error("Failed to send transaction", "tx", signedTx, "nonce", nonce, "err", err)
return err
}
return nil
}

View File

@ -18,6 +18,7 @@ package k8s
import (
"context"
"crypto/ecdsa"
"errors"
"math/big"
"sync"
@ -30,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/getamis/go-ethereum/crypto"
"github.com/getamis/istanbul-tools/charts"
"github.com/getamis/istanbul-tools/client"
istcommon "github.com/getamis/istanbul-tools/common"
@ -45,6 +47,12 @@ func NewEthereum(options ...Option) *ethereum {
opt(eth)
}
var err error
eth.key, err = crypto.HexToECDSA(eth.nodekey)
if err != nil {
log.Error("Failed to create private key from nodekey", "nodekey", eth.nodekey)
return nil
}
eth.chart = charts.NewValidatorChart(eth.name, eth.args)
return eth
@ -55,6 +63,8 @@ type ethereum struct {
name string
args []string
nodekey string
key *ecdsa.PrivateKey
k8sClient *kubernetes.Clientset
}
@ -108,7 +118,7 @@ func (eth *ethereum) NodeAddress() string {
}
func (eth *ethereum) Address() common.Address {
return common.Address{}
return crypto.PubkeyToAddress(eth.key.PublicKey)
}
func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) {

View File

@ -67,6 +67,7 @@ func Mine() Option {
func NodeKeyHex(hex string) Option {
return func(eth *ethereum) {
eth.nodekey = hex
eth.args = append(eth.args, fmt.Sprintf("ethereum.nodekey.hex=%s", hex))
}
}

51
k8s/rich_man.go Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package k8s
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
istcommon "github.com/getamis/istanbul-tools/common"
)
type RichMan interface {
GiveEther(context.Context, []common.Address, *big.Int) error
}
func (eth *ethereum) GiveEther(ctx context.Context, accounts []common.Address, amount *big.Int) error {
client := eth.NewClient()
if client == nil {
return errors.New("failed to retrieve client")
}
nonce, err := client.NonceAt(context.Background(), eth.Address(), nil)
if err != nil {
log.Error("Failed to get nonce", "addr", eth.Address(), "err", err)
return err
}
for _, account := range accounts {
_ = istcommon.SendEther(client, eth.key, account, amount, nonce)
nonce++
}
return nil
}