Skip to content

Commit

Permalink
fix: resolved unmarhaling for blobPointer and queryMerkleProofInput t…
Browse files Browse the repository at this point in the history
…imeout
  • Loading branch information
RISHABHAGRAWALZRA committed May 2, 2024
1 parent d0b437e commit da50864
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 247 deletions.
14 changes: 1 addition & 13 deletions das/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package avail
import (
"context"
"fmt"
"os"
"strings"

"time"
Expand Down Expand Up @@ -94,16 +93,6 @@ func NewAvailDA(cfg DAConfig, l1Client arbutil.L1Interface) (*AvailDA, error) {
// Contract address
contractAddress := common.HexToAddress(cfg.VectorX)

// Contract ABI (Application Binary Interface)
pwd, _ := os.Getwd()
log.Info(pwd)
// byteValue, err := os.ReadFile(pwd + "/das/avail/vectorx/abi/Vectorx.abi.json")
// if err != nil {
// log.Warn("⚠️ cannot read abi for vectorX: error:%v", err)
// return nil, err
// }
// vectorxABI := string(byteValue)

// Parse the contract ABI
abi, err := abi.JSON(strings.NewReader(vectorx.VectorxABI))
if err != nil {
Expand Down Expand Up @@ -156,7 +145,6 @@ func (a *AvailDA) Store(ctx context.Context, message []byte) ([]byte, error) {
}

nonce := GetAccountNonce(uint32(accountInfo.Nonce))
// fmt.Println("Nonce from localDatabase:", nonce, " :::::::: from acountInfo:", accountInfo.Nonce)
o := gsrpc_types.SignatureOptions{
BlockHash: a.genesisHash,
Era: gsrpc_types.ExtrinsicEra{IsMortalEra: false},
Expand Down Expand Up @@ -228,7 +216,7 @@ outer:
}
log.Info("Finalized extrinsic", "extrinsicIndex", extrinsicIndex)

merkleProofInput, err := QueryMerkleProofInput(finalizedblockHash.Hex(), extrinsicIndex)
merkleProofInput, err := QueryMerkleProofInput(finalizedblockHash.Hex(), extrinsicIndex, 1200)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion das/avail/avail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestMarshallingAndUnmarshalingBlobPointer(t *testing.T) {
extrinsicIndex := 1
bridgeApiBaseURL := "https://hex-bridge-api.sandbox.avail.tools"
blockHashPath := "/eth/proof/" + "0xf53613fa06b6b7f9dc5e4cf5f2849affc94e19d8a9e8999207ece01175c988ed" //+ finalizedblockHash.Hex()
blockHashPath := "/eth/proof/" + "0x1672d81d105b9efb5689913ae3c608488419bc6e32a5f8cc7766d194e8865f30" //+ finalizedblockHash.Hex()
params := url.Values{}
params.Add("index", fmt.Sprint(extrinsicIndex))

Expand Down
84 changes: 6 additions & 78 deletions das/avail/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ var arguments = abi.Arguments{
// MarshalBinary encodes the BlobPointer to binary
// serialization format: AvailMessageHeaderFlag + BlockHash + Sender + Nonce + DasTreeRootHash + MerkleProofInput
//
// minimum size = 330 bytes
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
// minimum size = 330 bytes
//
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// | 1 byte | 32 byte | 48 byte | 8 byte | 32 byte | minimum bytes size = 210 |
//
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// |<-- AvailMessageHeaderFlag -->|<----- BlockHash ----->|<----- Sender ----->|<----- Nonce ----->|<----- DasTreeRootHash ----->|<----- MerkleProofInput ----->|
//
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
func (b *BlobPointer) MarshalToBinary() ([]byte, error) {
packedData, err := arguments.PackValues([]interface{}{b.BlockHash, b.Sender, b.Nonce, b.DasTreeRootHash, b.MerkleProofInput})
if err != nil {
Expand All @@ -61,7 +62,7 @@ func (b *BlobPointer) MarshalToBinary() ([]byte, error) {
}

func (b *BlobPointer) UnmarshalFromBinary(data []byte) error {
unpackedData, err := arguments.UnpackValues(data[1:])
unpackedData, err := arguments.UnpackValues(data)
if err != nil {
return fmt.Errorf("unable to covert the data bytes into blobPointer and getting error:%v", err)
}
Expand All @@ -72,76 +73,3 @@ func (b *BlobPointer) UnmarshalFromBinary(data []byte) error {
b.MerkleProofInput = unpackedData[4].(MerkleProofInput)
return nil
}

// func (b *BlobPointer) MarshalToBinary() ([]byte, error) {

// buf := new(bytes.Buffer)

// // Encoding at first the avail message header flag
// if err := binary.Write(buf, binary.BigEndian, AvailMessageHeaderFlag); err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }

// // Marshaling in between: The Merkle proof input, which will be required for DA verification
// merkleProofInput, err := b.MerkleProofInput.MarshalToBinary()
// if err != nil {
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }
// buf.Write(merkleProofInput)

// // Encoding at last: blockHash, sender address, nonce and DASTreeRootHash which will not be required for DA verification
// if err := binary.Write(buf, binary.BigEndian, b.BlockHash); err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }
// var senderBytes = []byte(b.Sender)
// if err = binary.Write(buf, binary.BigEndian, uint8(len(senderBytes))); err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }
// if err = binary.Write(buf, binary.BigEndian, senderBytes); err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }
// if err = binary.Write(buf, binary.BigEndian, b.Nonce); err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the avail block referece into array of bytes and getting error:%w", err)
// }
// if err = binary.Write(buf, binary.BigEndian, b.DasTreeRootHash); err != nil {
// fmt.Println("binary.Write failed:", err)
// }

// return buf.Bytes(), nil
// }

// // UnmarshalBinary decodes the binary to BlobPointer
// func (b *BlobPointer) UnmarshalFromBinary(blobPointerData []byte) error {
// buf := bytes.NewReader(blobPointerData)

// if err := b.MerkleProofInput.UnmarshalFromBinary(buf); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &b.BlockHash); err != nil {
// return err
// }

// var len uint8
// if err := binary.Read(buf, binary.BigEndian, &len); err != nil {
// return err
// }
// var senderBytes = make([]byte, len)
// if err := binary.Read(buf, binary.BigEndian, &senderBytes); err != nil {
// return err
// }
// b.Sender = string(senderBytes)
// if err := binary.Read(buf, binary.BigEndian, &b.Nonce); err != nil {
// return err
// }
// if err := binary.Read(buf, binary.BigEndian, &b.DasTreeRootHash); err != nil {
// return err
// }

// return nil
// }
127 changes: 7 additions & 120 deletions das/avail/merkleProofInput.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,126 +20,13 @@ type MerkleProofInput struct {
LeafIndex uint64 `json:"leafIndex"`
}

// MarshalBinary encodes the MerkleProofInput to binary
// serialization format: Len(DataRootProof)+ + MerkleProofInput
// minimum size = 210 bytes
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// MarshalBinary encodes the MerkleProofInput to binary
// serialization format: Len(DataRootProof)+ + MerkleProofInput
// minimum size = 210 bytes
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// | 1 byte uint8 : DataRootProof length | 32*(len) byte : DataRootProof | 1 byte uint8 : LeafProof length | 32*(len) byte : LeafProof | 32 byte : RangeHash | 8 byte uint64 : DataRootIndex | 32 byte : BlobRoot | 32 byte : BridgeRoot | 32 byte : Leaf | 8 byte uint64 : LeafIndex |
// | 1 byte uint8 : DataRootProof length | 32*(len) byte : DataRootProof | 1 byte uint8 : LeafProof length | 32*(len) byte : LeafProof | 32 byte : RangeHash | 8 byte uint64 : DataRootIndex | 32 byte : BlobRoot | 32 byte : BridgeRoot | 32 byte : Leaf | 8 byte uint64 : LeafIndex |
//
// <-------- len(DataRootProof) -------->|<------- DataRootProof ------->|<------- len(LeafProof) -------->|<------- LeafProof ------->|<---- RangeHash ---->|<------- DataRootIndex ------->|<---- BlobRoot ---->|<---- BridgeRoot ---->|<---- Leaf ---->|<------- LeafIndex ------->|
// <-------- len(DataRootProof) -------->|<------- DataRootProof ------->|<------- len(LeafProof) -------->|<------- LeafProof ------->|<---- RangeHash ---->|<------- DataRootIndex ------->|<---- BlobRoot ---->|<---- BridgeRoot ---->|<---- Leaf ---->|<------- LeafIndex ------->|
//
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// func (i *MerkleProofInput) MarshalToBinary() ([]byte, error) {
// buf := new(bytes.Buffer)
// err := binary.Write(buf, binary.BigEndian, uint8(len(i.DataRootProof)))
// if err != nil {
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.DataRootProof)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, uint8(len(i.LeafProof)))
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.LeafProof)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.RangeHash)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.DataRootIndex)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.BlobRoot)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.BridgeRoot)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.Leaf)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// err = binary.Write(buf, binary.BigEndian, i.LeafIndex)
// if err != nil {
// fmt.Println("binary.Write failed:", err)
// return []byte{}, fmt.Errorf("unable to covert the merkle proof input into array of bytes and getting error:%w", err)
// }

