Skip to content

Commit

Permalink
Merge branch 'master' into testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
studyzy committed Jun 5, 2020
2 parents b2a5f45 + c195c8c commit d128b6a
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 376 deletions.
279 changes: 0 additions & 279 deletions consensus/jury/contractReq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,302 +20,23 @@ package jury

import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"sync"
"time"

"github.com/palletone/go-palletone/contracts"
"github.com/palletone/go-palletone/contracts/comm"

"github.com/ethereum/go-ethereum/rlp"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/crypto"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/common/util"
"github.com/palletone/go-palletone/contracts/contractcfg"
"github.com/palletone/go-palletone/contracts/ucc"
pb "github.com/palletone/go-palletone/core/vmContractPub/protos/peer"
"github.com/palletone/go-palletone/dag/errors"
"github.com/palletone/go-palletone/dag/modules"
"github.com/palletone/go-palletone/dag/rwset"
)

func (p *Processor) ContractInstallReq(from, to common.Address, daoAmount, daoFee uint64, tplName, path, version string,
description, abi, language string, local bool, addrs []common.Address) (reqId common.Hash, TplId []byte, err error) {
if from == (common.Address{}) || to == (common.Address{}) || tplName == "" || path == "" || version == "" {
log.Error("ContractInstallReq, param is error")
return common.Hash{}, nil, errors.New("ContractInstallReq request param is error")
}
if len(tplName) > MaxLengthTplName || len(path) > MaxLengthTplPath || len(version) > MaxLengthTplVersion ||
len(description) > MaxLengthDescription || len(abi) > MaxLengthAbi || len(language) > MaxLengthLanguage ||
len(addrs) > MaxNumberTplEleAddrHash {
log.Error("ContractInstallReq", "request param len overflow,len(tplName)",
len(tplName), "len(path)", len(path), "len(version)", len(version), "len(description)", len(description),
"len(abi)", len(abi), "len(language)", len(language), "len(addrs)", len(addrs))
return common.Hash{}, nil, errors.New("ContractInstallReq, request param len overflow")
}
if !p.dag.IsContractDeveloper(from) {
return common.Hash{}, nil, fmt.Errorf("ContractInstallReq, address[%s] is not developer", from.String())
}
if len(addrs) > 0 {
jjhAd := p.dag.GetChainParameters().FoundationAddress
if jjhAd != from.Str() {
log.Debugf("ContractInstallReq, requestAddr[%s] not foundationAddress", from.String())
return common.Hash{}, nil, fmt.Errorf("ContractInstallReq,"+
" request address[%s] is not foundationAddress(when specifying the contract install address)", from.String())
}
}
addrHash := make([]common.Hash, 0)
//去重
resultAddress := getValidAddress(addrs)
for _, addr := range resultAddress {
addrHash = append(addrHash, util.RlpHash(addr))
}
log.Debug("ContractInstallReq", "enter, tplName ", tplName, "path", path, "version", version, "addrHash", addrHash)

if daoFee == 0 { //dynamic calculation fee
fee, _, _, err := p.ContractInstallReqFee(from, to, daoAmount, daoFee, tplName, path, version, description, abi, language, local, addrs)
if err != nil {
return common.Hash{}, nil, fmt.Errorf("ContractInstallReq, ContractInstallReqFee err:%s", err.Error())
}
daoFee = uint64(fee) + 1
log.Debug("ContractInstallReq", "dynamic calculation fee:", daoFee)
}
msgReq := &modules.Message{
App: modules.APP_CONTRACT_TPL_REQUEST,
Payload: &modules.ContractInstallRequestPayload{
TplName: tplName,
Path: path,
Version: version,
AddrHash: addrHash,
TplDescription: description,
Abi: abi,
Language: language,
Creator: from.String(),
},
}
reqId, reqTx, err := p.createContractTxReq(common.Address{}, from, to, daoAmount, daoFee, nil, msgReq)
if err != nil {
return common.Hash{}, nil, err
}
//add local and broadcast
//go p.AddLocalTx(reqTx)

isLocal := true //todo
if isLocal {
sigTx, err := p.RunAndSignTx(reqTx, rwset.RwM, p.dag, from)
if err != nil{
log.Errorf("ContractInstallReq, local RunAndSignTx err:%s", err.Error())
return reqId, nil, err
}

_, tpl, err := getContractTxContractInfo(sigTx, modules.APP_CONTRACT_TPL)
if err != nil || tpl == nil {
errMsg := fmt.Sprintf("[%s]ContractInstallReq getContractTxContractInfo fail, tpl Name[%s]", reqId.ShortStr(), tplName)
return common.Hash{}, nil, errors.New(errMsg)
}
templateId := tpl.Payload.(*modules.ContractTplPayload).TemplateId
log.Infof("[%s]ContractInstallReq ok, reqId[%s] templateId[%x]", reqId.ShortStr(), reqId.String(), templateId)

//broadcast
go p.ptn.ContractBroadcast(ContractEvent{CType: CONTRACT_EVENT_COMMIT, Tx: sigTx}, false)

return reqId, templateId, nil
}
//net mode

return reqId, nil, nil
}

