-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/evm' into 'master'
Feature/evm See merge request proof/pressure-test!3
- Loading branch information
Showing
16 changed files
with
7,703 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package call | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
chainCommon "github.com/33cn/chain33/common" | ||
chainAddress "github.com/33cn/chain33/common/address" | ||
"github.com/33cn/chain33/common/crypto" | ||
"github.com/33cn/chain33/rpc/grpcclient" | ||
_ "github.com/33cn/chain33/system/address/eth" | ||
ethAddr "github.com/33cn/chain33/system/address/eth" | ||
chainTypes "github.com/33cn/chain33/types" | ||
chainUtil "github.com/33cn/chain33/util" | ||
evmAbi "github.com/33cn/plugin/plugin/dapp/evm/executor/abi" | ||
evmCommon "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common" | ||
evmtypes "github.com/33cn/plugin/plugin/dapp/evm/types" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
yccChainId = 999 | ||
defaultFeeRate = 100000 | ||
) | ||
|
||
var ( | ||
Ty = int32(chainTypes.SECP256K1) | ||
AddrType = int32(chainAddress.DefaultID) | ||
) | ||
|
||
func InitTy(chianType string) { | ||
if chianType == "ycc" { | ||
AddrType = int32(ethAddr.ID) | ||
// 加载, 确保在evm使能高度前, eth地址驱动已使能 | ||
driver, err := chainAddress.LoadDriver(AddrType, -1) | ||
if err != nil { | ||
panic(fmt.Sprintf("address driver must enable before %d", 0)) | ||
} | ||
evmCommon.InitEvmAddressTypeOnce(driver) | ||
} | ||
Ty = chainTypes.EncodeSignID(chainTypes.SECP256K1, AddrType) | ||
} | ||
|
||
// CallContract 成功部署后的合约 | ||
type CallContract struct { | ||
ContractAddr string | ||
ParaName string | ||
Abi string | ||
DeployerPri crypto.PrivKey | ||
} | ||
|
||
// LocalCreateYCCEVMTx 本地构造ycc的evm交易 | ||
func (c *CallContract) LocalCreateYCCEVMTx(parameter string) (*chainTypes.Transaction, error) { | ||
exec := c.ParaName + evmtypes.ExecutorName | ||
toAddr, err := chainAddress.GetExecAddress(exec, AddrType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, packedParameter, err := evmAbi.Pack(parameter, c.Abi, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
action := evmtypes.EVMContractAction{ | ||
Para: packedParameter, | ||
ContractAddr: c.ContractAddr, | ||
} | ||
|
||
tx := &chainTypes.Transaction{Execer: []byte(exec), Payload: chainTypes.Encode(&action), Fee: 0, To: toAddr} | ||
// 十倍手续费保证成功 | ||
tx.Fee, _ = tx.GetRealFee(10 * defaultFeeRate) | ||
|
||
random := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
tx.Nonce = random.Int63() | ||
tx.ChainID = yccChainId | ||
tx.Sign(chainTypes.SECP256K1, c.DeployerPri) | ||
|
||
return tx, nil | ||
} | ||
|
||
// LocalCreateYCCEVMGroupTx 本地构造ycc的evm交易组 | ||
func (c *CallContract) LocalCreateYCCEVMGroupTx(parameters, privkeys []string) (*chainTypes.Transaction, error) { | ||
exec := c.ParaName + evmtypes.ExecutorName | ||
toAddr, err := chainAddress.GetExecAddress(exec, AddrType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parameterCount := len(parameters) | ||
privkeysCount := len(privkeys) | ||
|
||
txList := make([]*chainTypes.Transaction, 0, parameterCount) | ||
for i := 0; i < parameterCount; i++ { | ||
_, packedParameter, err := evmAbi.Pack(parameters[i], c.Abi, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
action := evmtypes.EVMContractAction{ | ||
Para: packedParameter, | ||
ContractAddr: c.ContractAddr, | ||
} | ||
|
||
tx := &chainTypes.Transaction{Execer: []byte(exec), Payload: chainTypes.Encode(&action), Fee: 0, To: toAddr} | ||
// 十倍手续费保证成功 | ||
tx.Fee, _ = tx.GetRealFee(10 * defaultFeeRate) | ||
|
||
random := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
tx.Nonce = random.Int63() | ||
tx.ChainID = yccChainId | ||
txList = append(txList, tx) | ||
} | ||
tg, err := chainTypes.CreateTxGroup(txList, 10*defaultFeeRate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for j := 0; j < len(txList); j++ { | ||
if privkeysCount > 0 && privkeysCount == parameterCount { | ||
tg.SignN(j, Ty, chainUtil.HexToPrivkey(privkeys[j])) | ||
} else { | ||
tg.SignN(j, Ty, c.DeployerPri) | ||
} | ||
} | ||
return tg.Tx(), nil | ||
} | ||
|
||
type DeployContract struct { | ||
Endpoint string | ||
ParaName string | ||
Bin string | ||
Abi string | ||
Parameter string | ||
DeployerPri crypto.PrivKey | ||
DeployerAddr string | ||
} | ||
|
||
// LocalCreateDeployTx create deploy contract tx | ||
func (d *DeployContract) LocalCreateDeployTx() (*chainTypes.Transaction, error) { | ||
exec := d.ParaName + evmtypes.ExecutorName | ||
toAddr, err := chainAddress.GetExecAddress(exec, AddrType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bCode, err := chainCommon.FromHex(d.Bin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if d.Parameter != "" { | ||
packData, err := evmAbi.PackContructorPara(d.Parameter, d.Abi) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bCode = append(bCode, packData...) | ||
} | ||
|
||
action := evmtypes.EVMContractAction{ | ||
Code: bCode, | ||
ContractAddr: toAddr, | ||
} | ||
|
||
tx := &chainTypes.Transaction{ | ||
Execer: []byte(exec), | ||
Payload: chainTypes.Encode(&action), | ||
Signature: nil, | ||
To: toAddr, | ||
ChainID: yccChainId, | ||
} | ||
// 十倍手续费保证成功 | ||
tx.Fee, _ = tx.GetRealFee(10 * defaultFeeRate) | ||
|
||
random := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
tx.Nonce = random.Int63() | ||
|
||
tx.Sign(Ty, d.DeployerPri) | ||
return tx, nil | ||
} | ||
|
||
// Deploy contract return txhash,ContractAddr | ||
func (d *DeployContract) Deploy() (string, string, error) { | ||
// 创建本地合约交易 | ||
tx, err := d.LocalCreateDeployTx() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
txHash := chainCommon.ToHex(tx.Hash()) | ||
|
||
conn, err := grpc.Dial(grpcclient.NewMultipleURL(d.Endpoint), grpc.WithInsecure()) | ||
if err != nil { | ||
fmt.Println("grpcclient.NewMultipleURL err:", err) | ||
return "", "", err | ||
} | ||
|
||
client := chainTypes.NewChain33Client(conn) | ||
|
||
// grpc发送交易 | ||
res, err := client.SendTransaction(context.Background(), tx) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if !res.IsOk { | ||
return "", "", fmt.Errorf("SendTransaction fail %v", string(res.Msg)) | ||
} | ||
|
||
// 获取合约地址 | ||
contractAddr := LocalGetContractAddr(d.DeployerAddr, tx.Hash()) | ||
|
||
return txHash, contractAddr, nil | ||
} | ||
|
||
func LocalGetContractAddr(caller string, txhash []byte) string { | ||
return evmCommon.NewContractAddress(*evmCommon.StringToAddress(caller), txhash).String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## 快速使用教程 | ||
1. 首先根据操作系统类型下载发送交易的对应可执行文件,下载地址: | ||
2. 运行下载来的可执行文件,对于习惯使用鼠标操作的用户,双击即可运行 | ||
3. 发送交易程序运行后,默认会发送20万笔交易,在区块链浏览器界面,打开链接即可看到区块信息: https://www.yuan.org/block | ||
4. 通过计算一段时间的总的交易量与耗时,就可以得出区块链的交易速度 | ||
注意: 发送会消耗手续费,上面的发送程序,默认扣费地址为 0x8efd65cacad0c0a7b3eace77eeaac04476943980 , | ||
如果使用时,提示 NoBalance,表示该地址里面,已经没有余额了,解决办法一个是使用下面的带配置选项的交易发送程序,修改扣费地址为一个有余额的地址, | ||
或者是联系工作人员,给这个默认地址转账,工作人员邮箱为: [email protected] | ||
|
||
|
||
## 带配置选项的交易发送使用教程 | ||
1. 使用流程和公链发送工具基本一样,只是增加了配置文件,可以配置发送的各项参数,带配置选项发送工具的下载地址为: | ||
2. 将交易发送工具和配置文件放置在同一个目录,发送交易的程序运行后,会自动加载当前目录下的 config.yaml 的配置文件,然后根据配置,发送交易 | ||
3. 交易发送完成后,按照上面的方法查看和计算区块链交易速度 |
Oops, something went wrong.