diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-05-19 21:26:02 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-09-17 16:57:29 +0800 |
commit | 2844d2aad3aa29e4e880aec6ab425dd1796672e7 (patch) | |
tree | 496e160c1bbf818542a99b21ba634886757f2caf | |
parent | e84135b88ceea2dda682c8e4a8a20c02f59fdde4 (diff) | |
download | go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar.gz go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar.bz2 go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar.lz go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar.xz go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.tar.zst go-tangerine-2844d2aad3aa29e4e880aec6ab425dd1796672e7.zip |
core: vm: prevent replace duplicate nodeKey
-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() { |