forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ethda | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"fmt" | ||
blobutils "github.com/crustio/blob-utils" | ||
"github.com/ethereum/go-ethereum/common" | ||
"math/big" | ||
) | ||
|
||
type EthdaBackend struct { | ||
ethdaClient *blobutils.Client | ||
chainId *big.Int | ||
ethdaRPCURL string | ||
privKey *ecdsa.PrivateKey | ||
} | ||
|
||
func New( | ||
ethdaRPCURL string, | ||
privKey *ecdsa.PrivateKey, | ||
) (*EthdaBackend, error) { | ||
if ethdaRPCURL == "" { | ||
return nil, fmt.Errorf("empty ethda rpc url") | ||
} | ||
|
||
if privKey == nil { | ||
return nil, fmt.Errorf("empty private key") | ||
} | ||
|
||
return &EthdaBackend{ | ||
privKey: privKey, | ||
ethdaRPCURL: ethdaRPCURL, | ||
}, nil | ||
} | ||
|
||
func (d *EthdaBackend) Init() error { | ||
ethdaClient, err := blobutils.New(d.ethdaRPCURL, d.privKey) | ||
if err != nil { | ||
return fmt.Errorf("create ethda client: %w", err) | ||
} | ||
|
||
d.ethdaClient = ethdaClient | ||
|
||
return nil | ||
} | ||
|
||
func (d *EthdaBackend) PostSequence(ctx context.Context, batchesData [][]byte) ([]byte, error) { | ||
var hashes []byte | ||
for _, batch := range batchesData { | ||
hash, err := d.ethdaClient.PostBlob(ctx, batch) | ||
if err != nil { | ||
return nil, fmt.Errorf("post batch to ethda: %w", err) | ||
} | ||
hashes = append(hashes, hash.Bytes()...) | ||
} | ||
|
||
return hashes, nil | ||
} | ||
|
||
func (d *EthdaBackend) GetSequence(ctx context.Context, batchHashes []common.Hash, dataAvailabilityMessage []byte) ([][]byte, error) { | ||
if len(dataAvailabilityMessage)%common.HashLength != 0 { | ||
return nil, fmt.Errorf("wrong da message length: %d", len(dataAvailabilityMessage)) | ||
} | ||
|
||
var data [][]byte | ||
for i := 0; i < len(dataAvailabilityMessage)/common.HashLength; i++ { | ||
start := common.HashLength * i | ||
hash := common.BytesToHash(dataAvailabilityMessage[start : start+common.HashLength]) | ||
|
||
r, err := d.ethdaClient.GetBlob(hash) | ||
if err != nil { | ||
return nil, fmt.Errorf("get blob from ethda: %w", err) | ||
} | ||
data = append(data, r) | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ethda | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestPostSequence(t *testing.T) { | ||
rpcUrl := "https://rpc-devnet2.ethda.io" | ||
|
||
// test key | ||
key, err := crypto.HexToECDSA("f26d6aca18e0c75ac948a262d4b9435a8173515f84c258d1f90d171143039024") | ||
require.NoError(t, err) | ||
|
||
da, err := New(rpcUrl, key) | ||
require.NoError(t, err) | ||
|
||
err = da.Init() | ||
require.NoError(t, err) | ||
|
||
batchData := [][]byte{[]byte("ethda")} | ||
msg, err := da.PostSequence(context.Background(), batchData) | ||
require.NoError(t, err) | ||
fmt.Println(common.Bytes2Hex(msg)) | ||
} | ||
|
||
func TestGetSequence(t *testing.T) { | ||
rpcUrl := "https://rpc-devnet2.ethda.io" | ||
|
||
// test key | ||
key, err := crypto.HexToECDSA("f26d6aca18e0c75ac948a262d4b9435a8173515f84c258d1f90d171143039024") | ||
require.NoError(t, err) | ||
da, err := New(rpcUrl, key) | ||
require.NoError(t, err) | ||
|
||
err = da.Init() | ||
require.NoError(t, err) | ||
|
||
data, err := da.GetSequence(context.Background(), nil, common.Hex2Bytes("76b56f65beab1cfe55242eb97eebfe2f32aacd957f202122835026bffb3ba282")) | ||
require.NoError(t, err) | ||
fmt.Println(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters