From 2018ab558d9fe041b533d09503e6eeeee8ad637e Mon Sep 17 00:00:00 2001 From: Miguel Montes <miguel.montes@unc.edu.ar> Date: Sat, 1 Dec 2018 22:37:02 -0300 Subject: [PATCH] =?UTF-8?q?Cambio=20del=20c=C3=B3digo=20de=20identificaci?= =?UTF-8?q?=C3=B3n=20del=20sellador=20de=20un=20bloque=20Uso=20de=20campos?= =?UTF-8?q?=20embebidos=20Creaci=C3=B3n=20de=20la=20interface=20BlockHeade?= =?UTF-8?q?r,=20implementada=20por=20Header=20y=20por=20Block?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bfa_client/src/bfa/node.go | 113 +++++++++++++++++++++++++------- bfa_client/src/clique/clique.go | 57 ++++++++++------ 2 files changed, 128 insertions(+), 42 deletions(-) diff --git a/bfa_client/src/bfa/node.go b/bfa_client/src/bfa/node.go index b0115d8..81c2580 100644 --- a/bfa_client/src/bfa/node.go +++ b/bfa_client/src/bfa/node.go @@ -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 { diff --git a/bfa_client/src/clique/clique.go b/bfa_client/src/clique/clique.go index 2e91d9b..697482b 100644 --- a/bfa_client/src/clique/clique.go +++ b/bfa_client/src/clique/clique.go @@ -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) } -- GitLab