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