aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-03-19 18:47:24 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:58 +0800
commitb62994e237147468d55fa2aa5013d68ff5278580 (patch)
tree1537d512f5e22d30eb33ff2c4474cb87ba97056a
parentbe7d019ae93a6c044dfa4f3c87695c94d3e88b5c (diff)
downloaddexon-b62994e237147468d55fa2aa5013d68ff5278580.tar.gz
dexon-b62994e237147468d55fa2aa5013d68ff5278580.tar.zst
dexon-b62994e237147468d55fa2aa5013d68ff5278580.zip
core: vm: emit event when transfering owner (#277)
-rw-r--r--core/vm/oracle_contract_abi.go17
-rw-r--r--core/vm/oracle_contracts.go17
-rw-r--r--core/vm/oracle_contracts_test.go14
3 files changed, 48 insertions, 0 deletions
diff --git a/core/vm/oracle_contract_abi.go b/core/vm/oracle_contract_abi.go
index af0affece..f0845eb7b 100644
--- a/core/vm/oracle_contract_abi.go
+++ b/core/vm/oracle_contract_abi.go
@@ -659,6 +659,23 @@ const GovernanceABIJSON = `
"type": "address"
},
{
+ "indexed": true,
+ "name": "NewOwnerAddress",
+ "type": "address"
+ }
+ ],
+ "name": "NodeOwnershipTransfered",
+ "type": "event"
+ },
+ {
+ "anonymous": false,
+ "inputs": [
+ {
+ "indexed": true,
+ "name": "NodeAddress",
+ "type": "address"
+ },
+ {
"indexed": false,
"name": "Amount",
"type": "uint256"
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go
index 62261a36b..f21700d86 100644
--- a/core/vm/oracle_contracts.go
+++ b/core/vm/oracle_contracts.go
@@ -1058,6 +1058,16 @@ func (s *GovernanceState) emitCRSProposed(round *big.Int, crs common.Hash) {
})
}
+// event NodeOwnershipTransfered(address indexed NodeAddress, address indexed NewOwnerAddress);
+func (s *GovernanceState) emitNodeOwnershipTransfered(nodeAddr, newNodeAddr common.Address) {
+ s.StateDB.AddLog(&types.Log{
+ Address: GovernanceContractAddress,
+ Topics: []common.Hash{GovernanceABI.Events["NodeOwnershipTransfered"].Id(),
+ nodeAddr.Hash(), newNodeAddr.Hash()},
+ Data: []byte{},
+ })
+}
+
// event Staked(address indexed NodeAddress, uint256 Amount);
func (s *GovernanceState) emitStaked(nodeAddr common.Address, amount *big.Int) {
s.StateDB.AddLog(&types.Log{
@@ -2373,6 +2383,11 @@ func (g *GovernanceContract) transferNodeOwnership(newOwner common.Address) ([]b
return nil, errExecutionReverted
}
+ newOffset := g.state.NodesOffsetByAddress(newOwner)
+ if newOffset.Cmp(big.NewInt(0)) >= 0 {
+ return nil, errExecutionReverted
+ }
+
node := g.state.Node(offset)
g.state.PutNodeOffsets(node, big.NewInt(0))
@@ -2380,6 +2395,8 @@ func (g *GovernanceContract) transferNodeOwnership(newOwner common.Address) ([]b
g.state.PutNodeOffsets(node, offset)
g.state.UpdateNode(offset, node)
+ g.state.emitNodeOwnershipTransfered(caller, newOwner)
+
return nil, nil
}
diff --git a/core/vm/oracle_contracts_test.go b/core/vm/oracle_contracts_test.go
index 69e13e1a1..4b25b39da 100644
--- a/core/vm/oracle_contracts_test.go
+++ b/core/vm/oracle_contracts_test.go
@@ -271,6 +271,20 @@ func (g *OracleContractsTestSuite) TestTransferNodeOwnership() {
g.Require().Equal(uint64(0), g.s.NodesOffsetByNodeKeyAddress(nodeKeyAddr).Uint64())
g.Require().Equal(offset.Uint64(), g.s.NodesOffsetByAddress(newAddr).Uint64())
g.Require().Equal(offset.Uint64(), g.s.NodesOffsetByNodeKeyAddress(newNodeKeyAddr).Uint64())
+
+ // Call with owner.
+ privKey2, addr2 := g.newPrefundAccount()
+ pk2 := crypto.FromECDSAPub(&privKey2.PublicKey)
+ input, err = GovernanceABI.ABI.Pack("register", pk2, "Test2", "test1@dexon.org", "Taipei", "https://dexon.org")
+ g.Require().NoError(err)
+ _, err = g.call(GovernanceContractAddress, addr2, input, amount)
+ g.Require().NoError(err)
+
+ // Transfer to duplicate owner address.
+ input, err = GovernanceABI.ABI.Pack("transferNodeOwnership", addr2)
+ g.Require().NoError(err)
+ _, err = g.call(GovernanceContractAddress, newAddr, input, amount)
+ g.Require().Error(err)
}
func (g *OracleContractsTestSuite) TestStakingMechanism() {