diff options
-rw-r--r-- | core/vm/oracle_contracts.go | 11 | ||||
-rw-r--r-- | core/vm/oracle_contracts_test.go | 16 |
2 files changed, 24 insertions, 3 deletions
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go index eebfa9a3a..72d929843 100644 --- a/core/vm/oracle_contracts.go +++ b/core/vm/oracle_contracts.go @@ -2730,13 +2730,18 @@ func (g *GovernanceContract) replaceNodePublicKey(newPublicKey []byte) ([]byte, return nil, errExecutionReverted } - node := g.state.Node(offset) - - _, err := publicKeyToNodeKeyAddress(newPublicKey) + newNodeKeyAddr, err := publicKeyToNodeKeyAddress(newPublicKey) if err != nil { return nil, errExecutionReverted } + newNodeKeyOffset := g.state.NodesOffsetByNodeKeyAddress(newNodeKeyAddr) + if newNodeKeyOffset.Cmp(big.NewInt(0)) >= 0 { + return nil, errExecutionReverted + } + + node := g.state.Node(offset) + g.state.DeleteNodeOffsets(node) node.PublicKey = newPublicKey diff --git a/core/vm/oracle_contracts_test.go b/core/vm/oracle_contracts_test.go index cbf08c273..7605fadf8 100644 --- a/core/vm/oracle_contracts_test.go +++ b/core/vm/oracle_contracts_test.go @@ -384,6 +384,22 @@ func (g *OracleContractsTestSuite) TestReplaceNodePublicKey() { g.Require().Equal(-1, int(g.s.NodesOffsetByNodeKeyAddress(addr).Int64())) g.Require().Equal(0, int(g.s.NodesOffsetByAddress(addr).Int64())) g.Require().Equal(0, int(g.s.NodesOffsetByNodeKeyAddress(addr2).Int64())) + + // Duplicate NodeKey. + _, addr3 := newPrefundAccount(g.stateDB) + pk3 := crypto.FromECDSAPub(&privKey.PublicKey) + input, err = GovernanceABI.ABI.Pack("register", pk3, "Test2", "test2@dexon.org", "Taipei", "https://dexon.org") + g.Require().NoError(err) + + _, err = g.call(GovernanceContractAddress, addr3, input, amount) + g.Require().NoError(err) + + input, err = GovernanceABI.ABI.Pack("replaceNodePublicKey", pk2) + g.Require().NoError(err) + + // Duplicate nodekey + _, err = g.call(GovernanceContractAddress, addr3, input, big.NewInt(0)) + g.Require().Error(err) } func (g *OracleContractsTestSuite) TestStakingMechanism() { |