Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代@CodeCmn提交只读跨链的实现,和3.10版本实现的方式一致 #289

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions bcs/ledger/xledger/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package state
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -914,6 +915,40 @@ func (t *State) QueryBlock(blockid []byte) (kledger.BlockHandle, error) {
return NewBlockAgent(block), nil

}

func (t *State) ResolveChain(chainName string) (*pb2.CrossQueryMeta, error) {
xmReader := t.CreateXMReader()
sandBoxCfg := &contract.SandboxConfig{
XMReader: xmReader,
}
sandBox, err := t.sctx.ContractMgr.NewStateSandbox(sandBoxCfg)
if err != nil {
return nil, err
}
contextConfig := &contract.ContextConfig{
State: sandBox,
ResourceLimits: contract.MaxLimits,
ContractName: "crossQueryNaming",
}

ctx, err := t.sctx.ContractMgr.NewContext(contextConfig)
if err != nil {
t.log.Warn("queryContractBannedStatus new context error", "error", err)
return nil, err
}
args := map[string][]byte{}
args["name"] = []byte(chainName)
invokeRes, invokeErr := ctx.Invoke("Resolve", args)
if invokeErr != nil {
ctx.Release()
return nil, invokeErr
}
ctx.Release()
res := &pb2.CrossQueryMeta{}
err = json.Unmarshal(invokeRes.Body, res)
return res, err
}

