Browse Source

Fix PoolL2Tx.RqTxCompressedData

feature/sql-semaphore1
arnaucube 3 years ago
parent
commit
d7a1ba19b9
4 changed files with 40 additions and 34 deletions
  1. +27
    -21
      common/pooll2tx.go
  2. +10
    -10
      common/pooll2tx_test.go
  3. +2
    -2
      db/l2db/views.go
  4. +1
    -1
      test/l2db.go

+ 27
- 21
common/pooll2tx.go

@ -38,7 +38,7 @@ type PoolL2Tx struct {
RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
RqNonce Nonce `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
AbsoluteFee float64 `meddler:"fee_usd,zeroisnull"`
AbsoluteFeeUpdate time.Time `meddler:"usd_update,utctimez"`
Type TxType `meddler:"tx_type"`
@ -108,7 +108,7 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
}
var b [31]byte
toBJJSign := byte(0)
if babyjub.PointCoordSign(tx.ToBJJ.X) {
if tx.ToBJJ != nil && babyjub.PointCoordSign(tx.ToBJJ.X) {
toBJJSign = byte(1)
}
b[0] = toBJJSign
@ -137,40 +137,43 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
return bi, nil
}
// TxCompressedDataV2 spec:
// [ 1 bits ] toBJJSign // 1 byte
// [ 8 bits ] userFee // 1 byte
// [ 40 bits ] nonce // 5 bytes
// [ 32 bits ] tokenID // 4 bytes
// [ 16 bits ] amountFloat16 // 2 bytes
// [ 48 bits ] toIdx // 6 bytes
// [ 48 bits ] fromIdx // 6 bytes
// RqTxCompressedDataV2 spec:
// [ 1 bits ] rqToBJJSign // 1 byte
// [ 8 bits ] rqUserFee // 1 byte
// [ 40 bits ] rqNonce // 5 bytes
// [ 32 bits ] rqTokenID // 4 bytes
// [ 16 bits ] rqAmountFloat16 // 2 bytes
// [ 48 bits ] rqToIdx // 6 bytes
// [ 48 bits ] rqFromIdx // 6 bytes
// Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
amountFloat16, err := NewFloat16(tx.Amount)
func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
if tx.RqAmount == nil {
tx.RqAmount = big.NewInt(0)
}
amountFloat16, err := NewFloat16(tx.RqAmount)
if err != nil {
return nil, err
}
var b [25]byte
toBJJSign := byte(0)
if babyjub.PointCoordSign(tx.ToBJJ.X) {
if tx.RqToBJJ != nil && babyjub.PointCoordSign(tx.RqToBJJ.X) {
toBJJSign = byte(1)
}
b[0] = toBJJSign
b[1] = byte(tx.Fee)
nonceBytes, err := tx.Nonce.Bytes()
b[1] = byte(tx.RqFee)
nonceBytes, err := tx.RqNonce.Bytes()
if err != nil {
return nil, err
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.TokenID.Bytes())
copy(b[7:11], tx.RqTokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
toIdxBytes, err := tx.ToIdx.Bytes()
toIdxBytes, err := tx.RqToIdx.Bytes()
if err != nil {
return nil, err
}
copy(b[13:19], toIdxBytes[:])
fromIdxBytes, err := tx.FromIdx.Bytes()
fromIdxBytes, err := tx.RqFromIdx.Bytes()
if err != nil {
return nil, err
}
@ -188,8 +191,11 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
}
toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
rqToEthAddr := EthAddrToBigInt(tx.RqToEthAddr)
toBJJAy := tx.ToBJJ.Y
rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
toBJJY := big.NewInt(0)
if tx.ToBJJ != nil {
toBJJY = tx.ToBJJ.Y
}
rqTxCompressedDataV2, err := tx.RqTxCompressedDataV2()
if err != nil {
return nil, err
}
@ -198,7 +204,7 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
rqToBJJY = tx.RqToBJJ.Y
}
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, rqToEthAddr, rqToBJJY})
return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJY, rqTxCompressedDataV2, rqToEthAddr, rqToBJJY})
}
// VerifySignature returns true if the signature verification is correct for the given PublicKey

+ 10
- 10
common/pooll2tx_test.go

@ -45,15 +45,15 @@ func TestTxCompressedData(t *testing.T) {
assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
tx = PoolL2Tx{
FromIdx: 7,
ToIdx: 8,
Amount: big.NewInt(9),
TokenID: 10,
Nonce: 11,
Fee: 12,
ToBJJ: sk.Public(),
RqFromIdx: 7,
RqToIdx: 8,
RqAmount: big.NewInt(9),
RqTokenID: 10,
RqNonce: 11,
RqFee: 12,
RqToBJJ: sk.Public(),
}
txCompressedData, err = tx.TxCompressedDataV2()
txCompressedData, err = tx.RqTxCompressedDataV2()
assert.Nil(t, err)
// test vector value generated from javascript implementation
expectedStr = "6571340879233176732837827812956721483162819083004853354503"
@ -80,7 +80,7 @@ func TestHashToSign(t *testing.T) {
}
toSign, err := tx.HashToSign()
assert.Nil(t, err)
assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
}
func TestVerifyTxSignature(t *testing.T) {
@ -99,7 +99,7 @@ func TestVerifyTxSignature(t *testing.T) {
}
toSign, err := tx.HashToSign()
assert.Nil(t, err)
assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
sig := sk.SignPoseidon(toSign)
tx.Signature = sig

+ 2
- 2
db/l2db/views.go

@ -30,7 +30,7 @@ type PoolL2TxWrite struct {
RqTokenID *common.TokenID `meddler:"rq_token_id"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
RqFee *common.FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"`
RqNonce *common.Nonce `meddler:"rq_nonce"`
Type common.TxType `meddler:"tx_type"`
}
@ -53,7 +53,7 @@ type PoolL2TxRead struct {
RqTokenID *common.TokenID `meddler:"rq_token_id"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
RqFee *common.FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"`
RqNonce *common.Nonce `meddler:"rq_nonce"`
Type common.TxType `meddler:"tx_type"`
// Extra read fileds
BatchNum *common.BatchNum `meddler:"batch_num"`

+ 1
- 1
test/l2db.go

@ -69,7 +69,7 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
tx.RqTokenID = common.TokenID(i)
tx.RqAmount = big.NewInt(int64(i))
tx.RqFee = common.FeeSelector(i)
tx.RqNonce = uint64(i)
tx.RqNonce = common.Nonce(i)
}
txs = append(txs, tx)
}

Loading…
Cancel
Save