Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
release: prepare for v0.0.8 (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Oct 28, 2022
1 parent 707a377 commit 5dbdd92
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 133 deletions.
10 changes: 7 additions & 3 deletions circuit/block_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ func VerifyBlock(
for i := 0; i < gasAssetCount; i++ {
blockGasDeltas[i] = Variable(0)
}
for i := 0; i < types.NbRoots; i++ {
roots[i] = Variable(0)
}

onChainOpsCount = 0
isOnChainOp, pendingPubData, roots, gasDeltas, err := VerifyTransaction(api, block.Txs[0], hFunc, block.CreatedAt, block.GasAssetIds)
isOnChainOp, pendingPubData, roots, gasDeltas, err := VerifyTransaction(api, block.Txs[0], hFunc, block.CreatedAt, block.GasAssetIds, roots)
if err != nil {
log.Println("unable to verify transaction, err:", err)
return err
Expand All @@ -105,7 +108,7 @@ func VerifyBlock(
for i := 1; i < block.TxsCount; i++ {
api.AssertIsEqual(block.Txs[i-1].StateRootAfter, block.Txs[i].StateRootBefore)
hFunc.Reset()
isOnChainOp, pendingPubData, roots, gasDeltas, err = VerifyTransaction(api, block.Txs[i], hFunc, block.CreatedAt, block.GasAssetIds)
isOnChainOp, pendingPubData, roots, gasDeltas, err = VerifyTransaction(api, block.Txs[i], hFunc, block.CreatedAt, block.GasAssetIds, roots)
if err != nil {
log.Println("unable to verify transaction, err:", err)
return err
Expand Down Expand Up @@ -138,7 +141,8 @@ func VerifyBlock(
atomicMatchTx := api.IsZero(api.Sub(block.Txs[i].TxType, types.TxTypeAtomicMatch))
withdrawNftTx := api.IsZero(api.Sub(block.Txs[i].TxType, types.TxTypeWithdrawNft))
transferNft := api.IsZero(api.Sub(block.Txs[i].TxType, types.TxTypeTransferNft))
needGas = api.Or(api.Or(api.Or(api.Or(api.Or(api.Or(api.Or(transferTx, withdrawTx), createCollectionTx), mintNftTx), cancelOfferTx), atomicMatchTx), withdrawNftTx), transferNft)
txNeedGas := api.Or(api.Or(api.Or(api.Or(api.Or(api.Or(api.Or(transferTx, withdrawTx), createCollectionTx), mintNftTx), cancelOfferTx), atomicMatchTx), withdrawNftTx), transferNft)
needGas = api.Or(needGas, txNeedGas)
}

types.IsVariableEqual(api, needGas, block.Gas.AccountInfoBefore.AccountIndex, block.GasAccountIndex)
Expand Down
9 changes: 6 additions & 3 deletions circuit/tx_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (circuit TxConstraints) Define(api API) error {
return err
}

_, _, _, _, err = VerifyTransaction(api, circuit, hFunc, 1633400952228, []int64{0})
_, _, _, _, err = VerifyTransaction(api, circuit, hFunc, 1633400952228, []int64{0}, [types.NbRoots]Variable{Variable(0), Variable(0)})
if err != nil {
return err
}
Expand All @@ -89,6 +89,7 @@ func VerifyTransaction(
hFunc MiMC,
blockCreatedAt Variable,
gasAssetIds []int64,
oldRoots [types.NbRoots]Variable,
) (isOnChainOp Variable, pubData [types.PubDataSizePerTx]Variable, roots [types.NbRoots]Variable,
gasDeltas [NbGasAssetsPerTx]GasDeltaConstraints, err error) {
// compute tx type
Expand Down Expand Up @@ -380,6 +381,7 @@ func VerifyTransaction(
hFunc.Reset()
// update merkle proof
newAccountRoot = types.UpdateMerkleProof(api, hFunc, accountNodeHash, tx.MerkleProofsAccountBefore[i][:], accountIndexMerkleHelper)
oldRoots[0] = api.Select(isEmptyTx, oldRoots[0], newAccountRoot)
}

//// nft tree
Expand Down Expand Up @@ -422,6 +424,7 @@ func VerifyTransaction(
hFunc.Reset()
// update merkle proof
newNftRoot = types.UpdateMerkleProof(api, hFunc, nftNodeHash, tx.MerkleProofsNftBefore[:], nftIndexMerkleHelper)
oldRoots[1] = api.Select(isEmptyTx, oldRoots[1], newNftRoot)

// check state root
hFunc.Reset()
Expand All @@ -432,8 +435,8 @@ func VerifyTransaction(
newStateRoot := hFunc.Sum()
types.IsVariableEqual(api, notEmptyTx, newStateRoot, tx.StateRootAfter)

roots[0] = newAccountRoot
roots[1] = newNftRoot
roots[0] = oldRoots[0]
roots[1] = oldRoots[1]
return isOnChainOp, pubData, roots, gasDeltas, nil
}

Expand Down
2 changes: 1 addition & 1 deletion circuit/types/withdraw_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func VerifyWithdrawNftTx(
nftBefore NftConstraints,
) (pubData [PubDataSizePerTx]Variable) {
fromAccount := 0
creatorAccount := 0
creatorAccount := 1
pubData = CollectPubDataFromWithdrawNft(api, *tx)
// verify params
// account index
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/consensys/gnark v0.7.0
github.com/consensys/gnark-crypto v0.7.0
github.com/ethereum/go-ethereum v1.10.17
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.1
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
26 changes: 13 additions & 13 deletions wasm/txtypes/atomic_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package txtypes
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"hash"
"log"
"math/big"

"github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
"github.com/consensys/gnark-crypto/ecc/bn254/twistededwards/eddsa"
"github.com/pkg/errors"
)

type AtomicMatchSegmentFormat struct {
Expand All @@ -47,7 +47,7 @@ type AtomicMatchSegmentFormat struct {
}

/*
ConstructMintNftTxInfo: construct mint nft tx, sign txInfo
ConstructMintNftTxInfo: construct mint nft tx, sign txInfo
*/
func ConstructAtomicMatchTxInfo(sk *PrivateKey, segmentStr string) (txInfo *AtomicMatchTxInfo, err error) {
var segmentFormat *AtomicMatchSegmentFormat
Expand Down Expand Up @@ -122,58 +122,58 @@ type AtomicMatchTxInfo struct {
func (txInfo *AtomicMatchTxInfo) Validate() error {
// AccountIndex
if txInfo.AccountIndex < minAccountIndex {
return fmt.Errorf("AccountIndex should not be less than %d", minAccountIndex)
return ErrAccountIndexTooLow
}
if txInfo.AccountIndex > maxAccountIndex {
return fmt.Errorf("AccountIndex should not be larger than %d", maxAccountIndex)
return ErrAccountIndexTooHigh
}

// BuyOffer
if txInfo.BuyOffer == nil {
return fmt.Errorf("BuyOffer should not be nil")
}
if err := txInfo.BuyOffer.Validate(); err != nil {
return fmt.Errorf("BuyOffer is invalid, %s", err.Error())
return errors.Wrap(ErrBuyOfferInvalid, err.Error())
}

// SellOffer
if txInfo.SellOffer == nil {
return fmt.Errorf("SellOffer should not be nil")
}
if err := txInfo.SellOffer.Validate(); err != nil {
return fmt.Errorf("SellOffer is invalid, %s", err.Error())
return errors.Wrap(ErrSellOfferInvalid, err.Error())
}

// GasAccountIndex
if txInfo.GasAccountIndex < minAccountIndex {
return fmt.Errorf("GasAccountIndex should not be less than %d", minAccountIndex)
return ErrGasAccountIndexTooLow
}
if txInfo.GasAccountIndex > maxAccountIndex {
return fmt.Errorf("GasAccountIndex should not be larger than %d", maxAccountIndex)
return ErrGasAccountIndexTooHigh
}

// GasFeeAssetId
if txInfo.GasFeeAssetId < minAssetId {
return fmt.Errorf("GasFeeAssetId should not be less than %d", minAssetId)
return ErrGasFeeAssetIdTooLow
}
if txInfo.GasFeeAssetId > maxAssetId {
return fmt.Errorf("GasFeeAssetId should not be larger than %d", maxAssetId)
return ErrGasFeeAssetIdTooHigh
}

// GasFeeAssetAmount
if txInfo.GasFeeAssetAmount == nil {
return fmt.Errorf("GasFeeAssetAmount should not be nil")
}
if txInfo.GasFeeAssetAmount.Cmp(minPackedFeeAmount) < 0 {
return fmt.Errorf("GasFeeAssetAmount should not be less than %s", minPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooLow
}
if txInfo.GasFeeAssetAmount.Cmp(maxPackedFeeAmount) > 0 {
return fmt.Errorf("GasFeeAssetAmount should not be larger than %s", maxPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooHigh
}

// Nonce
if txInfo.Nonce < minNonce {
return fmt.Errorf("Nonce should not be less than %d", minNonce)
return ErrNonceTooLow
}

return nil
Expand Down
20 changes: 10 additions & 10 deletions wasm/txtypes/cancel_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,47 +95,47 @@ type CancelOfferTxInfo struct {
func (txInfo *CancelOfferTxInfo) Validate() error {
// AccountIndex
if txInfo.AccountIndex < minAccountIndex {
return fmt.Errorf("AccountIndex should not be less than %d", minAccountIndex)
return ErrGasAccountIndexTooLow
}
if txInfo.AccountIndex > maxAccountIndex {
return fmt.Errorf("AccountIndex should not be larger than %d", maxAccountIndex)
return ErrGasAccountIndexTooHigh
}

// OfferId
if txInfo.OfferId < 0 {
return fmt.Errorf("OfferId should not be less than 0")
return ErrOfferIdTooLow
}

// GasAccountIndex
if txInfo.GasAccountIndex < minAccountIndex {
return fmt.Errorf("GasAccountIndex should not be less than %d", minAccountIndex)
return ErrGasAccountIndexTooLow
}
if txInfo.GasAccountIndex > maxAccountIndex {
return fmt.Errorf("GasAccountIndex should not be larger than %d", maxAccountIndex)
return ErrGasAccountIndexTooHigh
}

// GasFeeAssetId
if txInfo.GasFeeAssetId < minAssetId {
return fmt.Errorf("GasFeeAssetId should not be less than %d", minAssetId)
return ErrGasFeeAssetIdTooLow
}
if txInfo.GasFeeAssetId > maxAssetId {
return fmt.Errorf("GasFeeAssetId should not be larger than %d", maxAssetId)
return ErrGasFeeAssetIdTooHigh
}

// GasFeeAssetAmount
if txInfo.GasFeeAssetAmount == nil {
return fmt.Errorf("GasFeeAssetAmount should not be nil")
}
if txInfo.GasFeeAssetAmount.Cmp(minPackedFeeAmount) < 0 {
return fmt.Errorf("GasFeeAssetAmount should not be less than %s", minPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooLow
}
if txInfo.GasFeeAssetAmount.Cmp(maxPackedFeeAmount) > 0 {
return fmt.Errorf("GasFeeAssetAmount should not be larger than %s", maxPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooHigh
}

// Nonce
if txInfo.Nonce < minNonce {
return fmt.Errorf("Nonce should not be less than %d", minNonce)
return ErrNonceTooLow
}

return nil
Expand Down
26 changes: 13 additions & 13 deletions wasm/txtypes/create_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type CreateCollectionSegmentFormat struct {
}

/*
ConstructCreateCollectionTxInfo: construct mint nft tx, sign txInfo
ConstructCreateCollectionTxInfo: construct mint nft tx, sign txInfo
*/
func ConstructCreateCollectionTxInfo(sk *PrivateKey, segmentStr string) (txInfo *CreateCollectionTxInfo, err error) {
var segmentFormat *CreateCollectionSegmentFormat
Expand Down Expand Up @@ -102,55 +102,55 @@ type CreateCollectionTxInfo struct {
func (txInfo *CreateCollectionTxInfo) Validate() error {
// AccountIndex
if txInfo.AccountIndex < minAccountIndex {
return fmt.Errorf("AccountIndex should not be less than %d", minAccountIndex)
return ErrAccountIndexTooLow
}
if txInfo.AccountIndex > maxAccountIndex {
return fmt.Errorf("AccountIndex should not be larger than %d", maxAccountIndex)
return ErrAccountIndexTooHigh
}

// Name
if len(txInfo.Name) < minCollectionNameLength {
return fmt.Errorf("length of Name should not be less than %d", minCollectionNameLength)
return ErrCollectionNameTooShort
}
if len(txInfo.Name) > maxCollectionNameLength {
return fmt.Errorf("length of Name should not be larger than %d", maxCollectionNameLength)
return ErrCollectionNameTooLong
}

// Introduction
if len(txInfo.Introduction) > maxCollectionIntroductionLength {
return fmt.Errorf("length of Introduction should not be larger than %d", maxCollectionIntroductionLength)
return ErrIntroductionTooLong
}

// GasAccountIndex
if txInfo.GasAccountIndex < minAccountIndex {
return fmt.Errorf("GasAccountIndex should not be less than %d", minAccountIndex)
return ErrGasAccountIndexTooLow
}
if txInfo.GasAccountIndex > maxAccountIndex {
return fmt.Errorf("GasAccountIndex should not be larger than %d", maxAccountIndex)
return ErrGasAccountIndexTooHigh
}

// GasFeeAssetId
if txInfo.GasFeeAssetId < minAssetId {
return fmt.Errorf("GasFeeAssetId should not be less than %d", minAssetId)
return ErrGasFeeAssetIdTooLow
}
if txInfo.GasFeeAssetId > maxAssetId {
return fmt.Errorf("GasFeeAssetId should not be larger than %d", maxAssetId)
return ErrGasFeeAssetIdTooHigh
}

// GasFeeAssetAmount
if txInfo.GasFeeAssetAmount == nil {
return fmt.Errorf("GasFeeAssetAmount should not be nil")
}
if txInfo.GasFeeAssetAmount.Cmp(minPackedFeeAmount) < 0 {
return fmt.Errorf("GasFeeAssetAmount should not be less than %s", minPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooLow
}
if txInfo.GasFeeAssetAmount.Cmp(maxPackedFeeAmount) > 0 {
return fmt.Errorf("GasFeeAssetAmount should not be larger than %s", maxPackedFeeAmount.String())
return ErrGasFeeAssetAmountTooHigh
}

// Nonce
if txInfo.Nonce < minNonce {
return fmt.Errorf("Nonce should not be less than %d", minNonce)
return ErrNonceTooLow
}

return nil
Expand Down
46 changes: 46 additions & 0 deletions wasm/txtypes/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package txtypes

import "fmt"

var (
ErrAccountIndexTooLow = fmt.Errorf("AccountIndex should not be less than %d", minAccountIndex)
ErrAccountIndexTooHigh = fmt.Errorf("AccountIndex should not be larger than %d", maxAccountIndex)
ErrGasAccountIndexTooLow = fmt.Errorf("GasAccountIndex should not be less than %d", minAccountIndex)
ErrGasAccountIndexTooHigh = fmt.Errorf("GasAccountIndex should not be larger than %d", maxAccountIndex)
ErrGasFeeAssetIdTooLow = fmt.Errorf("GasFeeAssetId should not be less than %d", minAssetId)
ErrGasFeeAssetIdTooHigh = fmt.Errorf("GasFeeAssetId should not be larger than %d", maxAssetId)
ErrGasFeeAssetAmountTooLow = fmt.Errorf("GasFeeAssetAmount should not be less than %s", minPackedFeeAmount.String())
ErrGasFeeAssetAmountTooHigh = fmt.Errorf("GasFeeAssetAmount should not be larger than %s", maxPackedFeeAmount.String())
ErrNonceTooLow = fmt.Errorf("Nonce should not be less than %d", minNonce)
ErrOfferTypeInvalid = fmt.Errorf("Type should only be buy(%d) and sell(%d)", BuyOfferType, SellOfferType)
ErrOfferIdTooLow = fmt.Errorf("OfferId should not be less than 0")
ErrNftIndexTooLow = fmt.Errorf("NftIndex should not be less than %d", minNftIndex)
ErrNftIndexTooHigh = fmt.Errorf("NftIndex should not be larger than %d", maxNftIndex)
ErrAssetIdTooLow = fmt.Errorf("AssetId should not be less than %d", minAssetId)
ErrAssetIdTooHigh = fmt.Errorf("AssetId should not be larger than %d", maxAssetId)
ErrAssetAmountTooLow = fmt.Errorf("AssetAmount should be larger than %s", minAssetAmount.String())
ErrAssetAmountTooHigh = fmt.Errorf("AssetAmount should not be larger than %s", maxAssetAmount.String())
ErrListedAtTooLow = fmt.Errorf("ListedAt should be larger than 0")
ErrTreasuryRateTooLow = fmt.Errorf("TreasuryRate should not be less than %d", minTreasuryRate)
ErrTreasuryRateTooHigh = fmt.Errorf("TreasuryRate should not be larger than %d", maxTreasuryRate)
ErrCollectionNameTooShort = fmt.Errorf("length of Name should not be less than %d", minCollectionNameLength)
ErrCollectionNameTooLong = fmt.Errorf("length of Name should not be larger than %d", maxCollectionNameLength)
ErrIntroductionTooLong = fmt.Errorf("length of Introduction should not be larger than %d", maxCollectionIntroductionLength)
ErrNftContentHashInvalid = fmt.Errorf("NftContentHash is invalid")
ErrNftCollectionIdTooLow = fmt.Errorf("NftCollectionId should not be less than %d", minCollectionId)
ErrNftCollectionIdTooHigh = fmt.Errorf("NftCollectionId should not be larger than %d", maxCollectionId)
ErrCallDataHashInvalid = fmt.Errorf("CallDataHash is invalid")

ErrCreatorAccountIndexTooLow = fmt.Errorf("CreatorAccountIndex should not be less than %d", minAccountIndex)
ErrCreatorAccountIndexTooHigh = fmt.Errorf("CreatorAccountIndex should not be larger than %d", maxAccountIndex)
ErrToAccountIndexTooLow = fmt.Errorf("ToAccountIndex should not be less than %d", minAccountIndex)
ErrToAccountIndexTooHigh = fmt.Errorf("ToAccountIndex should not be larger than %d", maxAccountIndex)
ErrToAccountNameHashInvalid = fmt.Errorf("ToAccountNameHash is invalid")
ErrCreatorTreasuryRateTooLow = fmt.Errorf("CreatorTreasuryRate should not be less than %d", minTreasuryRate)
ErrCreatorTreasuryRateTooHigh = fmt.Errorf("CreatorTreasuryRate should not be larger than %d", maxTreasuryRate)
ErrFromAccountIndexTooLow = fmt.Errorf("FromAccountIndex should not be less than %d", minAccountIndex)
ErrFromAccountIndexTooHigh = fmt.Errorf("FromAccountIndex should not be larger than %d", maxAccountIndex)
ErrToAddressInvalid = fmt.Errorf("ToAddress is invalid")
ErrBuyOfferInvalid = fmt.Errorf("BuyOffer is invalid")
ErrSellOfferInvalid = fmt.Errorf("SellOffer is invalid")
)
Loading

0 comments on commit 5dbdd92

Please sign in to comment.