func (t *State) QueryTransaction(txid []byte) (*pb2.Transaction, error) {
ltx, err := t.sctx.Ledger.QueryTransaction(txid)
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion bcs/ledger/xledger/state/tx_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,17 +586,32 @@ func (t *State) verifyTxRWSets(tx *pb.Transaction) (bool, error) {
XMReader: reader,
UTXOReader: utxoReader,
}

sandBox, err := t.sctx.ContractMgr.NewStateSandbox(sandBoxConfig)
if err != nil {
t.log.Error("NewStateSandbox error", "err", err)
return false, err
}

transContractName, transAmount, err := txn.ParseContractTransferRequest(req)
// 构建CrossQueryCache 依据xuperchain v3.10的流程添加
// 1.ParseCrossQuery()
crossQueries, err := sandbox.ParseCrossQuery(tx)
if err != nil {
t.log.Warn("PrepareEnv ParseCrossQuery error", "err", err.Error())
return false, err
}
// 2.IsCrossQueryEffective()
if ok := sandbox.IsCrossQueryEffective(crossQueries, tx); !ok {
t.log.Warn("PrepareEnv IsCrossQueryEffective error")
return false, errors.New("PrepareEnv CheckCrossQueryEffective error")
}
// 3.crossQueries 缓存赋值到 XMCache 中
sandBox.CrossQueryCache(crossQueries)

transContractName, transAmount, err := txn.ParseContractTransferRequest(req)
if err != nil {
return false, err
}
contextConfig := &contract.ContextConfig{
State: sandBox,
Initiator: tx.GetInitiator(),
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ module github.com/xuperchain/xupercore
go 1.14

require (
github.com/ChainSafe/go-schnorrkel v0.0.0-20200626160457-b38283118816 // indirect
github.com/aws/aws-sdk-go v1.32.4
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
github.com/dgraph-io/badger/v3 v3.2103.1
github.com/docker/go-connections v0.4.1-0.20180821093606-97c2040d34df // indirect
github.com/docker/go-units v0.4.0
github.com/emirpasic/gods v1.12.1-0.20201118132343-79df803e554c
github.com/fsouza/go-dockerclient v1.6.0
github.com/gammazero/deque v0.1.0
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.4.3
github.com/golang/snappy v0.0.3
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/grpc-ecosystem/grpc-gateway v1.9.0
github.com/hashicorp/golang-lru v0.5.4
github.com/hyperledger/burrow v0.30.5
github.com/ipfs/go-ipfs-addr v0.0.1
Expand All @@ -39,7 +37,12 @@ require (
github.com/xuperchain/xvm v0.0.0-20210126142521-68fd016c56d7
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0

github.com/tjfoc/gmsm v1.4.1

)

replace github.com/hyperledger/burrow => github.com/xuperchain/burrow v0.30.6-0.20211229032028-fbee6a05ab0f
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 h1:FlFbCRLd5Jr4iYXZufAvgWN6Ao0JrI5chLINnUXDDr0=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f h1:8N8XWLZelZNibkhM1FuF+3Ad3YIbgirjdMiVA0eUkaM=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
Expand Down
7 changes: 7 additions & 0 deletions kernel/consensus/mock/mock_pluggable_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ func (c *FakeKContext) QueryBlock(blockid []byte) (*xldgpb.InternalBlock, error)
func (c *FakeKContext) QueryTransaction(txid []byte) (*pb.Transaction, error) {
return &pb.Transaction{}, nil
}
func (c *FakeKContext) CrossQuery(crossQueryRequest *pb.CrossQueryRequest, queryMeta *pb.CrossQueryMeta) (*pb.ContractResponse, error) {
return nil, nil
}

type FakeManager struct {
R *FakeRegistry
Expand All @@ -384,6 +387,10 @@ func (m *FakeManager) GetKernRegistry() contract.KernRegistry {
return m.R
}

func (m *FakeManager) CrossQuery(crossQueryRequest *pb.CrossQueryRequest, queryMeta *pb.CrossQueryMeta) (*pb.ContractResponse, error) {
return nil, nil
}

type FakeRegistry struct {
M map[string]contract.KernMethod
}
Expand Down
113 changes: 113 additions & 0 deletions kernel/contract/bridge/cross_uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package bridge

import (
"fmt"
"net/url"

"github.com/xuperchain/xupercore/kernel/contract/bridge/pb"
)

const (
// XuperScheme define the xuper scheme
XuperScheme = "xuper"
)

// CrossChainURI Standard
// [scheme:][//chain_name][path][?query]
// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase
type CrossChainURI struct {
*url.URL
}

// ParseCrossChainURI will parse uri to cross chain uri instance
func ParseCrossChainURI(crossChainURI string) (*CrossChainURI, error) {
uri, err := url.Parse(crossChainURI)
if err != nil {
return nil, err
}

return &CrossChainURI{
uri,
}, nil
}

// GetScheme return cross chain uri scheme
func (ccu *CrossChainURI) GetScheme() string {
return ccu.URL.Scheme
}

// GetChainName return cross chain uri chain name
func (ccu *CrossChainURI) GetChainName() string {
return ccu.URL.Host
}

// GetPath return cross chain uri path
func (ccu *CrossChainURI) GetPath() string {
return ccu.URL.Path
}

// GetQuery return cross chain uri query
func (ccu *CrossChainURI) GetQuery() url.Values {
return ccu.URL.Query()
}

// CrossChainScheme define the interface of CrossChainScheme
type CrossChainScheme interface {
GetName() string
GetCrossQueryRequest(*CrossChainURI, []*pb.ArgPair, string, []string) (*pb.CrossQueryRequest, error)
}

// CrossXuperScheme define the xuper scheme
type CrossXuperScheme struct {
}

// GetCrossQueryRequest return XupeScheme instance with CrossChainURI
// [scheme:][//chain_name][?query]
// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase
func (cxs *CrossXuperScheme) GetCrossQueryRequest(crossChainURI *CrossChainURI,
argPair []*pb.ArgPair, initiator string, authRequire []string) (*pb.CrossQueryRequest, error) {
if initiator == "" {
return nil, fmt.Errorf("GetCrossQueryRequest initiator is nil")
}

querys := crossChainURI.GetQuery()
module := querys.Get("module")
bcname := querys.Get("bcname")
contractName := querys.Get("contract_name")
methodName := querys.Get("method_name")
if module == "" || bcname == "" || contractName == "" || methodName == "" {
return nil, fmt.Errorf("GetCrossQueryRequest query is nil")
}
args := make(map[string][]byte)
for _, arg := range argPair {
args[arg.GetKey()] = arg.GetValue()
}

crossQueryRequest := &pb.CrossQueryRequest{
Bcname: bcname,
Initiator: initiator,
AuthRequire: authRequire,
Request: &pb.InvokeRequest{
ModuleName: module,
ContractName: contractName,
MethodName: methodName,
Args: args,
},
}
return crossQueryRequest, nil
}

// GetName return cross xuper scheme name
func (cxs *CrossXuperScheme) GetName() string {
return XuperScheme
}

// GetChainScheme return chain scheme by scheme
func GetChainScheme(scheme string) CrossChainScheme {
switch scheme {
case XuperScheme:
return &CrossXuperScheme{}
default:
return &CrossXuperScheme{}
}
}
Loading