Browse Source

Update log package with fields & file log

Update log package with fields & file log, and update constants in the
repository code to be MixedCase.

resolves #91, resolves #92, resolves #95
feature/sql-semaphore1
arnaucube 4 years ago
parent
commit
fdaf2c9ce2
7 changed files with 71 additions and 62 deletions
  1. +9
    -9
      common/account.go
  2. +3
    -3
      common/account_test.go
  3. +3
    -3
      common/fee.go
  4. +20
    -20
      db/statedb/statedb.go
  5. +28
    -24
      log/log.go
  6. +7
    -2
      log/log_test.go
  7. +1
    -1
      test/lang.go

+ 9
- 9
common/account.go

@ -13,8 +13,8 @@ import (
) )
const ( 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 is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1)
maxNonceValue = 0xffffffffff maxNonceValue = 0xffffffffff
// maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int // 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. // 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 { if a.Nonce > maxNonceValue {
return b, fmt.Errorf("%s Nonce", ErrNumOverflow) 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 // 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() b, err := a.Bytes()
if err != nil { if err != nil {
@ -100,11 +100,11 @@ func (a *Account) HashValue() (*big.Int, error) {
} }
// AccountFromBigInts returns a Account from a [5]*big.Int // 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[:]) { if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
return nil, ErrNotInFF 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[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[32:64], SwapEndianness(e[1].Bytes())) copy(b[32:64], SwapEndianness(e[1].Bytes()))
copy(b[64:96], SwapEndianness(e[2].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 // 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]) tokenID := binary.LittleEndian.Uint32(b[0:4])
var nonceBytes5 [5]byte var nonceBytes5 [5]byte
copy(nonceBytes5[:], b[4:9]) copy(nonceBytes5[:], b[4:9])

+ 3
- 3
common/account_test.go

@ -114,20 +114,20 @@ func TestAccountErrNotInFF(t *testing.T) {
// Q-1 should not give error // Q-1 should not give error
r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1)) 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) _, err := AccountFromBigInts(e)
assert.Nil(t, err) assert.Nil(t, err)
// Q should give error // Q should give error
r = cryptoConstants.Q r = cryptoConstants.Q
e = [NLEAFELEMS]*big.Int{z, z, r, r}
e = [NLeafElems]*big.Int{z, z, r, r}
_, err = AccountFromBigInts(e) _, err = AccountFromBigInts(e)
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err) assert.Equal(t, ErrNotInFF, err)
// Q+1 should give error // Q+1 should give error
r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1)) 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) _, err = AccountFromBigInts(e)
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err) assert.Equal(t, ErrNotInFF, err)

+ 3
- 3
common/fee.go