// return buf.Bytes(), nil
// }

// func (m *MerkleProofInput) UnmarshalFromBinary(buf *bytes.Reader) error {
// var len uint8
// if err := binary.Read(buf, binary.BigEndian, &len); err != nil {
// return err
// }

// m.DataRootProof = make([]gsrpc_types.Hash, len)
// for i := uint8(0); i < len; i++ {
// if err := binary.Read(buf, binary.BigEndian, &m.DataRootProof[i]); err != nil {
// return err
// }
// }

// if err := binary.Read(buf, binary.BigEndian, &len); err != nil {
// return err
// }
// m.LeafProof = make([]gsrpc_types.Hash, len)
// for i := uint8(0); i < len; i++ {
// if err := binary.Read(buf, binary.BigEndian, &m.LeafProof[i]); err != nil {
// return err
// }
// }

// if err := binary.Read(buf, binary.BigEndian, &m.RangeHash); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &m.DataRootIndex); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &m.BlobRoot); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &m.BridgeRoot); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &m.Leaf); err != nil {
// return err
// }

// if err := binary.Read(buf, binary.BigEndian, &m.LeafIndex); err != nil {
// return err
// }

// return nil
// }
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
79 changes: 45 additions & 34 deletions das/avail/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type BridgeApiResponse struct {
RangeHash gsrpc_types.Hash `json:"rangeHash"`
}

