aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/oracle.go
blob: 493736e8cc428147f27a0366d347a120d985b3b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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,
    }
}