-
Notifications
You must be signed in to change notification settings - Fork 4
/
runtime.go
118 lines (104 loc) · 4.16 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package history
import (
"context"
"fmt"
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/nexus/common"
"github.com/oasisprotocol/nexus/config"
"github.com/oasisprotocol/nexus/storage/oasis/connections"
"github.com/oasisprotocol/nexus/storage/oasis/nodeapi"
)
var _ nodeapi.RuntimeApiLite = (*HistoryRuntimeApiLite)(nil)
type HistoryRuntimeApiLite struct {
Runtime common.Runtime
History *config.History
APIs map[string]nodeapi.RuntimeApiLite
}
func NewHistoryRuntimeApiLite(ctx context.Context, history *config.History, sdkPT *sdkConfig.ParaTime, nodes map[string]*config.ArchiveConfig, fastStartup bool, runtime common.Runtime) (*HistoryRuntimeApiLite, error) {
apis := map[string]nodeapi.RuntimeApiLite{}
for _, record := range history.Records {
if archiveConfig, ok := nodes[record.ArchiveName]; ok {
sdkConn, err := connections.SDKConnect(ctx, record.ChainContext, archiveConfig.ResolvedRuntimeNode(runtime), fastStartup)
if err != nil {
return nil, err
}
sdkClient := sdkConn.Runtime(sdkPT)
rawConn, err := connections.RawConnect(archiveConfig.ResolvedRuntimeNode(runtime))
if err != nil {
return nil, fmt.Errorf("oasis-node RawConnect: %w", err)
}
apis[record.ArchiveName] = nodeapi.NewUniversalRuntimeApiLite(sdkPT.Namespace(), rawConn, &sdkClient)
}
}
return &HistoryRuntimeApiLite{
Runtime: runtime,
History: history,
APIs: apis,
}, nil
}
func (rc *HistoryRuntimeApiLite) Close() error {
var firstErr error
for _, api := range rc.APIs {
if err := api.Close(); err != nil && firstErr == nil {
firstErr = err
// Do not return yet; keep closing others.
}
}
if firstErr != nil {
return fmt.Errorf("closing apis failed, first encountered error was: %w", firstErr)
}
return nil
}
func (rc *HistoryRuntimeApiLite) APIForRound(round uint64) (nodeapi.RuntimeApiLite, error) {
record, err := rc.History.RecordForRuntimeRound(rc.Runtime, round)
if err != nil {
return nil, fmt.Errorf("determining archive: %w", err)
}
api, ok := rc.APIs[record.ArchiveName]
if !ok {
return nil, fmt.Errorf("archive %s has no node configured", record.ArchiveName)
}
return api, nil
}
func (rc *HistoryRuntimeApiLite) GetEventsRaw(ctx context.Context, round uint64) ([]nodeapi.RuntimeEvent, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.GetEventsRaw(ctx, round)
}
func (rc *HistoryRuntimeApiLite) EVMSimulateCall(ctx context.Context, round uint64, gasPrice []byte, gasLimit uint64, caller []byte, address []byte, value []byte, data []byte) (*nodeapi.FallibleResponse, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.EVMSimulateCall(ctx, round, gasPrice, gasLimit, caller, address, value, data)
}
func (rc *HistoryRuntimeApiLite) EVMGetCode(ctx context.Context, round uint64, address []byte) ([]byte, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.EVMGetCode(ctx, round, address)
}
func (rc *HistoryRuntimeApiLite) GetBlockHeader(ctx context.Context, round uint64) (*nodeapi.RuntimeBlockHeader, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.GetBlockHeader(ctx, round)
}
func (rc *HistoryRuntimeApiLite) GetNativeBalance(ctx context.Context, round uint64, addr nodeapi.Address) (*common.BigInt, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.GetNativeBalance(ctx, round, addr)
}
func (rc *HistoryRuntimeApiLite) GetTransactionsWithResults(ctx context.Context, round uint64) ([]nodeapi.RuntimeTransactionWithResults, error) {
api, err := rc.APIForRound(round)
if err != nil {
return nil, fmt.Errorf("getting api for runtime %s round %d: %w", rc.Runtime, round, err)
}
return api.GetTransactionsWithResults(ctx, round)
}