Skip to content

Commit

Permalink
fixed bug where if tickdata and quorumtick data are empty but with le…
Browse files Browse the repository at this point in the history
…n(txs) > 0 tick is not valid
  • Loading branch information
0xluk committed Feb 18, 2024
1 parent cc769f1 commit 9095df1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test-nocache:
strategy:
matrix:
go-version: [1.21.x]
go-version: [1.20.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -21,5 +21,5 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21.x
go-version: 1.20.x
- run: go test ./...
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ go 1.20

require (
github.com/ardanlabs/conf v1.5.0
github.com/cloudflare/circl v1.3.6
github.com/cloudflare/circl v1.3.7
github.com/cockroachdb/pebble v1.1.0
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/pkg/errors v0.9.1
github.com/qubic/go-node-connector v0.2.1
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.61.0
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
)

Expand Down
17 changes: 6 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg=
github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudflare/fourq v0.0.0-20170427000316-8ada258cf9c8 h1:748sGeXXbplK0UVPDLbhh53hejCnvv/u6jn2RPBfyI8=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4=
Expand Down Expand Up @@ -230,15 +230,11 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -505,8 +501,8 @@ google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKa
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand All @@ -533,7 +529,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func run() error {
Qubic struct {
NodeIp string `conf:"default:212.51.150.253"`
NodePort string `conf:"default:21841"`
FallbackTick uint64 `conf:"default:12522443"`
FallbackTick uint64 `conf:"default:12543674"`
BatchSize uint64 `conf:"default:500"`
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/exec"
)

var debug = false
var debug = true

func FourQSigVerify(ctx context.Context, pubkey [32]byte, digest [32]byte, sig [64]byte) error {
pubKeyHex := hex.EncodeToString(pubkey[:])
Expand Down
30 changes: 18 additions & 12 deletions validator/tx/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,59 @@ import (
"github.com/qubic/go-archiver/store"
"github.com/qubic/go-archiver/utils"
"github.com/qubic/go-node-connector/types"
"log"
)

var emptyTxDigest [32]byte

func Validate(ctx context.Context, transactions []types.Transaction, tickData types.TickData) error {
func Validate(ctx context.Context, transactions []types.Transaction, tickData types.TickData) ([]types.Transaction, error) {
digestsMap := createTxDigestsMap(tickData)
if len(transactions) != len(digestsMap) {
return errors.Errorf("tx count mismatch. tx count: %d, digests count: %d", len(transactions), len(digestsMap))
return nil, errors.Errorf("tx count mismatch. tx count: %d, digests count: %d", len(transactions), len(digestsMap))
}

err := validateTransactions(ctx, transactions, digestsMap)
validTxs, err := validateTransactions(ctx, transactions, digestsMap)
if err != nil {
return errors.Wrap(err, "validating transactions")
return nil, errors.Wrap(err, "validating transactions")
}
return nil

return validTxs, nil
}

func validateTransactions(ctx context.Context, transactions []types.Transaction, digestsMap map[string]struct{}) error {
func validateTransactions(ctx context.Context, transactions []types.Transaction, digestsMap map[string]struct{}) ([]types.Transaction, error) {
validTransactions := make([]types.Transaction, 0, len(transactions))
for _, tx := range transactions {
txDigest, err := getDigestFromTransaction(tx)
if err != nil {
return errors.Wrap(err, "getting digest from tx data")
return nil, errors.Wrap(err, "getting digest from tx data")
}

hexDigest := hex.EncodeToString(txDigest[:])
if _, ok := digestsMap[hexDigest]; !ok {
return errors.Errorf("tx not found in digests map: %s", hexDigest)
log.Printf("tx not found in digests map: %s. not \n", hexDigest)
continue
}

txDataBytes, err := tx.MarshallBinary()
if err != nil {
return errors.Wrap(err, "marshalling tx data")
return nil, errors.Wrap(err, "marshalling tx data")
}

constructedDigest, err := utils.K12Hash(txDataBytes[:len(txDataBytes)-64])
if err != nil {
return errors.Wrap(err, "constructing digest from tx data")
return nil, errors.Wrap(err, "constructing digest from tx data")
}

err = utils.FourQSigVerify(ctx, tx.SourcePublicKey, constructedDigest, tx.Signature)
if err != nil {
return errors.Wrap(err, "verifying tx signature")
return nil, errors.Wrap(err, "verifying tx signature")
}
validTransactions = append(validTransactions, tx)

//log.Printf("Validated tx: %s. Count: %d\n", hexDigest, index)
}

return nil
return validTransactions, nil
}

func getDigestFromTransaction(tx types.Transaction) ([32]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func (v *Validator) ValidateTick(ctx context.Context, tickNumber uint64) error {

log.Printf("Validating %d transactions\n", len(transactions))

err = tx.Validate(ctx, transactions, tickData)
validTxs, err := tx.Validate(ctx, transactions, tickData)
if err != nil {
return errors.Wrap(err, "validating transactions")
}

log.Printf("Validated %d transactions\n", len(transactions))
log.Printf("Validated %d transactions\n", len(validTxs))

err = quorum.Store(ctx, v.store, quorumVotes)
if err != nil {
Expand All @@ -109,7 +109,7 @@ func (v *Validator) ValidateTick(ctx context.Context, tickNumber uint64) error {

log.Printf("Stored tick data\n")

err = tx.Store(ctx, v.store, transactions)
err = tx.Store(ctx, v.store, validTxs)
if err != nil {
return errors.Wrap(err, "storing transactions")
}
Expand Down

0 comments on commit 9095df1

Please sign in to comment.