Skip to content

Commit

Permalink
cmd/e2etest: add createaccts test
Browse files Browse the repository at this point in the history
and also BenchmarkAPICreateNAccounts
  • Loading branch information
altergui authored and p4u committed Jul 3, 2024
1 parent 4e80470 commit 271b3ce
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
45 changes: 44 additions & 1 deletion cmd/end2endtest/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@ func init() {
example: os.Args[0] + " --operation=tokentxs " +
"--host http://127.0.0.1:9090/v2",
}

ops["createaccts"] = operation{
testFunc: func() VochainTest {
return &E2ECreateAccts{}
},
description: "Creates N accounts",
example: os.Args[0] + " --operation=createaccts --votes=1000 " +
"--host http://127.0.0.1:9090/v2",
}
}

var _ VochainTest = (*E2ETokenTxs)(nil)
var (
_ VochainTest = (*E2ETokenTxs)(nil)
_ VochainTest = (*E2ECreateAccts)(nil)
)

type E2ETokenTxs struct {
api *apiclient.HTTPclient
Expand All @@ -42,6 +54,8 @@ type E2ETokenTxs struct {
aliceFP *models.FaucetPackage
}

type E2ECreateAccts struct{ e2eElection }

func (t *E2ETokenTxs) Setup(api *apiclient.HTTPclient, config *config) error {
t.api = api
t.config = config
Expand Down Expand Up @@ -382,3 +396,32 @@ func ensureAccountMetadataEquals(api *apiclient.HTTPclient, metadata *apipkg.Acc
}
return nil, fmt.Errorf("cannot set account %s metadata after %d retries", api.MyAddress(), retries)
}

func (t *E2ECreateAccts) Setup(api *apiclient.HTTPclient, c *config) error {
t.api = api
t.config = c

return nil
}

func (*E2ECreateAccts) Teardown() error {
// nothing to do here
return nil
}

func (t *E2ECreateAccts) Run() error {
startTime := time.Now()

voterAccounts := ethereum.NewSignKeysBatch(t.config.nvotes)
if err := t.registerAnonAccts(voterAccounts); err != nil {
return err
}

log.Infow("accounts created successfully",
"n", t.config.nvotes, "time", time.Since(startTime),
"vps", int(float64(t.config.nvotes)/time.Since(startTime).Seconds()))

log.Infof("voterAccounts: %d", len(voterAccounts))

return nil
}
6 changes: 6 additions & 0 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,8 @@ func (t *e2eElection) registerAnonAccts(voterAccounts []*ethereum.SignKeys) erro
errorChan := make(chan error)
wg := &sync.WaitGroup{}

sem := make(chan any, t.config.parallelCount)

for i, acc := range voterAccounts {
if i%10 == 0 {
// Print some information about progress on large censuses
Expand All @@ -771,7 +773,11 @@ func (t *e2eElection) registerAnonAccts(voterAccounts []*ethereum.SignKeys) erro

wg.Add(1)
go func(i int, acc *ethereum.SignKeys) {
sem <- nil // acquire a token
defer func() { <-sem }() // release the token

defer wg.Done()

pKey := acc.PrivateKey()
if _, _, err := t.createAccount(pKey.String()); err != nil &&
!strings.Contains(err.Error(), "createAccountTx: account already exists") {
Expand Down
60 changes: 60 additions & 0 deletions test/createaccounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test

import (
"encoding/json"
"testing"

qt "github.com/frankban/quicktest"
"github.com/google/uuid"
"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/test/testcommon"
"go.vocdoni.io/dvote/test/testcommon/testutil"
)

func BenchmarkAPICreateNAccounts(b *testing.B) {
server := testcommon.APIserver{}
server.Start(b,
api.ChainHandler,
api.CensusHandler,
api.VoteHandler,
api.AccountHandler,
api.ElectionHandler,
api.WalletHandler,
)
token1 := uuid.New()
c := testutil.NewTestHTTPclient(b, server.ListenAddr, &token1)

// Block 1
server.VochainAPP.AdvanceTestBlock()
waitUntilHeight(b, c, 1)

countAccts := func() uint64 {
// get accounts count
resp, code := c.Request("GET", nil, "accounts", "count")
qt.Assert(b, code, qt.Equals, 200, qt.Commentf("response: %s", resp))

countAccts := struct {
Count uint64 `json:"count"`
}{}

err := json.Unmarshal(resp, &countAccts)
qt.Assert(b, err, qt.IsNil)

return countAccts.Count
}

// create a new account
initBalance := uint64(80)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = createAccount(b, c, server, initBalance)
}
// Block 2
server.VochainAPP.AdvanceTestBlock()
waitUntilHeight(b, c, 2)

if count := countAccts(); count < uint64(b.N) {
qt.Assert(b, count, qt.Equals, b.N)
}
}

0 comments on commit 271b3ce

Please sign in to comment.