/
client.go
80 lines (63 loc) · 1.97 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package core
import (
"context"
"encoding/json"
"github.com/axiomesh/guardian/repo"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type Client interface {
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
SubscribeFilterLogs(context.Context, ethereum.FilterQuery, chan<- types.Log) (ethereum.Subscription, error)
}
var _ Client = (*MockClient)(nil)
type MockClient struct {
}
func (mc *MockClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
log, err := generateLog()
if err != nil {
return nil, err
}
logs := []types.Log{*log}
return logs, nil
}
func (mc *MockClient) SubscribeFilterLogs(context.Context, ethereum.FilterQuery, chan<- types.Log) (ethereum.Subscription, error) {
return &MockSubscription{}, nil
}
func generateLog() (*types.Log, error) {
nodeProposal := &NodeProposal{
BaseProposal: BaseProposal{
ID: 1,
Type: NodeUpgrade,
Strategy: SimpleStrategy,
Proposer: "0xff00000000000000000000000000000000001001",
Title: "mock title",
Desc: "mock desc",
BlockNumber: 1000,
TotalVotes: 4,
PassVotes: []string{"0x110000000000000000000000000000000000ffff", "0x220000000000000000000000000000000000ffff", "0x330000000000000000000000000000000000ffff"},
RejectVotes: nil,
Status: Approved,
},
DownloadUrls: []string{"http://localhost:9111/axiom-dev.tar.gz", "http://localhost:9112/axiom-dev.tar.gz"},
CheckHash: "596d31575d39232ac8b80522e74d7e2c85ce177a0936a38f9616b8ceef3e97d1",
}
data, err := json.Marshal(nodeProposal)
if err != nil {
return nil, err
}
log := &types.Log{
Address: common.HexToAddress(repo.NodeManagerContractAddr),
Data: data,
}
return log, nil
}
type MockSubscription struct {
}
func (ms *MockSubscription) Unsubscribe() {
}
func (ms *MockSubscription) Err() <-chan error {
errChan := make(<-chan error)
return errChan
}