Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
contrib
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
blockchain
contrib
Commits
b9f732de
Commit
b9f732de
authored
6 years ago
by
Miguel Montes
Browse files
Options
Downloads
Patches
Plain Diff
Refactorización del código de obtención de bloques
parent
adb994fd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bfa_client/src/bfa/node.go
+40
-111
40 additions, 111 deletions
bfa_client/src/bfa/node.go
bfa_client/src/client/bfa_client.go
+15
-0
15 additions, 0 deletions
bfa_client/src/client/bfa_client.go
with
55 additions
and
111 deletions
bfa_client/src/bfa/node.go
+
40
−
111
View file @
b9f732de
...
...
@@ -3,7 +3,7 @@ package bfa
import
(
.
"../clique"
.
"../util"
"e
ncoding/json
"
"e
rrors
"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/clique"
...
...
@@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"math/big"
"reflect"
"sort"
"strconv"
)
...
...
@@ -20,28 +19,49 @@ type Node rpc.Client
type
BigInt
big
.
Int
func
(
b
BigInt
)
MarshalJSON
()
([]
byte
,
error
)
{
i
:=
(
big
.
Int
)(
b
)
func
(
b
igInt
*
BigInt
)
MarshalJSON
()
([]
byte
,
error
)
{
i
:=
(
*
big
.
Int
)(
b
igInt
)
return
[]
byte
(
i
.
String
()),
nil
}
func
(
b
*
BigInt
)
String
()
string
{
return
(
*
big
.
Int
)(
b
)
.
String
()
func
(
bigInt
*
BigInt
)
UnmarshalJSON
(
b
[]
byte
)
error
{
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
type
RPCTransaction
struct
{
BlockHash
string
`json:"blockHash"`
BlockNumber
int64
`json:"blockNumber"`
BlockNumber
U
int64
`json:"blockNumber"`
From
string
`json:"from"`
Gas
u
int64
`json:"gas"`
GasPrice
uint64
`json:"gasPrice"`
Gas
U
int64
`json:"gas"`
GasPrice
*
BigInt
`json:"gasPrice"`
Hash
string
`json:"hash"`
Input
string
`json:"input"`
Nonce
u
int64
`json:"nonce"`
Nonce
U
int64
`json:"nonce"`
To
string
`json:"to"`
TransactionIndex
u
int
`json:"transactionIndex"`
Value
string
`json:"value"`
TransactionIndex
U
int
64
`json:"transactionIndex"`
Value
*
BigInt
`json:"value"`
V
*
BigInt
`json:"v"`
R
*
BigInt
`json:"r"`
S
*
BigInt
`json:"s"`
...
...
@@ -54,86 +74,19 @@ type Block struct {
TxHash
string
`json:"transactionsRoot"`
ReceiptHash
string
`json:"receiptsRoot"`
Bloom
string
`json:"logsBloom"`
Difficulty
u
int64
`json:"difficulty"`
Number
u
int64
`json:"number"`
GasLimit
u
int64
`json:"gasLimit"`
GasUsed
u
int64
`json:"gasUsed"`
Time
u
int64
`json:"timestamp"`
Difficulty
U
int64
`json:"difficulty"`
Number
U
int64
`json:"number"`
GasLimit
U
int64
`json:"gasLimit"`
GasUsed
U
int64
`json:"gasUsed"`
Time
U
int64
`json:"timestamp"`
Extra
string
`json:"extraData"`
MixDigest
string
`json:"mixHash"`
Nonce
u
int64
`json:"nonce"`
Nonce
U
int64
`json:"nonce"`
Transactions
[]
RPCTransaction
`json:"transactions"`
TotalDifficulty
u
int64
`json:"totalDifficulty"`
TotalDifficulty
U
int64
`json:"totalDifficulty"`
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
{
Number
uint64
`json:"number"`
// Block number 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) {
number
=
fmt
.
Sprintf
(
"0x%x"
,
blockNumber
)
}
node
.
Call
(
&
block
,
"eth_getBlockByNumber"
,
number
,
true
)
block
.
Signer
=
node
.
BlockSigner
(
blockNumber
)
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
)
{
header
:=
node
.
HeaderByNumber
(
blockNumber
)
signer
,
err
:=
GetSigner
(
&
header
)
...
...
This diff is collapsed.
Click to expand it.
bfa_client/src/client/bfa_client.go
+
15
−
0
View file @
b9f732de
...
...
@@ -411,6 +411,20 @@ func 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
()
{
var
(
commands
=
map
[
string
]
func
(){
...
...
@@ -420,6 +434,7 @@ func main() {
"autovote"
:
autovote
,
"status"
:
status
,
"block"
:
block
,
"snapshot"
:
snapshot
,
}
validCommands
[]
string
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment