From 353275575720415947f6983d4e451ce2e00bb0f2 Mon Sep 17 00:00:00 2001 From: Miguel Montes <miguel.montes@gmail.com> Date: Mon, 18 Mar 2019 17:10:10 -0300 Subject: [PATCH] Agregado reporte de transferencias --- bfa_client/src/bfa/block.go | 8 ++++ bfa_client/src/bfa/node.go | 12 +++++ bfa_client/src/client/bfa_client.go | 71 +++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/bfa_client/src/bfa/block.go b/bfa_client/src/bfa/block.go index 248ebf9..3d591f5 100644 --- a/bfa_client/src/bfa/block.go +++ b/bfa_client/src/bfa/block.go @@ -28,6 +28,10 @@ func (bigInt *BigInt) UnmarshalJSON(b []byte) error { } +func (bigInt *BigInt) String() string { + return (*big.Int)(bigInt).String() +} + func (bigInt *BigInt) Int64() int64 { return (*big.Int)(bigInt).Int64() } @@ -36,6 +40,10 @@ func (bigInt *BigInt) Uint64() uint64 { return (*big.Int)(bigInt).Uint64() } +func (bigInt *BigInt) IsZero() bool { + return (*big.Int)(bigInt).BitLen() == 0 +} + type Uint64 uint64 func (u *Uint64) UnmarshalJSON(b []byte) (err error) { diff --git a/bfa_client/src/bfa/node.go b/bfa_client/src/bfa/node.go index 6fc48e2..8394639 100644 --- a/bfa_client/src/bfa/node.go +++ b/bfa_client/src/bfa/node.go @@ -3,6 +3,7 @@ package bfa import ( . "../util" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" @@ -65,6 +66,11 @@ func (node *Node) BlockNumber() int64 { return bn.Int64() } +func (node *Node) GasPrice() (gp int64) { + node.Call(&gp, "eth_gasPrice") + return gp +} + func (node *Node) Accounts() (accounts []string) { node.Call(&accounts, "eth_accounts") return @@ -142,6 +148,12 @@ func (node *Node) BlockByHash(hash common.Hash) (block Block) { return } +func (node *Node) BlockTransactionCount(blockNumber int64) (count int64) { + var num hexutil.Uint64 + node.Call(&num, "eth_getBlockTransactionCountByNumber", hexBlockNumber(blockNumber)) + return int64(num) +} + func (node *Node) BlockSigner(blockNumber int64) (signer common.Address) { if blockNumber == 0 { // we return an empty signer for genesis block return diff --git a/bfa_client/src/client/bfa_client.go b/bfa_client/src/client/bfa_client.go index 1eb1666..224a13f 100644 --- a/bfa_client/src/client/bfa_client.go +++ b/bfa_client/src/client/bfa_client.go @@ -502,6 +502,76 @@ func snapshot() { util.PrintJson(snapshot) } +func transfers() { + type Transfer struct { + From string `json:"from"` + To string `json:"to"` + Amount *big.Int `json:"amount"` + BlockNumber int64 `json:"blockNumber"` + } + var ( + first, last, end int64 + ) + description = "Muestra transferencias en un rango de bloques, y opcionalmente, restringidas a ciertas direcciones." + otherArgs = "[dirección...]" + setFlags() + flags.Int64Var(&first, "first-block", latest, "Primer bloque del rango (-1 para especificar el último bloque)") + flags.Int64Var(&last, "last-block", latest, "Último bloque del rango (-1 para especificar el último bloque)") + parseFlags(true) + url = updateURL(url) + node, err := bfa.Dial(url) + util.Check(err) + defer node.Close() + set := make(map[common.Address]bool) + txs := make([]Transfer, 0) + for i := 0; i < flags.NArg(); i++ { + address := flags.Arg(i) + util.Ensure(util.IsAddress(address), "'%v' no es una dirección válida", address) + set[common.HexToAddress(address)] = true + } + latest := node.BlockNumber() + if first < 0 { + first = latest + } + if last < 0 || last > latest { + end = latest + 1 + } else { + end = last + 1 + } + for first < end { + for i := first; i < end; i++ { + if node.BlockTransactionCount(i) > 0 { + block := node.BlockByNumber(i) + for _, transaction := range block.Transactions { + if !transaction.Value.IsZero() { + src := transaction.From + dst := transaction.To + if len(set) == 0 || set[src] || set[dst] { + if json { + txs = append(txs, Transfer{src.Hex(), dst.Hex(), (*big.Int)(transaction.Value), i}) + } else { + fmt.Printf("%v -> %v: %v (%v)\n", src.Hex(), dst.Hex(), transaction.Value, transaction.BlockNumber) + } + } + } + } + } + } + first = end + if last < 0 || last > end { + latest = node.BlockNumber() + if latest < last { + end = latest + 1 + } else { + end = last + 1 + } + } + } + if json { + util.PrintJson(txs) + } +} + func main() { var ( commands = map[string]func(){ @@ -512,6 +582,7 @@ func main() { "status": status, "block": block, "snapshot": snapshot, + "transfers": transfers, } validCommands []string command func() -- GitLab