From b9d1869d95725be88018cf4b2391afb1dc81d6cc Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 21 Jun 2022 11:32:19 +0530 Subject: [PATCH 01/10] added getHeaderWithAuthor function to change the coinbase filed in header with actual miner address --- internal/ethapi/api.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 59b6feba52..2768b6fbf3 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -814,6 +814,18 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H return nil } +// getHeaderWithAuthor: changes the miner (0x0, coinbase) with the original miner address +func (s *PublicBlockChainAPI) getHeaderWithAuthor(head *types.Header) error { + // get author using From: Backend -> Engine -> Author + author, err := s.b.Engine().Author(head) + if err != nil { + return err + } + // change the coinbase (0x0) with the miner address + head.Coinbase = author + return nil +} + // GetBlockByNumber returns the requested canonical block. // * When blockNr is -1 the chain head is returned. // * When blockNr is -2 the pending chain head is returned. From b3c02842b96709e728920ee1a3dc74fd57944623 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 21 Jun 2022 11:34:00 +0530 Subject: [PATCH 02/10] modified GetBlockByNumber and GetBlockByHash function to call getHeaderWithAuthor function --- internal/ethapi/api.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 2768b6fbf3..10656ff026 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -834,6 +834,9 @@ func (s *PublicBlockChainAPI) getHeaderWithAuthor(head *types.Header) error { func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, number) if block != nil && err == nil { + if err = s.getHeaderWithAuthor(block.Header()); err != nil { + return nil, err + } response, err := s.rpcMarshalBlock(ctx, block, true, fullTx) if err == nil && number == rpc.PendingBlockNumber { // Pending blocks need to nil out a few fields @@ -857,6 +860,9 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByHash(ctx, hash) if block != nil { + if err = s.getHeaderWithAuthor(block.Header()); err != nil { + return nil, err + } response, err := s.rpcMarshalBlock(ctx, block, true, fullTx) // append marshalled bor transaction if err == nil && response != nil { From aec6d0525593c7736000e87f81582f8716be8d53 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 22 Jun 2022 12:13:43 +0530 Subject: [PATCH 03/10] minor fix --- internal/ethapi/api.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 10656ff026..87aa6d72e7 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -814,16 +814,15 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H return nil } -// getHeaderWithAuthor: changes the miner (0x0, coinbase) with the original miner address -func (s *PublicBlockChainAPI) getHeaderWithAuthor(head *types.Header) error { +// getAuthor: returns the author of the Block +func (s *PublicBlockChainAPI) getAuthor(head *types.Header) (*common.Address, error) { // get author using From: Backend -> Engine -> Author author, err := s.b.Engine().Author(head) if err != nil { - return err + return nil, err } // change the coinbase (0x0) with the miner address - head.Coinbase = author - return nil + return &author, nil } // GetBlockByNumber returns the requested canonical block. @@ -834,9 +833,7 @@ func (s *PublicBlockChainAPI) getHeaderWithAuthor(head *types.Header) error { func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, number) if block != nil && err == nil { - if err = s.getHeaderWithAuthor(block.Header()); err != nil { - return nil, err - } + response, err := s.rpcMarshalBlock(ctx, block, true, fullTx) if err == nil && number == rpc.PendingBlockNumber { // Pending blocks need to nil out a few fields @@ -845,6 +842,14 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B } } + if err == nil && number != rpc.PendingBlockNumber { + author, err := s.getAuthor(block.Header()) + if err != nil { + return nil, err + } + response["miner"] = author + } + // append marshalled bor transaction if err == nil && response != nil { response = s.appendRPCMarshalBorTransaction(ctx, block, response, fullTx) @@ -860,12 +865,16 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByHash(ctx, hash) if block != nil { - if err = s.getHeaderWithAuthor(block.Header()); err != nil { - return nil, err - } response, err := s.rpcMarshalBlock(ctx, block, true, fullTx) // append marshalled bor transaction if err == nil && response != nil { + + author, err := s.getAuthor(block.Header()) + if err != nil { + return nil, err + } + response["miner"] = author + return s.appendRPCMarshalBorTransaction(ctx, block, response, fullTx), err } return response, err From 1970743f9d6328335f1073b71d72fac1c6c1cc23 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 24 Jun 2022 13:28:22 +0530 Subject: [PATCH 04/10] modified comment --- internal/ethapi/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 87aa6d72e7..095ba7322b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -816,7 +816,7 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H // getAuthor: returns the author of the Block func (s *PublicBlockChainAPI) getAuthor(head *types.Header) (*common.Address, error) { - // get author using From: Backend -> Engine -> Author + // get author using Author() function from: /consensus/clique/clique.go author, err := s.b.Engine().Author(head) if err != nil { return nil, err From 1b941f3274020eb94acb7b0f6b3960819544015f Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 24 Jun 2022 13:29:02 +0530 Subject: [PATCH 05/10] getAuthor returns 0x0 for genesis block --- consensus/clique/clique.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 685186817d..d63e0c37e3 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -210,6 +210,10 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique { // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (c *Clique) Author(header *types.Header) (common.Address, error) { + if header.Number.Uint64() == 0 { + add := common.HexToAddress("0x0000000000000000000000000000000000000000") + return add, nil + } return ecrecover(header, c.signatures) } From 9b29d13a5df9292fe2fa38be3be148b45c071c3e Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 24 Jun 2022 19:31:44 +0530 Subject: [PATCH 06/10] removed lint error --- internal/ethapi/api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 095ba7322b..f8924e873b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -847,6 +847,7 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B if err != nil { return nil, err } + response["miner"] = author } @@ -873,6 +874,7 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Ha if err != nil { return nil, err } + response["miner"] = author return s.appendRPCMarshalBorTransaction(ctx, block, response, fullTx), err From 0173d61d9fca9aba4a41fa3321cedb5c361b0ab9 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Sat, 25 Jun 2022 10:20:29 +0530 Subject: [PATCH 07/10] modified Authour() function in bor.go --- consensus/bor/bor.go | 4 ++++ internal/ethapi/api.go | 1 + 2 files changed, 5 insertions(+) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 2eb2645f18..37e5f87c13 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -288,6 +288,10 @@ func New( // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (c *Bor) Author(header *types.Header) (common.Address, error) { + if header.Number.Uint64() == 0 { + add := common.HexToAddress("0x0000000000000000000000000000000000000000") + return add, nil + } return ecrecover(header, c.signatures, c.config) } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index f8924e873b..5a5de6bc9d 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -817,6 +817,7 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H // getAuthor: returns the author of the Block func (s *PublicBlockChainAPI) getAuthor(head *types.Header) (*common.Address, error) { // get author using Author() function from: /consensus/clique/clique.go + // In Production: get author using Author() function from: /consensus/bor/bor.go author, err := s.b.Engine().Author(head) if err != nil { return nil, err From c0ad424824bda2cbc2659564e03a041714a230c6 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 28 Jun 2022 18:34:29 +0530 Subject: [PATCH 08/10] addressed comments, Author() never returns error, returns 0x0 insted --- consensus/bor/bor.go | 10 +++++++++- consensus/clique/clique.go | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 37e5f87c13..05246390fe 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -292,7 +292,15 @@ func (c *Bor) Author(header *types.Header) (common.Address, error) { add := common.HexToAddress("0x0000000000000000000000000000000000000000") return add, nil } - return ecrecover(header, c.signatures, c.config) + + add, err := ecrecover(header, c.signatures, c.config) + + if err != nil { + add := common.HexToAddress("0x0000000000000000000000000000000000000000") + return add, nil + } else { + return add, nil + } } // VerifyHeader checks whether a header conforms to the consensus rules. diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index d63e0c37e3..ce9b636764 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -214,7 +214,15 @@ func (c *Clique) Author(header *types.Header) (common.Address, error) { add := common.HexToAddress("0x0000000000000000000000000000000000000000") return add, nil } - return ecrecover(header, c.signatures) + + add, err := ecrecover(header, c.signatures) + + if err != nil { + add := common.HexToAddress("0x0000000000000000000000000000000000000000") + return add, nil + } else { + return add, nil + } } // VerifyHeader checks whether a header conforms to the consensus rules. From de03f3fb6a8ff5a02e94e53a7e033e960b08a34c Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 28 Jun 2022 19:02:47 +0530 Subject: [PATCH 09/10] reverted back and made changes in the getAuthour API --- consensus/bor/bor.go | 10 +--------- consensus/clique/clique.go | 10 +--------- internal/ethapi/api.go | 4 +++- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 05246390fe..37e5f87c13 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -292,15 +292,7 @@ func (c *Bor) Author(header *types.Header) (common.Address, error) { add := common.HexToAddress("0x0000000000000000000000000000000000000000") return add, nil } - - add, err := ecrecover(header, c.signatures, c.config) - - if err != nil { - add := common.HexToAddress("0x0000000000000000000000000000000000000000") - return add, nil - } else { - return add, nil - } + return ecrecover(header, c.signatures, c.config) } // VerifyHeader checks whether a header conforms to the consensus rules. diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index ce9b636764..d63e0c37e3 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -214,15 +214,7 @@ func (c *Clique) Author(header *types.Header) (common.Address, error) { add := common.HexToAddress("0x0000000000000000000000000000000000000000") return add, nil } - - add, err := ecrecover(header, c.signatures) - - if err != nil { - add := common.HexToAddress("0x0000000000000000000000000000000000000000") - return add, nil - } else { - return add, nil - } + return ecrecover(header, c.signatures) } // VerifyHeader checks whether a header conforms to the consensus rules. diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 5a5de6bc9d..9f57da33b9 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -819,8 +819,10 @@ func (s *PublicBlockChainAPI) getAuthor(head *types.Header) (*common.Address, er // get author using Author() function from: /consensus/clique/clique.go // In Production: get author using Author() function from: /consensus/bor/bor.go author, err := s.b.Engine().Author(head) + // make sure we don't send error to the user, return 0x0 instead if err != nil { - return nil, err + add := common.HexToAddress("0x0000000000000000000000000000000000000000") + return &add, nil } // change the coinbase (0x0) with the miner address return &author, nil From 473e09cd2d348ecb792ffe44f64cbea072759f0b Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 28 Jun 2022 21:03:36 +0530 Subject: [PATCH 10/10] removed check from consensus, handelling everything in api.go --- consensus/bor/bor.go | 4 ---- consensus/clique/clique.go | 4 ---- internal/ethapi/api.go | 17 +++++------------ 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 37e5f87c13..2eb2645f18 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -288,10 +288,6 @@ func New( // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (c *Bor) Author(header *types.Header) (common.Address, error) { - if header.Number.Uint64() == 0 { - add := common.HexToAddress("0x0000000000000000000000000000000000000000") - return add, nil - } return ecrecover(header, c.signatures, c.config) } diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index d63e0c37e3..685186817d 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -210,10 +210,6 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique { // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (c *Clique) Author(header *types.Header) (common.Address, error) { - if header.Number.Uint64() == 0 { - add := common.HexToAddress("0x0000000000000000000000000000000000000000") - return add, nil - } return ecrecover(header, c.signatures) } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 9f57da33b9..2e22095460 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -815,17 +815,17 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H } // getAuthor: returns the author of the Block -func (s *PublicBlockChainAPI) getAuthor(head *types.Header) (*common.Address, error) { +func (s *PublicBlockChainAPI) getAuthor(head *types.Header) *common.Address { // get author using Author() function from: /consensus/clique/clique.go // In Production: get author using Author() function from: /consensus/bor/bor.go author, err := s.b.Engine().Author(head) // make sure we don't send error to the user, return 0x0 instead if err != nil { add := common.HexToAddress("0x0000000000000000000000000000000000000000") - return &add, nil + return &add } // change the coinbase (0x0) with the miner address - return &author, nil + return &author } // GetBlockByNumber returns the requested canonical block. @@ -846,10 +846,7 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B } if err == nil && number != rpc.PendingBlockNumber { - author, err := s.getAuthor(block.Header()) - if err != nil { - return nil, err - } + author := s.getAuthor(block.Header()) response["miner"] = author } @@ -872,11 +869,7 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Ha response, err := s.rpcMarshalBlock(ctx, block, true, fullTx) // append marshalled bor transaction if err == nil && response != nil { - - author, err := s.getAuthor(block.Header()) - if err != nil { - return nil, err - } + author := s.getAuthor(block.Header()) response["miner"] = author