Skip to content

Commit

Permalink
Merge pull request #852 from ava-labs/pre-migration
Browse files Browse the repository at this point in the history
Pre migration
  • Loading branch information
StephenButtolph authored May 21, 2021
2 parents 9584187 + 1212f76 commit d0c6a17
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
16 changes: 12 additions & 4 deletions snow/engine/avalanche/state/unique_vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,26 @@ func newUniqueVertex(s *Serializer, b []byte) (*uniqueVertex, error) {
if err := innerVertex.Verify(); err != nil {
return nil, err
}

unparsedTxs := innerVertex.Txs()
txs := make([]snowstorm.Tx, len(unparsedTxs))
for i, txBytes := range unparsedTxs {
tx, err := vtx.serializer.vm.ParseTx(txBytes)
if err != nil {
return nil, err
}
txs[i] = tx
}

vtx.v.vtx = innerVertex
vtx.v.txs = txs

// If the vertex has already been fetched,
// skip persisting the vertex.
if vtx.v.status.Fetched() {
return vtx, nil
}

if _, err := vtx.Txs(); err != nil {
return nil, err
}

// The vertex is newly parsed, so set the status
// and persist it.
vtx.v.status = choices.Processing
Expand Down
55 changes: 51 additions & 4 deletions snow/engine/avalanche/state/unique_vertex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowstorm"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/utils/hashing"
)

func newSerializer(t *testing.T, parse func([]byte) (snowstorm.Tx, error)) *Serializer {
Expand Down Expand Up @@ -281,12 +282,58 @@ func TestParseVertexWithInvalidTxs(t *testing.T) {
}
vtxBytes := statelessVertex.Bytes()

s := newSerializer(t, func([]byte) (snowstorm.Tx, error) {
return nil, errors.New("invalid tx")
s := newSerializer(t, func(b []byte) (snowstorm.Tx, error) {
switch {
case bytes.Equal(b, []byte{1}):
return nil, errors.New("invalid tx")
case bytes.Equal(b, []byte{2}):
return &snowstorm.TestTx{}, nil
default:
return nil, errors.New("invalid tx")
}
})

_, err = s.ParseVtx(vtxBytes)
if err == nil {
if _, err := s.ParseVtx(vtxBytes); err == nil {
t.Fatal("should have failed to parse the vertex due to invalid transactions")
}

if _, err := s.ParseVtx(vtxBytes); err == nil {
t.Fatal("should have failed to parse the vertex after previously error on parsing invalid transactions")
}

vtxID := hashing.ComputeHash256Array(vtxBytes)
if _, err := s.GetVtx(vtxID); err == nil {
t.Fatal("should have failed to lookup invalid vertex after previously error on parsing invalid transactions")
}

childStatelessVertex, err := vertex.Build(
ctx.ChainID,
1,
0,
[]ids.ID{vtxID},
[][]byte{{2}},
nil,
)
if err != nil {
t.Fatal(err)
}
childVtxBytes := childStatelessVertex.Bytes()

childVtx, err := s.ParseVtx(childVtxBytes)
if err != nil {
t.Fatal(err)
}

parents, err := childVtx.Parents()
if err != nil {
t.Fatal(err)
}
if len(parents) != 1 {
t.Fatal("wrong number of parents")
}
parent := parents[0]

if parent.Status().Fetched() {
t.Fatal("the parent is invalid, so it shouldn't be marked as fetched")
}
}

0 comments on commit d0c6a17

Please sign in to comment.