github.com/annchain/OG@v0.0.9/rpc/debugger_controllers.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package rpc
    15  
    16  import (
    17  	"encoding/hex"
    18  	"fmt"
    19  	"github.com/annchain/OG/common"
    20  	"github.com/annchain/OG/common/crypto"
    21  	"github.com/annchain/OG/common/math"
    22  	"github.com/gin-gonic/gin"
    23  	"net/http"
    24  )
    25  
    26  func (r *RpcController) DebugCreateContract() error {
    27  	from := common.HexToAddress("0x60ce04e6a1cc8887fa5dcd43f87c38be1d41827e")
    28  	to := common.BytesToAddress(nil)
    29  	value := math.NewBigInt(0)
    30  	nonce := uint64(1)
    31  
    32  	contractCode := "6060604052341561000f57600080fd5b600a60008190555060006001819055506102078061002e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631c0f72e11461006b57806360fe47b114610094578063c605f76c146100b7578063e5aa3d5814610145575b34600181905550005b341561007657600080fd5b61007e61016e565b6040518082815260200191505060405180910390f35b341561009f57600080fd5b6100b56004808035906020019091905050610174565b005b34156100c257600080fd5b6100ca61017e565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561010a5780820151818401526020810190506100ef565b50505050905090810190601f1680156101375780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015057600080fd5b6101586101c1565b6040518082815260200191505060405180910390f35b60015481565b8060008190555050565b6101866101c7565b6040805190810160405280600a81526020017f68656c6c6f576f726c6400000000000000000000000000000000000000000000815250905090565b60005481565b6020604051908101604052806000815250905600a165627a7a723058208e1bdbeee227900e60082cfcc0e44d400385e8811ae77ac6d7f3b72f630f04170029"
    33  	data, _ := hex.DecodeString(contractCode)
    34  
    35  	return r.DebugCreateTxAndSendToBuffer(from, to, value, data, nonce)
    36  }
    37  
    38  func (r *RpcController) DebugQueryContract() ([]byte, error) {
    39  	from := common.HexToAddress("0x60ce04e6a1cc8887fa5dcd43f87c38be1d41827e")
    40  	contractAddr := crypto.CreateAddress(from, uint64(1))
    41  
    42  	calldata := "e5aa3d58"
    43  	callTx := &types.Tx{}
    44  	callTx.From = &from
    45  	callTx.Value = math.NewBigInt(0)
    46  	callTx.To = contractAddr
    47  	callTx.Data, _ = hex.DecodeString(calldata)
    48  
    49  	b, _, err := r.Og.Dag.ProcessTransaction(callTx, false)
    50  	return b, err
    51  }
    52  
    53  func (r *RpcController) DebugSetContract(n string) error {
    54  	from := common.HexToAddress("0x60ce04e6a1cc8887fa5dcd43f87c38be1d41827e")
    55  	to := crypto.CreateAddress(from, uint64(1))
    56  	value := math.NewBigInt(0)
    57  	curnonce, err := r.Og.Dag.GetLatestNonce(from)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	nonce := curnonce + 1
    62  	setdata := fmt.Sprintf("60fe47b1%s", n)
    63  	data, err := hex.DecodeString(setdata)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	return r.DebugCreateTxAndSendToBuffer(from, to, value, data, nonce)
    69  }
    70  
    71  func (r *RpcController) DebugCallerCreate() error {
    72  	from := common.HexToAddress("0xc18969f0b7e3d192d86e220c44be2f03abdaeda2")
    73  	to := common.BytesToAddress(nil)
    74  	value := math.NewBigInt(0)
    75  	nonce := uint64(1)
    76  
    77  	contractCode := "6060604052341561000f57600080fd5b732fd82147682e011063adb8534b2d1d8831f529696000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610247806100d46000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063671ac9c51461005c578063ceee2e201461007f578063f1850c1b146100d4575b600080fd5b341561006757600080fd5b61007d6004808035906020019091905050610129565b005b341561008a57600080fd5b6100926101d0565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100df57600080fd5b6100e76101f5565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166360fe47b1826040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b15156101b957600080fd5b6102c65a03f115156101ca57600080fd5b50505050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820a1651c4d2b88805e349363d97e5141614ba060f6fd871434b856d5cc689006eb0029"
    78  	data, _ := hex.DecodeString(contractCode)
    79  
    80  	return r.DebugCreateTxAndSendToBuffer(from, to, value, data, nonce)
    81  }
    82  
    83  func (r *RpcController) DebugCallerCall(setvalue string) error {
    84  	from := common.HexToAddress("0xc18969f0b7e3d192d86e220c44be2f03abdaeda2")
    85  	to := crypto.CreateAddress(from, uint64(1))
    86  	value := math.NewBigInt(0)
    87  	curnonce, err := r.Og.Dag.GetLatestNonce(from)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	nonce := curnonce + 1
    92  	datastr := fmt.Sprintf("671ac9c5%s", setvalue)
    93  	data, err := hex.DecodeString(datastr)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	return r.DebugCreateTxAndSendToBuffer(from, to, value, data, nonce)
    99  }
   100  
   101  func (r *RpcController) DebugCreateTxAndSendToBuffer(from, to common.Address, value *math.BigInt, data []byte, nonce uint64) error {
   102  	pubstr := "0x0104a391a55b84e45858748324534a187c60046266b17a5c77161104c4a6c4b1511789de692b898768ff6ee731e2fe068d6234a19a1e20246c968df9c0ca797498e7"
   103  	pub, _ := crypto.PublicKeyFromString(pubstr)
   104  
   105  	sigstr := "6060604052341561000f57600080fd5b600a60008190555060006001819055506102078061002e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631c0f72e11461006b57806360fe47b114610094578063c605f76c146100b7578063e5aa3d5814610145575b34600181905550005b341561007657600080fd5b61007e61016e565b6040518082815260200191505060405180910390f35b341561009f57600080fd5b6100b56004808035906020019091905050610174565b005b34156100c257600080fd5b6100ca61017e565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561010a5780820151818401526020810190506100ef565b50505050905090810190601f1680156101375780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015057600080fd5b6101586101c1565b6040518082815260200191505060405180910390f35b60015481565b8060008190555050565b6101866101c7565b6040805190810160405280600a81526020017f68656c6c6f576f726c6400000000000000000000000000000000000000000000815250905090565b60005481565b6020604051908101604052806000815250905600a165627a7a723058208e1bdbeee227900e60082cfcc0e44d400385e8811ae77ac6d7f3b72f630f04170029"
   106  	sigb, _ := hex.DecodeString(sigstr)
   107  	sig := crypto.SignatureFromBytes(crypto.CryptoTypeSecp256k1, sigb)
   108  
   109  	tx, err := r.TxCreator.NewTxWithSeal(ogcore.TxWithSealBuildRequest{
   110  		From:    from,
   111  		To:      to,
   112  		Value:   value,
   113  		Data:    data,
   114  		Nonce:   nonce,
   115  		Pubkey:  pub,
   116  		Sig:     sig,
   117  		TokenId: 0,
   118  	})
   119  	if err != nil {
   120  		return err
   121  	}
   122  	r.TxBuffer.ReceivedNewTxChan <- tx
   123  	return nil
   124  }
   125  
   126  func (r *RpcController) Debug(c *gin.Context) {
   127  	p := c.Request.URL.Query().Get("f")
   128  	switch p {
   129  	case "1":
   130  		r.NewRequestChan <- types.TxBaseTypeNormal
   131  	case "2":
   132  		r.NewRequestChan <- types.TxBaseTypeSequencer
   133  	case "cc":
   134  		err := r.DebugCreateContract()
   135  		if err != nil {
   136  			Response(c, http.StatusInternalServerError, fmt.Errorf("new contract failed, err: %v", err), nil)
   137  			return
   138  		} else {
   139  			Response(c, http.StatusOK, nil, nil)
   140  			return
   141  		}
   142  	case "qc":
   143  		ret, err := r.DebugQueryContract()
   144  		if err != nil {
   145  			Response(c, http.StatusInternalServerError, fmt.Errorf("query contract failed, err: %v", err), nil)
   146  			return
   147  		} else {
   148  			Response(c, http.StatusOK, nil, fmt.Sprintf("%x", ret))
   149  			return
   150  		}
   151  	case "sc":
   152  		param := c.Request.URL.Query().Get("param")
   153  		err := r.DebugSetContract(param)
   154  		if err != nil {
   155  			Response(c, http.StatusInternalServerError, fmt.Errorf("set contract failed, err: %v", err), nil)
   156  			return
   157  		} else {
   158  			Response(c, http.StatusOK, nil, nil)
   159  			return
   160  		}
   161  	case "callerCreate":
   162  		err := r.DebugCallerCreate()
   163  		if err != nil {
   164  			Response(c, http.StatusInternalServerError, fmt.Errorf("create caller contract failed, err: %v", err), nil)
   165  			return
   166  		} else {
   167  			Response(c, http.StatusOK, nil, nil)
   168  			return
   169  		}
   170  	case "callerCall":
   171  		value := c.Request.URL.Query().Get("value")
   172  		err := r.DebugCallerCall(value)
   173  		if err != nil {
   174  			Response(c, http.StatusInternalServerError, fmt.Errorf("call caller contract failed, err: %v", err), nil)
   175  			return
   176  		} else {
   177  			Response(c, http.StatusOK, nil, nil)
   178  			return
   179  		}
   180  	}
   181  }