Skip to content

Commit e13f13e

Browse files
committed
fix ntor retry sync
1 parent dd73ea7 commit e13f13e

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

backups/testnet/N3_test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":null,"version":"1.0","scrypt":{"n":16384,"r":8,"p":8},"accounts":[{"address":"NeWfKfigfYXmEbi7SwbiSS8DtaVexCnsdN","label":null,"isDefault":false,"lock":false,"key":"6PYW3MNVdLsYt1SyMarsvU7pyBdokvSBsNMkhZ1tXW2Um3y9fRHdCNe7Bq","contract":{"script":"DCECGYb5CyWWMiyfaBxCgUI3VVcyQR6MUXEAxwK5HNQE8JBBVuezJw==","parameters":[{"name":"signature","type":"Signature"}],"deployed":false},"extra":null}],"extra":null}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func setupApp() *cli.App {
2727
app := cli.NewApp()
2828
app.Usage = "NEO Relayer"
2929
app.Action = startSync
30-
app.Copyright = "Copyright in 2020 The NEO Project"
30+
app.Copyright = "Copyright in 2021 The NEO Project"
3131
app.Flags = []cli.Flag{
3232
cmd.LogLevelFlag,
3333
cmd.ConfigPathFlag,

service/ntorMethods.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,25 @@ func (this *SyncService) syncProofToRelay(key string, height uint32) error {
9898

9999
// get state root
100100
srGot := false
101+
var height2 uint32
101102
stateRoot := mpt.StateRoot{}
102-
height2 := height
103+
if height >= this.neoStateRootHeight {
104+
height2 = height
105+
} else {
106+
height2 = this.neoStateRootHeight
107+
}
103108
for !srGot {
104109
res2 := this.neoSdk.GetStateRoot(height2)
105110
if res2.HasError() {
106111
this.db.PutRetry(sink.Bytes())
107112
return fmt.Errorf("[syncProofToRelay] neoSdk.GetStateRootByIndex error: %s", res2.Error.Message)
108113
}
109114
stateRoot = res2.Result
110-
if len(stateRoot.Witnesses) == 0 {
115+
if len(stateRoot.Witnesses) == 0 { // no witness
111116
height2++
112117
} else {
113118
srGot = true
119+
this.neoStateRootHeight = height2 // next tx can start from this height to get state root
114120
}
115121
}
116122
buff := io.NewBufBinaryWriter()
@@ -135,6 +141,12 @@ func (this *SyncService) syncProofToRelay(key string, height uint32) error {
135141
}
136142
log.Info("proof: %s", helper.BytesToHex(proof))
137143

144+
// following for testing only
145+
//id, k, proofs, err := mpt.ResolveProof(proof)
146+
//root, _ := helper.UInt256FromString(stateRoot.RootHash)
147+
//value, err := mpt.VerifyProof(root, id, k, proofs)
148+
//log.Infof("value: %s", helper.BytesToHex(value))
149+
138150
//sending SyncProof transaction to Relay Chain
139151
txHash, err := this.relaySdk.Native.Ccm.ImportOuterTransfer(this.config.NeoChainID, nil, height, proof, this.relayAccount.Address[:], crossChainMsg, this.relayAccount)
140152
if err != nil {
@@ -170,11 +182,22 @@ func (this *SyncService) retrySyncProofToRelay(v []byte) error {
170182
}
171183

172184
// get state root
173-
res2 := this.neoSdk.GetStateRoot(retry.Height)
174-
if res2.HasError() {
175-
return fmt.Errorf("[retrySyncProofToRelay] neoSdk.GetStateRootByIndex error: %s", res2.Error.Message)
185+
srGot := false
186+
stateRoot := mpt.StateRoot{}
187+
height2 := retry.Height
188+
for !srGot {
189+
res2 := this.neoSdk.GetStateRoot(height2)
190+
if res2.HasError() {
191+
// try once, log error
192+
return fmt.Errorf("[retrySyncProofToRelay] neoSdk.GetStateRootByIndex error: %s", res2.Error.Message)
193+
}
194+
stateRoot = res2.Result
195+
if len(stateRoot.Witnesses) == 0 {
196+
height2++
197+
} else {
198+
srGot = true
199+
}
176200
}
177-
stateRoot := res2.Result
178201
buff := io.NewBufBinaryWriter()
179202
stateRoot.Serialize(buff.BinaryWriter)
180203
crossChainMsg := buff.Bytes()
@@ -216,7 +239,7 @@ func (this *SyncService) retrySyncProofToRelay(v []byte) error {
216239
}
217240

218241
func (this *SyncService) waitForRelayBlock() {
219-
_, err := this.relaySdk.WaitForGenerateBlock(30*time.Second, 3)
242+
_, err := this.relaySdk.WaitForGenerateBlock(60*time.Second, 3)
220243
if err != nil {
221244
log.Errorf("[waitForRelayBlock] error: %s", err)
222245
}

service/ntorService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func (this *SyncService) neoToRelay(m, n uint32) error {
9898
// this loop confirm tx is a cross chain tx
9999
for _, notification := range execution.Notifications {
100100
u, _ := helper.UInt160FromString(notification.Contract)
101-
102101
if "0x"+u.String() == this.config.NeoCCMC && notification.EventName == "CrossChainLockEvent" {
103102
if notification.State.Type != "Array" {
104103
return fmt.Errorf("[neoToRelay] notification.State.Type error: Type is not Array")
@@ -142,6 +141,7 @@ func (this *SyncService) neoToRelay(m, n uint32) error {
142141
} else {
143142
passed = currentRelayChainSyncHeight
144143
}
144+
log.Infof("now process neo tx: " + tx.Hash)
145145
err = this.syncProofToRelay(key, passed)
146146
if err != nil {
147147
log.Errorf("--------------------------------------------------")

service/syncService.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ type SyncService struct {
1717
relaySyncHeight uint32
1818
relayPubKeys [][]byte
1919

20-
nwh *wallet.WalletHelper
21-
neoSdk *rpc.RpcClient
22-
neoSyncHeight uint32
23-
neoNextConsensus string
20+
nwh *wallet.WalletHelper
21+
neoSdk *rpc.RpcClient
22+
neoSyncHeight uint32
23+
neoNextConsensus string
24+
neoStateRootHeight uint32
2425

2526
db *db.BoltDB
2627
config *config.Config
@@ -37,12 +38,13 @@ func NewSyncService(acct *rsdk.Account, relaySdk *rsdk.PolySdk, neoAccount *wall
3738
os.Exit(1)
3839
}
3940
syncSvr := &SyncService{
40-
relayAccount: acct,
41-
relaySdk: relaySdk,
42-
neoSdk: client,
43-
nwh: neoAccount,
44-
db: boltDB,
45-
config: config.DefConfig,
41+
relayAccount: acct,
42+
relaySdk: relaySdk,
43+
neoSdk: client,
44+
neoStateRootHeight: 0,
45+
nwh: neoAccount,
46+
db: boltDB,
47+
config: config.DefConfig,
4648
}
4749
return syncSvr
4850
}

0 commit comments

Comments
 (0)