From 99e895d600161821d505eb2289c65818fc920978 Mon Sep 17 00:00:00 2001 From: Fernando Torres Date: Thu, 22 Dec 2022 15:48:09 -0300 Subject: [PATCH] Fix mongo types in Fly --- devnet/mongo-rs-config.yaml | 12 ++++++++++ fly/storage/documents.go | 26 +++++++++++++++++--- fly/storage/repository.go | 47 ++++++++++++++++++++++++++++++++----- fly/storage/types.go | 33 ++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 fly/storage/types.go diff --git a/devnet/mongo-rs-config.yaml b/devnet/mongo-rs-config.yaml index 1f74fe0f..bb986c2b 100644 --- a/devnet/mongo-rs-config.yaml +++ b/devnet/mongo-rs-config.yaml @@ -8,6 +8,18 @@ spec: metadata: name: mongo-rs-config spec: + initContainers: + - name: init-mongodb + image: mongo:latest + command: + - /bin/bash + - -c + - |- + until mongosh --eval 'db.runCommand("ping").ok' mongo-0.mongo:27017/test --quiet; + do + echo "**** Waiting for MongoDB ..."; + sleep 5; + done containers: - name: mongo image: mongo:latest diff --git a/fly/storage/documents.go b/fly/storage/documents.go index 638373ae..0b0b3178 100644 --- a/fly/storage/documents.go +++ b/fly/storage/documents.go @@ -67,19 +67,39 @@ type GovernorStatusUpdate struct { type ChainGovernorStatusChain struct { ChainId uint32 `bson:"chainid"` - RemainingAvailableNotional uint64 `bson:"remainingavailablenotional"` + RemainingAvailableNotional Uint64 `bson:"remainingavailablenotional"` Emitters []*ChainGovernorStatusEmitter `bson:"emitters"` } type ChainGovernorStatusEmitter struct { EmitterAddress string `bson:"emitteraddress"` - TotalEnqueuedVaas uint64 `bson:"totalenqueuedvaas"` + TotalEnqueuedVaas Uint64 `bson:"totalenqueuedvaas"` EnqueuedVaas []*ChainGovernorStatusEnqueuedVAA `bson:"enqueuedvaas"` } type ChainGovernorStatusEnqueuedVAA struct { Sequence string `bson:"sequence"` ReleaseTime uint32 `bson:"releasetime"` - NotionalValue uint64 `bson:"notionalvalue"` + NotionalValue Uint64 `bson:"notionalvalue"` TxHash string `bson:"txhash"` } + +type ChainGovernorConfigUpdate struct { + NodeName string + Counter int64 + Timestamp int64 + Chains []*ChainGovernorConfigChain + Tokens []*ChainGovernorConfigToken +} + +type ChainGovernorConfigChain struct { + ChainId uint32 + NotionalLimit Uint64 + BigTransactionSize Uint64 +} + +type ChainGovernorConfigToken struct { + OriginChainId uint32 + OriginAddress string + Price float32 +} diff --git a/fly/storage/repository.go b/fly/storage/repository.go index 5bc2fd12..e40f1ba9 100644 --- a/fly/storage/repository.go +++ b/fly/storage/repository.go @@ -138,13 +138,17 @@ func (s *Repository) UpsertHeartbeat(hb *gossipv1.Heartbeat) error { func (s *Repository) UpsertGovernorConfig(govC *gossipv1.SignedChainGovernorConfig) error { id := hex.EncodeToString(govC.GuardianAddr) now := time.Now() - var cfg gossipv1.ChainGovernorConfig - err := proto.Unmarshal(govC.Config, &cfg) + var gCfg gossipv1.ChainGovernorConfig + err := proto.Unmarshal(govC.Config, &gCfg) if err != nil { s.log.Error("Error unmarshalling govr config", zap.Error(err)) return err } + + cfg := toGovernorConfigUpdate(&gCfg) + update := bson.D{{Key: "$set", Value: govC}, {Key: "$set", Value: bson.D{{Key: "parsedConfig", Value: cfg}}}, {Key: "$set", Value: bson.D{{Key: "updatedAt", Value: now}}}, {Key: "$setOnInsert", Value: bson.D{{Key: "createdAt", Value: now}}}} + opts := options.Update().SetUpsert(true) _, err2 := s.collections.governorConfig.UpdateByID(context.TODO(), id, update, opts) @@ -178,7 +182,7 @@ func (s *Repository) UpsertGovernorStatus(govS *gossipv1.SignedChainGovernorStat } func (s *Repository) updateVAACount(chainID vaa.ChainID) { - update := bson.D{{Key: "$inc", Value: bson.D{{Key: "count", Value: 1}}}} + update := bson.D{{Key: "$inc", Value: bson.D{{Key: "count", Value: uint64(1)}}}} opts := options.Update().SetUpsert(true) _, _ = s.collections.vaaCounts.UpdateByID(context.TODO(), chainID, update, opts) } @@ -213,7 +217,7 @@ func toGovernorStatusUpdate(s *gossipv1.ChainGovernorStatus) *GovernorStatusUpda enqueuedVaa := &ChainGovernorStatusEnqueuedVAA{ Sequence: strconv.FormatUint(ev.Sequence, 10), ReleaseTime: ev.ReleaseTime, - NotionalValue: ev.NotionalValue, + NotionalValue: Uint64(ev.NotionalValue), TxHash: ev.TxHash, } enqueuedVaas = append(enqueuedVaas, enqueuedVaa) @@ -221,7 +225,7 @@ func toGovernorStatusUpdate(s *gossipv1.ChainGovernorStatus) *GovernorStatusUpda emitter := &ChainGovernorStatusEmitter{ EmitterAddress: e.EmitterAddress, - TotalEnqueuedVaas: e.TotalEnqueuedVaas, + TotalEnqueuedVaas: Uint64(e.TotalEnqueuedVaas), EnqueuedVaas: enqueuedVaas, } emitters = append(emitters, emitter) @@ -229,7 +233,7 @@ func toGovernorStatusUpdate(s *gossipv1.ChainGovernorStatus) *GovernorStatusUpda chain := &ChainGovernorStatusChain{ ChainId: c.ChainId, - RemainingAvailableNotional: c.RemainingAvailableNotional, + RemainingAvailableNotional: Uint64(c.RemainingAvailableNotional), Emitters: emitters, } chains = append(chains, chain) @@ -243,3 +247,34 @@ func toGovernorStatusUpdate(s *gossipv1.ChainGovernorStatus) *GovernorStatusUpda } return status } + +func toGovernorConfigUpdate(c *gossipv1.ChainGovernorConfig) *ChainGovernorConfigUpdate { + + var chains []*ChainGovernorConfigChain + for _, c := range c.Chains { + chain := &ChainGovernorConfigChain{ + ChainId: c.ChainId, + NotionalLimit: Uint64(c.NotionalLimit), + BigTransactionSize: Uint64(c.BigTransactionSize), + } + chains = append(chains, chain) + } + + var tokens []*ChainGovernorConfigToken + for _, t := range c.Tokens { + token := &ChainGovernorConfigToken{ + OriginChainId: t.OriginChainId, + OriginAddress: t.OriginAddress, + Price: t.Price, + } + tokens = append(tokens, token) + } + + return &ChainGovernorConfigUpdate{ + NodeName: c.NodeName, + Counter: c.Counter, + Timestamp: c.Timestamp, + Chains: chains, + Tokens: tokens, + } +} diff --git a/fly/storage/types.go b/fly/storage/types.go new file mode 100644 index 00000000..34561a47 --- /dev/null +++ b/fly/storage/types.go @@ -0,0 +1,33 @@ +package storage + +import ( + "errors" + "strconv" + + "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" +) + +type Uint64 uint64 + +func (u Uint64) MarshalBSONValue() (bsontype.Type, []byte, error) { + ui64Str := strconv.FormatUint(uint64(u), 10) + d128, err := primitive.ParseDecimal128(ui64Str) + return bsontype.Decimal128, bsoncore.AppendDecimal128(nil, d128), err +} + +func (u *Uint64) UnmarshalBSONValue(t bsontype.Type, b []byte) error { + d128, _, ok := bsoncore.ReadDecimal128(b) + if !ok { + return errors.New("Uint64 UnmarshalBSONValue error") + } + + ui64, err := strconv.ParseUint(d128.String(), 10, 64) + if err != nil { + return err + } + + *u = Uint64(ui64) + return nil +}