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

Cambio del código de identificación del sellador de un bloque

Uso de campos embebidos
Creación de la interface BlockHeader, implementada por Header y por Block
parent 6dc27573
No related branches found
No related tags found
No related merge requests found
......@@ -84,28 +84,96 @@ type RPCTransaction struct {
S *BigInt `json:"s"`
}
type Header struct {
ParentHash common.Hash `json:"parentHash"`
UncleHash common.Hash `json:"sha3Uncles"`
Coinbase common.Address `json:"miner"`
Root common.Hash `json:"stateRoot"`
TxHash common.Hash `json:"transactionsRoot"`
ReceiptHash common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *BigInt `json:"difficulty"`
Number *BigInt `json:"number"`
GasLimit Uint64 `json:"gasLimit"`
GasUsed Uint64 `json:"gasUsed"`
Time *BigInt `json:"timestamp"`
Extra Bytes `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce types.BlockNonce `json:"nonce"`
}
func (header *Header) ParentHsh() common.Hash {
return header.ParentHash
}
func (header *Header) UncleHsh() common.Hash {
return header.UncleHash
}
func (header *Header) RootHsh() common.Hash {
return header.Root
}
func (header *Header) TxHsh() common.Hash {
return header.TxHash
}
func (header *Header) ReceiptHsh() common.Hash {
return header.ReceiptHash
}
func (header *Header) MixDigestHsh() common.Hash {
return header.MixDigest
}
func (header *Header) LogsBloom() types.Bloom {
return header.Bloom
}
func (header *Header) BlockNonce() types.BlockNonce {
return header.Nonce
}
func (header *Header) BlockDifficulty() *big.Int {
return (*big.Int)(header.Difficulty)
}
func (header *Header) BlockNumber() *big.Int {
return (*big.Int)(header.Number)
}
func (header *Header) Timestamp() *big.Int {
return (*big.Int)(header.Time)
}
func (header *Header) MaxGas() uint64 {
return uint64(header.GasLimit)
}
func (header *Header) Gas() uint64 {
return uint64(header.GasUsed)
}
func (header *Header) ExtraWithoutSignature() []byte {
return header.Extra[:len(header.Extra)-65]
}
func (header *Header) Signature() []byte {
return header.Extra[len(header.Extra)-65:]
}
func (header *Header) Miner() common.Address {
return header.Coinbase
}
type Block struct {
ParentHash common.Hash `json:"parentHash"`
UncleHash common.Hash `json:"sha3Uncles"`
Coinbase common.Address `json:"miner"`
Root common.Hash `json:"stateRoot"`
TxHash common.Hash `json:"transactionsRoot"`
ReceiptHash common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *BigInt `json:"difficulty"`
Number *BigInt `json:"number"`
GasLimit Uint64 `json:"gasLimit"`
GasUsed Uint64 `json:"gasUsed"`
Time *BigInt `json:"timestamp"`
Extra Bytes `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce types.BlockNonce `json:"nonce"`
Header
Transactions []RPCTransaction `json:"transactions"`
TotalDifficulty Uint64 `json:"totalDifficulty"`
Signer common.Address `json:"signer"`
}
func (block *Block) Header() (header types.Header) {
func (block *Block) GetHeader() (header types.Header) {
header.ParentHash = block.ParentHash
header.UncleHash = block.UncleHash
header.Coinbase = block.Coinbase
......@@ -125,8 +193,7 @@ func (block *Block) Header() (header types.Header) {
}
func (block *Block) setSigner() {
header := block.Header()
block.Signer = GetSigner(&header)
block.Signer = GetSigner(block)
}
type Snapshot struct {
......@@ -251,7 +318,7 @@ func (node *Node) BlockNumberInRange(number int64) (blockNumber int64) {
return
}
func (node *Node) HeaderByNumber(blockNumber int64) (header types.Header) {
func (node *Node) HeaderByNumber(blockNumber int64) (header Header) {
Require(blockNumber == -1 || blockNumber == node.BlockNumberInRange(blockNumber), "block number out of range")
node.Call(&header, "eth_getBlockByNumber", hexBlockNumber(blockNumber), true)
return
......@@ -377,14 +444,14 @@ func (node *Node) SealersStatus(blockNumber int64) (status map[string]*SealerSta
}
notSeen := int64(len(status))
block := node.HeaderByNumber(blockNumber)
blockNumber = block.Number.Int64()
blockNumber = block.BlockNumber().Int64()
until := Max(1, blockNumber-5*notSeen)
for notSeen > 0 {
signer := BytesToHex(GetSigner(&block).Bytes())
if status[signer].LastBlock == 0 {
status[signer].LastBlock = block.Number.Int64()
status[signer].Time = block.Time.Uint64()
status[signer].Difficulty = block.Difficulty.Uint64()
status[signer].LastBlock = block.BlockNumber().Int64()
status[signer].Time = block.Timestamp().Uint64()
status[signer].Difficulty = block.BlockDifficulty().Uint64()
notSeen--
}
if blockNumber == until {
......
......@@ -6,35 +6,54 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
"math/big"
)
func sigHash(header *types.Header) (hash common.Hash) {
type BlockHeader interface {
ParentHsh() common.Hash
UncleHsh() common.Hash
Miner() common.Address
RootHsh() common.Hash
TxHsh() common.Hash
ReceiptHsh() common.Hash
LogsBloom() types.Bloom
BlockDifficulty() *big.Int
BlockNumber() *big.Int
MaxGas() uint64
Gas() uint64
Timestamp() *big.Int
ExtraWithoutSignature() []byte
MixDigestHsh() common.Hash
BlockNonce() types.BlockNonce
Signature() []byte
}
func sigHash(header BlockHeader) (hash common.Hash) {
hasher := sha3.NewKeccak256()
_ = rlp.Encode(hasher, []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short
header.MixDigest,
header.Nonce,
header.ParentHsh(),
header.UncleHsh(),
header.Miner(),
header.RootHsh(),
header.TxHsh(),
header.ReceiptHsh(),
header.LogsBloom(),
header.BlockDifficulty(),
header.BlockNumber(),
header.MaxGas(),
header.Gas(),
header.Timestamp(),
header.ExtraWithoutSignature(),
header.MixDigestHsh(),
header.BlockNonce(),
})
hasher.Sum(hash[:0])
return hash
}
func GetSigner(header *types.Header) (address common.Address) {
signature := header.Extra[len(header.Extra)-65:]
func GetSigner(header BlockHeader) (address common.Address) {
hash := sigHash(header).Bytes()
pubkey, err := crypto.Ecrecover(hash, signature)
pubkey, err := crypto.Ecrecover(hash, header.Signature())
if err != nil {
panic(err)
}
......
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