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

migrate from eggroll to rollmelette #48

Merged
merged 10 commits into from
Apr 19, 2024
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
4 changes: 2 additions & 2 deletions cli/cmd/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func bountyRun(cmd *cobra.Command, args []string) {
}
durationSecs := time.Duration(bountyDuration) * time.Second
deadline := time.Now().UTC().Add(durationSecs).Unix()
input := &shared.CreateAppBounty{
payload := &shared.CreateAppBounty{
Name: bountyName,
ImgLink: bountyImgLink,
Description: bountyDescription,
Deadline: deadline,
CodeZipBinary: code,
}
sendInput(input)
sendInput(shared.CreateAppBountyInputKind, payload)
}

func bountyLoadCode() (string, error) {
Expand Down
19 changes: 0 additions & 19 deletions cli/cmd/codecs.go

This file was deleted.

4 changes: 2 additions & 2 deletions cli/cmd/exploit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ var (
)

func exploitRun(cmd *cobra.Command, args []string) {
input := &shared.SendExploit{
payload := &shared.SendExploit{
BountyIndex: exploitBountyIndex,
Name: exploitName,
ImgLink: exploitImgLink,
Exploit: loadExploit(exploitPath),
}
sendInput(input)
sendInput(shared.SendExploitInputKind, payload)
}

func init() {
Expand Down
100 changes: 54 additions & 46 deletions cli/cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,99 @@ package cmd
import (
"bugless/shared"
"context"
"encoding/json"
"log"
"math/big"
"os/exec"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/gligneul/eggroll"
"github.com/gligneul/eggroll/eggeth"
"github.com/gligneul/eggroll/eggtypes"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/gligneul/rollmelette"
"github.com/spf13/cobra"
)

var sendArgs struct {
accountIndex uint32
fromAddress string
}

var sendCmd = &cobra.Command{
Use: "send",
Short: "Send an advance-state input to the contract",
}

func sendDo(send func(context.Context, *eggroll.Client, eggeth.Signer) (int, error)) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

client, signer, err := eggroll.NewDevClient(ctx, shared.Codecs())
if err != nil {
log.Fatal(err)
}

if sendArgs.accountIndex != 0 {
err := signer.(*eggeth.MnemonicSigner).SetAccount(sendArgs.accountIndex)
if err != nil {
log.Fatal(err)
}
}
var addressBook = rollmelette.NewAddressBook()

inputIndex, err := send(ctx, client, signer)
var dappAddress = "0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C"

func sendDo(inputKind shared.InputKind, payload any, send func(string, context.Context) ([]byte, error)) {
payloadJson, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
log.Printf("added input %v", inputIndex)

result, err := client.WaitFor(ctx, inputIndex)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gligneul
Do you think something like waitForInput function is a good helper function to replace the WaitFor function above?

Similarly, to implement the stateRun function, seems this getNodeState function would be a good helper, but it's not exported.

input := shared.Input{
Kind: inputKind,
Payload: payloadJson,
}
inputJson, err := json.Marshal(input)
if err != nil {
log.Fatal(err)
}
inputJsonStr := hexutil.Encode(inputJson)

if result.Status == eggtypes.CompletionStatusAccepted {
log.Print("input accepted")
} else {
log.Print("input not accepted")
}

for _, voucher := range result.Vouchers {
log.Printf("voucher to %v with payload 0x%v",
voucher.Destination, common.Bytes2Hex(voucher.Payload))
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

for _, logMsg := range result.Logs() {
log.Printf("contract: %v", logMsg)
output, err := send(inputJsonStr, ctx)
if err != nil {
log.Fatal(string(output), err)
}
log.Printf("input added\n%s", output)
}

func sendEther(txValue *big.Int, input any) {
sendDo(func(ctx context.Context, c *eggroll.Client, s eggeth.Signer) (int, error) {
return c.SendEther(ctx, s, txValue, input)
func sendEther(txValue *big.Int, inputKind shared.InputKind, payload any) {
sendDo(inputKind, payload, func(inputJsonStr string, ctx context.Context) ([]byte, error) {
cmd := exec.CommandContext(ctx,
"cast", "send",
"--unlocked", "--from", sendArgs.fromAddress,
"--value", txValue.String(),
addressBook.EtherPortal.String(), // TO
"depositEther(address,bytes)", // SIG
dappAddress, inputJsonStr, // ARGS
)
return cmd.CombinedOutput()
})
}

func sendInput(input any) {
sendDo(func(ctx context.Context, c *eggroll.Client, s eggeth.Signer) (int, error) {
return c.SendInput(ctx, s, input)
func sendInput(inputKind shared.InputKind, payload any) {
sendDo(inputKind, payload, func(inputJsonStr string, ctx context.Context) ([]byte, error) {
cmd := exec.CommandContext(ctx,
"cast", "send",
"--unlocked", "--from", sendArgs.fromAddress,
addressBook.InputBox.String(), // TO
"addInput(address,bytes)(bytes32)", // SIG
dappAddress, inputJsonStr, // ARGS
)
return cmd.CombinedOutput()
})
}

func sendDAppAddress() {
sendDo(func(ctx context.Context, c *eggroll.Client, s eggeth.Signer) (int, error) {
return c.SendDAppAddress(ctx, s)
sendDo("", "", func(inputJsonStr string, ctx context.Context) ([]byte, error) {
cmd := exec.CommandContext(ctx,
"cast", "send",
"--unlocked", "--from", sendArgs.fromAddress,
addressBook.AppAddressRelay.String(), // TO
"relayDAppAddress(address)", // SIG
dappAddress, // ARGS
)
return cmd.CombinedOutput()
})
}

func init() {
rootCmd.AddCommand(sendCmd)

sendCmd.PersistentFlags().Uint32VarP(&sendArgs.accountIndex,
"account-index", "a", 0, "Forge account index when sending the transaction")
sendCmd.PersistentFlags().StringVarP(&sendArgs.fromAddress,
"from-address", "f", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"Sender address when sending the transaction")
Comment on lines -91 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: instead of using account index, we now use address as argument directly

}
4 changes: 2 additions & 2 deletions cli/cmd/sponsor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func sponsorRun(cmd *cobra.Command, args []string) {
weiValue := new(big.Float).Mul(etherValue, tenToEighteen)
value := new(big.Int)
weiValue.Int(value)
input := &shared.AddSponsorship{
payload := &shared.AddSponsorship{
BountyIndex: sponsorBountyIndex,
Name: sponsorName,
ImgLink: sponsorImgLink,
}
sendEther(value, input)
sendEther(value, shared.AddSponsorshipInputKind, payload)
}

func init() {
Expand Down
44 changes: 26 additions & 18 deletions cli/cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"log"
"time"

"github.com/gligneul/eggroll"
"github.com/gligneul/eggroll/eggtypes"
"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/pkg/readerclient"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/cobra"
)

Expand All @@ -23,17 +24,13 @@ func stateRun(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

client, _, err := eggroll.NewDevClient(ctx, shared.Codecs())
client := graphql.NewClient("http://127.0.0.1:8080/graphql", nil)
inputs, err := readerclient.GetInputs(ctx, client)
if err != nil {
log.Fatal(err)
}

results, err := client.GetResults(ctx, 0)
if err != nil {
log.Fatal(err)
}

state := findLastState(client, results)
state := findLastState(inputs)
if state == nil {
fmt.Println("{\"Bounties\":[]}")
} else {
Expand All @@ -45,20 +42,31 @@ func stateRun(cmd *cobra.Command, args []string) {
}
}

func findLastState(client *eggroll.Client, results []*eggtypes.AdvanceResult) *shared.BugLessState {
for i := len(results) - 1; i >= 0; i-- {
if len(results[i].RawReturn()) == 0 {
func findLastState(inputs []readerclient.Input) *shared.BugLessState {
for i := len(inputs) - 1; i >= 0; i-- {
if len(inputs[i].Reports) == 0 {
continue
}
return_ := client.DecodeReturn(results[i])
if return_ == nil {

if inputs[i].Status != readerclient.CompletionStatusAccepted {
continue
}
state, ok := return_.(*shared.BugLessState)
if !ok {
log.Fatalf("failed to decode return: %v", return_)

report := inputs[i].Reports[0] // each input only has 1 report at the moment
payloadString := report.Payload.String()
payload, err := hexutil.Decode(payloadString)
if err != nil {
log.Fatal(err)
}

var state shared.BugLessState
err = json.Unmarshal(payload, &state)

if err != nil {
log.Fatal(err)
}
return state

return &state
}
return nil
}
Expand Down
22 changes: 11 additions & 11 deletions cli/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package cmd
import (
"bugless/shared"
"context"
"encoding/json"
"log"
"time"

"github.com/gligneul/eggroll"
"github.com/gligneul/eggroll/eggtypes"
"github.com/gligneul/rollmelette/integration"
"github.com/spf13/cobra"
)

Expand All @@ -24,31 +24,31 @@ var (
)

func testRun(cmd *cobra.Command, args []string) {
InspectEndpoint := "http://localhost:8080/inspect"

input := &shared.TestExploit{
BountyIndex: testBountyIndex,
Exploit: loadExploit(testPath),
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

client, _, err := eggroll.NewDevClient(ctx, shared.Codecs())
inputJson, err := json.Marshal(input)
if err != nil {
log.Fatal(err)
}

result, err := client.Inspect(ctx, input)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

result, err := integration.Inspect(ctx, InspectEndpoint, inputJson)
if err != nil {
log.Fatal(err)
}

if result.Status == eggtypes.CompletionStatusAccepted {
if result.Status == integration.Accepted {
log.Print("input accepted")
} else {
log.Print("input not accepted")
}

for _, logMsg := range result.Logs() {
for _, logMsg := range result.Reports {
log.Printf("contract: %v", logMsg)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ var withdrawCmd = &cobra.Command{
Use: "withdraw",
Short: "Withdraw from a bounties",
Run: func(cmd *cobra.Command, args []string) {
input := &shared.WithdrawSponsorship{
payload := &shared.WithdrawSponsorship{
BountyIndex: withdrawArgs.bountyIndex,
}
sendInput(input)
sendInput(shared.WithdrawSponsorshipInputKind, payload)
},
}

Expand Down
Loading