func (p *Processor) ContractDeployReq(from, to common.Address, daoAmount, daoFee uint64, templateId []byte,
args [][]byte, extData []byte, timeout time.Duration) (common.Hash, common.Address, error) {
if from == (common.Address{}) || to == (common.Address{}) || templateId == nil {
log.Error("ContractDeployReq, param is error")
return common.Hash{}, common.Address{}, errors.New("ContractDeployReq request param is error")
}
if len(templateId) > MaxLengthTplId || len(args) > MaxNumberArgs || len(extData) > MaxLengthExtData {
log.Error("ContractDeployReq", "request param len overflow, len(templateId)",
len(templateId), "len(args)", len(args), "len(extData)", len(extData))
return common.Hash{}, common.Address{}, errors.New("ContractDeployReq request param len overflow")
}
for _, arg := range args {
if len(arg) > MaxLengthArgs {
log.Error("ContractDeployReq", "request param len overflow,len(arg)", len(arg))
return common.Hash{}, common.Address{}, errors.New("ContractDeployReq request param len overflow")
}
}
if p.ptn.EnableGasFee() {
if daoFee == 0 { //dynamic calculation fee
fee, _, _, err := p.ContractDeployReqFee(from, to, daoAmount, daoFee, templateId, args, extData, timeout)
if err != nil {
return common.Hash{}, common.Address{}, fmt.Errorf("ContractDeployReq, ContractDeployReqFee err:%s", err.Error())
}
daoFee = uint64(fee) + 1
log.Debug("ContractDeployReq", "dynamic calculation fee:", daoFee)
}
}
msgReq := &modules.Message{
App: modules.APP_CONTRACT_DEPLOY_REQUEST,
Payload: &modules.ContractDeployRequestPayload{
TemplateId: templateId,
Args: args,
ExtData: extData,
Timeout: uint32(timeout),
},
}
reqId, tx, err := p.createContractTxReq(common.Address{}, from, to, daoAmount, daoFee, nil, msgReq)
if err != nil {
return common.Hash{}, common.Address{}, err
}
contractId := crypto.RequestIdToContractAddress(reqId)
log.Infof("[%s]ContractDeployReq ok, reqId[%s] templateId[%x],contractId[%s] ",
reqId.ShortStr(), reqId.String(), templateId, contractId.String())

//add local and broadcast
go p.AddLocalTx(tx)

//broadcast
go p.ptn.ContractBroadcast(ContractEvent{Ele: nil, CType: CONTRACT_EVENT_ELE, Tx: tx}, true)
return reqId, contractId, err
}

func (p *Processor) ContractInvokeReq(from, to common.Address, daoAmount, daoFee uint64, certID *big.Int,
contractId common.Address, args [][]byte, timeout uint32) (common.Hash, error) {
if from == (common.Address{}) || to == (common.Address{}) || contractId == (common.Address{}) || args == nil {
log.Error("ContractInvokeReq, param is error")
return common.Hash{}, errors.New("ContractInvokeReq request param is error")
}
if len(args) > MaxNumberArgs {
log.Error("ContractInvokeReq", "len(args)", len(args))
return common.Hash{}, errors.New("ContractInvokeReq request param len overflow")
}
for _, arg := range args {
if len(arg) > MaxLengthArgs {
log.Error("ContractInvokeReq", "request param len overflow,len(arg)", len(arg))
return common.Hash{}, errors.New("ContractInvokeReq request param args len overflow")
}
}
if daoFee == 0 { //dynamic calculation fee
fee, _, _, err := p.ContractInvokeReqFee(from, to, daoAmount, daoFee, certID, contractId, args, timeout)
if err != nil {
return common.Hash{}, fmt.Errorf("ContractInvokeReq, ContractInvokeReqFee err:%s", err.Error())
}
daoFee = uint64(fee) + 1
log.Debug("ContractInvokeReq", "dynamic calculation fee:", daoFee)
}
msgReq := &modules.Message{
App: modules.APP_CONTRACT_INVOKE_REQUEST,
Payload: &modules.ContractInvokeRequestPayload{
ContractId: contractId.Bytes(),
Args: args,
Timeout: timeout,
},
}
reqId, tx, err := p.createContractTxReq(contractId, from, to, daoAmount, daoFee, certID, msgReq)
if err != nil {
return common.Hash{}, err
}
log.Infof("[%s]ContractInvokeReq ok, reqId[%s], contractId[%s]",
reqId.ShortStr(), reqId.String(), contractId.String())
log.DebugDynamic(func() string {
rjson, _ := json.Marshal(tx)
rdata, _ := rlp.EncodeToBytes(tx)
return fmt.Sprintf("Request data fro debug json:%s,\r\n rlp:%x", string(rjson), rdata)
})
//broadcast
go p.ptn.ContractBroadcast(ContractEvent{CType: CONTRACT_EVENT_EXEC, Ele: p.mtx[reqId].eleNode, Tx: tx}, true)
return reqId, nil
}

