prysm-pulse/sharding/internal/client_helper.go
Raul Jordan f009381a83 Refactor MockClient Into Internal Package (#210)
sharding: refactor mockClient into internal package

Former-commit-id: 5bb5142c92cffe0aeca0da06fdf0467d0570daee [formerly 4f9a27b0c3ddbbd578bf4d13f7818c7871d3d376]
Former-commit-id: b4164ac956933babeeb3918ac2a5c37613c67377
2018-06-21 16:17:14 -07:00

112 lines
2.8 KiB
Go

package internal
import (
"context"
"math/big"
"testing"
"time"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/sharding/contracts"
shardparams "github.com/ethereum/go-ethereum/sharding/params"
)
var (
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
accountBalance, _ = new(big.Int).SetString("1001000000000000000000", 10)
)
// MockClient for testing proposer.
type MockClient struct {
SMC *contracts.SMC
T *testing.T
depositFlag bool
Backend *backends.SimulatedBackend
BlockNumber int64
}
func (m *MockClient) Account() *accounts.Account {
return &accounts.Account{Address: addr}
}
func (m *MockClient) SMCCaller() *contracts.SMCCaller {
return &m.SMC.SMCCaller
}
func (m *MockClient) ChainReader() ethereum.ChainReader {
return nil
}
func (m *MockClient) SMCTransactor() *contracts.SMCTransactor {
return &m.SMC.SMCTransactor
}
func (m *MockClient) SMCFilterer() *contracts.SMCFilterer {
return &m.SMC.SMCFilterer
}
func (m *MockClient) WaitForTransaction(ctx context.Context, hash common.Hash, durationInSeconds time.Duration) error {
m.CommitWithBlock()
m.FastForward(1)
return nil
}
func (m *MockClient) TransactionReceipt(hash common.Hash) (*types.Receipt, error) {
return m.Backend.TransactionReceipt(context.Background(), hash)
}
func (m *MockClient) CreateTXOpts(value *big.Int) (*bind.TransactOpts, error) {
txOpts := TransactOpts()
txOpts.Value = value
return txOpts, nil
}
func (m *MockClient) SetDepositFlag(value bool) {
m.depositFlag = value
}
func (m *MockClient) DepositFlag() bool {
return m.depositFlag
}
func (m *MockClient) Sign(hash common.Hash) ([]byte, error) {
return nil, nil
}
func (m *MockClient) GetShardCount() (int64, error) {
return 100, nil
}
func (m *MockClient) CommitWithBlock() {
m.Backend.Commit()
m.BlockNumber = m.BlockNumber + 1
}
func (m *MockClient) FastForward(p int) {
for i := 0; i < p*int(shardparams.DefaultConfig.PeriodLength); i++ {
m.CommitWithBlock()
}
}
func TransactOpts() *bind.TransactOpts {
return bind.NewKeyedTransactor(key)
}
func SetupMockClient(t *testing.T) (*backends.SimulatedBackend, *contracts.SMC) {
backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance}})
_, _, SMC, err := contracts.DeploySMC(TransactOpts(), backend)
if err != nil {
t.Fatalf("Failed to deploy SMC contract: %v", err)
}
backend.Commit()
return backend, SMC
}