Skip to content

Commit 5cbeca5

Browse files
committed
只读的跨链实现
1 parent 9e93e31 commit 5cbeca5

24 files changed

Lines changed: 5037 additions & 1381 deletions

bcs/ledger/xledger/state/state.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package state
44
import (
55
"bytes"
66
"encoding/hex"
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"math/big"
@@ -899,6 +900,40 @@ func (t *State) QueryBlock(blockid []byte) (kledger.BlockHandle, error) {
899900
return NewBlockAgent(block), nil
900901

901902
}
903+
904+
func (t *State) ResolveChain(chainName string) (*pb2.CrossQueryMeta, error) {
905+
xmReader := t.CreateXMReader()
906+
sandBoxCfg := &contract.SandboxConfig{
907+
XMReader: xmReader,
908+
}
909+
sandBox, err := t.sctx.ContractMgr.NewStateSandbox(sandBoxCfg)
910+
if err != nil {
911+
return nil, err
912+
}
913+
contextConfig := &contract.ContextConfig{
914+
State: sandBox,
915+
ResourceLimits: contract.MaxLimits,
916+
ContractName: "crossQueryNaming",
917+
}
918+
919+
ctx, err := t.sctx.ContractMgr.NewContext(contextConfig)
920+
if err != nil {
921+
t.log.Warn("queryContractBannedStatus new context error", "error", err)
922+
return nil, err
923+
}
924+
args := map[string][]byte{}
925+
args["name"] = []byte(chainName)
926+
invokeRes, invokeErr := ctx.Invoke("Resolve", args)
927+
if invokeErr != nil {
928+
ctx.Release()
929+
return nil, invokeErr
930+
}
931+
ctx.Release()
932+
res := &pb2.CrossQueryMeta{}
933+
err = json.Unmarshal(invokeRes.Body, res)
934+
return res, err
935+
}
936+
902937
func (t *State) QueryTransaction(txid []byte) (*pb2.Transaction, error) {
903938
ltx, err := t.sctx.Ledger.QueryTransaction(txid)
904939
if err != nil {

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/golang/snappy v0.0.3
1818
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa // indirect
1919
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
20+
github.com/grpc-ecosystem/grpc-gateway v1.9.0
2021
github.com/hashicorp/golang-lru v0.5.4
2122
github.com/hyperledger/burrow v0.30.5
2223
github.com/ipfs/go-ipfs-addr v0.0.1
@@ -38,7 +39,10 @@ require (
3839
github.com/xuperchain/xvm v0.0.0-20210126142521-68fd016c56d7
3940
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
4041
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
42+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
4143
google.golang.org/grpc v1.35.0
44+
google.golang.org/protobuf v1.25.0
45+
4246
)
4347

4448
replace github.com/hyperledger/burrow => github.com/xuperchain/burrow v0.30.6-0.20210317023017-369050d94f4a

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
230230
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 h1:FlFbCRLd5Jr4iYXZufAvgWN6Ao0JrI5chLINnUXDDr0=
231231
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
232232
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
233+
github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI=
233234
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
234235
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f h1:8N8XWLZelZNibkhM1FuF+3Ad3YIbgirjdMiVA0eUkaM=
235236
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=

kernel/consensus/mock/mock_pluggable_consensus.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ func (c *FakeKContext) QueryBlock(blockid []byte) (*xldgpb.InternalBlock, error)
363363
func (c *FakeKContext) QueryTransaction(txid []byte) (*pb.Transaction, error) {
364364
return &pb.Transaction{}, nil
365365
}
366+
func (c *FakeKContext) CrossQuery(crossQueryRequest *pb.CrossQueryRequest, queryMeta *pb.CrossQueryMeta) (*pb.ContractResponse, error) {
367+
return nil, nil
368+
}
366369

367370
type FakeManager struct {
368371
R *FakeRegistry
@@ -380,6 +383,10 @@ func (m *FakeManager) GetKernRegistry() contract.KernRegistry {
380383
return m.R
381384
}
382385

386+
func (m *FakeManager) CrossQuery(crossQueryRequest *pb.CrossQueryRequest, queryMeta *pb.CrossQueryMeta) (*pb.ContractResponse, error) {
387+
return nil, nil
388+
}
389+
383390
type FakeRegistry struct {
384391
M map[string]contract.KernMethod
385392
}

kernel/consensus/pluggable_consensus_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func TestUpdateConsensus(t *testing.T) {
272272
return
273273
}
274274
fakeCtx := mock.NewFakeKContext(NewUpdateArgs(), NewUpdateM())
275-
np.updateConsensus(fakeCtx)
275+
//np.updateConsensus(fakeCtx)
276276
if len(np.stepConsensus.cons) != 2 {
277277
t.Error("Update consensus error!")
278278
return
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package bridge
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/xuperchain/xupercore/kernel/contract/bridge/pb"
8+
)
9+
10+
const (
11+
// XuperScheme define the xuper scheme
12+
XuperScheme = "xuper"
13+
)
14+
15+
// CrossChainURI Standard
16+
// [scheme:][//chain_name][path][?query]
17+
// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase
18+
type CrossChainURI struct {
19+
*url.URL
20+
}
21+
22+
// ParseCrossChainURI will parse uri to cross chain uri instance
23+
func ParseCrossChainURI(crossChainURI string) (*CrossChainURI, error) {
24+
uri, err := url.Parse(crossChainURI)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return &CrossChainURI{
30+
uri,
31+
}, nil
32+
}
33+
34+
// GetScheme return cross chain uri scheme
35+
func (ccu *CrossChainURI) GetScheme() string {
36+
return ccu.URL.Scheme
37+
}
38+
39+
// GetChainName return cross chain uri chain name
40+
func (ccu *CrossChainURI) GetChainName() string {
41+
return ccu.URL.Host
42+
}
43+
44+
// GetPath return cross chain uri path
45+
func (ccu *CrossChainURI) GetPath() string {
46+
return ccu.URL.Path
47+
}
48+
49+
// GetQuery return cross chain uri query
50+
func (ccu *CrossChainURI) GetQuery() url.Values {
51+
return ccu.URL.Query()
52+
}
53+
54+
// CrossChainScheme define the interface of CrossChainScheme
55+
type CrossChainScheme interface {
56+
GetName() string
57+
GetCrossQueryRequest(*CrossChainURI, []*pb.ArgPair, string, []string) (*pb.CrossQueryRequest, error)
58+
}
59+
60+
// CrossXuperScheme define the xuper scheme
61+
type CrossXuperScheme struct {
62+
}
63+
64+
// GetCrossQueryRequest return XupeScheme instance with CrossChainURI
65+
// [scheme:][//chain_name][?query]
66+
// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase
67+
func (cxs *CrossXuperScheme) GetCrossQueryRequest(crossChainURI *CrossChainURI,
68+
argPair []*pb.ArgPair, initiator string, authRequire []string) (*pb.CrossQueryRequest, error) {
69+
if initiator == "" {
70+
return nil, fmt.Errorf("GetCrossQueryRequest initiator is nil")
71+
}
72+
73+
querys := crossChainURI.GetQuery()
74+
module := querys.Get("module")
75+
bcname := querys.Get("bcname")
76+
contractName := querys.Get("contract_name")
77+
methodName := querys.Get("method_name")
78+
if module == "" || bcname == "" || contractName == "" || methodName == "" {
79+
return nil, fmt.Errorf("GetCrossQueryRequest query is nil")
80+
}
81+
args := make(map[string][]byte)
82+
for _, arg := range argPair {
83+
args[arg.GetKey()] = arg.GetValue()
84+
}
85+
86+
crossQueryRequest := &pb.CrossQueryRequest{
87+
Bcname: bcname,
88+
Initiator: initiator,
89+
AuthRequire: authRequire,
90+
Request: &pb.InvokeRequest{
91+
ModuleName: module,
92+
ContractName: contractName,
93+
MethodName: methodName,
94+
Args: args,
95+
},
96+
}
97+
return crossQueryRequest, nil
98+
}
99+
100+
// GetName return cross xuper scheme name
101+
func (cxs *CrossXuperScheme) GetName() string {
102+
return XuperScheme
103+
}
104+
105+
// GetChainScheme return chain scheme by scheme
106+
func GetChainScheme(scheme string) CrossChainScheme {
107+
switch scheme {
108+
case XuperScheme:
109+
return &CrossXuperScheme{}
110+
default:
111+
return &CrossXuperScheme{}
112+
}
113+
}

0 commit comments

Comments
 (0)