From 866ff83223700991cbcf7ef5b98939a57d42e116 Mon Sep 17 00:00:00 2001 From: Uh Sado Date: Fri, 26 May 2023 08:32:22 +0000 Subject: [PATCH] wemix: Updated block coinbase retrieval mechanism to fetch latest data from governance --- wemix/admin.go | 20 ++++++++++++++++++-- wemix/miner_limit.go | 9 +++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/wemix/admin.go b/wemix/admin.go index ea196ee70baa..d3986407d0b5 100644 --- a/wemix/admin.go +++ b/wemix/admin.go @@ -1214,7 +1214,23 @@ func signBlock(height *big.Int, hash common.Hash) (coinbase common.Address, sig prvKey := admin.stack.Server().PrivateKey sig, err = crypto.Sign(data, prvKey) if admin.self != nil { - coinbase = admin.self.Addr + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + num := new(big.Int).Sub(height, common.Big1) + _, gov, _, _, err2 := admin.getRegGovEnvContracts(ctx, num) + if err2 != nil { + err = err2 + return + } + + nodeId := crypto.FromECDSAPub(&prvKey.PublicKey)[1:] + if addr, err2 := enodeExists(ctx, height, gov, nodeId); err2 != nil { + err = err2 + return + } else { + coinbase = addr + } } else if admin.nodeInfo != nil && admin.nodeInfo.ID == admin.bootNodeId { coinbase = admin.bootAccount } @@ -1244,7 +1260,7 @@ func verifyBlockSig(height *big.Int, coinbase common.Address, nodeId []byte, has data = append(height.Bytes(), hash.Bytes()...) data = crypto.Keccak256(data) } else { - if ok, err := enodeExists(ctx, height, gov, nodeId); err != nil || !ok { + if _, err := enodeExists(ctx, height, gov, nodeId); err != nil { return false } data = hash.Bytes() diff --git a/wemix/miner_limit.go b/wemix/miner_limit.go index 7452440396bd..d1283152ea3b 100644 --- a/wemix/miner_limit.go +++ b/wemix/miner_limit.go @@ -10,6 +10,7 @@ import ( "sort" "time" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/log" @@ -108,16 +109,16 @@ func coinbaseExists(ctx context.Context, height *big.Int, gov *metclient.RemoteC } // returns true if enode exists in governance at given height-1 -func enodeExists(ctx context.Context, height *big.Int, gov *metclient.RemoteContract, enode []byte) (bool, error) { +func enodeExists(ctx context.Context, height *big.Int, gov *metclient.RemoteContract, enode []byte) (common.Address, error) { e, err := getCoinbaseEnodeCache(ctx, new(big.Int).Sub(height, common.Big1), gov) if err != nil { - return false, err + return common.Address{}, err } ix, ok := e.enode2index[string(enode)] if !ok { - return false, nil + return common.Address{}, ethereum.NotFound } - return ix >= 1, nil + return e.nodes[ix-1].Addr, nil } // returns wemix nodes at given height