Skip to content
Snippets Groups Projects
Commit b9f732de authored by Miguel Montes's avatar Miguel Montes
Browse files

Refactorización del código de obtención de bloques

parent adb994fd
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ package bfa ...@@ -3,7 +3,7 @@ package bfa
import ( import (
. "../clique" . "../clique"
. "../util" . "../util"
"encoding/json" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/consensus/clique"
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"math/big" "math/big"
"reflect"
"sort" "sort"
"strconv" "strconv"
) )
...@@ -20,28 +19,49 @@ type Node rpc.Client ...@@ -20,28 +19,49 @@ type Node rpc.Client
type BigInt big.Int type BigInt big.Int
func (b BigInt) MarshalJSON() ([]byte, error) { func (bigInt *BigInt) MarshalJSON() ([]byte, error) {
i := (big.Int)(b) i := (*big.Int)(bigInt)
return []byte(i.String()), nil return []byte(i.String()), nil
} }
func (b *BigInt) String() string { func (bigInt *BigInt) UnmarshalJSON(b []byte) error {
return (*big.Int)(b).String() if i, ok := new(big.Int).SetString(string(b[1:len(b)-1]), 0); ok {
*bigInt = BigInt(*i)
return nil
}
return errors.New("can't unmarshal BigInt")
}
func (bigInt *BigInt) String() string {
return (*big.Int)(bigInt).String()
}
type Uint64 uint64
func (u *Uint64) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatUint(*(*uint64)(u), 10)), nil
}
func (u *Uint64) UnmarshalJSON(b []byte) (err error) {
i, err := strconv.ParseUint(string(b[1:len(b)-1]), 0, 64)
*u = Uint64(i)
return
} }
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct { type RPCTransaction struct {
BlockHash string `json:"blockHash"` BlockHash string `json:"blockHash"`
BlockNumber int64 `json:"blockNumber"` BlockNumber Uint64 `json:"blockNumber"`
From string `json:"from"` From string `json:"from"`
Gas uint64 `json:"gas"` Gas Uint64 `json:"gas"`
GasPrice uint64 `json:"gasPrice"` GasPrice *BigInt `json:"gasPrice"`
Hash string `json:"hash"` Hash string `json:"hash"`
Input string `json:"input"` Input string `json:"input"`
Nonce uint64 `json:"nonce"` Nonce Uint64 `json:"nonce"`
To string `json:"to"` To string `json:"to"`
TransactionIndex uint `json:"transactionIndex"` TransactionIndex Uint64 `json:"transactionIndex"`
Value string `json:"value"` Value *BigInt `json:"value"`
V *BigInt `json:"v"` V *BigInt `json:"v"`
R *BigInt `json:"r"` R *BigInt `json:"r"`
S *BigInt `json:"s"` S *BigInt `json:"s"`
...@@ -54,86 +74,19 @@ type Block struct { ...@@ -54,86 +74,19 @@ type Block struct {
TxHash string `json:"transactionsRoot"` TxHash string `json:"transactionsRoot"`
ReceiptHash string `json:"receiptsRoot"` ReceiptHash string `json:"receiptsRoot"`
Bloom string `json:"logsBloom"` Bloom string `json:"logsBloom"`
Difficulty uint64 `json:"difficulty"` Difficulty Uint64 `json:"difficulty"`
Number uint64 `json:"number"` Number Uint64 `json:"number"`
GasLimit uint64 `json:"gasLimit"` GasLimit Uint64 `json:"gasLimit"`
GasUsed uint64 `json:"gasUsed"` GasUsed Uint64 `json:"gasUsed"`
Time uint64 `json:"timestamp"` Time Uint64 `json:"timestamp"`
Extra string `json:"extraData"` Extra string `json:"extraData"`
MixDigest string `json:"mixHash"` MixDigest string `json:"mixHash"`
Nonce uint64 `json:"nonce"` Nonce Uint64 `json:"nonce"`
Transactions []RPCTransaction `json:"transactions"` Transactions []RPCTransaction `json:"transactions"`
TotalDifficulty uint64 `json:"totalDifficulty"` TotalDifficulty Uint64 `json:"totalDifficulty"`
Signer string `json:"signer"` Signer string `json:"signer"`
} }
func mapToStruct(m map[string]interface{}, s interface{}) {
v := reflect.ValueOf(s).Elem()
numFields := v.NumField()
for i := 0; i < numFields; i++ {
field := v.Field(i)
jsonName := v.Type().Field(i).Tag.Get("json")
if value, ok := m[jsonName]; ok {
switch x := value.(type) {
case string:
switch field.Kind() {
case reflect.String:
field.SetString(x)
case reflect.Uint64, reflect.Uint:
if uintValue, err := strconv.ParseUint(x, 0, 64); err == nil {
field.SetUint(uintValue)
}
case reflect.Int64, reflect.Int:
if intValue, err := strconv.ParseInt(x, 0, 64); err == nil {
field.SetInt(intValue)
}
}
}
}
}
}
func (block *Block) UnmarshalJSON(b []byte) error {
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
mapToStruct(m, block)
//v := reflect.ValueOf(block).Elem()
//numFields := v.NumField()
//for i := 0; i < numFields; i++ {
// field := v.Field(i)
// jsonName := v.Type().Field(i).Tag.Get("json")
// if value, ok := m[jsonName]; ok {
// switch x := value.(type) {
// case string:
// switch field.Kind() {
// case reflect.String:
// field.SetString(x)
// case reflect.Uint64:
// if uintValue, err := strconv.ParseUint(x, 0, 64); err == nil {
// field.SetUint(uintValue)
// }
// }
// }
// }
//}
for _, x := range m["transactions"].([]interface{}) {
t := new(RPCTransaction)
m := x.(map[string]interface{})
mapToStruct(m, t)
v, _ := new(big.Int).SetString(m["v"].(string), 0)
r, _ := new(big.Int).SetString(m["r"].(string), 0)
s, _ := new(big.Int).SetString(m["s"].(string), 0)
t.V = (*BigInt)(v)
t.R = (*BigInt)(r)
t.S = (*BigInt)(s)
fmt.Println(t.R, t.S, t.V)
block.Transactions = append(block.Transactions, *t)
}
return nil
}
type Snapshot struct { type Snapshot struct {
Number uint64 `json:"number"` // Block number where the snapshot was created Number uint64 `json:"number"` // Block number where the snapshot was created
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
...@@ -266,34 +219,10 @@ func (node *Node) BlockByNumber(blockNumber int64) (block Block) { ...@@ -266,34 +219,10 @@ func (node *Node) BlockByNumber(blockNumber int64) (block Block) {
number = fmt.Sprintf("0x%x", blockNumber) number = fmt.Sprintf("0x%x", blockNumber)
} }
node.Call(&block, "eth_getBlockByNumber", number, true) node.Call(&block, "eth_getBlockByNumber", number, true)
block.Signer = node.BlockSigner(blockNumber)
return return
} }
//func (node *Node) BlockxByNumber(blockNumber int64) (block Block) {
// ethClient := ethclient.NewClient((*rpc.Client)(node))
// b, err := ethClient.BlockByNumber(context.Background(), big.NewInt(blockNumber))
// h := b.Header()
// Check(err)
// block.ParentHash = h.ParentHash
// block.Coinbase = h.Coinbase
// block.Root = h.Root
// block.TxHash = h.TxHash
// block.ReceiptHash = h.ReceiptHash
// block.Bloom = h.Bloom
// block.Difficulty = h.Difficulty
// block.Number = h.Number
// block.GasLimit = h.GasLimit
// block.GasUsed = h.GasUsed
// block.Time = h.Time
// block.Extra = h.Extra
// block.MixDigest = h.MixDigest
// block.Nonce = h.Nonce
// block.Transactions = b.Transactions()
// block.Signer, err = GetSigner(h)
// block.TotalDifficulty = b.DeprecatedTd()
// return block
//}
func (node *Node) BlockSigner(blockNumber int64) (signer string) { func (node *Node) BlockSigner(blockNumber int64) (signer string) {
header := node.HeaderByNumber(blockNumber) header := node.HeaderByNumber(blockNumber)
signer, err := GetSigner(&header) signer, err := GetSigner(&header)
......
...@@ -411,6 +411,20 @@ func block() { ...@@ -411,6 +411,20 @@ func block() {
util.PrintJson(block) util.PrintJson(block)
} }
func snapshot() {
var blockNumber int64
description = "Muestra el snapshot en un bloque"
setFlags()
flags.Int64Var(&blockNumber, "block-number", latest, "Número del bloque en el cual se quiere conocer la lista de selladores (-1 para el último)")
parseFlags()
url = updateURL(url)
node, err := bfa.Dial(url)
util.Check(err)
defer node.Close()
snapshot := node.SnapshotAtBlock(blockNumber)
util.PrintJson(snapshot)
}
func main() { func main() {
var ( var (
commands = map[string]func(){ commands = map[string]func(){
...@@ -420,6 +434,7 @@ func main() { ...@@ -420,6 +434,7 @@ func main() {
"autovote": autovote, "autovote": autovote,
"status": status, "status": status,
"block": block, "block": block,
"snapshot": snapshot,
} }
validCommands []string validCommands []string
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment