From 9095df13ddd579cb918b341b275241f4d8f0b609 Mon Sep 17 00:00:00 2001 From: 0xluk Date: Sun, 18 Feb 2024 09:31:18 +0200 Subject: [PATCH] fixed bug where if tickdata and quorumtick data are empty but with len(txs) > 0 tick is not valid --- .github/workflows/test.yaml | 4 ++-- go.mod | 6 +++--- go.sum | 17 ++++++----------- main.go | 2 +- utils/utils.go | 2 +- validator/tx/validator.go | 30 ++++++++++++++++++------------ validator/validator.go | 6 +++--- 7 files changed, 34 insertions(+), 33 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 855cc66..cbf15cf 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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: @@ -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 ./... \ No newline at end of file diff --git a/go.mod b/go.mod index 9fbbdea..94002d0 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index d90a7ee..c0b0c10 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/main.go b/main.go index 84d7e7a..f62bddc 100644 --- a/main.go +++ b/main.go @@ -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"` } } diff --git a/utils/utils.go b/utils/utils.go index 8e0b02b..8b27289 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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[:]) diff --git a/validator/tx/validator.go b/validator/tx/validator.go index ec9eba1..eea6f62 100644 --- a/validator/tx/validator.go +++ b/validator/tx/validator.go @@ -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) { diff --git a/validator/validator.go b/validator/validator.go index 54c3e39..9bccace 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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 { @@ -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") }