Refactor MockClient Into Internal Package (#210)

sharding: refactor mockClient into internal package

Former-commit-id: 5bb5142c92cffe0aeca0da06fdf0467d0570daee [formerly 4f9a27b0c3ddbbd578bf4d13f7818c7871d3d376]
Former-commit-id: b4164ac956933babeeb3918ac2a5c37613c67377
This commit is contained in:
Raul Jordan
2018-06-21 18:17:14 -05:00
committed by terence tsao
parent 1ddb19bba6
commit f009381a83
9 changed files with 158 additions and 32 deletions

File diff suppressed because one or more lines are too long

View File

@@ -19,6 +19,7 @@ contract SMC {
bytes32 chunkRoot; // Root hash of the collation body
address proposer; // Address of the proposer
bool isElected; // True if the collation has reached quorum size
bytes signature; // Signature of the collation header after proposer signs
}
// Notary state variables
@@ -170,7 +171,8 @@ contract SMC {
function addHeader(
uint _shardId,
uint _period,
bytes32 _chunkRoot
bytes32 _chunkRoot,
bytes _signature
) public {
require((_shardId >= 0) && (_shardId < shardCount));
require(_period == block.number / PERIOD_LENGTH);
@@ -181,7 +183,8 @@ contract SMC {
collationRecords[_shardId][_period] = CollationRecord({
chunkRoot: _chunkRoot,
proposer: msg.sender,
isElected: false
isElected: false,
signature: _signature
});
lastSubmittedCollation[_shardId] = block.number / PERIOD_LENGTH;

View File

@@ -147,7 +147,8 @@ func (s *smcTestHelper) deregisterNotaries(params ...int) error {
// addHeader is a helper function to add header to smc.
func (s *smcTestHelper) addHeader(a *testAccount, shard *big.Int, period *big.Int, chunkRoot uint8) error {
_, err := s.smc.AddHeader(a.txOpts, shard, period, [32]byte{chunkRoot})
sig := []byte{'S', 'I', 'G', 'N', 'A', 'T', 'U', 'R', 'E'}
_, err := s.smc.AddHeader(a.txOpts, shard, period, [32]byte{chunkRoot}, sig)
if err != nil {
return err
}

View File

@@ -0,0 +1,111 @@
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
}

View File

@@ -1,4 +1,4 @@
package utils
package internal
import (
"testing"

View File

@@ -63,5 +63,6 @@ type RecordFetcher interface {
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
}, error)
}

View File

@@ -28,7 +28,7 @@ func AddHeader(transactor mainchain.ContractTransactor, collation *sharding.Coll
var chunkRoot [32]byte
copy(chunkRoot[:], collation.Header().ChunkRoot().Bytes())
tx, err := transactor.SMCTransactor().AddHeader(txOps, collation.Header().ShardID(), collation.Header().Period(), chunkRoot)
tx, err := transactor.SMCTransactor().AddHeader(txOps, collation.Header().ShardID(), collation.Header().Period(), chunkRoot, collation.Header().Sig())
if err != nil {
return fmt.Errorf("unable to add header to SMC: %v", err)
}

View File

@@ -35,11 +35,13 @@ func (f *faultySMCCaller) CollationRecords(opts *bind.CallOpts, arg0 *big.Int, a
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
}, error) {
res := new(struct {
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
})
return *res, errors.New("error fetching collation record")
}
@@ -48,11 +50,13 @@ func (g *goodSMCCaller) CollationRecords(opts *bind.CallOpts, arg0 *big.Int, arg
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
}, error) {
res := new(struct {
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
})
body := []byte{1, 2, 3, 4, 5}
res.ChunkRoot = [32]byte(types.DeriveSha(sharding.Chunks(body)))

View File

@@ -40,11 +40,13 @@ func (f *faultySMCCaller) CollationRecords(opts *bind.CallOpts, arg0 *big.Int, a
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
}, error) {
res := new(struct {
ChunkRoot [32]byte
Proposer common.Address
IsElected bool
Signature []byte
})
return *res, errors.New("error fetching collation record")
}