aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/oracle.go
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-01-25 19:24:35 +0800
committerWei-Ning Huang <w@dexon.org>2019-03-12 12:19:09 +0800
commitae0afc07282e21345904f1a2177eff24d2594429 (patch)
tree9d6e436d6264664299b6cd9b106ebf45fb48d29e /core/vm/oracle.go
parent64932c8ccf85754da0631f736967bf36dc41e213 (diff)
downloaddexon-ae0afc07282e21345904f1a2177eff24d2594429.tar
dexon-ae0afc07282e21345904f1a2177eff24d2594429.tar.gz
dexon-ae0afc07282e21345904f1a2177eff24d2594429.tar.bz2
dexon-ae0afc07282e21345904f1a2177eff24d2594429.tar.lz
dexon-ae0afc07282e21345904f1a2177eff24d2594429.tar.xz
dexon-ae0afc07282e21345904f1a2177eff24d2594429.tar.zst
dexon-ae0afc07282e21345904f1a2177eff24d2594429.zip
core: vm: refactor governance and add node info oracle (#174)
Diffstat (limited to 'core/vm/oracle.go')
-rw-r--r--core/vm/oracle.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/core/vm/oracle.go b/core/vm/oracle.go
new file mode 100644
index 000000000..493736e8c
--- /dev/null
+++ b/core/vm/oracle.go
@@ -0,0 +1,88 @@
+// Copyright 2019 The dexon-consensus Authors
+// This file is part of the dexon-consensus library.
+//
+// The dexon-consensus library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "strings"
+
+ "github.com/dexon-foundation/dexon/accounts/abi"
+ "github.com/dexon-foundation/dexon/common"
+)
+
+var GovernanceContractAddress = common.HexToAddress("63751838d6485578b23e8b051d40861ecc416794")
+var NodeInfoOracleAddress = common.HexToAddress("58a7c88ad1f32e7252bebba54def98d3e7b3df11")
+
+var GovernanceABI *OracleContractABI
+var NodeInfoOracleABI *OracleContractABI
+
+func init() {
+ GovernanceABI = NewOracleContractABI(GovernanceABIJSON)
+ NodeInfoOracleABI = NewOracleContractABI(NodeInfoOracleABIJSON)
+}
+
+// OracleContract represent special system contracts written in Go.
+type OracleContract interface {
+ Run(evm *EVM, input []byte, contract *Contract) (ret []byte, err error)
+}
+
+// A map representing available system oracle contracts.
+var OracleContracts = map[common.Address]OracleContract{
+ GovernanceContractAddress: &GovernanceContract{},
+ NodeInfoOracleAddress: &NodeInfoOracleContract{},
+}
+
+// Run oracle contract.
+func RunOracleContract(oracle OracleContract, evm *EVM, input []byte, contract *Contract) (ret []byte, err error) {
+ return oracle.Run(evm, input, contract)
+}
+
+// OracleContractABI represents ABI information for a given contract.
+type OracleContractABI struct {
+ ABI abi.ABI
+ Name2Method map[string]abi.Method
+ Sig2Method map[string]abi.Method
+ Events map[string]abi.Event
+}
+
+// NewOracleContractABI parse the ABI.
+func NewOracleContractABI(abiDefinition string) *OracleContractABI {
+ abiObject, err := abi.JSON(strings.NewReader(abiDefinition))
+ if err != nil {
+ panic(err)
+ }
+
+ sig2Method := make(map[string]abi.Method)
+ name2Method := make(map[string]abi.Method)
+
+ for _, method := range abiObject.Methods {
+ sig2Method[string(method.Id())] = method
+ name2Method[method.Name] = method
+ }
+
+ events := make(map[string]abi.Event)
+ for _, event := range abiObject.Events {
+ events[event.Name] = event
+ }
+
+ return &OracleContractABI{
+ ABI: abiObject,
+ Name2Method: name2Method,
+ Sig2Method: sig2Method,
+ Events: events,
+ }
+}