Skip to content

Commit

Permalink
Merge pull request #64 from getamis/develop
Browse files Browse the repository at this point in the history
Release v0.2.7
  • Loading branch information
bailantaotao authored Jul 24, 2018
2 parents c5e6c90 + 5d85873 commit 925672c
Show file tree
Hide file tree
Showing 23 changed files with 944 additions and 199 deletions.
41 changes: 9 additions & 32 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ package client
import (
"context"
"errors"
"fmt"
"math/big"

ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand All @@ -37,9 +35,6 @@ import (

var (
ErrInvalidTDFormat = errors.New("invalid td format")

// maxUncles represents maximum number of uncles allowed in a single block
maxUncles = 2
)

//go:generate mockery -name EthClient
Expand All @@ -54,12 +49,10 @@ type EthClient interface {
UncleByBlockHashAndPosition(ctx context.Context, hash common.Hash, position uint) (*types.Header, error)
UnclesByBlockHash(ctx context.Context, blockHash common.Hash) ([]*types.Header, error)
SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)
DumpBlock(ctx context.Context, blockNr int64) (*state.Dump, error)
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
ModifiedAccountStatesByNumber(ctx context.Context, num uint64) (*state.DirtyDump, error)
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
GetERC20(ctx context.Context, addr common.Address, num int64) (*model.ERC20, error)
GetERC20(ctx context.Context, addr common.Address) (*model.ERC20, error)
GetTotalDifficulty(ctx context.Context, hash common.Hash) (*big.Int, error)
GetBlockReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
// Get ETH transfer logs
Expand Down Expand Up @@ -133,7 +126,7 @@ func (c *client) UncleByBlockHashAndPosition(ctx context.Context, hash common.Ha

func (c *client) UnclesByBlockHash(ctx context.Context, blockHash common.Hash) ([]*types.Header, error) {
var result []*types.Header
for i := 0; i < maxUncles; i++ {
for i := 0; i < model.MaxUncles; i++ {
h, err := c.UncleByBlockHashAndPosition(ctx, blockHash, uint(i))
if err != ethereum.NotFound {
return nil, err
Expand All @@ -146,18 +139,15 @@ func (c *client) UnclesByBlockHash(ctx context.Context, blockHash common.Hash) (
return result, nil
}

func (c *client) DumpBlock(ctx context.Context, blockNr int64) (*state.Dump, error) {
r := &state.Dump{}
err := c.rpc.CallContext(ctx, r, "debug_dumpBlock", fmt.Sprintf("0x%x", blockNr))
return r, err
}

func (c *client) GetTotalDifficulty(ctx context.Context, hash common.Hash) (*big.Int, error) {
var r string
err := c.rpc.CallContext(ctx, &r, "debug_getTotalDifficulty", hash.Hex())
if err != nil {
return nil, err
}
if len(r) <= 2 {
return nil, ethereum.NotFound
}
// Remove the '0x' prefix
td, ok := new(big.Int).SetString(r[2:], 16)
if !ok {
Expand All @@ -172,24 +162,11 @@ func (c *client) GetBlockReceipts(ctx context.Context, hash common.Hash) (types.
return r, err
}

func (c *client) ModifiedAccountStatesByNumber(ctx context.Context, num uint64) (*state.DirtyDump, error) {
r := &state.DirtyDump{}
err := c.rpc.CallContext(ctx, r, "debug_getModifiedAccountStatesByNumber", num)
return r, err
}

func (c *client) GetERC20(ctx context.Context, addr common.Address, num int64) (*model.ERC20, error) {
logger := log.New("addr", addr, "number", num)
code, err := c.CodeAt(ctx, addr, nil)
if err != nil {
return nil, err
}
func (c *client) GetERC20(ctx context.Context, addr common.Address) (*model.ERC20, error) {
logger := log.New("addr", addr)
erc20 := &model.ERC20{
Address: addr.Bytes(),
Code: code,
BlockNumber: num,
Address: addr.Bytes(),
}

caller, err := contracts.NewERC20TokenCaller(addr, c)
if err != nil {
logger.Warn("Failed to initiate contract caller", "err", err)
Expand Down
61 changes: 7 additions & 54 deletions client/mocks/EthClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions cmd/indexer/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"encoding/json"
"strconv"

"github.com/getamis/sirius/log"
"github.com/spf13/cobra"
Expand All @@ -40,23 +39,20 @@ var (
var vp = viper.New()

// LoadTokensFromConfig is the function to return addresses and blocks from config file
func LoadTokensFromConfig() ([]string, []int64, error) {
func LoadTokensFromConfig() ([]string, error) {
for _, v := range list {
data, _ := json.Marshal(v)
result := make(map[string]string)
err := json.Unmarshal(data, &result)
if err != nil {
return nil, nil, err
return nil, err
}

addr := result["address"]
addresses = append(addresses, addr)

block, _ := strconv.ParseInt(result["block"], 10, 64)
blocks = append(blocks, block)
}

return addresses, blocks, nil
return addresses, nil
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/indexer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ var ServerCmd = &cobra.Command{
indexer := indexer.New(ethClient, store.NewManager(db))

if subscribeErc20token {
erc20Addresses, erc20BlockNumbers, err := LoadTokensFromConfig()
erc20Addresses, err := LoadTokensFromConfig()
if err != nil {
log.Error("Fail to load ERC20Token List from Config File", "err", err)
return err
}
log.Debug("erc20Addresses Successfully Loaded")

if err := indexer.SubscribeErc20Tokens(ctx, erc20Addresses, erc20BlockNumbers); err != nil {
if err := indexer.SubscribeErc20Tokens(ctx, erc20Addresses); err != nil {
log.Error("Fail to subscribe ERC20Tokens and write to database", "err", err)
return err
}
Expand Down
2 changes: 0 additions & 2 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const (
var (
// ErrWrongSigner is returned if it's a wrong signer
ErrWrongSigner = errors.New("wrong signer")
// ErrInconsistentRoot is returned if the block and dump states have different root
ErrInconsistentRoot = errors.New("inconsistent root")
// ErrInconsistentStates is returned if the number of blocks, dumps or receipts are different
ErrInconsistentStates = errors.New("inconsistent states")
// ErrInvalidTD is returned when a block has invalid TD
Expand Down
5 changes: 0 additions & 5 deletions configs/erc20.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
erc20:
mith:
address: "0x3893b9422Cd5D70a81eDeFfe3d5A1c6A978310BB"
block: "5039624"
usdt:
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7"
block: "4634748"
eos:
address: "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0"
block: "5703095"
trx:
address: "0xf230b790e05390fc8295f4d3f60332c93bed42e2"
block: "5754798"
ven:
address: "0xd850942ef8811f2a866692a623011bde52a462c1"
block: "5183019"
5 changes: 5 additions & 0 deletions migration/db/migrate/20180718053750_remove_erc20_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveErc20Code < ActiveRecord::Migration[5.2]
def change
remove_column :erc20, :code, :binary
end
end
8 changes: 5 additions & 3 deletions migration/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_07_15_064231) do
ActiveRecord::Schema.define(version: 2018_07_18_053750) do

create_table "accounts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t|
t.binary "address", limit: 20, null: false
Expand Down Expand Up @@ -39,17 +39,19 @@
t.string "miner_reward", limit: 32, null: false
t.string "uncles_inclusion_reward", limit: 32, null: false
t.string "txs_fee", limit: 32, null: false
t.string "uncles_reward", limit: 32
t.binary "uncle1_hash", limit: 32
t.binary "uncle2_hash", limit: 32
t.string "uncle1_reward", limit: 32
t.binary "uncle1_coinbase", limit: 20
t.string "uncle2_reward", limit: 32
t.binary "uncle2_coinbase", limit: 20
t.index ["hash"], name: "index_block_headers_on_hash", unique: true
t.index ["number"], name: "index_block_headers_on_number", unique: true
end

create_table "erc20", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", force: :cascade do |t|
t.binary "address", limit: 20, null: false
t.bigint "block_number", null: false
t.binary "code", limit: 16777215, null: false
t.string "name", limit: 32
t.string "total_supply", limit: 32
t.bigint "decimals"
Expand Down
1 change: 0 additions & 1 deletion model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ func (s TotalBalance) TableName() string {
type ERC20 struct {
BlockNumber int64
Address []byte
Code []byte
TotalSupply string
Decimals int
Name string
Expand Down
15 changes: 4 additions & 11 deletions service/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
)

var (
//ErrInconsistentLength returns if the length of ERC20 addresses and block numbers are not eqaul
ErrInconsistentLength = errors.New("inconsistent length")
//ErrInvalidAddress returns if invalid ERC20 address is detected
ErrInvalidAddress = errors.New("invalid address")
)
Expand All @@ -54,13 +52,8 @@ type indexer struct {
}

// Init ensures all tables for erc20 contracts are created
func (idx *indexer) SubscribeErc20Tokens(ctx context.Context, addresses []string, numbers []int64) error {
if len(addresses) != len(numbers) {
log.Error("Inconsistent array length", "addrs", len(addresses), "numbers", len(numbers))
return ErrInconsistentLength
}

for i, addr := range addresses {
func (idx *indexer) SubscribeErc20Tokens(ctx context.Context, addresses []string) error {
for _, addr := range addresses {
if !ethCommon.IsHexAddress(addr) {
return ErrInvalidAddress
}
Expand All @@ -76,7 +69,7 @@ func (idx *indexer) SubscribeErc20Tokens(ctx context.Context, addresses []string
return err
}

erc20, err := idx.client.GetERC20(ctx, address, int64(numbers[i]))
erc20, err := idx.client.GetERC20(ctx, address)
if err != nil {
log.Error("Failed to get ERC20", "addr", addr, "err", err)
return err
Expand Down Expand Up @@ -329,7 +322,7 @@ func (idx *indexer) addBlockMaybeReorg(ctx context.Context, target int64) (*type
targetTD.Add(targetTD, rootTD)

// Compare currentTd with the new branch
if idx.currentTD.Cmp(targetTD) >= 0 {
if idx.currentTD.Cmp(targetTD) > 0 {
logger.Debug("Ignore this block due to lower TD", "targetTD", targetTD)
return nil, nil, nil
}
Expand Down
Loading

0 comments on commit 925672c

Please sign in to comment.