Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e add new projectID for liveness #800

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 44 additions & 21 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2e
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"log"
"math/big"
Expand All @@ -11,6 +12,7 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -72,6 +74,11 @@ func TestE2E(t *testing.T) {
// Setup VM
gnarkVMContainer, gnarkVMEndpoint, err := services.SetupGnarkVM()
require.NoError(t, err)
t.Cleanup(func() {
if err := gnarkVMContainer.Terminate(context.Background()); err != nil {
t.Logf("failed to terminate vm container: %v", err)
}
})

// APINode init
tempApiNodeDB, err := os.CreateTemp("", "apinode.db")
Expand All @@ -91,8 +98,7 @@ func TestE2E(t *testing.T) {
defer tempSequencerDB.Close()
sequencer, err := sequencerInit(chDSN, tempSequencerDB.Name(), chainEndpoint, contracts)
require.NoError(t, err)
err = sendETH(t, chainEndpoint, payerHex, sequencer.Address(), 200)
require.NoError(t, err)
sendETH(t, chainEndpoint, payerHex, sequencer.Address(), 200)
err = sequencer.Start()
require.NoError(t, err)
defer sequencer.Stop()
Expand All @@ -109,36 +115,53 @@ func TestE2E(t *testing.T) {
require.NoError(t, err)
defer prover.Stop()

// Register project
projectOwnerKey, err := crypto.GenerateKey()
require.NoError(t, err)
projectOwnerAddr := crypto.PubkeyToAddress(projectOwnerKey.PublicKey)
err = sendETH(t, chainEndpoint, payerHex, projectOwnerAddr, 20)
require.NoError(t, err)
projectID, err := registerProject(t, chainEndpoint, contracts, projectOwnerKey)
require.NoError(t, err)

// Register prover
proverAddr := crypto.PubkeyToAddress(proverKey.PublicKey)
err = sendETH(t, chainEndpoint, payerHex, proverAddr, 20)
require.NoError(t, err)
sendETH(t, chainEndpoint, payerHex, proverAddr, 20)
err = registerProver(t, chainEndpoint, contracts, proverKey)
require.NoError(t, err)

// Register device
deviceKey, err := crypto.GenerateKey()
require.NoError(t, err)
deviceAddr := crypto.PubkeyToAddress(deviceKey.PublicKey)
err = sendETH(t, chainEndpoint, payerHex, deviceAddr, 20)
require.NoError(t, err)
registerIoID(t, chainEndpoint, contracts, deviceKey, projectID)
sendETH(t, chainEndpoint, payerHex, deviceAddr, 20)
//registerIoID(t, chainEndpoint, contracts, deviceKey, projectID)

t.Run("GNARK", func(t *testing.T) {
// Register project
projectOwnerKey, err := crypto.GenerateKey()
require.NoError(t, err)
projectOwnerAddr := crypto.PubkeyToAddress(projectOwnerKey.PublicKey)
sendETH(t, chainEndpoint, payerHex, projectOwnerAddr, 20)
projectID := big.NewInt(1)
registerIoID(t, chainEndpoint, contracts, deviceKey, projectID)
registerProject(t, chainEndpoint, contracts, projectOwnerKey, projectID, common.HexToAddress(contracts.MockDapp))

gnarkCodePath := "./testdata/gnark.code"
gnarkMetadataPath := "./testdata/gnark.metadata"
project := &project.Project{Configs: []*project.Config{{Version: "v1", VMTypeID: 1}}}
// Upload project
uploadProject(t, chainEndpoint, ipfsEndpoint, project, &gnarkCodePath, &gnarkMetadataPath, contracts, projectOwnerKey, projectID)
require.NoError(t, err)
// Wait a few seconds for the device info synced on api node
time.Sleep(2 * time.Second)
// Send message: prove 1+1=2
data, err := hex.DecodeString("00000001000000010000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001")
require.NoError(t, err)
taskid := sendMessage(t, data, projectID, nil, deviceKey, apiNodeUrl)
waitSettled(t, taskid, apiNodeUrl)
})
t.Run("GNARK-liveness", func(t *testing.T) {
t.Cleanup(func() {
if err := gnarkVMContainer.Terminate(context.Background()); err != nil {
t.Logf("failed to terminate vm container: %v", err)
}
})
// Register project
projectOwnerKey, err := crypto.GenerateKey()
require.NoError(t, err)
projectOwnerAddr := crypto.PubkeyToAddress(projectOwnerKey.PublicKey)
sendETH(t, chainEndpoint, payerHex, projectOwnerAddr, 20)
projectID := big.NewInt(2)
registerIoID(t, chainEndpoint, contracts, deviceKey, projectID)
registerProject(t, chainEndpoint, contracts, projectOwnerKey, projectID, common.HexToAddress(contracts.MockDappLiveness))

gnarkCodePath := "./testdata/pebble.circuit"
gnarkMetadataPath := "./testdata/pebble.pk"
project := &project.Project{Configs: []*project.Config{{
Expand Down
13 changes: 5 additions & 8 deletions e2e/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func proverInit(chDSN, dbFile, chainEndpoint string, vmEndpoints map[int]string,
}

func registerProject(t *testing.T, chainEndpoint string,
contractDeployments *services.ContractsDeployments, projectOwner *ecdsa.PrivateKey) (*big.Int, error) {
contractDeployments *services.ContractsDeployments, projectOwner *ecdsa.PrivateKey, projectID *big.Int, dpp common.Address) {
client, err := ethclient.Dial(chainEndpoint)
require.NoError(t, err)
chainID, err := client.ChainID(context.Background())
Expand All @@ -141,7 +141,6 @@ func registerProject(t *testing.T, chainEndpoint string,
require.NoError(t, err)
_, err = services.WaitForTransactionReceipt(client, tx.Hash())
require.NoError(t, err)
newProjectID := big.NewInt(1)

// Register project in w3bstream
projectRegistrarContract, err := projectregistrar.NewProjectRegistrar(
Expand All @@ -152,7 +151,7 @@ func registerProject(t *testing.T, chainEndpoint string,
tOpts, err = bind.NewKeyedTransactorWithChainID(projectOwner, chainID)
require.NoError(t, err)
tOpts.Value = registerFee
tx, err = projectRegistrarContract.Register(tOpts, newProjectID)
tx, err = projectRegistrarContract.Register(tOpts, projectID)
require.NoError(t, err)
_, err = services.WaitForTransactionReceipt(client, tx.Hash())
require.NoError(t, err)
Expand All @@ -168,11 +167,11 @@ func registerProject(t *testing.T, chainEndpoint string,
common.HexToAddress(contractDeployments.ProjectReward), client)
require.NoError(t, err)
require.NoError(t, err)
tx, err = projectRewardContract.SetReward(tOpts, newProjectID, rewardAmount)
tx, err = projectRewardContract.SetReward(tOpts, projectID, rewardAmount)
require.NoError(t, err)
_, err = services.WaitForTransactionReceipt(client, tx.Hash())
require.NoError(t, err)
tx, err = projectRewardContract.SetRewardToken(tOpts, newProjectID, mockerc20Addr)
tx, err = projectRewardContract.SetRewardToken(tOpts, projectID, mockerc20Addr)
require.NoError(t, err)
_, err = services.WaitForTransactionReceipt(client, tx.Hash())
require.NoError(t, err)
Expand All @@ -190,12 +189,10 @@ func registerProject(t *testing.T, chainEndpoint string,
// Bind dapp to router
router, err := router.NewRouter(common.HexToAddress(contractDeployments.Router), client)
require.NoError(t, err)
tx, err = router.BindDapp(tOpts, newProjectID, common.HexToAddress(contractDeployments.MockDapp))
tx, err = router.BindDapp(tOpts, projectID, dpp)
require.NoError(t, err)
_, err = services.WaitForTransactionReceipt(client, tx.Hash())
require.NoError(t, err)

return newProjectID, nil
}

func uploadProject(t *testing.T, chainEndpoint, ipfsURL string,
Expand Down
27 changes: 16 additions & 11 deletions e2e/services/contractdeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ var (
wsProjectRe = regexp.MustCompile(`W3bstreamProject deployed to (\S+)`)
routerRe = regexp.MustCompile(`W3bstreamRouter deployed to (\S+)`)
mockDappRe = regexp.MustCompile(`MockDapp deployed to (\S+)`)
mockDappLivenessRe = regexp.MustCompile(`MockDappLiveness deployed to (\S+)`)
projectRewardRe = regexp.MustCompile(`W3bstreamProjectReward deployed to (\S+)`)
debitsRe = regexp.MustCompile(`W3bstreamDebits deployed to (\S+)`)
ioIDRe = regexp.MustCompile(`MockIoID deployed to (\S+)`)
)

type ContractsDeployments struct {
TaskManager string
Prover string
Minter string
Registrar string
MockProject string
WSProject string
Router string
MockDapp string
ProjectReward string
Debits string
IoID string
TaskManager string
Prover string
Minter string
Registrar string
MockProject string
WSProject string
Router string
MockDapp string
MockDappLiveness string
ProjectReward string
Debits string
IoID string
}

func DeployContract(endpoint string, payerHex string) (*ContractsDeployments, error) {
Expand Down Expand Up @@ -103,6 +105,9 @@ func DeployContract(endpoint string, payerHex string) (*ContractsDeployments, er
if match := mockDappRe.FindStringSubmatch(output); len(match) > 1 {
deployments.MockDapp = match[1]
}
if match := mockDappLivenessRe.FindStringSubmatch(output); len(match) > 1 {
deployments.MockDappLiveness = match[1]
}
if match := routerRe.FindStringSubmatch(output); len(match) > 1 {
deployments.Router = match[1]
}
Expand Down
16 changes: 4 additions & 12 deletions e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,14 @@ func waitUntil(f func() (bool, error), timeOut time.Duration) error {
}
}

func sendETH(t *testing.T, chainEndpoint string, payerHex string, toAddress common.Address, amount uint64) error {
func sendETH(t *testing.T, chainEndpoint string, payerHex string, toAddress common.Address, amount uint64) {
client, err := ethclient.Dial(chainEndpoint)
if err != nil {
return err
}
require.NoError(t, err)
defer client.Close()

// 2. Load the sender's private key
privateKey, err := crypto.HexToECDSA(payerHex) // Replace with actual private key
if err != nil {
log.Fatalf("Failed to load private key: %v", err)
}
require.NoError(t, err)

// 3. Get the sender's address from the private key
publicKey := privateKey.Public()
Expand All @@ -170,9 +166,7 @@ func sendETH(t *testing.T, chainEndpoint string, payerHex string, toAddress comm
value := big.NewInt(0).Mul(big.NewInt(int64(amount)), big.NewInt(1e18)) // Amount in Wei (1 ETH = 10^18 Wei)
gasLimit := uint64(21000) // Gas limit for simple ETH transfer
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatalf("Failed to get suggested gas price: %v", err)
}
require.NoError(t, err)

// 6. Create the transaction
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil)
Expand All @@ -189,6 +183,4 @@ func sendETH(t *testing.T, chainEndpoint string, payerHex string, toAddress comm

_, err = services.WaitForTransactionReceipt(client, signedTx.Hash())
require.NoError(t, err)

return nil
}
4 changes: 4 additions & 0 deletions vm/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
)

func LoadPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
if len(projectConfig.SignedKeys) > 0 {
return encodePebblePayload(task, projectConfig)
}
return task.Payload, nil
// switch task.ProjectID.String() {
// case _pebbleProjectID.String():
return encodePebblePayload(task, projectConfig)
Expand Down
Loading