func (p *Processor) ContractInvokeReqToken(from, to common.Address, token *modules.Asset, daoAmount, daoFee uint64,
contractAddress common.Address, args [][]byte, timeout uint32) (common.Hash, error) {

if from == (common.Address{}) || to == (common.Address{}) || contractAddress == (common.Address{}) || args == nil {
log.Error("ContractInvokeReqToken, param is error")
return common.Hash{}, errors.New("ContractInvokeReqToken request param is error")
}
if len(args) > MaxNumberArgs {
log.Error("ContractInvokeReqToken", "len(args)", len(args))
return common.Hash{}, errors.New("ContractInvokeReqToken request param len overflow")
}
for _, arg := range args {
if len(arg) > MaxLengthArgs {
log.Error("ContractInvokeReqToken", "request param len overflow,len(arg)", len(arg))
return common.Hash{}, errors.New("ContractInvokeReqToken request param args len overflow")
}
}
if daoFee == 0 { //dynamic calculation fee
fee, _, _, err := p.ContractInvokeReqFee(from, to, daoAmount, daoFee, nil, contractAddress, args, timeout)
if err != nil {
return common.Hash{}, fmt.Errorf("ContractInvokeReqToken, ContractInvokeReqFee err:%s", err.Error())
}
daoFee = uint64(fee) + 1
log.Debug("ContractInvokeReqToken", "dynamic calculation fee:", daoFee)
}
msgReq := &modules.Message{
App: modules.APP_CONTRACT_INVOKE_REQUEST,
Payload: &modules.ContractInvokeRequestPayload{
ContractId: contractAddress.Bytes(),
Args: args,
Timeout: timeout,
},
}
reqId, tx, err := p.createContractTxReqToken(contractAddress, from, to, token, daoAmount, daoFee, msgReq)
if err != nil {
return common.Hash{}, err
}
log.Infof("[%s]ContractInvokeReqToken ok, reqId[%s] contractId[%s]",
reqId.ShortStr(), reqId.String(), contractAddress.Bytes())
//broadcast
go p.ptn.ContractBroadcast(ContractEvent{CType: CONTRACT_EVENT_EXEC, Ele: p.mtx[reqId].eleNode, Tx: tx}, true)
return reqId, nil
}

func (p *Processor) ContractStopReq(from, to common.Address, daoAmount, daoFee uint64,
contractId common.Address, deleteImage bool) (common.Hash, error) {
if from == (common.Address{}) || to == (common.Address{}) || contractId == (common.Address{}) {
log.Error("ContractStopReq, param is error")
return common.Hash{}, errors.New("ContractStopReq request param is error")
}
randNum, err := crypto.GetRandomNonce()
if err != nil {
return common.Hash{}, errors.New("ContractStopReq, GetRandomNonce error")
}
if p.ptn.EnableGasFee() {
if daoFee == 0 { //dynamic calculation fee
fee, _, _, err := p.ContractStopReqFee(from, to, daoAmount, daoFee, contractId, deleteImage)
if err != nil {
return common.Hash{}, fmt.Errorf("ContractStopReq, ContractStopReqFee err:%s", err.Error())
}
daoFee = uint64(fee) + 1
log.Debug("ContractStopReq", "dynamic calculation fee:", daoFee)
}
}
msgReq := &modules.Message{
App: modules.APP_CONTRACT_STOP_REQUEST,
Payload: &modules.ContractStopRequestPayload{
ContractId: contractId.Bytes(),
Txid: hex.EncodeToString(randNum),
DeleteImage: deleteImage,
},
}
reqId, tx, err := p.createContractTxReq(contractId, from, to, daoAmount, daoFee, nil, msgReq)
if err != nil {
return common.Hash{}, err
}
log.Infof("[%s]ContractStopReq ok, reqId[%s], contractId[%s], txId[%s]",
reqId.ShortStr(), reqId.String(), contractId, hex.EncodeToString(randNum))

//add local and broadcast
go p.AddLocalTx(tx)

//broadcast
go p.ptn.ContractBroadcast(ContractEvent{CType: CONTRACT_EVENT_EXEC, Ele: p.mtx[reqId].eleNode, Tx: tx}, true)
return reqId, nil
}

