2018-02-06 00:50:32 +00:00
|
|
|
package sharding
|
|
|
|
|
|
|
|
import (
|
2018-02-07 01:06:47 +00:00
|
|
|
"context"
|
2018-02-08 02:00:20 +00:00
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
2018-02-06 00:50:32 +00:00
|
|
|
"testing"
|
2018-02-07 01:06:47 +00:00
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/sharding/contracts"
|
|
|
|
|
2018-02-07 01:06:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-02-06 00:50:32 +00:00
|
|
|
)
|
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
type FakeCollatorClient struct {
|
|
|
|
accountAccount *accounts.Account
|
|
|
|
accountError error
|
|
|
|
chainReader FakeChainReader
|
|
|
|
contractCaller FakeContractCaller
|
|
|
|
}
|
|
|
|
|
2018-02-27 17:14:00 +00:00
|
|
|
func (c FakeCollatorClient) Account() *accounts.Account {
|
|
|
|
return c.accountAccount
|
2018-02-07 01:06:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
func (c FakeCollatorClient) ChainReader() ethereum.ChainReader {
|
|
|
|
return c.chainReader
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
func (c FakeCollatorClient) SMCCaller() *contracts.SMCCaller {
|
|
|
|
SMCCaller, err := contracts.NewSMCCaller(common.HexToAddress("0x0"), c.contractCaller)
|
2018-02-08 02:00:20 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-08 17:34:15 +00:00
|
|
|
return SMCCaller
|
2018-02-08 02:00:20 +00:00
|
|
|
}
|
2018-02-06 04:41:40 +00:00
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
type FakeChainReader struct {
|
|
|
|
subscribeNewHeadSubscription ethereum.Subscription
|
|
|
|
subscribeNewHeadError error
|
|
|
|
}
|
2018-02-07 01:06:47 +00:00
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
func (r FakeChainReader) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
|
|
|
|
return r.subscribeNewHeadSubscription, r.subscribeNewHeadError
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
func (r FakeChainReader) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type FakeContractCaller struct {
|
|
|
|
codeAtBytes []byte
|
|
|
|
codeAtError error
|
|
|
|
callContractBytes []byte
|
|
|
|
callContractError error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c FakeContractCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return c.codeAtBytes, c.codeAtError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c FakeContractCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return c.callContractBytes, c.callContractError
|
|
|
|
}
|
|
|
|
|
2018-03-09 02:05:39 +00:00
|
|
|
func TestCheckSMCForCollator(t *testing.T) {
|
2018-02-08 02:00:20 +00:00
|
|
|
tests := []struct {
|
|
|
|
Name string
|
|
|
|
Head *types.Header
|
|
|
|
ExpectedPeriod *big.Int
|
|
|
|
ExpectedError string
|
|
|
|
CollatorClient FakeCollatorClient
|
|
|
|
}{
|
|
|
|
{
|
2018-03-08 17:34:15 +00:00
|
|
|
Name: "SMCCaller.checkSMCForCollator should return an error",
|
2018-02-08 02:00:20 +00:00
|
|
|
ExpectedError: "there is no cake",
|
|
|
|
CollatorClient: FakeCollatorClient{
|
|
|
|
accountAccount: &accounts.Account{},
|
|
|
|
contractCaller: FakeContractCaller{
|
|
|
|
callContractError: errors.New("there is no cake"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Head: &types.Header{Number: big.NewInt(100)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.Name, func(t *testing.T) {
|
2018-03-08 17:34:15 +00:00
|
|
|
if err := checkSMCForCollator(tt.CollatorClient, tt.Head); !strings.Contains(safeError(err), tt.ExpectedError) {
|
2018-02-08 02:00:20 +00:00
|
|
|
t.Fatalf("Incorrect error! Wanted %v, got %v", tt.ExpectedError, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-02-06 04:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 02:00:20 +00:00
|
|
|
func safeError(err error) string {
|
2018-02-07 01:06:47 +00:00
|
|
|
if err != nil {
|
2018-02-08 02:00:20 +00:00
|
|
|
return err.Error()
|
2018-02-07 01:06:47 +00:00
|
|
|
}
|
2018-02-08 02:00:20 +00:00
|
|
|
return "nil"
|
2018-02-06 00:50:32 +00:00
|
|
|
}
|