|
17 | 17 | package ethclient |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "context" |
| 21 | + "errors" |
20 | 22 | "fmt" |
21 | 23 | "math/big" |
22 | 24 | "reflect" |
23 | 25 | "testing" |
| 26 | + "time" |
24 | 27 |
|
25 | 28 | "github.com/ethereum/go-ethereum" |
26 | 29 | "github.com/ethereum/go-ethereum/common" |
| 30 | + "github.com/ethereum/go-ethereum/consensus/ethash" |
| 31 | + "github.com/ethereum/go-ethereum/core" |
| 32 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 33 | + "github.com/ethereum/go-ethereum/core/types" |
| 34 | + "github.com/ethereum/go-ethereum/crypto" |
| 35 | + "github.com/ethereum/go-ethereum/eth" |
| 36 | + "github.com/ethereum/go-ethereum/node" |
| 37 | + "github.com/ethereum/go-ethereum/params" |
27 | 38 | ) |
28 | 39 |
|
29 | 40 | // Verify that Client implements the ethereum interfaces. |
@@ -150,3 +161,143 @@ func TestToFilterArg(t *testing.T) { |
150 | 161 | }) |
151 | 162 | } |
152 | 163 | } |
| 164 | + |
| 165 | +var ( |
| 166 | + testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 167 | + testAddr = crypto.PubkeyToAddress(testKey.PublicKey) |
| 168 | + testBalance = big.NewInt(2e10) |
| 169 | +) |
| 170 | + |
| 171 | +func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { |
| 172 | + // Generate test chain. |
| 173 | + genesis, blocks := generateTestChain() |
| 174 | + |
| 175 | + // Start Ethereum service. |
| 176 | + var ethservice *eth.Ethereum |
| 177 | + n, err := node.New(&node.Config{}) |
| 178 | + n.Register(func(ctx *node.ServiceContext) (node.Service, error) { |
| 179 | + config := ð.Config{Genesis: genesis} |
| 180 | + config.Ethash.PowMode = ethash.ModeFake |
| 181 | + ethservice, err = eth.New(ctx, config) |
| 182 | + return ethservice, err |
| 183 | + }) |
| 184 | + |
| 185 | + // Import the test chain. |
| 186 | + if err := n.Start(); err != nil { |
| 187 | + t.Fatalf("can't start test node: %v", err) |
| 188 | + } |
| 189 | + if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil { |
| 190 | + t.Fatalf("can't import test blocks: %v", err) |
| 191 | + } |
| 192 | + return n, blocks |
| 193 | +} |
| 194 | + |
| 195 | +func generateTestChain() (*core.Genesis, []*types.Block) { |
| 196 | + db := rawdb.NewMemoryDatabase() |
| 197 | + config := params.AllEthashProtocolChanges |
| 198 | + genesis := &core.Genesis{ |
| 199 | + Config: config, |
| 200 | + Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, |
| 201 | + ExtraData: []byte("test genesis"), |
| 202 | + Timestamp: 9000, |
| 203 | + } |
| 204 | + generate := func(i int, g *core.BlockGen) { |
| 205 | + g.OffsetTime(5) |
| 206 | + g.SetExtra([]byte("test")) |
| 207 | + } |
| 208 | + gblock := genesis.ToBlock(db) |
| 209 | + engine := ethash.NewFaker() |
| 210 | + blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) |
| 211 | + blocks = append([]*types.Block{gblock}, blocks...) |
| 212 | + return genesis, blocks |
| 213 | +} |
| 214 | + |
| 215 | +func TestHeader(t *testing.T) { |
| 216 | + backend, chain := newTestBackend(t) |
| 217 | + client, _ := backend.Attach() |
| 218 | + defer backend.Stop() |
| 219 | + defer client.Close() |
| 220 | + |
| 221 | + tests := map[string]struct { |
| 222 | + block *big.Int |
| 223 | + want *types.Header |
| 224 | + wantErr error |
| 225 | + }{ |
| 226 | + "genesis": { |
| 227 | + block: big.NewInt(0), |
| 228 | + want: chain[0].Header(), |
| 229 | + }, |
| 230 | + "first_block": { |
| 231 | + block: big.NewInt(1), |
| 232 | + want: chain[1].Header(), |
| 233 | + }, |
| 234 | + "future_block": { |
| 235 | + block: big.NewInt(1000000000), |
| 236 | + want: nil, |
| 237 | + }, |
| 238 | + } |
| 239 | + for name, tt := range tests { |
| 240 | + t.Run(name, func(t *testing.T) { |
| 241 | + ec := NewClient(client) |
| 242 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 243 | + defer cancel() |
| 244 | + |
| 245 | + got, err := ec.HeaderByNumber(ctx, tt.block) |
| 246 | + if tt.wantErr != nil && (err == nil || err.Error() != tt.wantErr.Error()) { |
| 247 | + t.Fatalf("HeaderByNumber(%v) error = %q, want %q", tt.block, err, tt.wantErr) |
| 248 | + } |
| 249 | + if got != nil && got.Number.Sign() == 0 { |
| 250 | + got.Number = big.NewInt(0) // hack to make DeepEqual work |
| 251 | + } |
| 252 | + if !reflect.DeepEqual(got, tt.want) { |
| 253 | + t.Fatalf("HeaderByNumber(%v)\n = %v\nwant %v", tt.block, got, tt.want) |
| 254 | + } |
| 255 | + }) |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +func TestBalanceAt(t *testing.T) { |
| 260 | + backend, _ := newTestBackend(t) |
| 261 | + client, _ := backend.Attach() |
| 262 | + defer backend.Stop() |
| 263 | + defer client.Close() |
| 264 | + |
| 265 | + tests := map[string]struct { |
| 266 | + account common.Address |
| 267 | + block *big.Int |
| 268 | + want *big.Int |
| 269 | + wantErr error |
| 270 | + }{ |
| 271 | + "valid_account": { |
| 272 | + account: testAddr, |
| 273 | + block: big.NewInt(1), |
| 274 | + want: testBalance, |
| 275 | + }, |
| 276 | + "non_existent_account": { |
| 277 | + account: common.Address{1}, |
| 278 | + block: big.NewInt(1), |
| 279 | + want: big.NewInt(0), |
| 280 | + }, |
| 281 | + "future_block": { |
| 282 | + account: testAddr, |
| 283 | + block: big.NewInt(1000000000), |
| 284 | + want: big.NewInt(0), |
| 285 | + wantErr: errors.New("header not found"), |
| 286 | + }, |
| 287 | + } |
| 288 | + for name, tt := range tests { |
| 289 | + t.Run(name, func(t *testing.T) { |
| 290 | + ec := NewClient(client) |
| 291 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 292 | + defer cancel() |
| 293 | + |
| 294 | + got, err := ec.BalanceAt(ctx, tt.account, tt.block) |
| 295 | + if tt.wantErr != nil && (err == nil || err.Error() != tt.wantErr.Error()) { |
| 296 | + t.Fatalf("BalanceAt(%x, %v) error = %q, want %q", tt.account, tt.block, err, tt.wantErr) |
| 297 | + } |
| 298 | + if got.Cmp(tt.want) != 0 { |
| 299 | + t.Fatalf("BalanceAt(%x, %v) = %v, want %v", tt.account, tt.block, got, tt.want) |
| 300 | + } |
| 301 | + }) |
| 302 | + } |
| 303 | +} |
0 commit comments