Skip to content

Commit

Permalink
Merge pull request #160 from renproject/fix/detach-chain-logic-from-b…
Browse files Browse the repository at this point in the history
…urnToParams

Fix destination adrress encoding/decoding while building transaction
  • Loading branch information
jazg committed May 20, 2021
2 parents 8be54c3 + 1fe3cc3 commit 3d22bd3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
env:
FILECOIN_FFI_COMMIT: a62d00da59d1b0fb35f3a4ae854efa9441af892d
SOLANA_FFI_COMMIT: df7838d724f5eaf262ed77ed93b35b3f1f652bd3
SOLANA_FFI_COMMIT: f6521b8a1af44f4d468bc8e7e67ba3766a5602ef
services:
solana:
image: renbot/ren-solana:latest
Expand Down
77 changes: 53 additions & 24 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type SolFetcher struct {
}

func NewSolFetcher(client *solanaRPC.Client, gatewayAddress string) SolFetcher {
seeds := []byte("GatewayState")
seeds := []byte("GatewayStateV0.1.1")
programDerivedAddress := solana.ProgramDerivedAddress(pack.Bytes(seeds), multichain.Address(gatewayAddress))
programPubk, err := solanaSDK.PublicKeyFromBase58(string(programDerivedAddress))
if err != nil {
Expand Down Expand Up @@ -315,7 +315,7 @@ func (watcher Watcher) watchLogShiftOuts(parent context.Context) {
// Get current block number and last checked block number.
currentHeight, err := watcher.blockHeightFetcher.FetchBlockHeight(ctx)
if err != nil {
watcher.logger.Errorf("[watcher] error loading block header: %v", err)
watcher.logger.Warnf("[watcher] error loading block header: %v", err)
return
}

Expand Down Expand Up @@ -353,7 +353,7 @@ func (watcher Watcher) watchLogShiftOuts(parent context.Context) {
// Fetch logs
c, err := watcher.burnLogFetcher.FetchBurnLogs(ctx, lastHeight, currentHeight)
if err != nil {
watcher.logger.Errorf("[watcher] error fetching LogBurn events from=%v to=%v: %v", lastHeight, currentHeight, err)
watcher.logger.Warnf("[watcher] error fetching LogBurn events from=%v to=%v: %v", lastHeight, currentHeight, err)
return
}

Expand All @@ -368,7 +368,7 @@ func (watcher Watcher) watchLogShiftOuts(parent context.Context) {
amount := burn.Amount
to := burn.ToBytes

watcher.logger.Infof("[watcher] detected burn for %v (to=%v, amount=%v, nonce=%v)", watcher.selector.String(), string(to), amount, nonce)
watcher.logger.Infof("[watcher] detected burn for %v with nonce=%v", watcher.selector.String(), nonce)

// Send the burn transaction to the resolver.
params, err := watcher.burnToParams(burn.Txid, amount, to, nonce, watcher.gpubkey)
Expand Down Expand Up @@ -415,35 +415,28 @@ func (watcher Watcher) lastCheckedBlockNumber(currentBlockN uint64) (uint64, err

// burnToParams constructs params for a SubmitTx request with given ref.
func (watcher Watcher) burnToParams(txid pack.Bytes, amount pack.U256, toBytes []byte, nonce pack.Bytes32, gpubkey pack.Bytes) (jsonrpc.ParamsSubmitTx, error) {

// For v0 burn, `to` can be base58 encoded
version := tx.Version1
to := string(toBytes)
switch watcher.selector.Asset() {
case multichain.BTC, multichain.BCH, multichain.ZEC:
decoder := AddressEncodeDecoder(watcher.selector.Asset().OriginChain(), watcher.network)
_, err := decoder.DecodeAddress(multichain.Address(to))
if err != nil {
to = base58.Encode(toBytes)
_, err = decoder.DecodeAddress(multichain.Address(to))
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
version = tx.Version0
}
var version tx.Version
var to multichain.Address
var toDecoded []byte
var err error
burnChain := watcher.selector.Source()
switch burnChain {
case multichain.Solana:
version, to, toDecoded, err = watcher.handleAssetAddrSolana(toBytes)
default:
version, to, toDecoded, err = watcher.handleAssetAddrEth(toBytes)
}

burnChain := watcher.selector.Destination()
toBytes, err := watcher.bindings.DecodeAddress(burnChain, multichain.Address(to))
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}

watcher.logger.Infof("[watcher] burn parameters (to=%v, amount=%v, nonce=%v)", watcher.selector.String(), string(to), amount, nonce)

txindex := pack.U32(0)
payload := pack.Bytes{}
phash := engine.Phash(payload)
nhash := engine.Nhash(nonce, txid, txindex)
ghash := engine.Ghash(watcher.selector, phash, toBytes, nonce)
ghash := engine.Ghash(watcher.selector, phash, toDecoded, nonce)
input, err := pack.Encode(engine.LockMintBurnReleaseInput{
Txid: txid,
Txindex: txindex,
Expand Down Expand Up @@ -484,6 +477,42 @@ func (watcher Watcher) burnToParams(txid pack.Bytes, amount pack.U256, toBytes [
return jsonrpc.ParamsSubmitTx{Tx: transaction}, nil
}

func (watcher Watcher) handleAssetAddrEth(toBytes []byte) (tx.Version, multichain.Address, []byte, error) {
// For v0 burn, `to` can be base58 encoded
version := tx.Version1
to := multichain.Address(toBytes)
switch watcher.selector.Asset() {
case multichain.BTC, multichain.BCH, multichain.ZEC:
decoder := AddressEncodeDecoder(watcher.selector.Asset().OriginChain(), watcher.network)
_, err := decoder.DecodeAddress(to)
if err != nil {
to = multichain.Address(base58.Encode(toBytes))
_, err = decoder.DecodeAddress(to)
if err != nil {
return "-1", "", nil, err
}
version = tx.Version0
}
}

burnChain := watcher.selector.Destination()
toBytes, err := watcher.bindings.DecodeAddress(burnChain, to)
if err != nil {
return "-1", "", nil, err
}

return version, to, toBytes, nil
}

func (watcher Watcher) handleAssetAddrSolana(toBytes []byte) (tx.Version, multichain.Address, []byte, error) {
encoder := AddressEncodeDecoder(watcher.selector.Asset().OriginChain(), watcher.network)
to, err := encoder.EncodeAddress(toBytes)
if err != nil {
return "-1", "", nil, fmt.Errorf("encoding raw asset address returned by solana: %v", err)
}
return tx.Version1, to, toBytes, nil
}

func AddressEncodeDecoder(chain multichain.Chain, network multichain.Network) multichain.AddressEncodeDecoder {
switch chain {
case multichain.Bitcoin, multichain.DigiByte, multichain.Dogecoin:
Expand Down

0 comments on commit 3d22bd3

Please sign in to comment.