Skip to content
This repository was archived by the owner on Jul 13, 2022. It is now read-only.

Commit 1b9058b

Browse files
authored
P1sar/feat/add resourceid to substrate call#543 (#550)
* initial try to add resourceID to substrate chain call * substrate extrincit sub call extended with optional params resourceID * update config docs * substrate test image reference update to stable one * update accroding to PR comments * substrate chain version updated in e2e tests doecker compose * tests config included to enable extanded substrate call * wrongly added additional resourceID removedd * small readme upddate
1 parent 9616134 commit 1b9058b

File tree

10 files changed

+54
-28
lines changed

10 files changed

+54
-28
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ install-subkey:
7676
## Runs go test for all packages except the solidity bindings
7777
test:
7878
@echo " > \033[32mRunning tests...\033[0m "
79-
go test `go list ./... | grep -v bindings | grep -v e2e`
79+
go test -v `go list ./... | grep -v bindings | grep -v e2e`
8080

8181
test-e2e:
8282
@echo " > \033[32mRunning e2e tests...\033[0m "

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Ethereum chains support the following additional options:
8181
"http": "true", // Whether the chain connection is ws or http (default: false)
8282
"startBlock": "1234", // The block to start processing events from (default: 0)
8383
"blockConfirmations": "10" // Number of blocks to wait before processing a block
84+
"useExtendedCall": "true" // Extend extrinsic calls to substrate with ResourceID. Used for backward compatibility with example pallet. *Default: false*
8485
}
8586
```
8687

chains/substrate/chain.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ func InitializeChain(cfg *core.ChainConfig, logger log15.Logger, sysErr chan<- e
100100
startBlock = uint64(curr.Number)
101101
}
102102

103+
ue := parseUseExtended(cfg)
104+
103105
// Setup listener & writer
104106
l := NewListener(conn, cfg.Name, cfg.Id, startBlock, logger, bs, stop, sysErr, m)
105-
w := NewWriter(conn, logger, sysErr, m)
107+
w := NewWriter(conn, logger, sysErr, m, ue)
106108
return &Chain{
107109
cfg: cfg,
108110
conn: conn,
@@ -117,12 +119,6 @@ func (c *Chain) Start() error {
117119
if err != nil {
118120
return err
119121
}
120-
121-
err = c.writer.start()
122-
if err != nil {
123-
return err
124-
}
125-
126122
c.conn.log.Debug("Successfully started chain", "chainId", c.cfg.Id)
127123
return nil
128124
}

chains/substrate/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ func parseStartBlock(cfg *core.ChainConfig) uint64 {
1919
}
2020
return 0
2121
}
22+
23+
func parseUseExtended(cfg *core.ChainConfig) bool {
24+
if b, ok := cfg.Opts["useExtendedCall"]; ok {
25+
res, err := strconv.ParseBool(b)
26+
if err != nil {
27+
panic(err)
28+
}
29+
return res
30+
}
31+
return false
32+
}

chains/substrate/test_helper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func TestMain(m *testing.M) {
9393
if err != nil {
9494
panic(err)
9595
}
96-
alice := NewWriter(aliceConn, AliceTestLogger, wSysErr, nil)
97-
bob := NewWriter(bobConn, BobTestLogger, wSysErr, nil)
96+
alice := NewWriter(aliceConn, AliceTestLogger, wSysErr, nil, true)
97+
bob := NewWriter(bobConn, BobTestLogger, wSysErr, nil, true)
9898
context = testContext{
9999
client: client,
100100
listener: l,

chains/substrate/types.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func (w *writer) createFungibleProposal(m msg.Message) (*proposal, error) {
6969
if err != nil {
7070
return nil, err
7171
}
72-
7372
call, err := types.NewCall(
7473
&meta,
7574
method,
@@ -79,6 +78,13 @@ func (w *writer) createFungibleProposal(m msg.Message) (*proposal, error) {
7978
if err != nil {
8079
return nil, err
8180
}
81+
if w.extendCall {
82+
eRID, err := types.EncodeToBytes(m.ResourceId)
83+
if err != nil {
84+
return nil, err
85+
}
86+
call.Args = append(call.Args, eRID...)
87+
}
8288

8389
return &proposal{
8490
depositNonce: depositNonce,
@@ -111,6 +117,13 @@ func (w *writer) createNonFungibleProposal(m msg.Message) (*proposal, error) {
111117
if err != nil {
112118
return nil, err
113119
}
120+
if w.extendCall {
121+
eRID, err := types.EncodeToBytes(m.ResourceId)
122+
if err != nil {
123+
return nil, err
124+
}
125+
call.Args = append(call.Args, eRID...)
126+
}
114127

115128
return &proposal{
116129
depositNonce: depositNonce,
@@ -133,10 +146,17 @@ func (w *writer) createGenericProposal(m msg.Message) (*proposal, error) {
133146
method,
134147
types.NewHash(m.Payload[0].([]byte)),
135148
)
136-
137149
if err != nil {
138150
return nil, err
139151
}
152+
if w.extendCall {
153+
eRID, err := types.EncodeToBytes(m.ResourceId)
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
call.Args = append(call.Args, eRID...)
159+
}
140160
return &proposal{
141161
depositNonce: types.U64(m.DepositNonce),
142162
call: call,

chains/substrate/writer.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@ var AcknowledgeProposal utils.Method = utils.BridgePalletName + ".acknowledge_pr
2424
var TerminatedError = errors.New("terminated")
2525

2626
type writer struct {
27-
conn *Connection
28-
log log15.Logger
29-
sysErr chan<- error
30-
metrics *metrics.ChainMetrics
27+
conn *Connection
28+
log log15.Logger
29+
sysErr chan<- error
30+
metrics *metrics.ChainMetrics
31+
extendCall bool // Extend extrinsic calls to substrate with ResourceID.Used for backward compatibility with example pallet.
3132
}
3233

33-
func NewWriter(conn *Connection, log log15.Logger, sysErr chan<- error, m *metrics.ChainMetrics) *writer {
34+
func NewWriter(conn *Connection, log log15.Logger, sysErr chan<- error, m *metrics.ChainMetrics, extendCall bool) *writer {
3435
return &writer{
35-
conn: conn,
36-
log: log,
37-
sysErr: sysErr,
38-
metrics: m,
36+
conn: conn,
37+
log: log,
38+
sysErr: sysErr,
39+
metrics: m,
40+
extendCall: extendCall,
3941
}
4042
}
4143

42-
func (w *writer) start() error {
43-
return nil
44-
}
45-
4644
func (w *writer) ResolveMessage(m msg.Message) bool {
4745
var prop *proposal
4846
var err error
@@ -106,7 +104,6 @@ func (w *writer) resolveResourceId(id [32]byte) (string, error) {
106104
if !exists {
107105
return "", fmt.Errorf("resource %x not found on chain", id)
108106
}
109-
110107
return string(res), nil
111108
}
112109

docker-compose-e2e.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
- "8546:8545"
1717

1818
sub-chain:
19-
image: "chainsafe/chainbridge-substrate-chain:v1.2.0"
19+
image: "chainsafe/chainbridge-substrate-chain:v1.3.0"
2020
container_name: sub-chain
2121
command: chainbridge-substrate-chain --dev --alice --ws-external --rpc-external
2222
ports:

docs/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Substrate supports the following additonal options:
4242
```
4343
{
4444
"startBlock": "1234" // The block to start processing events from (default: 0)
45+
"useExtendedCall": "true" // Extend extrinsic calls to substrate with ResourceID. Used for backward compatibility with example pallet.
4546
}
4647
```
4748

e2e/substrate/substrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func CreateConfig(key string, chain msg.ChainId) *core.ChainConfig {
4545
Insecure: true,
4646
FreshStart: true,
4747
BlockstorePath: os.TempDir(),
48-
Opts: map[string]string{},
48+
Opts: map[string]string{"useExtendedCall": "true"},
4949
}
5050
}
5151

0 commit comments

Comments
 (0)