Compare commits

..

1 Commits

Author SHA1 Message Date
Péter Garamvölgyi
61c44037e9 add 30 batch per tx limit 2023-05-31 20:38:30 +02:00
7 changed files with 13 additions and 245 deletions

View File

@@ -231,7 +231,10 @@ func (p *BatchProposer) TryCommitBatches() {
index := 0
commit := false
calldataByteLen := uint64(0)
for ; index < len(p.batchDataBuffer); index++ {
// allow at most 30 batches to be committed in a single transaction
// note: we need a more fine-tuned approach, the batch proposer
// must break up a transaction if we run into "exceeds block gas limit"
for ; index < len(p.batchDataBuffer) && index < 30; index++ {
calldataByteLen += bridgeabi.GetBatchCalldataLength(&p.batchDataBuffer[index].Batch)
if calldataByteLen > p.commitCalldataSizeLimit {
commit = true

View File

@@ -1,138 +0,0 @@
package docker
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/stdcopy"
"io"
"strings"
"time"
)
// Container state.
type dockerState string
var (
runningState dockerState = "running"
exitedState dockerState = "exited"
)
func getSpecifiedContainer(image string, keyword string) (string, bool) {
containers, _ := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
})
var ID string
for _, container := range containers {
// Just use the exited container.
if container.Image != image ||
!strings.Contains(container.Names[0], keyword) {
continue
}
// If container state is running choose and return.
if container.State == string(runningState) {
return container.ID, true
}
// If state is exited just set id.
if container.State == string(exitedState) {
ID = container.ID
}
}
return ID, ID != ""
}
func startContainer(id string, stdout io.Writer) (io.Closer, uint16, error) {
// Get container again, check state and handle public port.
ct, err := getContainerByID(id)
if err != nil {
return nil, 0, err
}
// start the exited container.
if ct.State == string(exitedState) {
// Start the exist container.
err = cli.ContainerStart(context.Background(), id, types.ContainerStartOptions{})
if err != nil {
return nil, 0, err
}
}
// Get container again, check state and handle public port.
ct, err = getContainerByID(id)
if err != nil {
return nil, 0, err
}
if ct.State != string(runningState) {
return nil, 0, fmt.Errorf("container state is not running, id: %s", id)
}
readCloser, err := cli.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
// Handle stdout.
errCh := make(chan error, 1)
go func() {
_, err2 := stdcopy.StdCopy(stdout, stdout, readCloser)
errCh <- err2
}()
select {
case <-time.After(time.Millisecond * 200):
case err = <-errCh:
}
return readCloser, ct.Ports[0].PublicPort, err
}
func getContainerByID(id string) (*types.Container, error) {
filter := filters.NewArgs()
filter.Add("id", id)
lst, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
Filters: filter,
})
if len(lst) == 1 {
return &lst[0], err
}
return nil, fmt.Errorf("the container is not exist, id: %s", id)
}
func InitConfig(name string) (string, error) {
res, err := cli.ConfigCreate(context.Background(), swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: name,
Labels: map[string]string{},
},
Data: []byte("postgres config for " + name),
})
if err != nil {
return "", err
}
return res.ID, nil
}
func SetConfigLabel(id string, keyword string, setOrRm bool) (int, error) {
filter := filters.NewArgs()
filter.Add("id", id)
cfgs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
Filters: filter,
})
if err != nil || len(cfgs) == 0 {
return 0, err
}
cfg := cfgs[0]
if setOrRm {
cfg.Spec.Labels[keyword] = ""
} else {
delete(cfg.Spec.Labels, keyword)
}
// Update config and return count of labels.
return len(cfg.Spec.Labels), cli.ConfigUpdate(context.Background(), id, cfg.Version, cfg.Spec)
}

View File

