Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit c56d7ec

Browse files
[FAB-12903] Allow contract to contract invocations
- Only return burrow contract accounts that have permissions for contract invocations - no change in storing the contracts as we only store the contract code. Change-Id: Iaebb3db83a271c23124d7a50d5f9bc97bc53685b Signed-off-by: Swetha Repakula <srepaku@us.ibm.com> Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
1 parent b354ec4 commit c56d7ec

File tree

6 files changed

+122
-11
lines changed

6 files changed

+122
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ examples/e2e_cli/crypto-config/*
4646
.tox/
4747
# vscode settings
4848
.vscode
49+
# Node artifacts from installing web3 for integration tests
50+
node_modules
51+
package-lock.json

Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/e2e/e2e_test.go

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ const LongEventualTimeout = time.Minute
3030

3131
var _ = Describe("EndToEnd", func() {
3232
var (
33-
testDir string
34-
client *docker.Client
35-
network *nwo.Network
36-
chaincode nwo.Chaincode
37-
process ifrit.Process
38-
zeroAddress = "0000000000000000000000000000000000000000"
39-
SimpleStorage = helpers.SimpleStorageContract()
33+
testDir string
34+
client *docker.Client
35+
network *nwo.Network
36+
chaincode nwo.Chaincode
37+
process ifrit.Process
38+
zeroAddress = "0000000000000000000000000000000000000000"
39+
SimpleStorage = helpers.SimpleStorageContract()
40+
InvokeContract = helpers.InvokeContract()
4041
)
4142

4243
BeforeEach(func() {
@@ -136,5 +137,55 @@ var _ = Describe("EndToEnd", func() {
136137
output, _ = sess.Command.CombinedOutput()
137138
fmt.Println(string(output))
138139
Expect(sess.Out).To(gbytes.Say("0000000000000000000000000000000000000000000000000000000000000003"))
140+
141+
By("deploying an InvokeContract to invoke SimpleStorage")
142+
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
143+
ChannelID: "testchannel",
144+
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
145+
Name: "evmcc",
146+
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, zeroAddress, InvokeContract.CompiledBytecode+"000000000000000000000000"+contractAddr),
147+
PeerAddresses: []string{
148+
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
149+
network.PeerAddress(network.Peer("Org2", "peer1"), nwo.ListenPort),
150+
},
151+
WaitForEvent: true,
152+
})
153+
Expect(err).NotTo(HaveOccurred())
154+
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
155+
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
156+
157+
output = sess.Err.Contents()
158+
invokeAddr := string(regexp.MustCompile(`Chaincode invoke successful. result: status:200 payload:"([0-9a-fA-F]{40})"`).FindSubmatch(output)[1])
159+
Expect(invokeAddr).ToNot(BeEmpty())
160+
161+
By("invoking SimpleStorage through the InvokeContract")
162+
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
163+
ChannelID: "testchannel",
164+
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
165+
Name: "evmcc",
166+
//InvokeContract.setVal(8) which will cause SimpleStorage.set(8) to be invoked.
167+
Ctor: fmt.Sprintf(`{"Args":["%s","%s0000000000000000000000000000000000000000000000000000000000000008"]}`, invokeAddr, InvokeContract.FunctionHashes["setVal"]),
168+
PeerAddresses: []string{
169+
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
170+
network.PeerAddress(network.Peer("Org2", "peer1"), nwo.ListenPort),
171+
},
172+
WaitForEvent: true,
173+
})
174+
Expect(err).NotTo(HaveOccurred())
175+
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
176+
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
177+
178+
By("querying the SimpleStorage smart contract")
179+
sess, err = network.PeerUserSession(peer, "User1", helpers.ChaincodeQueryWithHex{
180+
ChannelID: "testchannel",
181+
Name: "evmcc",
182+
//get()
183+
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, contractAddr, SimpleStorage.FunctionHashes["get"]),
184+
})
185+
Expect(err).NotTo(HaveOccurred())
186+
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
187+
output, _ = sess.Command.CombinedOutput()
188+
fmt.Println(string(output))
189+
Expect(sess.Out).To(gbytes.Say("0000000000000000000000000000000000000000000000000000000000000008"))
139190
})
140191
})

integration/helpers/contracts.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,41 @@ func SimpleStorageContract() Contract {
3838
FunctionHashes: functionHashes,
3939
}
4040
}
41+
42+
func InvokeContract() Contract {
43+
/* Invokes a previously deployed SimpleStorage Contract
44+
pragma solidity ^0.4.18;
45+
46+
interface StorageInterface{
47+
function get() external returns (uint);
48+
function set(uint _val);
49+
}
50+
51+
contract Invoke{
52+
StorageInterface store;
53+
54+
constructor(StorageInterface _store) public {
55+
store = _store;
56+
}
57+
58+
function getVal() public view returns (uint result) {
59+
return store.get();
60+
}
61+
62+
function setA(uint _val) public returns (uint result) {
63+
store.set(_val);
64+
return _val;
65+
}
66+
}
67+
*/
68+
69+
functionHashes := make(map[string]string)
70+
functionHashes["getVal"] = "e1cb0e52"
71+
functionHashes["setVal"] = "3d4197f0"
72+
73+
return Contract{
74+
CompiledBytecode: "608060405234801561001057600080fd5b50604051602080610256833981016040525160008054600160a060020a03909216600160a060020a0319909216919091179055610204806100526000396000f30060806040526004361061004b5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633d4197f08114610050578063e1cb0e521461007a575b600080fd5b34801561005c57600080fd5b5061006860043561008f565b60408051918252519081900360200190f35b34801561008657600080fd5b50610068610120565b60008054604080517f60fe47b100000000000000000000000000000000000000000000000000000000815260048101859052905173ffffffffffffffffffffffffffffffffffffffff909216916360fe47b191602480820192869290919082900301818387803b15801561010257600080fd5b505af1158015610116573d6000803e3d6000fd5b5093949350505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d4ce63c6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156101a757600080fd5b505af11580156101bb573d6000803e3d6000fd5b505050506040513d60208110156101d157600080fd5b50519050905600a165627a7a723058208873497fb521d304cc79b588e9adad54377e155c77b1e5b3c35f2563657fb0700029",
75+
RuntimeBytecode: "60806040526004361061004b5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633d4197f08114610050578063e1cb0e521461007a575b600080fd5b34801561005c57600080fd5b5061006860043561008f565b60408051918252519081900360200190f35b34801561008657600080fd5b50610068610120565b60008054604080517f60fe47b100000000000000000000000000000000000000000000000000000000815260048101859052905173ffffffffffffffffffffffffffffffffffffffff909216916360fe47b191602480820192869290919082900301818387803b15801561010257600080fd5b505af1158015610116573d6000803e3d6000fd5b5093949350505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d4ce63c6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156101a757600080fd5b505af11580156101bb573d6000803e3d6000fd5b505050506040513d60208110156101d157600080fd5b50519050905600a165627a7a723058208873497fb521d304cc79b588e9adad54377e155c77b1e5b3c35f2563657fb0700029",
76+
FunctionHashes: functionHashes,
77+
}
78+
}

