Move apitypes & stateapiupdater into api dir

This commit is contained in:
arnaucube
2021-03-22 16:51:10 +01:00
parent c2c74e14f1
commit 206c8e6e8f
17 changed files with 14 additions and 14 deletions

264
api/apitypes/apitypes.go Normal file
View File

@@ -0,0 +1,264 @@
package apitypes
import (
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
// BigIntStr is used to scan/value *big.Int directly into strings from/to sql DBs.
// It assumes that *big.Int are inserted/fetched to/from the DB using the BigIntMeddler meddler
// defined at github.com/hermeznetwork/hermez-node/db. Since *big.Int is
// stored as DECIMAL in SQL, there's no need to implement Scan()/Value()
// because DECIMALS are encoded/decoded as strings by the sql driver, and
// BigIntStr is already a string.
type BigIntStr string
// NewBigIntStr creates a *BigIntStr from a *big.Int.
// If the provided bigInt is nil the returned *BigIntStr will also be nil
func NewBigIntStr(bigInt *big.Int) *BigIntStr {
if bigInt == nil {
return nil
}
bigIntStr := BigIntStr(bigInt.String())
return &bigIntStr
}
// StrBigInt is used to unmarshal BigIntStr directly into an alias of big.Int
type StrBigInt big.Int
// UnmarshalText unmarshals a StrBigInt
func (s *StrBigInt) UnmarshalText(text []byte) error {
bi, ok := (*big.Int)(s).SetString(string(text), 10)
if !ok {
return tracerr.Wrap(fmt.Errorf("could not unmarshal %s into a StrBigInt", text))
}
*s = StrBigInt(*bi)
return nil
}
// CollectedFeesAPI is send common.batch.CollectedFee through the API
type CollectedFeesAPI map[common.TokenID]BigIntStr
// NewCollectedFeesAPI creates a new CollectedFeesAPI from a *big.Int map
func NewCollectedFeesAPI(m map[common.TokenID]*big.Int) CollectedFeesAPI {
c := CollectedFeesAPI(make(map[common.TokenID]BigIntStr))
for k, v := range m {
c[k] = *NewBigIntStr(v)
}
return c
}
// HezEthAddr is used to scan/value Ethereum Address directly into strings that follow the Ethereum address hez format (^hez:0x[a-fA-F0-9]{40}$) from/to sql DBs.
// It assumes that Ethereum Address are inserted/fetched to/from the DB using the default Scan/Value interface
type HezEthAddr string
// NewHezEthAddr creates a HezEthAddr from an Ethereum addr
func NewHezEthAddr(addr ethCommon.Address) HezEthAddr {
return HezEthAddr("hez:" + addr.String())
}
// ToEthAddr returns an Ethereum Address created from HezEthAddr
func (a HezEthAddr) ToEthAddr() (ethCommon.Address, error) {
addrStr := strings.TrimPrefix(string(a), "hez:")
var addr ethCommon.Address
return addr, addr.UnmarshalText([]byte(addrStr))
}
// Scan implements Scanner for database/sql
func (a *HezEthAddr) Scan(src interface{}) error {
ethAddr := &ethCommon.Address{}
if err := ethAddr.Scan(src); err != nil {
return tracerr.Wrap(err)
}
if ethAddr == nil {
return nil
}
*a = NewHezEthAddr(*ethAddr)
return nil
}
// Value implements valuer for database/sql
func (a HezEthAddr) Value() (driver.Value, error) {
ethAddr, err := a.ToEthAddr()
if err != nil {
return nil, tracerr.Wrap(err)
}
return ethAddr.Value()
}
// StrHezEthAddr is used to unmarshal HezEthAddr directly into an alias of ethCommon.Address
type StrHezEthAddr ethCommon.Address
// UnmarshalText unmarshals a StrHezEthAddr
func (s *StrHezEthAddr) UnmarshalText(text []byte) error {
withoutHez := strings.TrimPrefix(string(text), "hez:")
var addr ethCommon.Address
if err := addr.UnmarshalText([]byte(withoutHez)); err != nil {
return tracerr.Wrap(err)
}
*s = StrHezEthAddr(addr)
return nil
}
// HezBJJ is used to scan/value *babyjub.PublicKeyComp directly into strings that follow the BJJ public key hez format (^hez:[A-Za-z0-9_-]{44}$) from/to sql DBs.
// It assumes that *babyjub.PublicKeyComp are inserted/fetched to/from the DB using the default Scan/Value interface
type HezBJJ string
// NewHezBJJ creates a HezBJJ from a *babyjub.PublicKeyComp.
// Calling this method with a nil bjj causes panic
func NewHezBJJ(pkComp babyjub.PublicKeyComp) HezBJJ {
sum := pkComp[0]
for i := 1; i < len(pkComp); i++ {
sum += pkComp[i]
}
bjjSum := append(pkComp[:], sum)
return HezBJJ("hez:" + base64.RawURLEncoding.EncodeToString(bjjSum))
}
func hezStrToBJJ(s string) (babyjub.PublicKeyComp, error) {
const decodedLen = 33
const encodedLen = 44
formatErr := errors.New("invalid BJJ format. Must follow this regex: ^hez:[A-Za-z0-9_-]{44}$")
encoded := strings.TrimPrefix(s, "hez:")
if len(encoded) != encodedLen {
return common.EmptyBJJComp, formatErr
}
decoded, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return common.EmptyBJJComp, formatErr
}
if len(decoded) != decodedLen {
return common.EmptyBJJComp, formatErr
}
bjjBytes := [decodedLen - 1]byte{}
copy(bjjBytes[:decodedLen-1], decoded[:decodedLen-1])
sum := bjjBytes[0]
for i := 1; i < len(bjjBytes); i++ {
sum += bjjBytes[i]
}
if decoded[decodedLen-1] != sum {
return common.EmptyBJJComp, tracerr.Wrap(errors.New("checksum verification failed"))
}
bjjComp := babyjub.PublicKeyComp(bjjBytes)
return bjjComp, nil
}
// ToBJJ returns a babyjub.PublicKeyComp created from HezBJJ
func (b HezBJJ) ToBJJ() (babyjub.PublicKeyComp, error) {
return hezStrToBJJ(string(b))
}
// Scan implements Scanner for database/sql
func (b *HezBJJ) Scan(src interface{}) error {
bjj := &babyjub.PublicKeyComp{}
if err := bjj.Scan(src); err != nil {
return tracerr.Wrap(err)
}
if bjj == nil {
return nil
}
*b = NewHezBJJ(*bjj)
return nil
}
// Value implements valuer for database/sql
func (b HezBJJ) Value() (driver.Value, error) {
bjj, err := b.ToBJJ()
if err != nil {
return nil, tracerr.Wrap(err)
}
return bjj.Value()
}
// StrHezBJJ is used to unmarshal HezBJJ directly into an alias of babyjub.PublicKeyComp
type StrHezBJJ babyjub.PublicKeyComp
// UnmarshalText unmarshalls a StrHezBJJ
func (s *StrHezBJJ) UnmarshalText(text []byte) error {
bjj, err := hezStrToBJJ(string(text))
if err != nil {
return tracerr.Wrap(err)
}
*s = StrHezBJJ(bjj)
return nil
}
// HezIdx is used to value common.Idx directly into strings that follow the Idx key hez format (hez:tokenSymbol:idx) to sql DBs.
// Note that this can only be used to insert to DB since there is no way to automatically read from the DB since it needs the tokenSymbol
type HezIdx string
// StrHezIdx is used to unmarshal HezIdx directly into an alias of common.Idx
type StrHezIdx common.Idx
// UnmarshalText unmarshals a StrHezIdx
func (s *StrHezIdx) UnmarshalText(text []byte) error {
withoutHez := strings.TrimPrefix(string(text), "hez:")
splitted := strings.Split(withoutHez, ":")
const expectedLen = 2
if len(splitted) != expectedLen {
return tracerr.Wrap(fmt.Errorf("can not unmarshal %s into StrHezIdx", text))
}
idxInt, err := strconv.Atoi(splitted[1])
if err != nil {
return tracerr.Wrap(err)
}
*s = StrHezIdx(common.Idx(idxInt))
return nil
}
// EthSignature is used to scan/value []byte representing an Ethereum signature directly into strings from/to sql DBs.
type EthSignature string
// NewEthSignature creates a *EthSignature from []byte
// If the provided signature is nil the returned *EthSignature will also be nil
func NewEthSignature(signature []byte) *EthSignature {
if signature == nil {
return nil
}
ethSignature := EthSignature("0x" + hex.EncodeToString(signature))
return &ethSignature
}
// Scan implements Scanner for database/sql
func (e *EthSignature) Scan(src interface{}) error {
if srcStr, ok := src.(string); ok {
// src is a string
*e = *(NewEthSignature([]byte(srcStr)))
return nil
} else if srcBytes, ok := src.([]byte); ok {
// src is []byte
*e = *(NewEthSignature(srcBytes))
return nil
} else {
// unexpected src
return tracerr.Wrap(fmt.Errorf("can't scan %T into apitypes.EthSignature", src))
}
}
// Value implements valuer for database/sql
func (e EthSignature) Value() (driver.Value, error) {
without0x := strings.TrimPrefix(string(e), "0x")
return hex.DecodeString(without0x)
}
// UnmarshalText unmarshals a StrEthSignature
func (e *EthSignature) UnmarshalText(text []byte) error {
without0x := strings.TrimPrefix(string(text), "0x")
signature, err := hex.DecodeString(without0x)
if err != nil {
return tracerr.Wrap(err)
}
*e = EthSignature([]byte(signature))
return nil
}

