|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "math/big" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum" |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | +) |
| 12 | + |
| 13 | +var ErrNotImplemented = errors.New("not implemented") |
| 14 | + |
| 15 | +var _ SyncEthereumClient = &TestClient{} |
| 16 | + |
| 17 | +type TestClient struct { |
| 18 | + headerChain []*types.Header |
| 19 | + latestHeadIndex int |
| 20 | + intialProgress bool |
| 21 | + latestHeadEmitter []chan<- *types.Header |
| 22 | + latestHeadSubscription []*Subscription |
| 23 | +} |
| 24 | + |
| 25 | +func NewSubscription(idx int) *Subscription { |
| 26 | + return &Subscription{ |
| 27 | + idx: idx, |
| 28 | + err: make(chan error, 1), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +type Subscription struct { |
| 33 | + idx int |
| 34 | + err chan error |
| 35 | +} |
| 36 | + |
| 37 | +func (su *Subscription) Unsubscribe() { |
| 38 | + // TODO: not implemented yet, but we don't want to panic |
| 39 | +} |
| 40 | + |
| 41 | +func (su *Subscription) Err() <-chan error { |
| 42 | + return su.err |
| 43 | +} |
| 44 | + |
| 45 | +type TestClientController struct { |
| 46 | + c *TestClient |
| 47 | +} |
| 48 | + |
| 49 | +func NewTestClient() (*TestClient, *TestClientController) { |
| 50 | + c := &TestClient{ |
| 51 | + headerChain: []*types.Header{}, |
| 52 | + latestHeadIndex: 0, |
| 53 | + } |
| 54 | + ctrl := &TestClientController{c} |
| 55 | + return c, ctrl |
| 56 | +} |
| 57 | + |
| 58 | +func (c *TestClientController) ProgressHead() bool { |
| 59 | + if c.c.latestHeadIndex >= len(c.c.headerChain)-1 { |
| 60 | + return false |
| 61 | + } |
| 62 | + c.c.latestHeadIndex++ |
| 63 | + return true |
| 64 | +} |
| 65 | + |
| 66 | +func (c *TestClientController) EmitEvents(ctx context.Context) error { |
| 67 | + if len(c.c.latestHeadEmitter) == 0 { |
| 68 | + return nil |
| 69 | + } |
| 70 | + h := c.c.getLatestHeader() |
| 71 | + for _, em := range c.c.latestHeadEmitter { |
| 72 | + select { |
| 73 | + case em <- h: |
| 74 | + case <-ctx.Done(): |
| 75 | + return ctx.Err() |
| 76 | + } |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (c *TestClientController) AppendNextHeaders(h ...*types.Header) { |
| 82 | + c.c.headerChain = append(c.c.headerChain, h...) |
| 83 | +} |
| 84 | + |
| 85 | +func (t *TestClient) ChainID(_ context.Context) (*big.Int, error) { |
| 86 | + return big.NewInt(42), nil |
| 87 | +} |
| 88 | + |
| 89 | +func (t *TestClient) Close() { |
| 90 | + // TODO: cleanup |
| 91 | +} |
| 92 | + |
| 93 | +func (t *TestClient) getLatestHeader() *types.Header { |
| 94 | + if len(t.headerChain) == 0 { |
| 95 | + return nil |
| 96 | + } |
| 97 | + return t.headerChain[t.latestHeadIndex] |
| 98 | +} |
| 99 | + |
| 100 | +func (t *TestClient) searchBlock(f func(*types.Header) bool) *types.Header { |
| 101 | + for i := t.latestHeadIndex; i >= 0; i-- { |
| 102 | + h := t.headerChain[i] |
| 103 | + if f(h) { |
| 104 | + return h |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (t *TestClient) searchBlockByNumber(number *big.Int) *types.Header { |
| 111 | + return t.searchBlock( |
| 112 | + func(h *types.Header) bool { |
| 113 | + return h.Number.Cmp(number) == 0 |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +func (t *TestClient) searchBlockByHash(hash common.Hash) *types.Header { |
| 118 | + return t.searchBlock( |
| 119 | + func(h *types.Header) bool { |
| 120 | + return hash.Cmp(h.Hash()) == 0 |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +func (t *TestClient) BlockNumber(_ context.Context) (uint64, error) { |
| 125 | + return t.getLatestHeader().Nonce.Uint64(), nil |
| 126 | +} |
| 127 | + |
| 128 | +func (t *TestClient) HeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error) { |
| 129 | + h := t.searchBlockByHash(hash) |
| 130 | + if h == nil { |
| 131 | + // TODO: what error? |
| 132 | + } |
| 133 | + return h, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (t *TestClient) HeaderByNumber(_ context.Context, number *big.Int) (*types.Header, error) { |
| 137 | + if number == nil { |
| 138 | + return t.getLatestHeader(), nil |
| 139 | + } |
| 140 | + h := t.searchBlockByNumber(number) |
| 141 | + if h == nil { |
| 142 | + // TODO: what error? |
| 143 | + } |
| 144 | + return h, nil |
| 145 | +} |
| 146 | + |
| 147 | +func (t *TestClient) SubscribeNewHead(_ context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { |
| 148 | + t.latestHeadEmitter = append(t.latestHeadEmitter, ch) |
| 149 | + su := NewSubscription(len(t.latestHeadSubscription) - 1) |
| 150 | + t.latestHeadSubscription = append(t.latestHeadSubscription, su) |
| 151 | + // TODO: unsubscribe and deleting from the array |
| 152 | + // TODO: filling error promise in the subscription |
| 153 | + return su, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (t *TestClient) FilterLogs(_ context.Context, q ethereum.FilterQuery) ([]types.Log, error) { |
| 157 | + panic(ErrNotImplemented) |
| 158 | +} |
| 159 | + |
| 160 | +func (t *TestClient) SubscribeFilterLogs(_ context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { |
| 161 | + panic(ErrNotImplemented) |
| 162 | +} |
| 163 | + |
| 164 | +func (t *TestClient) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) { |
| 165 | + panic(ErrNotImplemented) |
| 166 | +} |
| 167 | + |
| 168 | +func (t *TestClient) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { |
| 169 | + panic(ErrNotImplemented) |
| 170 | +} |
0 commit comments