Synchronizer main loop & reorg (#82)

* Synchronizer

* mend

Synchronizer main loop & reorg

* mend

Synchronizer main loop & reorg

* mend

Synchronizer main loop & reorg

* Update PR and apply small changes

Update PR and apply small changes:
- Remove arbitrary line jumps (for example after an `err:=` there are cases with extra line before error check, and there are cases without the extra jumpline. Another example is the empty lines between a comment and the line of comment that is explained)
- Fix some typo
- Fix value printing of `lastSavedBlock` instead of `latestBlockNum`
- Uncomment parameters of structs and use linter syntax to avoid unused checks

* Update Synchr after master-pull to last types to compile

Co-authored-by: Toni Ramírez <toni@iden3.com>
Co-authored-by: arnaucube <root@arnaucube.com>
This commit is contained in:
Toni Ramírez
2020-09-06 18:12:52 +02:00
committed by GitHub
parent 4d02308057
commit 9db9508b44
5 changed files with 353 additions and 1 deletions

View File

@@ -0,0 +1,72 @@
package synchronizer
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/db/statedb"
"github.com/hermeznetwork/hermez-node/eth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test(t *testing.T) {
// Int State DB
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := statedb.NewStateDB(dir, true, 32)
assert.Nil(t, err)
// Init History DB
pass := os.Getenv("POSTGRES_PASS")
historyDB, err := historydb.NewHistoryDB(5432, "localhost", "hermez", pass, "history")
require.Nil(t, err)
err = historyDB.Reorg(0)
assert.Nil(t, err)
// Init eth client
ehtClientDialURL := os.Getenv("ETHCLIENT_DIAL_URL")
ethClient, err := ethclient.Dial(ehtClientDialURL)
require.Nil(t, err)
client := eth.NewClient(ethClient, nil, nil, nil)
// Create Synchronizer
s := NewSynchronizer(client, historyDB, sdb)
// Test Sync
err = s.Sync()
require.Nil(t, err)
// TODO: Reorg will be properly tested once we have the mock ethClient implemented
/*
// Force a Reorg
lastSavedBlock, err := historyDB.GetLastBlock()
require.Nil(t, err)
lastSavedBlock.EthBlockNum++
err = historyDB.AddBlock(lastSavedBlock)
require.Nil(t, err)
lastSavedBlock.EthBlockNum++
err = historyDB.AddBlock(lastSavedBlock)
require.Nil(t, err)
log.Debugf("Wait for the blockchain to generate some blocks...")
time.Sleep(40 * time.Second)
err = s.Sync()
require.Nil(t, err)
*/
// Close History DB
if err := historyDB.Close(); err != nil {
fmt.Println("Error closing the history DB:", err)
}
}