prysm-pulse/sharding/internal/client_helper.go
Preston Van Loon 68eba02cc2 Remove most of the remaining geth code and set up bazel (#235)
* Remove most of the remaining geth code and set up bazel for this

* chmod +x

* Add flake check

* better flake detection


Former-commit-id: 5c332ecbf2923943f646f1fe40befa95be883329 [formerly 99590fc354514584700e5ce8d7d30a8a7d541f29]
Former-commit-id: e5f919b553fe698e98090965d34eb721990b5693
2018-07-07 13:23:19 -04:00

120 lines
3.2 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/prysmaticlabs/geth-sharding/sharding/contracts"
shardparams "github.com/prysmaticlabs/geth-sharding/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 (m *MockClient) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
return nil, nil
}
func (m *MockClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return types.NewBlockWithHeader(&types.Header{Number: big.NewInt(m.BlockNumber)}), nil
}
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
}