statemanager/statemanager.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@ import (
1111

1212
"github.com/hyperledger/burrow/account"
1313
"github.com/hyperledger/burrow/binary"
14+
"github.com/hyperledger/burrow/permission"
15+
"github.com/hyperledger/burrow/permission/types"
1416
"github.com/hyperledger/fabric/core/chaincode/shim"
1517
)
1618

19+
//Permissions for contract to send CallTx or SendTx to another contract
20+
const ContractPermFlags = permission.Call | permission.Send
21+
22+
var ContractPerms = types.AccountPermissions{
23+
Base: types.BasePermissions{
24+
Perms: ContractPermFlags,
25+
SetBit: ContractPermFlags,
26+
},
27+
Roles: []string{},
28+
}
29+
1730
type StateManager interface {
1831
GetAccount(address account.Address) (account.Account, error)
1932
GetStorage(address account.Address, key binary.Word256) (binary.Word256, error)
@@ -46,10 +59,14 @@ func (s *stateManager) GetAccount(address account.Address) (account.Account, err
4659
return account.ConcreteAccount{}.Account(), nil
4760
}
4861

49-
return account.ConcreteAccount{
62+
acct := account.ConcreteAccount{
5063
Address: address,
5164
Code: code,
52-
}.Account(), nil
65+
}.MutableAccount()
66+
67+
//Setting permission on account to allow contract invocations and queries
68+
acct.SetPermissions(ContractPerms)
69+
return acct, nil
5370
}
5471

5572
func (s *stateManager) GetStorage(address account.Address, key binary.Word256) (binary.Word256, error) {

statemanager/statemanager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ var _ = Describe("Statemanager", func() {
6363
expectedAcct := account.ConcreteAccount{
6464
Address: addr,
6565
Code: []byte("account code"),
66-
}.Account()
66+
}.MutableAccount()
67+
68+
expectedAcct.SetPermissions(statemanager.ContractPerms)
6769

6870
acct, err := sm.GetAccount(addr)
6971
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)