Skip to content

Commit b40055e

Browse files
authored
mock protocol (iotexproject#1678)
1 parent 398ec50 commit b40055e

File tree

7 files changed

+377
-273
lines changed

7 files changed

+377
-273
lines changed

action/protocol/managers.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package protocol
2+
3+
import (
4+
"strings"
5+
6+
"github.com/pkg/errors"
7+
8+
"github.com/iotexproject/go-pkgs/hash"
9+
"github.com/iotexproject/iotex-address/address"
10+
"github.com/iotexproject/iotex-core/action"
11+
"github.com/iotexproject/iotex-core/db"
12+
"github.com/iotexproject/iotex-core/state"
13+
)
14+
15+
// ChainManager defines the blockchain interface
16+
type ChainManager interface {
17+
// ChainID returns the chain ID
18+
ChainID() uint32
19+
// CandidatesByHeight returns the candidate list by a given height
20+
CandidatesByHeight(height uint64) ([]*state.Candidate, error)
21+
// ProductivityByEpoch returns the number of produced blocks per delegate in an epoch
22+
ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error)
23+
// SimulateExecution simulates a running of smart contract operation
24+
SimulateExecution(caller address.Address, ex *action.Execution) ([]byte, *action.Receipt, error)
25+
}
26+
27+
// StateManager defines the state DB interface atop IoTeX blockchain
28+
type StateManager interface {
29+
// Accounts
30+
Height() uint64
31+
Snapshot() int
32+
Revert(int) error
33+
// General state
34+
State(hash.Hash160, interface{}) error
35+
PutState(hash.Hash160, interface{}) error
36+
DelState(pkHash hash.Hash160) error
37+
GetDB() db.KVStore
38+
GetCachedBatch() db.CachedBatch
39+
}
40+
41+
// DummyChainManager mocks ChainManager interface
42+
type DummyChainManager struct {
43+
}
44+
45+
// Nonce mocks base method
46+
func (m *DummyChainManager) Nonce(addr string) (uint64, error) {
47+
if strings.EqualFold("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", addr) {
48+
return 0, errors.New("DummyChainManager nonce error")
49+
}
50+
return 2, nil
51+
}
52+
53+
// ChainID return chain ID
54+
func (m *DummyChainManager) ChainID() uint32 {
55+
return 0
56+
}
57+
58+
// CandidatesByHeight returns the candidate list by a given height
59+
func (m *DummyChainManager) CandidatesByHeight(height uint64) ([]*state.Candidate, error) {
60+
return nil, nil
61+
}
62+
63+
// ProductivityByEpoch returns the number of produced blocks per delegate in an epoch
64+
func (m *DummyChainManager) ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error) {
65+
return 0, nil, nil
66+
}
67+
68+
// SimulateExecution simulates a running of smart contract operation
69+
func (m *DummyChainManager) SimulateExecution(caller address.Address, ex *action.Execution) ([]byte, *action.Receipt, error) {
70+
return nil, nil, nil
71+
}

action/protocol/mock_protocol_test.go

+196
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/poll/nativestaking_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
func TestStaking(t *testing.T) {
3838
require := require.New(t)
3939

40-
mcm := &protocol.MockChainManager{}
40+
mcm := &protocol.DummyChainManager{}
4141
getTime := func() (time.Time, error) { return time.Now(), nil }
4242
ns, err := NewNativeStaking(mcm, nil)
4343
require.Error(err)

action/protocol/protocol.go

-63
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ package protocol
88

99
import (
1010
"context"
11-
"strings"
1211

13-
"github.com/iotexproject/iotex-address/address"
1412
"github.com/pkg/errors"
1513

16-
"github.com/iotexproject/go-pkgs/hash"
1714
"github.com/iotexproject/iotex-core/action"
18-
"github.com/iotexproject/iotex-core/db"
19-
"github.com/iotexproject/iotex-core/state"
2015
)
2116

2217
var (
@@ -47,61 +42,3 @@ type ActionEnvelopeValidator interface {
4742
type ActionHandler interface {
4843
Handle(context.Context, action.Action, StateManager) (*action.Receipt, error)
4944
}
50-
51-
// ChainManager defines the blockchain interface
52-
type ChainManager interface {
53-
// ChainID returns the chain ID
54-
ChainID() uint32
55-
// CandidatesByHeight returns the candidate list by a given height
56-
CandidatesByHeight(height uint64) ([]*state.Candidate, error)
57-
// ProductivityByEpoch returns the number of produced blocks per delegate in an epoch
58-
ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error)
59-
// SimulateExecution simulates a running of smart contract operation
60-
SimulateExecution(caller address.Address, ex *action.Execution) ([]byte, *action.Receipt, error)
61-
}
62-
63-
// StateManager defines the state DB interface atop IoTeX blockchain
64-
type StateManager interface {
65-
// Accounts
66-
Height() uint64
67-
Snapshot() int
68-
Revert(int) error
69-
// General state
70-
State(hash.Hash160, interface{}) error
71-
PutState(hash.Hash160, interface{}) error
72-
DelState(pkHash hash.Hash160) error
73-
GetDB() db.KVStore
74-
GetCachedBatch() db.CachedBatch
75-
}
76-
77-
// MockChainManager mocks ChainManager interface
78-
type MockChainManager struct {
79-
}
80-
81-
// Nonce mocks base method
82-
func (m *MockChainManager) Nonce(addr string) (uint64, error) {
83-
if strings.EqualFold("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", addr) {
84-
return 0, errors.New("MockChainManager nonce error")
85-
}
86-
return 2, nil
87-
}
88-
89-
// ChainID return chain ID
90-
func (m *MockChainManager) ChainID() uint32 {
91-
return 0
92-
}
93-
94-
// CandidatesByHeight returns the candidate list by a given height
95-
func (m *MockChainManager) CandidatesByHeight(height uint64) ([]*state.Candidate, error) {
96-
return nil, nil
97-
}
98-
99-
// ProductivityByEpoch returns the number of produced blocks per delegate in an epoch
100-
func (m *MockChainManager) ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error) {
101-
return 0, nil, nil
102-
}
103-
104-
// SimulateExecution simulates a running of smart contract operation
105-
func (m *MockChainManager) SimulateExecution(caller address.Address, ex *action.Execution) ([]byte, *action.Receipt, error) {
106-
return nil, nil, nil
107-
}

0 commit comments

Comments
 (0)