You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
11 KiB

  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/tracerr"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-iden3-crypto/poseidon"
  11. )
  12. // PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged
  13. type PoolL2Tx struct {
  14. // Stored in DB: mandatory fileds
  15. // TxID (12 bytes) for L2Tx is:
  16. // bytes: | 1 | 6 | 5 |
  17. // values: | type | FromIdx | Nonce |
  18. TxID TxID `meddler:"tx_id"`
  19. FromIdx Idx `meddler:"from_idx"`
  20. ToIdx Idx `meddler:"to_idx,zeroisnull"`
  21. AuxToIdx Idx `meddler:"-"` // AuxToIdx is only used internally at the StateDB to avoid repeated computation when processing transactions (from Synchronizer, TxSelector, BatchBuilder)
  22. ToEthAddr ethCommon.Address `meddler:"to_eth_addr,zeroisnull"`
  23. ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
  24. TokenID TokenID `meddler:"token_id"`
  25. Amount *big.Int `meddler:"amount,bigint"` // TODO: change to float16
  26. Fee FeeSelector `meddler:"fee"`
  27. Nonce Nonce `meddler:"nonce"` // effective 40 bits used
  28. State PoolL2TxState `meddler:"state"`
  29. Signature babyjub.SignatureComp `meddler:"signature"` // tx signature
  30. Timestamp time.Time `meddler:"timestamp,utctime"` // time when added to the tx pool
  31. // Stored in DB: optional fileds, may be uninitialized
  32. RqFromIdx Idx `meddler:"rq_from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  33. RqToIdx Idx `meddler:"rq_to_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  34. RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr,zeroisnull"`
  35. RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"` // TODO: stop using json, use scanner/valuer
  36. RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
  37. RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
  38. RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
  39. RqNonce Nonce `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
  40. AbsoluteFee float64 `meddler:"fee_usd,zeroisnull"`
  41. AbsoluteFeeUpdate time.Time `meddler:"usd_update,utctimez"`
  42. Type TxType `meddler:"tx_type"`
  43. // Extra metadata, may be uninitialized
  44. RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
  45. }
  46. // NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated
  47. // from the L2Tx values
  48. func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) {
  49. // calculate TxType
  50. var txType TxType
  51. if poolL2Tx.ToIdx >= IdxUserThreshold {
  52. txType = TxTypeTransfer
  53. } else if poolL2Tx.ToIdx == 1 {
  54. txType = TxTypeExit
  55. } else if poolL2Tx.ToIdx == 0 {
  56. if poolL2Tx.ToBJJ != nil && poolL2Tx.ToEthAddr == FFAddr {
  57. txType = TxTypeTransferToBJJ
  58. } else if poolL2Tx.ToEthAddr != FFAddr && poolL2Tx.ToEthAddr != EmptyAddr {
  59. txType = TxTypeTransferToEthAddr
  60. }
  61. } else {
  62. return nil, tracerr.Wrap(errors.New("malformed transaction"))
  63. }
  64. // if TxType!=poolL2Tx.TxType return error
  65. if poolL2Tx.Type != "" && poolL2Tx.Type != txType {
  66. return poolL2Tx, tracerr.Wrap(fmt.Errorf("type: %s, should be: %s", poolL2Tx.Type, txType))
  67. }
  68. poolL2Tx.Type = txType
  69. var txid [TxIDLen]byte
  70. txid[0] = TxIDPrefixL2Tx
  71. fromIdxBytes, err := poolL2Tx.FromIdx.Bytes()
  72. if err != nil {
  73. return poolL2Tx, tracerr.Wrap(err)
  74. }
  75. copy(txid[1:7], fromIdxBytes[:])
  76. nonceBytes, err := poolL2Tx.Nonce.Bytes()
  77. if err != nil {
  78. return poolL2Tx, tracerr.Wrap(err)
  79. }
  80. copy(txid[7:12], nonceBytes[:])
  81. txID := TxID(txid)
  82. // if TxID!=poolL2Tx.TxID return error
  83. if poolL2Tx.TxID != (TxID{}) && poolL2Tx.TxID != txID {
  84. return poolL2Tx, tracerr.Wrap(fmt.Errorf("id: %s, should be: %s", poolL2Tx.TxID.String(), txID.String()))
  85. }
  86. poolL2Tx.TxID = txID
  87. return poolL2Tx, nil
  88. }
  89. // TxCompressedData spec:
  90. // [ 1 bits ] toBJJSign // 1 byte
  91. // [ 8 bits ] userFee // 1 byte
  92. // [ 40 bits ] nonce // 5 bytes
  93. // [ 32 bits ] tokenID // 4 bytes
  94. // [ 16 bits ] amountFloat16 // 2 bytes
  95. // [ 48 bits ] toIdx // 6 bytes
  96. // [ 48 bits ] fromIdx // 6 bytes
  97. // [ 16 bits ] chainId // 2 bytes
  98. // [ 32 bits ] signatureConstant // 4 bytes
  99. // Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
  100. func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
  101. // sigconstant
  102. sc, ok := new(big.Int).SetString("3322668559", 10)
  103. if !ok {
  104. return nil, tracerr.Wrap(fmt.Errorf("error parsing SignatureConstant"))
  105. }
  106. amountFloat16, err := NewFloat16(tx.Amount)
  107. if err != nil {
  108. return nil, tracerr.Wrap(err)
  109. }
  110. var b [31]byte
  111. toBJJSign := byte(0)
  112. if tx.ToBJJ != nil && babyjub.PointCoordSign(tx.ToBJJ.X) {
  113. toBJJSign = byte(1)
  114. }
  115. b[0] = toBJJSign
  116. b[1] = byte(tx.Fee)
  117. nonceBytes, err := tx.Nonce.Bytes()
  118. if err != nil {
  119. return nil, tracerr.Wrap(err)
  120. }
  121. copy(b[2:7], nonceBytes[:])
  122. copy(b[7:11], tx.TokenID.Bytes())
  123. copy(b[11:13], amountFloat16.Bytes())
  124. toIdxBytes, err := tx.ToIdx.Bytes()
  125. if err != nil {
  126. return nil, tracerr.Wrap(err)
  127. }
  128. copy(b[13:19], toIdxBytes[:])
  129. fromIdxBytes, err := tx.FromIdx.Bytes()
  130. if err != nil {
  131. return nil, tracerr.Wrap(err)
  132. }
  133. copy(b[19:25], fromIdxBytes[:])
  134. copy(b[25:27], []byte{0, 1}) // TODO this will be generated by the ChainID config parameter
  135. copy(b[27:31], sc.Bytes())
  136. bi := new(big.Int).SetBytes(b[:])
  137. return bi, nil
  138. }
  139. // TxCompressedDataV2 spec:
  140. // [ 1 bits ] toBJJSign // 1 byte
  141. // [ 8 bits ] userFee // 1 byte
  142. // [ 40 bits ] nonce // 5 bytes
  143. // [ 32 bits ] tokenID // 4 bytes
  144. // [ 16 bits ] amountFloat16 // 2 bytes
  145. // [ 48 bits ] toIdx // 6 bytes
  146. // [ 48 bits ] fromIdx // 6 bytes
  147. // Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
  148. func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
  149. if tx.Amount == nil {
  150. tx.Amount = big.NewInt(0)
  151. }
  152. amountFloat16, err := NewFloat16(tx.Amount)
  153. if err != nil {
  154. return nil, tracerr.Wrap(err)
  155. }
  156. var b [25]byte
  157. toBJJSign := byte(0)
  158. if tx.ToBJJ != nil && babyjub.PointCoordSign(tx.ToBJJ.X) {
  159. toBJJSign = byte(1)
  160. }
  161. b[0] = toBJJSign
  162. b[1] = byte(tx.Fee)
  163. nonceBytes, err := tx.Nonce.Bytes()
  164. if err != nil {
  165. return nil, tracerr.Wrap(err)
  166. }
  167. copy(b[2:7], nonceBytes[:])
  168. copy(b[7:11], tx.TokenID.Bytes())
  169. copy(b[11:13], amountFloat16.Bytes())
  170. toIdxBytes, err := tx.ToIdx.Bytes()
  171. if err != nil {
  172. return nil, tracerr.Wrap(err)
  173. }
  174. copy(b[13:19], toIdxBytes[:])
  175. fromIdxBytes, err := tx.FromIdx.Bytes()
  176. if err != nil {
  177. return nil, tracerr.Wrap(err)
  178. }
  179. copy(b[19:25], fromIdxBytes[:])
  180. bi := new(big.Int).SetBytes(b[:])
  181. return bi, nil
  182. }
  183. // RqTxCompressedDataV2 is like the TxCompressedDataV2 but using the 'Rq'
  184. // parameters. In a future iteration of the hermez-node, the 'Rq' parameters
  185. // can be inside a struct, which contains the 'Rq' transaction grouped inside,
  186. // so then computing the 'RqTxCompressedDataV2' would be just calling
  187. // 'tx.Rq.TxCompressedDataV2()'.
  188. // RqTxCompressedDataV2 spec:
  189. // [ 1 bits ] rqToBJJSign // 1 byte
  190. // [ 8 bits ] rqUserFee // 1 byte
  191. // [ 40 bits ] rqNonce // 5 bytes
  192. // [ 32 bits ] rqTokenID // 4 bytes
  193. // [ 16 bits ] rqAmountFloat16 // 2 bytes
  194. // [ 48 bits ] rqToIdx // 6 bytes
  195. // [ 48 bits ] rqFromIdx // 6 bytes
  196. // Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
  197. func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
  198. if tx.RqAmount == nil {
  199. tx.RqAmount = big.NewInt(0)
  200. }
  201. amountFloat16, err := NewFloat16(tx.RqAmount)
  202. if err != nil {
  203. return nil, tracerr.Wrap(err)
  204. }
  205. var b [25]byte
  206. toBJJSign := byte(0)
  207. if tx.RqToBJJ != nil && babyjub.PointCoordSign(tx.RqToBJJ.X) {
  208. toBJJSign = byte(1)
  209. }
  210. b[0] = toBJJSign
  211. b[1] = byte(tx.RqFee)
  212. nonceBytes, err := tx.RqNonce.Bytes()
  213. if err != nil {
  214. return nil, tracerr.Wrap(err)
  215. }
  216. copy(b[2:7], nonceBytes[:])
  217. copy(b[7:11], tx.RqTokenID.Bytes())
  218. copy(b[11:13], amountFloat16.Bytes())
  219. toIdxBytes, err := tx.RqToIdx.Bytes()
  220. if err != nil {
  221. return nil, tracerr.Wrap(err)
  222. }
  223. copy(b[13:19], toIdxBytes[:])
  224. fromIdxBytes, err := tx.RqFromIdx.Bytes()
  225. if err != nil {
  226. return nil, tracerr.Wrap(err)
  227. }
  228. copy(b[19:25], fromIdxBytes[:])
  229. bi := new(big.Int).SetBytes(b[:])
  230. return bi, nil
  231. }
  232. // HashToSign returns the computed Poseidon hash from the *PoolL2Tx that will be signed by the sender.
  233. func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
  234. toCompressedData, err := tx.TxCompressedData()
  235. if err != nil {
  236. return nil, tracerr.Wrap(err)
  237. }
  238. toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
  239. rqToEthAddr := EthAddrToBigInt(tx.RqToEthAddr)
  240. toBJJY := big.NewInt(0)
  241. if tx.ToBJJ != nil {
  242. toBJJY = tx.ToBJJ.Y
  243. }
  244. rqTxCompressedDataV2, err := tx.RqTxCompressedDataV2()
  245. if err != nil {
  246. return nil, tracerr.Wrap(err)
  247. }
  248. rqToBJJY := big.NewInt(0)
  249. if tx.RqToBJJ != nil {
  250. rqToBJJY = tx.RqToBJJ.Y
  251. }
  252. return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJY, rqTxCompressedDataV2, rqToEthAddr, rqToBJJY})
  253. }
  254. // VerifySignature returns true if the signature verification is correct for the given PublicKey
  255. func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
  256. h, err := tx.HashToSign()
  257. if err != nil {
  258. return false
  259. }
  260. s, err := tx.Signature.Decompress()
  261. if err != nil {
  262. return false
  263. }
  264. return pk.VerifyPoseidon(h, s)
  265. }
  266. // L2Tx returns a *L2Tx from the PoolL2Tx
  267. func (tx PoolL2Tx) L2Tx() L2Tx {
  268. return L2Tx{
  269. TxID: tx.TxID,
  270. FromIdx: tx.FromIdx,
  271. ToIdx: tx.ToIdx,
  272. Amount: tx.Amount,
  273. Fee: tx.Fee,
  274. Nonce: tx.Nonce,
  275. Type: tx.Type,
  276. }
  277. }
  278. // Tx returns a *Tx from the PoolL2Tx
  279. func (tx PoolL2Tx) Tx() Tx {
  280. return Tx{
  281. TxID: tx.TxID,
  282. FromIdx: tx.FromIdx,
  283. ToIdx: tx.ToIdx,
  284. Amount: tx.Amount,
  285. TokenID: tx.TokenID,
  286. Nonce: &tx.Nonce,
  287. Fee: &tx.Fee,
  288. Type: tx.Type,
  289. }
  290. }
  291. // PoolL2TxsToL2Txs returns an array of []L2Tx from an array of []PoolL2Tx
  292. func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) {
  293. var r []L2Tx
  294. for _, poolTx := range txs {
  295. r = append(r, poolTx.L2Tx())
  296. }
  297. return r, nil
  298. }
  299. // PoolL2TxState is a struct that represents the status of a L2 transaction
  300. type PoolL2TxState string
  301. const (
  302. // PoolL2TxStatePending represents a valid L2Tx that hasn't started the forging process
  303. PoolL2TxStatePending PoolL2TxState = "pend"
  304. // PoolL2TxStateForging represents a valid L2Tx that has started the forging process
  305. PoolL2TxStateForging PoolL2TxState = "fing"
  306. // PoolL2TxStateForged represents a L2Tx that has already been forged
  307. PoolL2TxStateForged PoolL2TxState = "fged"
  308. // PoolL2TxStateInvalid represents a L2Tx that has been invalidated
  309. PoolL2TxStateInvalid PoolL2TxState = "invl"
  310. )