Skip to content

Commit a51a6c6

Browse files
Snow VM (#1831)
1 parent fc27d02 commit a51a6c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+3292
-4313
lines changed

Diff for: .github/workflows/hypersdk-ci.yml

-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ jobs:
135135
working-directory: ./examples/morpheusvm
136136
shell: bash
137137
run: scripts/tests.unit.sh
138-
- name: Run integration tests
139-
working-directory: ./examples/morpheusvm
140-
shell: bash
141-
run: scripts/tests.integration.sh
142138

143139
morpheusvm-e2e-tests:
144140
needs: [morpheusvm-lint, morpheusvm-unit-tests]

Diff for: api/dependencies.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ type VM interface {
3535
ctx context.Context,
3636
txs []*chain.Transaction,
3737
) (errs []error)
38-
LastAcceptedBlockResult() *chain.ExecutedBlock
38+
LastAcceptedBlock(ctx context.Context) (*chain.StatelessBlock, error)
3939
UnitPrices(context.Context) (fees.Dimensions, error)
4040
CurrentValidators(
4141
context.Context,
4242
) (map[ids.NodeID]*validators.GetValidatorOutput, map[string]struct{})
43-
GetVerifyAuth() bool
4443
ReadState(ctx context.Context, keys [][]byte) ([][]byte, []error)
4544
ImmutableState(ctx context.Context) (state.Immutable, error)
4645
BalanceHandler() chain.BalanceHandler

Diff for: api/indexer/indexer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (i *Indexer) initBlocks() error {
8888
return err
8989
}
9090

91-
i.blockIDToHeight.Put(blk.Block.ID(), blk.Block.Hght)
91+
i.blockIDToHeight.Put(blk.Block.GetID(), blk.Block.Hght)
9292
i.blockHeightToBlock.Put(blk.Block.Hght, blk)
9393
lastHeight = blk.Block.Hght
9494
}
@@ -135,7 +135,7 @@ func (i *Indexer) storeBlock(blk *chain.ExecutedBlock) error {
135135
return err
136136
}
137137

138-
i.blockIDToHeight.Put(blk.Block.ID(), blk.Block.Hght)
138+
i.blockIDToHeight.Put(blk.Block.GetID(), blk.Block.Hght)
139139
i.blockHeightToBlock.Put(blk.Block.Hght, blk)
140140
i.lastHeight.Store(blk.Block.Hght)
141141

Diff for: api/indexer/indexer_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ func checkBlocks(
5252
expectedLatestBlk := expectedBlocks[len(expectedBlocks)-1]
5353
latestBlk, err := indexer.GetLatestBlock()
5454
require.NoError(err)
55-
require.Equal(expectedLatestBlk.Block.ID(), latestBlk.Block.ID())
55+
require.Equal(expectedLatestBlk.Block.GetID(), latestBlk.Block.GetID())
5656

5757
// Confirm all blocks in the window are retrievable
5858
for i := 0; i < blockWindow; i++ {
5959
expectedBlk := expectedBlocks[len(expectedBlocks)-1-i]
6060
height := expectedBlk.Block.Hght
6161
blkByHeight, err := indexer.GetBlockByHeight(height)
6262
require.NoError(err)
63-
require.Equal(expectedBlk.Block.ID(), blkByHeight.Block.ID())
63+
require.Equal(expectedBlk.Block.GetID(), blkByHeight.Block.GetID())
6464

65-
blkByID, err := indexer.GetBlock(expectedBlk.Block.ID())
65+
blkByID, err := indexer.GetBlock(expectedBlk.Block.GetID())
6666
require.NoError(err)
67-
require.Equal(expectedBlk.Block.ID(), blkByID.Block.ID())
67+
require.Equal(expectedBlk.Block.GetID(), blkByID.Block.GetID())
6868
}
6969

7070
// Confirm blocks outside the window are not retrievable

Diff for: api/jsonrpc/server.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ type LastAcceptedReply struct {
116116
Timestamp int64 `json:"timestamp"`
117117
}
118118

119-
func (j *JSONRPCServer) LastAccepted(_ *http.Request, _ *struct{}, reply *LastAcceptedReply) error {
120-
blk := j.vm.LastAcceptedBlockResult()
121-
reply.Height = blk.Block.Hght
122-
reply.BlockID = blk.Block.ID()
123-
reply.Timestamp = blk.Block.Tmstmp
119+
func (j *JSONRPCServer) LastAccepted(req *http.Request, _ *struct{}, reply *LastAcceptedReply) error {
120+
blk, err := j.vm.LastAcceptedBlock(req.Context())
121+
if err != nil {
122+
return err
123+
}
124+
reply.Height = blk.Hght
125+
reply.BlockID = blk.GetID()
126+
reply.Timestamp = blk.Tmstmp
124127
return nil
125128
}
126129

Diff for: api/state/server.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package state
66
import (
77
"net/http"
88

9+
"github.com/ava-labs/avalanchego/database"
10+
911
"github.com/ava-labs/hypersdk/api"
1012
)
1113

@@ -53,8 +55,14 @@ func (s *JSONRPCStateServer) ReadState(req *http.Request, args *ReadStateRequest
5355

5456
var errs []error
5557
res.Values, errs = s.stateReader.ReadState(ctx, args.Keys)
56-
for _, err := range errs {
57-
res.Errors = append(res.Errors, err.Error())
58+
res.Errors = make([]string, len(errs))
59+
for i, err := range errs {
60+
if err != nil {
61+
res.Errors[i] = err.Error()
62+
}
63+
if err != nil && err != database.ErrNotFound {
64+
return err
65+
}
5866
}
5967
return nil
6068
}

Diff for: auth/provider.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"os"
10-
11-
"github.com/ava-labs/avalanchego/utils/wrappers"
1210
)
1311

1412
var (
@@ -17,8 +15,8 @@ var (
1715
)
1816

1917
// WithDefaultPrivateKeyFactories registers the default PrivateKeyFactories
20-
func WithDefaultPrivateKeyFactories(authProvider *AuthProvider, errs *wrappers.Errs) {
21-
errs.Add(
18+
func WithDefaultPrivateKeyFactories(authProvider *AuthProvider) error {
19+
return errors.Join(
2220
authProvider.Register(ED25519Key, NewED25519PrivateKeyFactory()),
2321
authProvider.Register(Secp256r1Key, NewSECP256R1PrivateKeyFactory()),
2422
authProvider.Register(BLSKey, NewBLSPrivateKeyFactory()),

Diff for: chain/accepter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ func NewAccepter(
2727
}
2828
}
2929

30-
func (a *Accepter) AcceptBlock(ctx context.Context, blk *ExecutionBlock) error {
30+
func (a *Accepter) AcceptBlock(ctx context.Context, blk *OutputBlock) error {
3131
_, span := a.tracer.Start(ctx, "Chain.AcceptBlock")
3232
defer span.End()
3333

3434
a.metrics.txsAccepted.Add(float64(len(blk.StatelessBlock.Txs)))
3535
a.validityWindow.Accept(blk)
3636

37-
return nil
37+
return blk.View.CommitToDB(ctx)
3838
}

Diff for: chain/assembler.go

-54
This file was deleted.

Diff for: chain/base.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func (b *Base) Execute(r Rules, timestamp int64) error {
3535
// TODO: make this modulus configurable
3636
return fmt.Errorf("%w: timestamp=%d", ErrMisalignedTime, b.Timestamp)
3737
case b.Timestamp < timestamp: // tx: 100 block: 110
38-
return ErrTimestampTooLate
38+
return fmt.Errorf("%w: tx timestamp (%d) < block timestamp (%d)", ErrTimestampTooLate, b.Timestamp, timestamp)
3939
case b.Timestamp > timestamp+r.GetValidityWindow(): // tx: 100 block 10
40-
return ErrTimestampTooEarly
40+
return fmt.Errorf("%w: tx timestamp (%d) > block timestamp (%d) + validity window (%d)", ErrTimestampTooEarly, b.Timestamp, timestamp, r.GetValidityWindow())
4141
case b.ChainID != r.GetChainID():
4242
return ErrInvalidChainID
4343
default:

Diff for: chain/builder.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ava-labs/avalanchego/ids"
1616
"github.com/ava-labs/avalanchego/trace"
1717
"github.com/ava-labs/avalanchego/utils/logging"
18-
"github.com/ava-labs/avalanchego/x/merkledb"
1918
"go.opentelemetry.io/otel/attribute"
2019
"go.uber.org/zap"
2120

@@ -45,8 +44,6 @@ func HandlePreExecute(log logging.Logger, err error) bool {
4544
return true
4645
case errors.Is(err, ErrTimestampTooLate):
4746
return false
48-
case errors.Is(err, ErrInvalidBalance):
49-
return false
5047
case errors.Is(err, ErrAuthNotActivated):
5148
return false
5249
case errors.Is(err, ErrAuthFailed):
@@ -96,19 +93,22 @@ func NewBuilder(
9693
}
9794
}
9895

99-
func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent *ExecutionBlock) (*ExecutionBlock, *ExecutedBlock, merkledb.View, error) {
96+
func (c *Builder) BuildBlock(ctx context.Context, parentOutputBlock *OutputBlock) (*ExecutionBlock, *OutputBlock, error) {
10097
ctx, span := c.tracer.Start(ctx, "Chain.BuildBlock")
10198
defer span.End()
10299

100+
parent := parentOutputBlock.ExecutionBlock
101+
parentView := parentOutputBlock.View
102+
103103
// Select next timestamp
104104
nextTime := time.Now().UnixMilli()
105105
r := c.ruleFactory.GetRules(nextTime)
106106
if nextTime < parent.Tmstmp+r.GetMinBlockGap() {
107107
c.log.Debug("block building failed", zap.Error(ErrTimestampTooEarly))
108-
return nil, nil, nil, ErrTimestampTooEarly
108+
return nil, nil, fmt.Errorf("%w: proposed build block time (%d) < parentTimestamp (%d) + minBlockGap (%d)", ErrTimestampTooEarly, nextTime, parent.Tmstmp, r.GetMinBlockGap())
109109
}
110110
var (
111-
parentID = parent.ID()
111+
parentID = parent.GetID()
112112
timestamp = nextTime
113113
height = parent.Hght + 1
114114
blockTransactions = []*Transaction{}
@@ -118,12 +118,12 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
118118
feeKey := FeeKey(c.metadataManager.FeePrefix())
119119
feeRaw, err := parentView.GetValue(ctx, feeKey)
120120
if err != nil {
121-
return nil, nil, nil, err
121+
return nil, nil, err
122122
}
123123
parentFeeManager := fees.NewManager(feeRaw)
124124
feeManager, err := parentFeeManager.ComputeNext(nextTime, r)
125125
if err != nil {
126-
return nil, nil, nil, err
126+
return nil, nil, err
127127
}
128128
maxUnits := r.GetMaxBlockUnits()
129129
targetUnits := r.GetWindowTargetUnits()
@@ -370,7 +370,7 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
370370
c.log.Debug("transactions restored to mempool", zap.Int("count", restored))
371371
}()
372372
c.log.Warn("build failed", zap.Error(execErr))
373-
return nil, nil, nil, execErr
373+
return nil, nil, execErr
374374
}
375375
break
376376
}
@@ -396,7 +396,7 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
396396
// Perform basic validity checks to make sure the block is well-formatted
397397
if len(blockTransactions) == 0 {
398398
if nextTime < parent.Tmstmp+r.GetMinEmptyBlockGap() {
399-
return nil, nil, nil, fmt.Errorf("%w: allowed in %d ms", ErrNoTxs, parent.Tmstmp+r.GetMinEmptyBlockGap()-nextTime) //nolint:spancheck
399+
return nil, nil, fmt.Errorf("%w: allowed in %d ms", ErrNoTxs, parent.Tmstmp+r.GetMinEmptyBlockGap()-nextTime) //nolint:spancheck
400400
}
401401
c.metrics.emptyBlockBuilt.Inc()
402402
}
@@ -422,27 +422,27 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
422422
len(keys),
423423
)
424424
if err := tsv.Insert(ctx, heightKey, binary.BigEndian.AppendUint64(nil, height)); err != nil {
425-
return nil, nil, nil, fmt.Errorf("%w: unable to insert height", err)
425+
return nil, nil, fmt.Errorf("%w: unable to insert height", err)
426426
}
427427
if err := tsv.Insert(ctx, timestampKey, binary.BigEndian.AppendUint64(nil, uint64(timestamp))); err != nil {
428-
return nil, nil, nil, fmt.Errorf("%w: unable to insert timestamp", err)
428+
return nil, nil, fmt.Errorf("%w: unable to insert timestamp", err)
429429
}
430430
if err := tsv.Insert(ctx, feeKey, feeManager.Bytes()); err != nil {
431-
return nil, nil, nil, fmt.Errorf("%w: unable to insert fees", err)
431+
return nil, nil, fmt.Errorf("%w: unable to insert fees", err)
432432
}
433433
tsv.Commit()
434434

435435
// Fetch [parentView] root as late as possible to allow
436436
// for async processing to complete
437437
parentStateRoot, err := parentView.GetMerkleRoot(ctx)
438438
if err != nil {
439-
return nil, nil, nil, err
439+
return nil, nil, err
440440
}
441441

442442
// Calculate new view from parent and state diff
443443
view, err := createView(ctx, c.tracer, parentView, ts.ChangedKeys())
444444
if err != nil {
445-
return nil, nil, nil, err
445+
return nil, nil, err
446446
}
447447

448448
// Initialize finalized metadata fields
@@ -454,7 +454,7 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
454454
parentStateRoot,
455455
)
456456
if err != nil {
457-
return nil, nil, nil, err
457+
return nil, nil, err
458458
}
459459

460460
// Kickoff root generation
@@ -467,13 +467,14 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
467467
}
468468
c.log.Info("merkle root generated",
469469
zap.Uint64("height", blk.Hght),
470-
zap.Stringer("blkID", blk.ID()),
470+
zap.Stringer("blkID", blk.GetID()),
471471
zap.Stringer("root", root),
472472
)
473473
c.metrics.rootCalculatedCount.Inc()
474474
c.metrics.rootCalculatedSum.Add(float64(time.Since(start)))
475475
}()
476476

477+
c.metrics.txsBuilt.Add(float64(len(blockTransactions)))
477478
c.log.Info(
478479
"built block",
479480
zap.Uint64("hght", height),
@@ -485,10 +486,13 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
485486
zap.Int64("block (t)", timestamp),
486487
)
487488
execBlock := NewExecutionBlock(blk)
488-
return execBlock, &ExecutedBlock{
489-
Block: blk,
490-
Results: results,
491-
UnitPrices: feeManager.UnitPrices(),
492-
UnitsConsumed: feeManager.UnitsConsumed(),
493-
}, view, nil
489+
return execBlock, &OutputBlock{
490+
ExecutionBlock: execBlock,
491+
View: view,
492+
ExecutionResults: ExecutionResults{
493+
Results: results,
494+
UnitPrices: feeManager.UnitPrices(),
495+
UnitsConsumed: feeManager.UnitsConsumed(),
496+
},
497+
}, nil
494498
}

0 commit comments

Comments
 (0)