diff --git a/common/account.go b/common/account.go index 193a4ba..6477aa5 100644 --- a/common/account.go +++ b/common/account.go @@ -13,8 +13,8 @@ import ( ) const ( - // NLEAFELEMS is the number of elements for a leaf - NLEAFELEMS = 4 + // NLeafElems is the number of elements for a leaf + NLeafElems = 4 // maxNonceValue is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1) maxNonceValue = 0xffffffffff // maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int @@ -41,8 +41,8 @@ func (a *Account) String() string { } // Bytes returns the bytes representing the Account, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array. -func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) { - var b [32 * NLEAFELEMS]byte +func (a *Account) Bytes() ([32 * NLeafElems]byte, error) { + var b [32 * NLeafElems]byte if a.Nonce > maxNonceValue { return b, fmt.Errorf("%s Nonce", ErrNumOverflow) @@ -69,8 +69,8 @@ func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) { } // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field -func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) { - e := [NLEAFELEMS]*big.Int{} +func (a *Account) BigInts() ([NLeafElems]*big.Int, error) { + e := [NLeafElems]*big.Int{} b, err := a.Bytes() if err != nil { @@ -100,11 +100,11 @@ func (a *Account) HashValue() (*big.Int, error) { } // AccountFromBigInts returns a Account from a [5]*big.Int -func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) { +func AccountFromBigInts(e [NLeafElems]*big.Int) (*Account, error) { if !cryptoUtils.CheckBigIntArrayInField(e[:]) { return nil, ErrNotInFF } - var b [32 * NLEAFELEMS]byte + var b [32 * NLeafElems]byte copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian copy(b[32:64], SwapEndianness(e[1].Bytes())) copy(b[64:96], SwapEndianness(e[2].Bytes())) @@ -114,7 +114,7 @@ func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) { } // AccountFromBytes returns a Account from a byte array -func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) { +func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) { tokenID := binary.LittleEndian.Uint32(b[0:4]) var nonceBytes5 [5]byte copy(nonceBytes5[:], b[4:9]) diff --git a/common/account_test.go b/common/account_test.go index a6261bd..d90db51 100644 --- a/common/account_test.go +++ b/common/account_test.go @@ -114,20 +114,20 @@ func TestAccountErrNotInFF(t *testing.T) { // Q-1 should not give error r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1)) - e := [NLEAFELEMS]*big.Int{z, z, r, r} + e := [NLeafElems]*big.Int{z, z, r, r} _, err := AccountFromBigInts(e) assert.Nil(t, err) // Q should give error r = cryptoConstants.Q - e = [NLEAFELEMS]*big.Int{z, z, r, r} + e = [NLeafElems]*big.Int{z, z, r, r} _, err = AccountFromBigInts(e) assert.NotNil(t, err) assert.Equal(t, ErrNotInFF, err) // Q+1 should give error r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1)) - e = [NLEAFELEMS]*big.Int{z, z, r, r} + e = [NLeafElems]*big.Int{z, z, r, r} _, err = AccountFromBigInts(e) assert.NotNil(t, err) assert.Equal(t, ErrNotInFF, err) diff --git a/common/fee.go b/common/fee.go index 4319636..d2e47bf 100644 --- a/common/fee.go +++ b/common/fee.go @@ -16,9 +16,9 @@ type RecommendedFee struct { // FeeSelector is used to select a percentage from the FeePlan. type FeeSelector uint8 -// MAXFEEPLAN is the maximum value of the FeePlan -const MAXFEEPLAN = 256 +// MaxFeePlan is the maximum value of the FeePlan +const MaxFeePlan = 256 // FeePlan represents the fee model, a position in the array indicates the // percentage of tokens paid in concept of fee for a transaction -var FeePlan = [MAXFEEPLAN]float64{} +var FeePlan = [MaxFeePlan]float64{} diff --git a/db/statedb/statedb.go b/db/statedb/statedb.go index 1cfb27a..3d23681 100644 --- a/db/statedb/statedb.go +++ b/db/statedb/statedb.go @@ -21,19 +21,19 @@ var ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a // already exists var ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists") -// KEYCURRENTBATCH is used as key in the db to store the current BatchNum -var KEYCURRENTBATCH = []byte("currentbatch") +// KeyCurrentBatch is used as key in the db to store the current BatchNum +var KeyCurrentBatch = []byte("currentbatch") -// PATHSTATEDB defines the subpath of the StateDB -const PATHSTATEDB = "/statedb" +// PathStateDB defines the subpath of the StateDB +const PathStateDB = "/statedb" -// PATHBATCHNUM defines the subpath of the Batch Checkpoint in the subpath of +// PathBatchNum defines the subpath of the Batch Checkpoint in the subpath of // the StateDB -const PATHBATCHNUM = "/BatchNum" +const PathBatchNum = "/BatchNum" -// PATHCURRENT defines the subpath of the current Batch in the subpath of the +// PathCurrent defines the subpath of the current Batch in the subpath of the // StateDB -const PATHCURRENT = "/current" +const PathCurrent = "/current" // StateDB represents the StateDB object type StateDB struct { @@ -50,7 +50,7 @@ type StateDB struct { func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) { var sto *pebble.PebbleStorage var err error - sto, err = pebble.NewPebbleStorage(path+PATHSTATEDB+PATHCURRENT, false) + sto, err = pebble.NewPebbleStorage(path+PathStateDB+PathCurrent, false) if err != nil { return nil, err } @@ -64,7 +64,7 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) { } sdb := &StateDB{ - path: path + PATHSTATEDB, + path: path + PathStateDB, db: sto, mt: mt, } @@ -85,7 +85,7 @@ func (s *StateDB) DB() *pebble.PebbleStorage { // GetCurrentBatch returns the current BatchNum stored in the StateDB func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) { - cbBytes, err := s.db.Get(KEYCURRENTBATCH) + cbBytes, err := s.db.Get(KeyCurrentBatch) if err == db.ErrNotFound { return 0, nil } @@ -101,7 +101,7 @@ func (s *StateDB) setCurrentBatch() error { if err != nil { return err } - tx.Put(KEYCURRENTBATCH, s.currentBatch.Bytes()) + tx.Put(KeyCurrentBatch, s.currentBatch.Bytes()) if err := tx.Commit(); err != nil { return err } @@ -113,7 +113,7 @@ func (s *StateDB) MakeCheckpoint() error { // advance currentBatch s.currentBatch++ - checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(s.currentBatch)) + checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(s.currentBatch)) err := s.setCurrentBatch() if err != nil { @@ -139,7 +139,7 @@ func (s *StateDB) MakeCheckpoint() error { // DeleteCheckpoint removes if exist the checkpoint of the given batchNum func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error { - checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(batchNum)) + checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum)) if _, err := os.Stat(checkpointPath); os.IsNotExist(err) { return fmt.Errorf("Checkpoint with batchNum %d does not exist in DB", batchNum) @@ -158,8 +158,8 @@ func (s *StateDB) Reset(batchNum common.BatchNum) error { return nil } - checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(batchNum)) - currentPath := s.path + PATHCURRENT + checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum)) + currentPath := s.path + PathCurrent // remove 'current' err := os.RemoveAll(currentPath) @@ -217,7 +217,7 @@ func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) if err != nil { return nil, err } - var b [32 * common.NLEAFELEMS]byte + var b [32 * common.NLeafElems]byte copy(b[:], accBytes) return common.AccountFromBytes(b) } @@ -345,9 +345,9 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er return nil } - synchronizerCheckpointPath := l.synchronizerStateDB.path + PATHBATCHNUM + strconv.Itoa(int(batchNum)) - checkpointPath := l.path + PATHBATCHNUM + strconv.Itoa(int(batchNum)) - currentPath := l.path + PATHCURRENT + synchronizerCheckpointPath := l.synchronizerStateDB.path + PathBatchNum + strconv.Itoa(int(batchNum)) + checkpointPath := l.path + PathBatchNum + strconv.Itoa(int(batchNum)) + currentPath := l.path + PathCurrent if fromSynchronizer { // use checkpoint from SynchronizerStateDB diff --git a/log/log.go b/log/log.go index 0ed8eea..08b4e09 100644 --- a/log/log.go +++ b/log/log.go @@ -2,7 +2,6 @@ package log import ( "fmt" - "os" "time" "go.uber.org/zap" @@ -11,25 +10,28 @@ import ( var log *zap.SugaredLogger -// errorsFile is the file where the errors are being written -var errorsFile *os.File - func init() { // default level: debug Init("debug", "") } // Init the logger with defined level. errorsPath defines the file where to store the errors, if set to "" will not store errors. -func Init(levelStr, errorsPath string) { +func Init(levelStr, logPath string) { var level zap.AtomicLevel err := level.UnmarshalText([]byte(levelStr)) if err != nil { panic(fmt.Errorf("Error on setting log level: %s", err)) } + outputPaths := []string{"stdout"} + if logPath != "" { + log.Infof("log file: %s", logPath) + outputPaths = append(outputPaths, logPath) + } + cfg := zap.Config{ Level: level, Encoding: "console", - OutputPaths: []string{"stdout"}, + OutputPaths: outputPaths, ErrorOutputPaths: []string{"stderr"}, EncoderConfig: zapcore.EncoderConfig{ MessageKey: "message", @@ -60,25 +62,9 @@ func Init(levelStr, errorsPath string) { withOptions := logger.WithOptions(zap.AddCallerSkip(1)) log = withOptions.Sugar() - if errorsPath != "" { - log.Infof("file where errors will be written: %s", errorsPath) - errorsFile, err = os.OpenFile(errorsPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) //nolint:gosec - if err != nil { - panic(err) - } - } - log.Infof("log level: %s", level) } -func writeToErrorsFile(msg string) { - if errorsFile == nil { - return - } - //nolint:errcheck - errorsFile.WriteString(fmt.Sprintf("%s %s\n", time.Now().Format(time.RFC3339), msg)) //nolint:gosec -} - // Debug calls log.Debug func Debug(args ...interface{}) { log.Debug(args...) @@ -97,7 +83,6 @@ func Warn(args ...interface{}) { // Error calls log.Error and stores the error message into the ErrorFile func Error(args ...interface{}) { log.Error(args...) - go writeToErrorsFile(fmt.Sprint(args...)) } // Debugf calls log.Debugf @@ -118,5 +103,24 @@ func Warnf(template string, args ...interface{}) { // Errorf calls log.Errorf and stores the error message into the ErrorFile func Errorf(template string, args ...interface{}) { log.Errorf(template, args...) - go writeToErrorsFile(fmt.Sprintf(template, args...)) +} + +// Debugw calls log.Debugw +func Debugw(template string, kv ...interface{}) { + log.Debugw(template, kv...) +} + +// Infow calls log.Infow +func Infow(template string, kv ...interface{}) { + log.Infow(template, kv...) +} + +// Warnw calls log.Warnw +func Warnw(template string, kv ...interface{}) { + log.Warnw(template, kv...) +} + +// Errorw calls log.Errorw and stores the error message into the ErrorFile +func Errorw(template string, kv ...interface{}) { + log.Errorw(template, kv...) } diff --git a/log/log_test.go b/log/log_test.go index f228d7d..61471ee 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -5,10 +5,15 @@ import ( ) func TestLog(t *testing.T) { - Info("Test log.Infow", "value", 10) + // Init("debug", "test.log") + + Info("Test log.Info", " value is ", 10) Infof("Test log.Infof %d", 10) + Infow("Test log.Infow", "value", 10) Debugf("Test log.Debugf %d", 10) - Error("Test log.Error", "value", 10) + Error("Test log.Error", " value is ", 10) Errorf("Test log.Errorf %d", 10) + Errorw("Test log.Errorw", "value", 10) Warnf("Test log.Warnf %d", 10) + Warnw("Test log.Warnw", "value", 10) } diff --git a/test/lang.go b/test/lang.go index 5ff92bd..90a870a 100644 --- a/test/lang.go +++ b/test/lang.go @@ -320,7 +320,7 @@ func (p *Parser) parseLine() (*Instruction, error) { c.Literal += line return c, err } - if fee > common.MAXFEEPLAN-1 { + if fee > common.MaxFeePlan-1 { line, _ := p.s.r.ReadString('\n') c.Literal += line return c, fmt.Errorf("Fee %d can not be bigger than 255", fee)