-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_test.go
More file actions
96 lines (83 loc) · 2.22 KB
/
scan_test.go
File metadata and controls
96 lines (83 loc) · 2.22 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package block_scan
import (
"context"
"testing"
"time"
"github.com/evolutionlandorg/block-scan/services"
"github.com/stretchr/testify/assert"
)
type MockChainIo struct {
}
func (m *MockChainIo) ReceiptLog(_ string) (*services.Receipts, error) {
return &services.Receipts{
BlockNumber: "1",
Logs: []services.Log{
{
Topics: []string{"awD"},
Data: "111",
Address: "222",
},
},
Status: "0x01",
ChainSource: "Crab",
BlockHash: "1",
}, nil
}
func (m *MockChainIo) BlockNumber() uint64 {
return 10
}
func (m *MockChainIo) FilterTrans(blockNum uint64, _ []string) (txn []string, contracts []string, timestamp uint64, transactionTo []string) {
if blockNum >= 1 {
transactionTo = append(transactionTo, "222")
timestamp = 123456789
contracts = append(contracts, "222")
txn = append(txn, "1")
return
}
return
}
func (m *MockChainIo) BlockHeader(_ uint64) *services.BlockHeader {
return &services.BlockHeader{
BlockTimeStamp: 123456789,
Hash: "1",
}
}
func (m *MockChainIo) GetTransactionStatus(_ string) string {
return "0x01"
}
type FakeCallback struct {
tx string
blockTimestamp uint64
receipt *services.Receipts
}
func (f *FakeCallback) FakeCallback(_ context.Context) error {
return nil
}
func TestStartScanChainEvents(t *testing.T) {
f := new(FakeCallback)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
assert.NoError(t, StartScanChainEvents(ctx, POLLING, services.ScanEventsOptions{
ChainIo: new(MockChainIo),
GetStartBlock: func() uint64 {
return 1
},
SetStartBlock: func(currentBlockNum uint64) {},
Chain: "Crab",
ContractsName: map[services.ContractsAddress]services.ContractsName{
services.ContractsAddress("222"): services.ContractsName("fake"),
},
GetCallbackFunc: func(tx string, blockTimestamp uint64, receipt *services.Receipts) interface{} {
f.tx = tx
f.blockTimestamp = blockTimestamp
f.receipt = receipt
return f
},
CallbackMethodPrefix: []string{"Fake"},
InitBlock: 1,
}))
assert.Equal(t, f.tx, "1")
assert.Equal(t, f.blockTimestamp, uint64(123456789))
assert.Equal(t, f.receipt.BlockHash, "1")
assert.Equal(t, f.receipt.BlockNumber, "1")
}