func QueryMerkleProofInput(blockHash string, extrinsicIndex int) (MerkleProofInput, error) {
func QueryMerkleProofInput(blockHash string, extrinsicIndex int, t int64) (MerkleProofInput, error) {
// Quering for merkle proof from Bridge Api
bridgeApiBaseURL := "https://hex-bridge-api.sandbox.avail.tools/"
blockHashPath := "/eth/proof/" + blockHash
Expand All @@ -73,43 +73,54 @@ func QueryMerkleProofInput(blockHash string, extrinsicIndex int) (MerkleProofInp
u.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", u)

var resp *http.Response
timeout := time.After(time.Duration(t) * time.Second)

outer:
for {
resp, err := http.Get(urlStr) //nolint
if err != nil {
return MerkleProofInput{}, fmt.Errorf("bridge Api request not successfull, err=%w", err)
}
defer resp.Body.Close()
select {
case <-timeout:
return MerkleProofInput{}, fmt.Errorf("⌛️ Timeout of %d seconds reached without merkleProofInput from bridge-api for blockHash %v and extrinsic index %v", t, blockHash, extrinsicIndex)

if resp.StatusCode != 200 {
log.Info("MerkleProofInput is not yet available from bridge-api", "status", resp.Status)
time.Sleep(3 * time.Minute)
continue
default:
var err error
resp, err = http.Get(urlStr) //nolint
if err != nil {
return MerkleProofInput{}, fmt.Errorf("bridge Api request not successfull, err=%w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Info("⚠️🥱 MerkleProofInput is not yet available from bridge-api", "status", resp.Status)
time.Sleep(3 * time.Minute)
continue
}
break outer
}
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return MerkleProofInput{}, err
}
fmt.Println(string(body))
var bridgeApiResponse BridgeApiResponse
err = json.Unmarshal(body, &bridgeApiResponse)
if err != nil {
return MerkleProofInput{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return MerkleProofInput{}, err
}
fmt.Println(string(body))
var bridgeApiResponse BridgeApiResponse
err = json.Unmarshal(body, &bridgeApiResponse)
if err != nil {
return MerkleProofInput{}, err
}

var dataRootProof [][32]byte
for _, hash := range bridgeApiResponse.DataRootProof {
var byte32Array [32]byte
copy(byte32Array[:], hash[:])
dataRootProof = append(dataRootProof, byte32Array)
}
var leafProof [][32]byte
for _, hash := range bridgeApiResponse.LeafProof {
var byte32Array [32]byte
copy(byte32Array[:], hash[:])
leafProof = append(leafProof, byte32Array)
}
var merkleProofInput MerkleProofInput = MerkleProofInput{dataRootProof, leafProof, bridgeApiResponse.RangeHash, bridgeApiResponse.DataRootIndex, bridgeApiResponse.BlobRoot, bridgeApiResponse.BridgeRoot, bridgeApiResponse.Leaf, bridgeApiResponse.LeafIndex}
return merkleProofInput, nil
var dataRootProof [][32]byte
for _, hash := range bridgeApiResponse.DataRootProof {
var byte32Array [32]byte
copy(byte32Array[:], hash[:])
dataRootProof = append(dataRootProof, byte32Array)
}
var leafProof [][32]byte
for _, hash := range bridgeApiResponse.LeafProof {
var byte32Array [32]byte
copy(byte32Array[:], hash[:])
leafProof = append(leafProof, byte32Array)
}
var merkleProofInput MerkleProofInput = MerkleProofInput{dataRootProof, leafProof, bridgeApiResponse.RangeHash, bridgeApiResponse.DataRootIndex, bridgeApiResponse.BlobRoot, bridgeApiResponse.BridgeRoot, bridgeApiResponse.Leaf, bridgeApiResponse.LeafIndex}
return merkleProofInput, nil
}
2 changes: 1 addition & 1 deletion das/avail/vectorx/vectorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (v *VectorX) SubscribeForHeaderUpdate(finalizedBlockNumber int, t int64) er
}
defer sub.Unsubscribe()

log.Info("🎧 Listening for vectorx HeadUpdate event")
log.Info("🎧 Listening for vectorx HeadUpdate event with", "blockNumber", finalizedBlockNumber)
timeout := time.After(time.Duration(t) * time.Second)
// Loop to process incoming events
for {
Expand Down

0 comments on commit da50864

Please sign in to comment.