View File

@@ -0,0 +1,419 @@
package apitypes
import (
"database/sql"
"encoding/json"
"io/ioutil"
"math/big"
"os"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
dbUtils "github.com/hermeznetwork/hermez-node/db"
"github.com/iden3/go-iden3-crypto/babyjub"
// nolint sqlite driver
_ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
"github.com/stretchr/testify/assert"
)
var db *sql.DB
func TestMain(m *testing.M) {
// Register meddler
meddler.Default = meddler.SQLite
meddler.Register("bigint", dbUtils.BigIntMeddler{})
meddler.Register("bigintnull", dbUtils.BigIntNullMeddler{})
// Create temporary sqlite DB
dir, err := ioutil.TempDir("", "db")
if err != nil {
panic(err)
}
db, err = sql.Open("sqlite3", dir+"sqlite.db")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir) //nolint
schema := `CREATE TABLE test (i BLOB);`
if _, err := db.Exec(schema); err != nil {
panic(err)
}
// Run tests
result := m.Run()
os.Exit(result)
}
func TestBigIntStrScannerValuer(t *testing.T) {
// Clean DB
_, err := db.Exec("delete from test")
assert.NoError(t, err)
// Example structs
type bigInMeddlerStruct struct {
I *big.Int `meddler:"i,bigint"` // note the bigint that instructs meddler to use BigIntMeddler
}
type bigIntStrStruct struct {
I BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
}
type bigInMeddlerStructNil struct {
I *big.Int `meddler:"i,bigintnull"` // note the bigint that instructs meddler to use BigIntNullMeddler
}
type bigIntStrStructNil struct {
I *BigIntStr `meddler:"i"` // note that no meddler is specified, and Scan/Value will be used
}
// Not nil case
// Insert into DB using meddler
const x = int64(12345)
fromMeddler := bigInMeddlerStruct{
I: big.NewInt(x),
}
err = meddler.Insert(db, "test", &fromMeddler)
assert.NoError(t, err)
// Read from DB using BigIntStr
toBigIntStr := bigIntStrStruct{}
err = meddler.QueryRow(db, &toBigIntStr, "select * from test")
assert.NoError(t, err)
assert.Equal(t, fromMeddler.I.String(), string(toBigIntStr.I))
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using BigIntStr
fromBigIntStr := bigIntStrStruct{
I: "54321",
}
err = meddler.Insert(db, "test", &fromBigIntStr)
assert.NoError(t, err)
// Read from DB using meddler
toMeddler := bigInMeddlerStruct{}
err = meddler.QueryRow(db, &toMeddler, "select * from test")
assert.NoError(t, err)
assert.Equal(t, string(fromBigIntStr.I), toMeddler.I.String())
// Nil case
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using meddler
fromMeddlerNil := bigInMeddlerStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromMeddlerNil)
assert.NoError(t, err)
// Read from DB using BigIntStr
foo := BigIntStr("foo")
toBigIntStrNil := bigIntStrStructNil{
I: &foo, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toBigIntStrNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toBigIntStrNil.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using BigIntStr
fromBigIntStrNil := bigIntStrStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromBigIntStrNil)
assert.NoError(t, err)
// Read from DB using meddler
toMeddlerNil := bigInMeddlerStructNil{
I: big.NewInt(x), // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toMeddlerNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toMeddlerNil.I)
}
func TestStrBigInt(t *testing.T) {
type testStrBigInt struct {
I StrBigInt
}
from := []byte(`{"I":"4"}`)
to := &testStrBigInt{}
assert.NoError(t, json.Unmarshal(from, to))
assert.Equal(t, big.NewInt(4), (*big.Int)(&to.I))
}
func TestStrHezEthAddr(t *testing.T) {
type testStrHezEthAddr struct {
I StrHezEthAddr
}
withoutHez := "0xaa942cfcd25ad4d90a62358b0dd84f33b398262a"
from := []byte(`{"I":"hez:` + withoutHez + `"}`)
var addr ethCommon.Address
if err := addr.UnmarshalText([]byte(withoutHez)); err != nil {
panic(err)
}
to := &testStrHezEthAddr{}
assert.NoError(t, json.Unmarshal(from, to))
assert.Equal(t, addr, ethCommon.Address(to.I))
}
func TestStrHezBJJ(t *testing.T) {
type testStrHezBJJ struct {
I StrHezBJJ
}
priv := babyjub.NewRandPrivKey()
hezBjj := NewHezBJJ(priv.Public().Compress())
from := []byte(`{"I":"` + hezBjj + `"}`)
to := &testStrHezBJJ{}
assert.NoError(t, json.Unmarshal(from, to))
assert.Equal(t, priv.Public().Compress(), (babyjub.PublicKeyComp)(to.I))
}
func TestStrHezIdx(t *testing.T) {
type testStrHezIdx struct {
I StrHezIdx
}
from := []byte(`{"I":"hez:foo:4"}`)
to := &testStrHezIdx{}
assert.NoError(t, json.Unmarshal(from, to))
assert.Equal(t, common.Idx(4), common.Idx(to.I))
}
func TestHezEthAddr(t *testing.T) {
// Clean DB
_, err := db.Exec("delete from test")
assert.NoError(t, err)
// Example structs
type ethAddrStruct struct {
I ethCommon.Address `meddler:"i"`
}
type hezEthAddrStruct struct {
I HezEthAddr `meddler:"i"`
}
type ethAddrStructNil struct {
I *ethCommon.Address `meddler:"i"`
}
type hezEthAddrStructNil struct {
I *HezEthAddr `meddler:"i"`
}
// Not nil case
// Insert into DB using ethCommon.Address Scan/Value
fromEth := ethAddrStruct{
I: ethCommon.BigToAddress(big.NewInt(73737373)),
}
err = meddler.Insert(db, "test", &fromEth)
assert.NoError(t, err)
// Read from DB using HezEthAddr Scan/Value
toHezEth := hezEthAddrStruct{}
err = meddler.QueryRow(db, &toHezEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, NewHezEthAddr(fromEth.I), toHezEth.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using HezEthAddr Scan/Value
fromHezEth := hezEthAddrStruct{
I: NewHezEthAddr(ethCommon.BigToAddress(big.NewInt(3786872586))),
}
err = meddler.Insert(db, "test", &fromHezEth)
assert.NoError(t, err)
// Read from DB using ethCommon.Address Scan/Value
toEth := ethAddrStruct{}
err = meddler.QueryRow(db, &toEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, fromHezEth.I, NewHezEthAddr(toEth.I))
// Nil case
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using ethCommon.Address Scan/Value
fromEthNil := ethAddrStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromEthNil)
assert.NoError(t, err)
// Read from DB using HezEthAddr Scan/Value
foo := HezEthAddr("foo")
toHezEthNil := hezEthAddrStructNil{
I: &foo, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toHezEthNil.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using HezEthAddr Scan/Value
fromHezEthNil := hezEthAddrStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromHezEthNil)
assert.NoError(t, err)
// Read from DB using ethCommon.Address Scan/Value
fooAddr := ethCommon.BigToAddress(big.NewInt(1))
toEthNil := ethAddrStructNil{
I: &fooAddr, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toEthNil.I)
}
func TestHezBJJ(t *testing.T) {
// Clean DB
_, err := db.Exec("delete from test")
assert.NoError(t, err)
// Example structs
type bjjStruct struct {
I babyjub.PublicKeyComp `meddler:"i"`
}
type hezBJJStruct struct {
I HezBJJ `meddler:"i"`
}
type bjjStructNil struct {
I *babyjub.PublicKeyComp `meddler:"i"`
}
type hezBJJStructNil struct {
I *HezBJJ `meddler:"i"`
}
// Not nil case
// Insert into DB using *babyjub.PublicKeyComp Scan/Value
priv := babyjub.NewRandPrivKey()
fromBJJ := bjjStruct{
I: priv.Public().Compress(),
}
err = meddler.Insert(db, "test", &fromBJJ)
assert.NoError(t, err)
// Read from DB using HezBJJ Scan/Value
toHezBJJ := hezBJJStruct{}
err = meddler.QueryRow(db, &toHezBJJ, "select * from test")
assert.NoError(t, err)
assert.Equal(t, NewHezBJJ(fromBJJ.I), toHezBJJ.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using HezBJJ Scan/Value
fromHezBJJ := hezBJJStruct{
I: NewHezBJJ(priv.Public().Compress()),
}
err = meddler.Insert(db, "test", &fromHezBJJ)
assert.NoError(t, err)
// Read from DB using *babyjub.PublicKeyComp Scan/Value
toBJJ := bjjStruct{}
err = meddler.QueryRow(db, &toBJJ, "select * from test")
assert.NoError(t, err)
assert.Equal(t, fromHezBJJ.I, NewHezBJJ(toBJJ.I))
// Nil case
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using *babyjub.PublicKeyComp Scan/Value
fromBJJNil := bjjStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromBJJNil)
assert.NoError(t, err)
// Read from DB using HezBJJ Scan/Value
foo := HezBJJ("foo")
toHezBJJNil := hezBJJStructNil{
I: &foo, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toHezBJJNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toHezBJJNil.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using HezBJJ Scan/Value
fromHezBJJNil := hezBJJStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromHezBJJNil)
assert.NoError(t, err)
// Read from DB using *babyjub.PublicKeyComp Scan/Value
bjjComp := priv.Public().Compress()
toBJJNil := bjjStructNil{
I: &bjjComp, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toBJJNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toBJJNil.I)
}
func TestEthSignature(t *testing.T) {
// Clean DB
_, err := db.Exec("delete from test")
assert.NoError(t, err)
// Example structs
type ethSignStruct struct {
I []byte `meddler:"i"`
}
type hezEthSignStruct struct {
I EthSignature `meddler:"i"`
}
type hezEthSignStructNil struct {
I *EthSignature `meddler:"i"`
}
// Not nil case
// Insert into DB using []byte Scan/Value
s := "someRandomFooForYou"
fromEth := ethSignStruct{
I: []byte(s),
}
err = meddler.Insert(db, "test", &fromEth)
assert.NoError(t, err)
// Read from DB using EthSignature Scan/Value
toHezEth := hezEthSignStruct{}
err = meddler.QueryRow(db, &toHezEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, NewEthSignature(fromEth.I), &toHezEth.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using EthSignature Scan/Value
fromHezEth := hezEthSignStruct{
I: *NewEthSignature([]byte(s)),
}
err = meddler.Insert(db, "test", &fromHezEth)
assert.NoError(t, err)
// Read from DB using []byte Scan/Value
toEth := ethSignStruct{}
err = meddler.QueryRow(db, &toEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, &fromHezEth.I, NewEthSignature(toEth.I))
// Nil case
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using []byte Scan/Value
fromEthNil := ethSignStruct{
I: nil,
}
err = meddler.Insert(db, "test", &fromEthNil)
assert.NoError(t, err)
// Read from DB using EthSignature Scan/Value
foo := EthSignature("foo")
toHezEthNil := hezEthSignStructNil{
I: &foo, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toHezEthNil.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using EthSignature Scan/Value
fromHezEthNil := hezEthSignStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromHezEthNil)
assert.NoError(t, err)
// Read from DB using []byte Scan/Value
toEthNil := ethSignStruct{
I: []byte(s), // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toEthNil.I)
}