//deploy -->invoke
func (p *Processor) ContractQuery(id []byte, args [][]byte, timeout time.Duration) (rsp []byte, err error) {
exist := false
Expand Down
32 changes: 0 additions & 32 deletions consensus/jury/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,38 +897,6 @@ func (p *Processor) CreateTokenTransaction(from, to common.Address, token *modul
return tx, daoFee, nil
}

func (p *Processor) createContractTxReqToken(contractId, from, to common.Address, token *modules.Asset,
daoAmountToken, daoFee uint64, msg *modules.Message) (common.Hash, *modules.Transaction, error) {
tx, _, err := p.CreateTokenTransaction(from, to, token, daoAmountToken, daoFee, msg)
if err != nil {
return common.Hash{}, nil, err
}
log.Debugf("[%s]createContractTxReqToken,contractId[%s],tx[%v]",
tx.RequestHash().ShortStr(), contractId.String(), tx)
return p.signGenericTx(contractId, from, tx)
}

func (p *Processor) createContractTxReq(contractId, from, to common.Address, daoAmount, daoFee uint64, certID *big.Int,
msg *modules.Message) (common.Hash, *modules.Transaction, error) {
tx, _, err := p.dag.CreateGenericTransaction(from, to, daoAmount, daoFee, certID, msg, p.ptn.EnableGasFee())
if err != nil {
return common.Hash{}, nil, err
}
// 构造 signature
if !p.ptn.EnableGasFee() && from.Equal(to) {
ks := p.ptn.GetKeyStore()
pubKey, err := ks.GetPublicKey(from)
if err != nil {
return common.Hash{}, nil, err
}
sign, err := ks.SigData(tx, from)
if err != nil {
return common.Hash{}, nil, err
}
tx.AddMessage(modules.NewMessage(modules.APP_SIGNATURE, modules.NewSignaturePayload(pubKey, sign)))
}
return p.signGenericTx(contractId, from, tx)
}
func (p *Processor) SignAndExecuteAndSendRequest(from common.Address,
tx *modules.Transaction) (*modules.Transaction, error) {
requestMsg := tx.Messages()[tx.GetRequestMsgIndex()]
Expand Down
11 changes: 0 additions & 11 deletions internal/ptnapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,7 @@ type Backend interface {
DecodeTx(hex string) (string, error)
DecodeJsonTx(hex string) (string, error)
EncodeTx(jsonStr string) (string, error)

ContractInstallReqTx(from, to common.Address, daoAmount, daoFee uint64, tplName, path, version string,
description, abi, language string, addrs []common.Address) (reqId common.Hash, tplId []byte, err error)
ContractDeployReqTx(from, to common.Address, daoAmount, daoFee uint64, templateId []byte, args [][]byte,
extData []byte, timeout time.Duration) (reqId common.Hash, contractAddr common.Address, err error)
ContractInvokeReqTx(from, to common.Address, daoAmount, daoFee uint64, certID *big.Int,
contractAddress common.Address, args [][]byte, timeout uint32) (reqId common.Hash, err error)
SendContractInvokeReqTx(requestTx *modules.Transaction) (reqId common.Hash, err error)
ContractInvokeReqTokenTx(from, to common.Address, token *modules.Asset, amountToken, fee uint64,
contractAddress common.Address, args [][]byte, timeout uint32) (reqId common.Hash, err error)
ContractStopReqTx(from, to common.Address, daoAmount, daoFee uint64, contractId common.Address,
deleteImage bool) (reqId common.Hash, err error)
ContractInstallReqTxFee(from, to common.Address, daoAmount, daoFee uint64, tplName, path, version string,
description, abi, language string, addrs []common.Address) (fee float64, size float64, tm uint32, err error)
ContractDeployReqTxFee(from, to common.Address, daoAmount, daoFee uint64, templateId []byte,
Expand Down

0 comments on commit d128b6a

Please sign in to comment.