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

Agregado muestra de bloque completo

parent b2e0fd29
No related branches found
No related tags found
No related merge requests found
...@@ -3,39 +3,135 @@ package bfa ...@@ -3,39 +3,135 @@ package bfa
import ( import (
. "../clique" . "../clique"
. "../util" . "../util"
"context" "encoding/json"
"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"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"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"
) )
type Node rpc.Client type Node rpc.Client
type BigInt big.Int
func (b BigInt) MarshalJSON() ([]byte, error) {
i := (big.Int)(b)
return []byte(i.String()), nil
}
func (b *BigInt) String() string {
return (*big.Int)(b).String()
}
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct {
BlockHash string `json:"blockHash"`
BlockNumber int64 `json:"blockNumber"`
From string `json:"from"`
Gas uint64 `json:"gas"`
GasPrice uint64 `json:"gasPrice"`
Hash string `json:"hash"`
Input string `json:"input"`
Nonce uint64 `json:"nonce"`
To string `json:"to"`
TransactionIndex uint `json:"transactionIndex"`
Value string `json:"value"`
V *BigInt `json:"v"`
R *BigInt `json:"r"`
S *BigInt `json:"s"`
}
type Block struct { type Block struct {
ParentHash common.Hash `json:"parentHash"` ParentHash string `json:"parentHash"`
Coinbase common.Address `json:"miner"` Coinbase string `json:"miner"`
Root common.Hash `json:"stateRoot"` Root string `json:"stateRoot"`
TxHash common.Hash `json:"transactionsRoot"` TxHash string `json:"transactionsRoot"`
ReceiptHash common.Hash `json:"receiptsRoot"` ReceiptHash string `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"` Bloom string `json:"logsBloom"`
Difficulty *big.Int `json:"difficulty"` Difficulty uint64 `json:"difficulty"`
Number *big.Int `json:"number"` Number uint64 `json:"number"`
GasLimit uint64 `json:"gasLimit"` GasLimit uint64 `json:"gasLimit"`
GasUsed uint64 `json:"gasUsed"` GasUsed uint64 `json:"gasUsed"`
Time *big.Int `json:"timestamp"` Time uint64 `json:"timestamp"`
Extra []byte `json:"extraData"` Extra string `json:"extraData"`
MixDigest common.Hash `json:"mixHash"` MixDigest string `json:"mixHash"`
Nonce types.BlockNonce `json:"nonce"` Nonce uint64 `json:"nonce"`
Transactions []types.Transaction `json:"transactions"` Transactions []RPCTransaction `json:"transactions"`
TotalDifficulty *big.Int `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 {
...@@ -161,32 +257,43 @@ func (node *Node) HeaderByNumber(blockNumber int64) types.Header { ...@@ -161,32 +257,43 @@ func (node *Node) HeaderByNumber(blockNumber int64) types.Header {
} }
func (node *Node) BlockByNumber(blockNumber int64) (block Block) { func (node *Node) BlockByNumber(blockNumber int64) (block Block) {
ethClient := ethclient.NewClient((*rpc.Client)(node)) var (
b, err := ethClient.BlockByNumber(context.Background(), nil) number string
h := b.Header() )
Check(err) if blockNumber < 0 {
block.ParentHash = h.ParentHash number = Latest
block.Coinbase = h.Coinbase } else {
block.Root = h.Root number = fmt.Sprintf("0x%x", blockNumber)
block.TxHash = h.TxHash }
block.ReceiptHash = h.ReceiptHash node.Call(&block, "eth_getBlockByNumber", number, true)
block.Bloom = h.Bloom return
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
for _, transaction := range b.Transactions() {
block.Transactions = append(block.Transactions, *transaction)
}
block.Signer, err = GetSigner(h)
block.TotalDifficulty = b.DeprecatedTd()
return block
} }
//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)
......
...@@ -407,7 +407,8 @@ func block() { ...@@ -407,7 +407,8 @@ func block() {
node, err := bfa.Dial(url) node, err := bfa.Dial(url)
util.Check(err) util.Check(err)
defer node.Close() defer node.Close()
util.PrintJson(node.BlockByNumber(blockNumber)) block := node.BlockByNumber(blockNumber)
util.PrintJson(block)
} }
func main() { func main() {
......
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