@@ -1,64 +0,0 @@
package docker_test
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"testing"
)
var (
cli *client.Client
)
func init() {
var err error
cli, err = client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
cli.NegotiateAPIVersion(context.Background())
}
func TestPP(t *testing.T) {
res, err := cli.ConfigCreate(context.Background(), swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: "zz",
Labels: map[string]string{},
},
Data: []byte("const postgres config"),
})
assert.NoError(t, err)
filter := filters.NewArgs()
filter.Add("id", res.ID)
lst, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
Filters: filter,
})
assert.NoError(t, err)
t.Logf("ID: %s, version: %d\n", res.ID, lst[0].Version.Index)
}
func TestCC(t *testing.T) {
//err := cli.ConfigUpdate(context.Background(), "zma9k3nsfcmt9saimdd42p9ii", swarm.Version{Index: 166}, swarm.ConfigSpec{
// Annotations: swarm.Annotations{
// Name: "xx",
// Labels: map[string]string{"1": "3"},
// },
//})
//assert.NoError(t, err)
filter := filters.NewArgs()
filter.Add("name", "zz")
lst, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
Filters: filter,
})
assert.NoError(t, err)
for _, l := range lst {
t.Log(l.ID, l.Spec.Labels, l.Spec)
err = cli.ConfigRemove(context.Background(), l.ID)
assert.NoError(t, err)
}
}

View File

@@ -54,7 +54,7 @@ func NewDockerApp() *App {
Timestamp: timestamp,
L1gethImg: newTestL1Docker(),
L2gethImg: newTestL2Docker(),
DBImg: newTestDBDocker(),
DBImg: newTestDBDocker("postgres"),
DBConfigFile: fmt.Sprintf("/tmp/%d_db-config.json", timestamp),
}
if err := app.mockDBConfig(); err != nil {
@@ -189,7 +189,7 @@ func newTestL2Docker() GethImgInstance {
return NewImgGeth("scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64()))
}
func newTestDBDocker() ImgInstance {
func newTestDBDocker(driverName string) ImgInstance {
id, _ := rand.Int(rand.Reader, big.NewInt(2000))
return NewImgDB("123456", dbStartPort+int(id.Int64()))
return NewImgDB(driverName, "123456", "test_db", dbStartPort+int(id.Int64()))
}

View File

@@ -27,17 +27,15 @@ type ImgDB struct {
}
// NewImgDB return postgres db img instance.
func NewImgDB(password string, port int) ImgInstance {
image := "postgres"
dbName := "test_db"
func NewImgDB(image, password, dbName string, port int) ImgInstance {
img := &ImgDB{
image: image,
name: fmt.Sprintf("%s-v1-%s_%d", image, dbName, port),
name: fmt.Sprintf("%s-%s_%d", image, dbName, port),
password: password,
dbName: dbName,
port: port,
}
//img.cmd = cmd.NewCmd(img.name, img.prepare()...)
img.cmd = cmd.NewCmd(img.name, img.prepare()...)
return img
}
@@ -86,15 +84,10 @@ func (i *ImgDB) IsRunning() bool {
}
func (i *ImgDB) prepare() []string {
cmd := []string{"docker", "run", "--rm", "-v", fmt.Sprintf("%s/pg-init-scripts:/docker-entrypoint-initdb.d", absPath), "--name", i.name, "-p", fmt.Sprintf("%d:5432", i.port)}
dbNames := i.dbName + "_0"
for idx := 1; idx < 10; idx++ {
dbNames += fmt.Sprintf(",%s_%d", i.dbName, idx)
}
cmd := []string{"docker", "run", "--rm", "--name", i.name, "-p", fmt.Sprintf("%d:5432", i.port)}
envs := []string{
"-e", "POSTGRES_PASSWORD=" + i.password,
"-e", fmt.Sprintf("POSTGRES_MULTIPLE_DATABASES=%s", dbNames),
"-e", fmt.Sprintf("POSTGRES_DB=%s", i.dbName),
}
cmd = append(cmd, envs...)

View File

@@ -3,7 +3,6 @@ package docker
import (
"context"
"math/big"
"path/filepath"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
@@ -11,8 +10,7 @@ import (
)
var (
cli *client.Client
absPath string
cli *client.Client
)
func init() {
@@ -22,8 +20,6 @@ func init() {
panic(err)
}
cli.NegotiateAPIVersion(context.Background())
absPath, _ = filepath.Abs("./")
}
// ImgInstance is an img instance.

View File

@@ -1,22 +0,0 @@
#!/bin/bash
set -e
set -u
function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi