github.com/Gessiux/neatchain@v1.3.1/neatabi/abi/abi.go (about)

     1  package abi
     2  
     3  import (
     4  	"math/big"
     5  	"strings"
     6  
     7  	"github.com/Gessiux/neatchain/chain/accounts/abi"
     8  	"github.com/Gessiux/neatchain/utilities/common"
     9  )
    10  
    11  type FunctionType struct {
    12  	id    int
    13  	cross bool // Tx type, cross chain / non cross chain
    14  	main  bool // allow to be execute on main chain or not
    15  	side  bool // allow to be execute on side chain or not
    16  }
    17  
    18  var (
    19  	// Cross Chain Function
    20  	CreateSideChain       = FunctionType{0, true, true, false}
    21  	JoinSideChain         = FunctionType{1, true, true, false}
    22  	DepositInMainChain    = FunctionType{2, true, true, false}
    23  	DepositInSideChain    = FunctionType{3, true, false, true}
    24  	WithdrawFromSideChain = FunctionType{4, true, false, true}
    25  	WithdrawFromMainChain = FunctionType{5, true, true, false}
    26  	SaveDataToMainChain   = FunctionType{6, true, true, false}
    27  	SetBlockReward        = FunctionType{7, true, false, true}
    28  	// Non-Cross Chain Function
    29  	VoteNextEpoch  = FunctionType{10, false, true, true}
    30  	RevealVote     = FunctionType{11, false, true, true}
    31  	Delegate       = FunctionType{12, false, true, true}
    32  	UnDelegate     = FunctionType{13, false, true, true}
    33  	Register       = FunctionType{14, false, true, true}
    34  	UnRegister     = FunctionType{15, false, true, true}
    35  	EditValidator  = FunctionType{16, false, true, true}
    36  	WithdrawReward = FunctionType{17, false, true, true}
    37  	UnForbidden    = FunctionType{18, false, true, true}
    38  	SetCommission  = FunctionType{19, false, true, true}
    39  	// Unknown
    40  	Unknown = FunctionType{-1, false, false, false}
    41  )
    42  
    43  func (t FunctionType) IsCrossChainType() bool {
    44  	return t.cross
    45  }
    46  
    47  func (t FunctionType) AllowInMainChain() bool {
    48  	return t.main
    49  }
    50  
    51  func (t FunctionType) AllowInSideChain() bool {
    52  	return t.side
    53  }
    54  
    55  func (t FunctionType) RequiredGas() uint64 {
    56  	switch t {
    57  	case CreateSideChain:
    58  		return 200000
    59  	case JoinSideChain:
    60  		return 100000
    61  	case DepositInMainChain:
    62  		return 200000
    63  	case DepositInSideChain:
    64  		return 0
    65  	case WithdrawFromSideChain:
    66  		return 200000
    67  	case WithdrawFromMainChain:
    68  		return 0
    69  	case SaveDataToMainChain:
    70  		return 0
    71  	case VoteNextEpoch:
    72  		return 100000
    73  	case RevealVote:
    74  		return 100000
    75  	case Delegate, UnDelegate, Register, UnRegister:
    76  		return 100000
    77  	case SetBlockReward:
    78  		return 100000
    79  	case EditValidator:
    80  		return 100000
    81  	case WithdrawReward:
    82  		return 100000
    83  	case UnForbidden:
    84  		return 100000
    85  	case SetCommission:
    86  		return 100000
    87  	default:
    88  		return 0
    89  	}
    90  }
    91  
    92  func (t FunctionType) String() string {
    93  	switch t {
    94  	case CreateSideChain:
    95  		return "CreateSideChain"
    96  	case JoinSideChain:
    97  		return "JoinSideChain"
    98  	case DepositInMainChain:
    99  		return "DepositInMainChain"
   100  	case DepositInSideChain:
   101  		return "DepositInSideChain"
   102  	case WithdrawFromSideChain:
   103  		return "WithdrawFromSideChain"
   104  	case WithdrawFromMainChain:
   105  		return "WithdrawFromMainChain"
   106  	case SaveDataToMainChain:
   107  		return "SaveDataToMainChain"
   108  	case VoteNextEpoch:
   109  		return "VoteNextEpoch"
   110  	case RevealVote:
   111  		return "RevealVote"
   112  	case Delegate:
   113  		return "Delegate"
   114  	case UnDelegate:
   115  		return "UnDelegate"
   116  	case Register:
   117  		return "Register"
   118  	case UnRegister:
   119  		return "UnRegister"
   120  	case SetBlockReward:
   121  		return "SetBlockReward"
   122  	case EditValidator:
   123  		return "EditValidator"
   124  	case WithdrawReward:
   125  		return "WithdrawReward"
   126  	case UnForbidden:
   127  		return "UnForbidden"
   128  	case SetCommission:
   129  		return "SetCommission"
   130  	default:
   131  		return "UnKnown"
   132  	}
   133  }
   134  
   135  func StringToFunctionType(s string) FunctionType {
   136  	switch s {
   137  	case "CreateSideChain":
   138  		return CreateSideChain
   139  	case "JoinSideChain":
   140  		return JoinSideChain
   141  	case "DepositInMainChain":
   142  		return DepositInMainChain
   143  	case "DepositInSideChain":
   144  		return DepositInSideChain
   145  	case "WithdrawFromSideChain":
   146  		return WithdrawFromSideChain
   147  	case "WithdrawFromMainChain":
   148  		return WithdrawFromMainChain
   149  	case "SaveDataToMainChain":
   150  		return SaveDataToMainChain
   151  	case "VoteNextEpoch":
   152  		return VoteNextEpoch
   153  	case "RevealVote":
   154  		return RevealVote
   155  	case "Delegate":
   156  		return Delegate
   157  	case "UnDelegate":
   158  		return UnDelegate
   159  	case "Register":
   160  		return Register
   161  	case "UnRegister":
   162  		return UnRegister
   163  	case "SetBlockReward":
   164  		return SetBlockReward
   165  	case "EditValidator":
   166  		return EditValidator
   167  	case "WithdrawReward":
   168  		return WithdrawReward
   169  	case "UnForbidden":
   170  		return UnForbidden
   171  	case "SetCommission":
   172  		return SetCommission
   173  	default:
   174  		return Unknown
   175  	}
   176  }
   177  
   178  type CreateSideChainArgs struct {
   179  	ChainId          string
   180  	MinValidators    uint16
   181  	MinDepositAmount *big.Int
   182  	StartBlock       *big.Int
   183  	EndBlock         *big.Int
   184  }
   185  
   186  type JoinSideChainArgs struct {
   187  	PubKey    []byte
   188  	ChainId   string
   189  	Signature []byte
   190  }
   191  
   192  type DepositInMainChainArgs struct {
   193  	ChainId string
   194  }
   195  
   196  type DepositInSideChainArgs struct {
   197  	ChainId string
   198  	TxHash  common.Hash
   199  }
   200  
   201  type WithdrawFromSideChainArgs struct {
   202  	ChainId string
   203  }
   204  
   205  type WithdrawFromMainChainArgs struct {
   206  	ChainId string
   207  	Amount  *big.Int
   208  	TxHash  common.Hash
   209  }
   210  
   211  type VoteNextEpochArgs struct {
   212  	VoteHash common.Hash
   213  }
   214  
   215  type RevealVoteArgs struct {
   216  	PubKey    []byte
   217  	Amount    *big.Int
   218  	Salt      string
   219  	Signature []byte
   220  }
   221  
   222  type DelegateArgs struct {
   223  	Candidate common.Address
   224  }
   225  
   226  type UnDelegateArgs struct {
   227  	Candidate common.Address
   228  	Amount    *big.Int
   229  }
   230  
   231  type RegisterArgs struct {
   232  	Pubkey     []byte
   233  	Signature  []byte
   234  	Commission uint8
   235  }
   236  
   237  type SetBlockRewardArgs struct {
   238  	ChainId string
   239  	Reward  *big.Int
   240  }
   241  
   242  type EditValidatorArgs struct {
   243  	Moniker  string
   244  	Website  string
   245  	Identity string
   246  	Details  string
   247  }
   248  
   249  type WithdrawRewardArgs struct {
   250  	DelegateAddress common.Address
   251  }
   252  
   253  type UnForbiddenArgs struct {
   254  }
   255  
   256  type SetCommissionArgs struct {
   257  	Commission uint8
   258  }
   259  
   260  const jsonChainABI = `
   261  [
   262  	{
   263  		"type": "function",
   264  		"name": "CreateSideChain",
   265  		"constant": false,
   266  		"inputs": [
   267  			{
   268  				"name": "chainId",
   269  				"type": "string"
   270  			},
   271  			{
   272  				"name": "minValidators",
   273  				"type": "uint16"
   274  			},
   275  			{
   276  				"name": "minDepositAmount",
   277  				"type": "uint256"
   278  			},
   279  			{
   280  				"name": "startBlock",
   281  				"type": "uint256"
   282  			},
   283  			{
   284  				"name": "endBlock",
   285  				"type": "uint256"
   286  			}
   287  		]
   288  	},
   289  	{
   290  		"type": "function",
   291  		"name": "JoinSideChain",
   292  		"constant": false,
   293  		"inputs": [
   294  			{
   295  				"name": "pubKey",
   296  				"type": "bytes"
   297  			},
   298  			{
   299  				"name": "chainId",
   300  				"type": "string"
   301  			},
   302  			{
   303  				"name": "signature",
   304  				"type": "bytes"
   305  			}
   306  		]
   307  	},
   308  	{
   309  		"type": "function",
   310  		"name": "DepositInMainChain",
   311  		"constant": false,
   312  		"inputs": [
   313  			{
   314  				"name": "chainId",
   315  				"type": "string"
   316  			}
   317  		]
   318  	},
   319  	{
   320  		"type": "function",
   321  		"name": "DepositInSideChain",
   322  		"constant": false,
   323  		"inputs": [
   324  			{
   325  				"name": "chainId",
   326  				"type": "string"
   327  			},
   328  			{
   329  				"name": "txHash",
   330  				"type": "bytes32"
   331  			}
   332  		]
   333  	},
   334  	{
   335  		"type": "function",
   336  		"name": "WithdrawFromSideChain",
   337  		"constant": false,
   338  		"inputs": [
   339  			{
   340  				"name": "chainId",
   341  				"type": "string"
   342  			}
   343  		]
   344  	},
   345  	{
   346  		"type": "function",
   347  		"name": "WithdrawFromMainChain",
   348  		"constant": false,
   349  		"inputs": [
   350  			{
   351  				"name": "chainId",
   352  				"type": "string"
   353  			},
   354  			{
   355  				"name": "amount",
   356  				"type": "uint256"
   357  			},
   358  			{
   359  				"name": "txHash",
   360  				"type": "bytes32"
   361  			}
   362  		]
   363  	},
   364  	{
   365  		"type": "function",
   366  		"name": "SaveDataToMainChain",
   367  		"constant": false,
   368  		"inputs": [
   369  			{
   370  				"name": "data",
   371  				"type": "bytes"
   372  			}
   373  		]
   374  	},
   375  	{
   376  		"type": "function",
   377  		"name": "VoteNextEpoch",
   378  		"constant": false,
   379  		"inputs": [
   380  			{
   381  				"name": "voteHash",
   382  				"type": "bytes32"
   383  			}
   384  		]
   385  	},
   386  	{
   387  		"type": "function",
   388  		"name": "RevealVote",
   389  		"constant": false,
   390  		"inputs": [
   391  			{
   392  				"name": "pubKey",
   393  				"type": "bytes"
   394  			},
   395  			{
   396  				"name": "amount",
   397  				"type": "uint256"
   398  			},
   399  			{
   400  				"name": "salt",
   401  				"type": "string"
   402  			},
   403  			{
   404  				"name": "signature",
   405  				"type": "bytes"
   406  			}
   407  		]
   408  	},
   409  	{
   410  		"type": "function",
   411  		"name": "Delegate",
   412  		"constant": false,
   413  		"inputs": [
   414  			{
   415  				"name": "candidate",
   416  				"type": "address"
   417  			}
   418  		]
   419  	},
   420  	{
   421  		"type": "function",
   422  		"name": "UnDelegate",
   423  		"constant": false,
   424  		"inputs": [
   425  			{
   426  				"name": "candidate",
   427  				"type": "address"
   428  			},
   429  			{
   430  				"name": "amount",
   431  				"type": "uint256"
   432  			}
   433  		]
   434  	},
   435  	{
   436  		"type": "function",
   437  		"name": "Register",
   438  		"constant": false,
   439  		"inputs": [
   440  			{
   441  				"name": "pubkey",
   442  				"type": "bytes"
   443  			},
   444              {
   445  				"name": "signature",
   446  				"type": "bytes"
   447  			},
   448  			{
   449  				"name": "commission",
   450  				"type": "uint8"
   451  			}
   452  		]
   453  	},
   454  	{
   455  		"type": "function",
   456  		"name": "UnRegister",
   457  		"constant": false,
   458  		"inputs": []
   459  	},
   460  	{
   461  		"type": "function",
   462  		"name": "SetBlockReward",
   463  		"constant": false,
   464  		"inputs": [
   465  			{
   466  				"name": "chainId",
   467  				"type": "string"
   468  			},
   469  			{
   470  				"name": "reward",
   471  				"type": "uint256"
   472  			}
   473  		]
   474  	},
   475  	{
   476  		"type": "function",
   477  		"name": "EditValidator",
   478  		"constant": false,
   479  		"inputs": [
   480  			{
   481  				"name": "moniker",
   482  				"type": "string"
   483  			},
   484  			{
   485  				"name": "website",
   486  				"type": "string"
   487  			},
   488  			{
   489  				"name": "identity",
   490  				"type": "string"
   491  			},
   492  			{
   493  				"name": "details",
   494  				"type": "string"
   495  			}
   496  		]
   497  	},
   498  	{
   499  		"type": "function",
   500  		"name": "WithdrawReward",
   501  		"constant": false,
   502  		"inputs": [
   503  			{
   504  				"name": "delegateAddress",
   505  				"type": "address"
   506  			}
   507  		]
   508  	},
   509  	{
   510  		"type": "function",
   511  		"name": "UnForbidden",
   512  		"constant": false,
   513  		"inputs": []
   514  	},
   515  	{
   516  		"type": "function",
   517  		"name": "SetCommission",
   518  		"constant": false,
   519  		"inputs": [
   520  			{
   521  				"name": "commission",
   522  				"type": "uint8"
   523  			}
   524  		]
   525  	}
   526  ]`
   527  
   528  // NeatChain Side Chain Token Incentive Address
   529  var SideChainTokenIncentiveAddr = common.StringToAddress("NEATCCCCCCCCCCCCCCCCCCCCCCCCCCCC")
   530  
   531  // NeatChain Internal Contract Address
   532  var ChainContractMagicAddr = common.StringToAddress("NEATBBBBBBBBBBBBBBBBBBBBBBBBBBBB") // don't conflict with neatchain/core/vm/contracts.go
   533  
   534  var ChainABI abi.ABI
   535  
   536  func init() {
   537  	var err error
   538  	ChainABI, err = abi.JSON(strings.NewReader(jsonChainABI))
   539  	if err != nil {
   540  		panic("fail to create the chain ABI: " + err.Error())
   541  	}
   542  }
   543  
   544  func IsNeatChainContractAddr(addr *common.Address) bool {
   545  	return addr != nil && *addr == ChainContractMagicAddr
   546  }
   547  
   548  func FunctionTypeFromId(sigdata []byte) (FunctionType, error) {
   549  	m, err := ChainABI.MethodById(sigdata)
   550  	if err != nil {
   551  		return Unknown, err
   552  	}
   553  
   554  	return StringToFunctionType(m.Name), nil
   555  }