Skip to content

Commit

Permalink
Add AVM state.Chain interface (ava-labs#2411)
Browse files Browse the repository at this point in the history
Co-authored-by: Chloe <[email protected]>
  • Loading branch information
StephenButtolph and coffeeavax authored Jan 12, 2023
1 parent 5b19d55 commit 166fe00
Show file tree
Hide file tree
Showing 22 changed files with 1,110 additions and 799 deletions.
129 changes: 0 additions & 129 deletions database/mockdb/db.go

This file was deleted.

78 changes: 0 additions & 78 deletions database/mockdb/db_test.go

This file was deleted.

26 changes: 26 additions & 0 deletions vms/avm/blocks/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ package blocks

import (
"fmt"
"reflect"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/avm/txs"
Expand Down Expand Up @@ -49,6 +52,29 @@ func NewParser(fxs []fxs.Fx) (Parser, error) {
}, errs.Err
}

func NewCustomParser(
typeToFxIndex map[reflect.Type]int,
clock *mockable.Clock,
log logging.Logger,
fxs []fxs.Fx,
) (Parser, error) {
p, err := txs.NewCustomParser(typeToFxIndex, clock, log, fxs)
if err != nil {
return nil, err
}
c := p.CodecRegistry()
gc := p.GenesisCodecRegistry()

errs := wrappers.Errs{}
errs.Add(
c.RegisterType(&StandardBlock{}),
gc.RegisterType(&StandardBlock{}),
)
return &parser{
Parser: p,
}, errs.Err
}

func (p *parser) ParseBlock(bytes []byte) (Block, error) {
return parse(p.Codec(), bytes)
}
Expand Down
16 changes: 4 additions & 12 deletions vms/avm/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestIndexTransaction_Ordered(t *testing.T) {
utxo := buildPlatformUTXO(utxoID, txAssetID, addr)

// save utxo to state
if err := vm.state.PutUTXO(utxo); err != nil {
t.Fatal("Error saving utxo", err)
}
vm.state.AddUTXO(utxo)

// issue transaction
if _, err := vm.IssueTx(tx.Bytes()); err != nil {
Expand Down Expand Up @@ -168,9 +166,7 @@ func TestIndexTransaction_MultipleTransactions(t *testing.T) {
utxo := buildPlatformUTXO(utxoID, txAssetID, addr)

// save utxo to state
if err := vm.state.PutUTXO(utxo); err != nil {
t.Fatal("Error saving utxo", err)
}
vm.state.AddUTXO(utxo)

// issue transaction
if _, err := vm.IssueTx(tx.Bytes()); err != nil {
Expand Down Expand Up @@ -264,9 +260,7 @@ func TestIndexTransaction_MultipleAddresses(t *testing.T) {
utxo := buildPlatformUTXO(utxoID, txAssetID, addr)

// save utxo to state
if err := vm.state.PutUTXO(utxo); err != nil {
t.Fatal("Error saving utxo", err)
}
vm.state.AddUTXO(utxo)

var inputUTXOs []*avax.UTXO //nolint:prealloc
for _, utxoID := range tx.Unsigned.InputUTXOs() {
Expand Down Expand Up @@ -325,9 +319,7 @@ func TestIndexTransaction_UnorderedWrites(t *testing.T) {
utxo := buildPlatformUTXO(utxoID, txAssetID, addr)

// save utxo to state
if err := vm.state.PutUTXO(utxo); err != nil {
t.Fatal("Error saving utxo", err)
}
vm.state.AddUTXO(utxo)

// issue transaction
if _, err := vm.IssueTx(tx.Bytes()); err != nil {
Expand Down
33 changes: 16 additions & 17 deletions vms/avm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ func TestServiceGetBalanceStrict(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(twoOfTwoUTXO)
require.NoError(t, err)
vm.state.AddUTXO(twoOfTwoUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs := &GetBalanceArgs{
Expand Down Expand Up @@ -349,8 +349,8 @@ func TestServiceGetBalanceStrict(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(oneOfTwoUTXO)
require.NoError(t, err)
vm.state.AddUTXO(oneOfTwoUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs = &GetBalanceArgs{
Expand Down Expand Up @@ -396,8 +396,8 @@ func TestServiceGetBalanceStrict(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(futureUTXO)
require.NoError(t, err)
vm.state.AddUTXO(futureUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs = &GetBalanceArgs{
Expand Down Expand Up @@ -500,8 +500,8 @@ func TestServiceGetAllBalances(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(twoOfTwoUTXO)
require.NoError(t, err)
vm.state.AddUTXO(twoOfTwoUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs := &GetAllBalancesArgs{
Expand Down Expand Up @@ -542,8 +542,8 @@ func TestServiceGetAllBalances(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(oneOfTwoUTXO)
require.NoError(t, err)
vm.state.AddUTXO(oneOfTwoUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs = &GetAllBalancesArgs{
Expand Down Expand Up @@ -587,8 +587,8 @@ func TestServiceGetAllBalances(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(futureUTXO)
require.NoError(t, err)
vm.state.AddUTXO(futureUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs = &GetAllBalancesArgs{
Expand Down Expand Up @@ -630,8 +630,8 @@ func TestServiceGetAllBalances(t *testing.T) {
},
}
// Insert the UTXO
err = vm.state.PutUTXO(otherAssetUTXO)
require.NoError(t, err)
vm.state.AddUTXO(otherAssetUTXO)
require.NoError(t, vm.state.Commit())

// Check the balance with IncludePartial set to true
balanceArgs = &GetAllBalancesArgs{
Expand Down Expand Up @@ -1829,10 +1829,9 @@ func TestServiceGetUTXOs(t *testing.T) {
},
},
}
if err := vm.state.PutUTXO(utxo); err != nil {
t.Fatal(err)
}
vm.state.AddUTXO(utxo)
}
require.NoError(t, vm.state.Commit())

sm := m.NewSharedMemory(constants.PlatformChainID)

Expand Down
Loading

0 comments on commit 166fe00

Please sign in to comment.