Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Sep 6, 2023
1 parent 06023f8 commit 78e901a
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 246 deletions.
8 changes: 4 additions & 4 deletions api/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func AddV2Routes(ctx *Context, router *web.Router, path string, indexBytes []byt
Get("/multisigalias/:owners", (*V2Context).GetMultisigAlias).
Post("/rewards", (*V2Context).GetRewardPost).
Get("/proposals", (*V2Context).ListDACProposals).
Get("/proposals/:id/votes", (*V2Context).GetDACProposalVotes)
Get("/proposals/:id", (*V2Context).GetDACProposalWithVotes)
}

// AVAX
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func (c *V2Context) ListDACProposals(w web.ResponseWriter, r *web.Request) {
})
}

func (c *V2Context) GetDACProposalVotes(w web.ResponseWriter, r *web.Request) {
func (c *V2Context) GetDACProposalWithVotes(w web.ResponseWriter, r *web.Request) {
collectors := utils.NewCollectors(
utils.NewCounterObserveMillisCollect(MetricMillis),
utils.NewCounterIncCollect(MetricCount),
Expand All @@ -1184,9 +1184,9 @@ func (c *V2Context) GetDACProposalVotes(w web.ResponseWriter, r *web.Request) {
proposalID := r.PathParams["id"]
c.WriteCacheable(w, caching.Cacheable{
TTL: 5 * time.Second,
Key: c.cacheKeyForID("get_dac_proposal_votes", proposalID),
Key: c.cacheKeyForID("get_dac_proposal", proposalID),
CacheableFn: func(ctx context.Context) (interface{}, error) {
return c.avaxReader.GetDACProposalVotes(ctx, proposalID)
return c.avaxReader.GetDACProposalWithVotes(ctx, proposalID)
},
})
}
130 changes: 117 additions & 13 deletions db/dbmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type Persist interface {
dbr.SessionRunner,
*Transactions,
) (*Transactions, error)
QueryMultipleTransactions(
context.Context,
dbr.SessionRunner,
[]string,
) (*[]Transactions, error)
InsertTransactions(
context.Context,
dbr.SessionRunner,
Expand Down Expand Up @@ -444,44 +449,59 @@ type Persist interface {
*Reward,
) error

// DAC Proposals
InsertDACProposal(
ctx context.Context,
session dbr.SessionRunner,
proposal *DACProposal,
) error

UpdateDACProposal(
ctx context.Context,
session dbr.SessionRunner,
proposalID string,
updatedProposal []byte,
) error
UpdateDACProposalsStatus(
ctx context.Context,
session dbr.SessionRunner,
proposalIDs []string,
proposalStatus models.ProposalStatus,
) error

FinishDACProposal(
ctx context.Context,
session dbr.SessionRunner,
proposalID string,
proposalStatus models.ProposalStatus,
outcome []byte,
) error

GetDACProposals(
ctx context.Context,
session dbr.SessionRunner,
proposalIDs []string,
) ([]DACProposal, error)
QueryDACProposals(
ctx context.Context,
session dbr.SessionRunner,
params *params.ListDACProposalsParams,
) ([]DACProposal, error)

// DAC Votes
InsertDACVote(
ctx context.Context,
session dbr.SessionRunner,
vote *DACVote,
) error

QueryDACProposalVotes(
ctx context.Context,
session dbr.SessionRunner,
proposalID string,
) ([]DACVote, error)

GetTxHeight(
ctx context.Context,
session dbr.SessionRunner,
txID string,
) (uint64, error)
}

type persist struct{}
Expand Down Expand Up @@ -542,6 +562,29 @@ func (p *persist) QueryTransactions(
return v, err
}

func (p *persist) QueryMultipleTransactions(
ctx context.Context,
sess dbr.SessionRunner,
txIDs []string,
) (*[]Transactions, error) {
v := &[]Transactions{}
err := sess.Select(
"id",
"chain_id",
"type",
"memo",
"created_at",
"canonical_serialization",
"txfee",
"genesis",
"network_id",
"status",
).From(TableTransactions).
Where("id IN ?", txIDs).
LoadOneContext(ctx, v)
return v, err
}

func (p *persist) InsertTransactions(
ctx context.Context,
sess dbr.SessionRunner,
Expand Down Expand Up @@ -2434,15 +2477,16 @@ func (p *persist) DeactivateReward(ctx context.Context, session dbr.SessionRunne
}

type DACProposal struct {
ID string `db:"id"` // proposal id, also addProposalTx id
ProposerAddr string `db:"proposer_addr"` // address which authorized proposal
StartTime time.Time `db:"start_time"` // time when proposal will become votable
EndTime time.Time `db:"end_time"` // time when proposal will become non-votable and will be executed if its successful
Type models.ProposalType `db:"type"` // proposal type
Options []byte `db:"options"` // proposal votable options
Memo []byte `db:"memo"` // addProposalTx memo
Outcome []byte `db:"outcome"` // outcome of successful proposal, usually is one or multiple options indexes
Status models.ProposalStatus `db:"status"` // current status of proposal
ID string `db:"id"` // proposal id, also addProposalTx id
ProposerAddr string `db:"proposer_addr"` // address which authorized proposal
StartTime time.Time `db:"start_time"` // time when proposal will become votable
EndTime time.Time `db:"end_time"` // time when proposal will become non-votable and will be executed if its successful
Type models.ProposalType `db:"type"` // proposal type
Options []byte `db:"options"` // proposal votable options
SerializedBytes []byte `db:"serialized_bytes"` // serialized dac.ProposalState
Memo []byte `db:"memo"` // addProposalTx memo
Outcome []byte `db:"outcome"` // outcome of successful proposal, usually is one or multiple options indexes
Status models.ProposalStatus `db:"status"` // current status of proposal
}

func (p *persist) InsertDACProposal(ctx context.Context, session dbr.SessionRunner, proposal *DACProposal) error {
Expand All @@ -2453,6 +2497,7 @@ func (p *persist) InsertDACProposal(ctx context.Context, session dbr.SessionRunn
Pair("start_time", proposal.StartTime).
Pair("end_time", proposal.EndTime).
Pair("type", proposal.Type).
Pair("serialized_bytes", proposal.SerializedBytes).
Pair("options", proposal.Options).
Pair("status", proposal.Status).
ExecContext(ctx)
Expand All @@ -2462,6 +2507,23 @@ func (p *persist) InsertDACProposal(ctx context.Context, session dbr.SessionRunn
return nil
}

func (p *persist) UpdateDACProposal(
ctx context.Context,
session dbr.SessionRunner,
proposalID string,
updatedProposal []byte,
) error {
_, err := session.
Update(TableDACProposals).
Set("serialized_bytes", updatedProposal).
Where("id=?", proposalID).
ExecContext(ctx)
if err != nil {
return EventErr(TableDACProposals, false, err)
}
return nil
}

func (p *persist) UpdateDACProposalsStatus(
ctx context.Context,
session dbr.SessionRunner,
Expand Down Expand Up @@ -2498,6 +2560,31 @@ func (p *persist) FinishDACProposal(
return nil
}

func (p *persist) GetDACProposals(
ctx context.Context,
session dbr.SessionRunner,
proposalIDs []string,
) ([]DACProposal, error) {
v := &[]DACProposal{}
query := session.Select(
"P.id",
"P.proposer_addr",
"P.start_time",
"P.end_time",
"P.type",
"P.serialized_bytes",
"P.options",
"T.memo",
"P.outcome",
"P.status",
).From(dbr.I(TableDACProposals).As("P")).
Join(dbr.I(TableTransactions).As("T"), "T.id=P.id").
Where("P.id in ?", proposalIDs)

_, err := query.LoadContext(ctx, v)
return *v, err
}

func (p *persist) QueryDACProposals(
ctx context.Context,
session dbr.SessionRunner,
Expand All @@ -2510,6 +2597,7 @@ func (p *persist) QueryDACProposals(
"P.start_time",
"P.end_time",
"P.type",
"P.serialized_bytes",
"P.options",
"T.memo",
"P.outcome",
Expand Down Expand Up @@ -2583,3 +2671,19 @@ func (p *persist) QueryDACProposalVotes(
LoadContext(ctx, v)
return *v, err
}

func (p *persist) GetTxHeight(
ctx context.Context,
session dbr.SessionRunner,
txID string,
) (uint64, error) {
v := uint64(0)
err := session.SelectBySql(`
SELECT PB.height
FROM magellan.pvm_blocks AS PB
WHERE PB.id IN (
SELECT TB.tx_block_id
FROM magellan.transactions_block AS TB
WHERE TB.id = ?);`, txID).LoadOneContext(ctx, &v)
return v, err
}
49 changes: 49 additions & 0 deletions db/dbmodel_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func NewPersistMock() *MockPersist {
KeyValueStore: make(map[string]*KeyValueStore),
NodeIndex: make(map[string]*NodeIndex),
MultisigAlias: make(map[string]*MultisigAlias),
DACProposals: make(map[string]*DACProposal),
DACVotes: make(map[string]*DACVote),
}
}

Expand All @@ -102,6 +104,18 @@ func (m *MockPersist) QueryTransactions(ctx context.Context, runner dbr.SessionR
return nil, nil
}

func (m *MockPersist) QueryMultipleTransactions(ctx context.Context, runner dbr.SessionRunner, txIDs []string) (*[]Transactions, error) {
m.lock.RLock()
defer m.lock.RUnlock()
var txs []Transactions
for _, txID := range txIDs {
if tx, ok := m.Transactions[txID]; ok {
txs = append(txs, *tx)
}
}
return &txs, nil
}

func (m *MockPersist) InsertTransactions(ctx context.Context, runner dbr.SessionRunner, v *Transactions, b bool) error {
m.lock.Lock()
defer m.lock.Unlock()
Expand Down Expand Up @@ -690,6 +704,17 @@ func (m *MockPersist) InsertDACProposal(ctx context.Context, session dbr.Session
return nil
}

func (m *MockPersist) UpdateDACProposal(ctx context.Context, session dbr.SessionRunner, proposalID string, updatedProposal []byte) error {
m.lock.Lock()
defer m.lock.Unlock()
proposal, ok := m.DACProposals[proposalID]
if !ok {
return nil
}
proposal.SerializedBytes = updatedProposal
return nil
}

// QueryDACProposals is not deterministic about return result, cause of random mapping order
func (m *MockPersist) QueryDACProposals(ctx context.Context, session dbr.SessionRunner, params *params.ListDACProposalsParams) ([]DACProposal, error) {
m.lock.Lock()
Expand Down Expand Up @@ -743,6 +768,16 @@ func (m *MockPersist) FinishDACProposal(ctx context.Context, session dbr.Session
return nil
}

func (m *MockPersist) GetDACProposals(ctx context.Context, session dbr.SessionRunner, proposalIDs []string) ([]DACProposal, error) {
proposals := make([]DACProposal, 0, len(proposalIDs))
for i := range proposalIDs {
if proposal, ok := m.DACProposals[proposalIDs[i]]; ok {
proposals = append(proposals, *proposal)
}
}
return proposals, nil
}

func (m *MockPersist) InsertDACVote(ctx context.Context, session dbr.SessionRunner, vote *DACVote) error {
m.lock.Lock()
defer m.lock.Unlock()
Expand All @@ -763,3 +798,17 @@ func (m *MockPersist) QueryDACProposalVotes(ctx context.Context, session dbr.Ses
}
return votes, nil
}

func (m *MockPersist) GetTxHeight(ctx context.Context, session dbr.SessionRunner, txID string) (uint64, error) {
m.lock.Lock()
defer m.lock.Unlock()
txBlock, ok := m.TransactionsBlock[txID]
if !ok {
return 0, dbr.ErrNotFound
}
pvmBlock, ok := m.PvmBlocks[txBlock.TxBlockID]
if !ok {
return 0, dbr.ErrNotFound
}
return pvmBlock.Height, nil
}
Loading

0 comments on commit 78e901a

Please sign in to comment.