-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update indexer to read consensus blocks from file
- Loading branch information
1 parent
37480f0
commit c03bb05
Showing
4 changed files
with
182 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package file | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api" | ||
"github.com/oasisprotocol/oasis-indexer/storage/oasis/nodeapi" | ||
|
||
// indexer-internal data types. | ||
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" | ||
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api" | ||
governance "github.com/oasisprotocol/oasis-core/go/governance/api" | ||
registry "github.com/oasisprotocol/oasis-core/go/registry/api" | ||
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api" | ||
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api" | ||
staking "github.com/oasisprotocol/oasis-core/go/staking/api" | ||
) | ||
|
||
// FileConsensusApiLite provides access to the consensus API of an Oasis node. | ||
// Since FileConsensusApiLite is backed by a file containing the cached responses | ||
// to `ConsensusApiLite` calls, this data is inherently compatible with the | ||
// current indexer and can thus handle heights from both Cobalt/Damask. | ||
type FileConsensusApiLite struct { | ||
currHeight int64 | ||
currData *nodeapi.ConsensusApiLiteBlockData // cache the current height's data to speed sequential reads. | ||
genesisDocument *genesis.Document | ||
filename string | ||
reader *json.Decoder | ||
} | ||
|
||
var _ nodeapi.ConsensusApiLite = (*FileConsensusApiLite)(nil) | ||
|
||
func NewFileConsensusApiLite(filename string) (*FileConsensusApiLite, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader := json.NewDecoder(f) | ||
var genesisDocument genesis.Document | ||
if err := reader.Decode(genesisDocument); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FileConsensusApiLite{ | ||
currHeight: 0, | ||
genesisDocument: &genesisDocument, | ||
filename: filename, | ||
reader: reader, | ||
}, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) jumpToHeight(height int64) error { | ||
if height == c.currHeight { | ||
return nil | ||
} | ||
if height < c.currHeight { | ||
f, err := os.Open(c.filename) | ||
if err != nil { | ||
return err | ||
} | ||
c.reader = json.NewDecoder(f) | ||
if height == 0 { | ||
return nil | ||
} | ||
|
||
var v interface{} | ||
c.reader.Decode(v) // skip genesis document | ||
c.reader.Decode(c.currData) | ||
c.currHeight = c.currData.Block.Height | ||
} | ||
|
||
for c.currHeight < height && c.reader.More() { | ||
if err := c.reader.Decode(c.currData); err != nil { // todo: use file reader readline() instead to avoid serializing into c.currData | ||
return err | ||
} | ||
c.currHeight = c.currHeight + 1 | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetGenesisDocument(ctx context.Context) (*genesis.Document, error) { | ||
return c.genesisDocument, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error) { | ||
panic("not implemented") // Only used by the statecheck as of 16/03/2023 | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetBlock(ctx context.Context, height int64) (*consensus.Block, error) { | ||
c.jumpToHeight(height) | ||
|
||
return &c.currData.Block, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetTransactionsWithResults(ctx context.Context, height int64) (*consensus.TransactionsWithResults, error) { | ||
c.jumpToHeight(height) | ||
|
||
return &c.currData.TransactionsWithResults, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetEpoch(ctx context.Context, height int64) (beacon.EpochTime, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.Epoch, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) RegistryEvents(ctx context.Context, height int64) ([]*registry.Event, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.RegistryEvents, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) StakingEvents(ctx context.Context, height int64) ([]*staking.Event, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.StakingEvents, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GovernanceEvents(ctx context.Context, height int64) ([]*governance.Event, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.GovernanceEvents, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) RoothashEvents(ctx context.Context, height int64) ([]*roothash.Event, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.RootHashEvents, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetValidators(ctx context.Context, height int64) ([]*scheduler.Validator, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.Validators, nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetCommittees(ctx context.Context, height int64, runtimeID common.Namespace) ([]*scheduler.Committee, error) { | ||
c.jumpToHeight(height) | ||
|
||
return c.currData.Committees[runtimeID], nil | ||
} | ||
|
||
func (c *FileConsensusApiLite) GetProposal(ctx context.Context, height int64, proposalID uint64) (*governance.Proposal, error) { | ||
panic("not implemented") | ||
} |