Skip to content

Commit

Permalink
Merge pull request #12 from EspressoSystems/jh/deploy
Browse files Browse the repository at this point in the history
Scratch for testing the integration
  • Loading branch information
ImJeremyHe authored Nov 16, 2023
2 parents 5fbdb1d + ce238ca commit c3c566d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[submodule "nitro-testnode"]
path = nitro-testnode
url = https://github.com/EspressoSystems/nitro-testnode
branch = integration
branch = integration
[submodule "espresso-sequencer"]
path = espresso-sequencer
url = https://github.com/EspressoSystems/espresso-sequencer.git
6 changes: 5 additions & 1 deletion arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func DeployOnL1(ctx context.Context, parentChainReader *headerreader.HeaderReade

type Config struct {
Sequencer bool `koanf:"sequencer"`
Espresso bool `koanf:"espresso"`
ParentChainReader headerreader.Config `koanf:"parent-chain-reader" reload:"hot"`
InboxReader InboxReaderConfig `koanf:"inbox-reader" reload:"hot"`
DelayedSequencer DelayedSequencerConfig `koanf:"delayed-sequencer" reload:"hot"`
Expand All @@ -299,6 +300,9 @@ type Config struct {
}

func (c *Config) Validate() error {
if c.Espresso && !c.Sequencer {
return errors.New("cannot enable espresso without enabling sequencer")
}
if c.ParentChainReader.Enable && c.Sequencer && !c.DelayedSequencer.Enable {
log.Warn("delayed sequencer is not enabled, despite sequencer and l1 reader being enabled")
}
Expand Down Expand Up @@ -620,7 +624,7 @@ func createNodeImpl(
if err != nil {
return nil, err
}
} else if config.Sequencer && !config.Dangerous.NoSequencerCoordinator {
} else if config.Sequencer && !config.Dangerous.NoSequencerCoordinator && !config.Espresso {
return nil, errors.New("sequencer must be enabled with coordinator, unless dangerous.no-sequencer-coordinator set")
}
dbs := []ethdb.Database{arbDb}
Expand Down
64 changes: 56 additions & 8 deletions espresso/client.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package espresso

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)

type Client struct {
baseUrl string
client *http.Client
log log.Logger
baseUrl string
client *http.Client
log log.Logger
namespace uint64
}

func NewClient(log log.Logger, url string) *Client {
func NewClient(log log.Logger, url string, namespace uint64) *Client {
if !strings.HasSuffix(url, "/") {
url += "/"
}
return &Client{
baseUrl: url,
client: http.DefaultClient,
log: log,
baseUrl: url,
client: http.DefaultClient,
log: log,
namespace: namespace,
}
}

Expand Down Expand Up @@ -52,8 +56,52 @@ func (c *Client) FetchHeader(ctx context.Context, blockHeight uint64) (Header, e
return res, nil
}

func (c *Client) FetchTransactionsInBlock(ctx context.Context, block uint64, header *Header, namespace uint64) (TransactionsInBlock, error) {
type RawTransaction struct {
Vm int `json:"vm"`
Payload []int8 `json:"payload"`
}

func (c *Client) SubmitTransaction(ctx context.Context, tx *types.Transaction) error {
var txnBytes, err = json.Marshal(tx)
if err != nil {
return err
}
// json.RawMessage is a []byte array, which is marshalled as a base64-encoded string.
// Our sequencer API expects a JSON array.
payload := make([]int8, len(txnBytes))
for i := range payload {
payload[i] = int8(txnBytes[i])
}
txn := RawTransaction{
Vm: int(c.namespace),
Payload: payload,
}
marshalled, err := json.Marshal(txn)
if err != nil {
return err
}
fmt.Println(c.baseUrl)
request, err := http.NewRequest("POST", c.baseUrl+"submit/submit", bytes.NewBuffer(marshalled))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return fmt.Errorf("receieved unexpected status code: %v", response.StatusCode)
}
return nil
}

func (c *Client) FetchTransactionsInBlock(ctx context.Context, block uint64, header *Header) (TransactionsInBlock, error) {
namespace := c.namespace
var res NamespaceResponse
log.Info(fmt.Sprintf("Fetching tx, block: %d, namespace: %d", block, namespace))
if err := c.get(ctx, &res, "availability/block/%d/namespace/%d", block, namespace); err != nil {
return TransactionsInBlock{}, err
}
Expand Down
13 changes: 8 additions & 5 deletions execution/gethexec/espresso_sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type HotShotState struct {
nextSeqBlockNum uint64
}

func NewHotShotState(log log.Logger, url string) *HotShotState {
func NewHotShotState(log log.Logger, url string, namespace uint64) *HotShotState {
return &HotShotState{
client: *espresso.NewClient(log, url),
client: *espresso.NewClient(log, url, namespace),
// TODO: Load this from the inbox reader so that new sequencers don't read redundant blocks
// https://github.com/EspressoSystems/espresso-sequencer/issues/734
nextSeqBlockNum: 0,
Expand All @@ -62,7 +62,7 @@ func NewEspressoSequencer(execEngine *ExecutionEngine, configFetcher SequencerCo
return &EspressoSequencer{
execEngine: execEngine,
config: configFetcher,
hotShotState: NewHotShotState(log.New(), config.HotShotUrl),
hotShotState: NewHotShotState(log.New(), config.HotShotUrl, config.EspressoNamespace),
}, nil
}

Expand All @@ -80,12 +80,11 @@ func (s *EspressoSequencer) createBlock(ctx context.Context) (returnValue bool)
nextSeqBlockNum := s.hotShotState.nextSeqBlockNum
log.Info("Attempting to sequence Espresso block", "block_num", nextSeqBlockNum)
header, err := s.hotShotState.client.FetchHeader(ctx, nextSeqBlockNum)
namespace := s.config().EspressoNamespace
if err != nil {
log.Warn("Unable to fetch header for block number, will retry", "block_num", nextSeqBlockNum)
return false
}
arbTxns, err := s.hotShotState.client.FetchTransactionsInBlock(ctx, nextSeqBlockNum, &header, namespace)
arbTxns, err := s.hotShotState.client.FetchTransactionsInBlock(ctx, nextSeqBlockNum, &header)
if err != nil {
log.Error("Error fetching transactions", "err", err)
return false
Expand Down Expand Up @@ -143,6 +142,10 @@ func (s *EspressoSequencer) Start(ctxIn context.Context) error {

// Required methods for the TransactionPublisher interface
func (s *EspressoSequencer) PublishTransaction(parentCtx context.Context, tx *types.Transaction, options *arbitrum_types.ConditionalOptions) error {
if err := s.hotShotState.client.SubmitTransaction(parentCtx, tx); err != nil {
log.Error("Failed to submit transaction", err)
return err
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion nitro-testnode

0 comments on commit c3c566d

Please sign in to comment.