@ -16,9 +16,9 @@ type RecommendedFee struct {
// FeeSelector is used to select a percentage from the FeePlan. // FeeSelector is used to select a percentage from the FeePlan.
type FeeSelector uint8 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 // FeePlan represents the fee model, a position in the array indicates the
// percentage of tokens paid in concept of fee for a transaction // percentage of tokens paid in concept of fee for a transaction
var FeePlan = [MAXFEEPLAN]float64{}
var FeePlan = [MaxFeePlan]float64{}

+ 20
- 20
db/statedb/statedb.go

@ -21,19 +21,19 @@ var ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a
// already exists // already exists
var ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account 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 // 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 // StateDB
const PATHCURRENT = "/current"
const PathCurrent = "/current"
// StateDB represents the StateDB object // StateDB represents the StateDB object
type StateDB struct { type StateDB struct {
@ -50,7 +50,7 @@ type StateDB struct {
func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) { func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
var sto *pebble.PebbleStorage var sto *pebble.PebbleStorage
var err error var err error
sto, err = pebble.NewPebbleStorage(path+PATHSTATEDB+PATHCURRENT, false)
sto, err = pebble.NewPebbleStorage(path+PathStateDB+PathCurrent, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -64,7 +64,7 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
} }
sdb := &StateDB{ sdb := &StateDB{
path: path + PATHSTATEDB,
path: path + PathStateDB,
db: sto, db: sto,
mt: mt, mt: mt,
} }
@ -85,7 +85,7 @@ func (s *StateDB) DB() *pebble.PebbleStorage {
// GetCurrentBatch returns the current BatchNum stored in the StateDB // GetCurrentBatch returns the current BatchNum stored in the StateDB
func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) { func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
cbBytes, err := s.db.Get(KEYCURRENTBATCH)
cbBytes, err := s.db.Get(KeyCurrentBatch)
if err == db.ErrNotFound { if err == db.ErrNotFound {
return 0, nil return 0, nil
} }
@ -101,7 +101,7 @@ func (s *StateDB) setCurrentBatch() error {
if err != nil { if err != nil {
return err return err
} }
tx.Put(KEYCURRENTBATCH, s.currentBatch.Bytes())
tx.Put(KeyCurrentBatch, s.currentBatch.Bytes())
if err := tx.Commit(); err != nil { if err := tx.Commit(); err != nil {
return err return err
} }
@ -113,7 +113,7 @@ func (s *StateDB) MakeCheckpoint() error {
// advance currentBatch // advance currentBatch
s.currentBatch++ s.currentBatch++
checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(s.currentBatch))
checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(s.currentBatch))
err := s.setCurrentBatch() err := s.setCurrentBatch()
if err != nil { if err != nil {
@ -139,7 +139,7 @@ func (s *StateDB) MakeCheckpoint() error {
// DeleteCheckpoint removes if exist the checkpoint of the given batchNum // DeleteCheckpoint removes if exist the checkpoint of the given batchNum
func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error { 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) { if _, err := os.Stat(checkpointPath); os.IsNotExist(err) {
return fmt.Errorf("Checkpoint with batchNum %d does not exist in DB", batchNum) 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 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' // remove 'current'
err := os.RemoveAll(currentPath) err := os.RemoveAll(currentPath)
@ -217,7 +217,7 @@ func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var b [32 * common.NLEAFELEMS]byte
var b [32 * common.NLeafElems]byte
copy(b[:], accBytes) copy(b[:], accBytes)
return common.AccountFromBytes(b) return common.AccountFromBytes(b)
} }
@ -345,9 +345,9 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
return nil 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 { if fromSynchronizer {
// use checkpoint from SynchronizerStateDB // use checkpoint from SynchronizerStateDB

+ 28
- 24
log/log.go

@ -2,7 +2,6 @@ package log
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
@ -11,25 +10,28 @@ import (
var log *zap.SugaredLogger var log *zap.SugaredLogger
// errorsFile is the file where the errors are being written
var errorsFile *os.File
func init() { func init() {
// default level: debug // default level: debug
Init("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. // 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 var level zap.AtomicLevel
err := level.UnmarshalText([]byte(levelStr)) err := level.UnmarshalText([]byte(levelStr))
if err != nil { if err != nil {
panic(fmt.Errorf("Error on setting log level: %s", err)) 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{ cfg := zap.Config{
Level: level, Level: level,
Encoding: "console", Encoding: "console",
OutputPaths: []string{"stdout"},
OutputPaths: outputPaths,
ErrorOutputPaths: []string{"stderr"}, ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{ EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message", MessageKey: "message",
@ -60,25 +62,9 @@ func Init(levelStr, errorsPath string) {
withOptions := logger.WithOptions(zap.AddCallerSkip(1)) withOptions := logger.WithOptions(zap.AddCallerSkip(1))
log = withOptions.Sugar() 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) 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 // Debug calls log.Debug
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
log.Debug(args...) log.Debug(args...)
@ -97,7 +83,6 @@ func Warn(args ...interface{}) {
// Error calls log.Error and stores the error message into the ErrorFile // Error calls log.Error and stores the error message into the ErrorFile
func Error(args ...interface{}) { func Error(args ...interface{}) {
log.Error(args...) log.Error(args...)
go writeToErrorsFile(fmt.Sprint(args...))
} }
// Debugf calls log.Debugf // 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 // Errorf calls log.Errorf and stores the error message into the ErrorFile
func Errorf(template string, args ...interface{}) { func Errorf(template string, args ...interface{}) {
log.Errorf(template, args...) 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...)
} }

+ 7
- 2
log/log_test.go

@ -5,10 +5,15 @@ import (
) )
func TestLog(t *testing.T) { 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) Infof("Test log.Infof %d", 10)
Infow("Test log.Infow", "value", 10)
Debugf("Test log.Debugf %d", 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) Errorf("Test log.Errorf %d", 10)
Errorw("Test log.Errorw", "value", 10)
Warnf("Test log.Warnf %d", 10) Warnf("Test log.Warnf %d", 10)
Warnw("Test log.Warnw", "value", 10)
} }

+ 1
- 1
test/lang.go

@ -320,7 +320,7 @@ func (p *Parser) parseLine() (*Instruction, error) {
c.Literal += line c.Literal += line
return c, err return c, err
} }
if fee > common.MAXFEEPLAN-1 {
if fee > common.MaxFeePlan-1 {
line, _ := p.s.r.ReadString('\n') line, _ := p.s.r.ReadString('\n')
c.Literal += line c.Literal += line
return c, fmt.Errorf("Fee %d can not be bigger than 255", fee) return c, fmt.Errorf("Fee %d can not be bigger than 255", fee)

Loading…
Cancel
Save