Skip to content

Commit

Permalink
Merge pull request #2757 from dessaya/fix-utxos
Browse files Browse the repository at this point in the history
Fix utxos
  • Loading branch information
dessaya authored Jul 28, 2023
2 parents 42fc64c + 1676c9a commit 881524e
Show file tree
Hide file tree
Showing 38 changed files with 365 additions and 160 deletions.
2 changes: 2 additions & 0 deletions packages/apilib/deploychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/iotaledger/wasp/packages/origin"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/registry"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
)

// TODO DeployChain on peering domain, not on committee
Expand Down Expand Up @@ -99,6 +100,7 @@ func CreateChainOrigin(
initParams,
utxoMap,
utxoIDsFromUtxoMap(utxoMap),
allmigrations.DefaultScheme.LatestSchemaVersion(),
)
if err != nil {
return isc.ChainID{}, fmt.Errorf("CreateChainOrigin: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions packages/chain/cons/bp/bp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
"github.com/iotaledger/wasp/packages/vm/gas"
)

Expand All @@ -40,6 +41,7 @@ func TestOffLedgerOrdering(t *testing.T) {
nil,
outputs,
outIDs,
allmigrations.DefaultScheme.LatestSchemaVersion(),
)
require.NoError(t, err)
stateAnchor, aliasOutput, err := transaction.GetAnchorFromTransaction(originTX)
Expand Down
2 changes: 2 additions & 0 deletions packages/chain/cons/cons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/vm/core/accounts"
"github.com/iotaledger/wasp/packages/vm/core/coreprocessors"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
"github.com/iotaledger/wasp/packages/vm/processors"
"github.com/iotaledger/wasp/packages/vm/vmimpl"
)
Expand Down Expand Up @@ -91,6 +92,7 @@ func testConsBasic(t *testing.T, n, f int) {
nil,
outputs,
outIDs,
allmigrations.DefaultScheme.LatestSchemaVersion(),
)
require.NoError(t, err)
stateAnchor, aliasOutput, err := transaction.GetAnchorFromTransaction(originTX)
Expand Down
6 changes: 5 additions & 1 deletion packages/kv/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ func (m *ImmutableMap) Len() uint32 {

// Erase the map.
func (m *Map) Erase() {
var keys [][]byte
m.IterateKeys(func(elemKey []byte) bool {
m.DelAt(elemKey)
keys = append(keys, elemKey)
return true
})
for _, k := range keys {
m.DelAt(k)
}
}

// Iterate non-deterministic
Expand Down
10 changes: 5 additions & 5 deletions packages/origin/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/iotaledger/wasp/packages/vm/core/evm/evmimpl"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/core/governance/governanceimpl"
"github.com/iotaledger/wasp/packages/vm/core/migrations"
"github.com/iotaledger/wasp/packages/vm/core/root"
"github.com/iotaledger/wasp/packages/vm/core/root/rootimpl"
"github.com/iotaledger/wasp/packages/vm/gas"
Expand Down Expand Up @@ -114,11 +113,11 @@ func InitChainByAliasOutput(chainStore state.Store, aliasOutput *isc.AliasOutput
return originBlock, nil
}

func calcStateMetadata(initParams dict.Dict, commonAccountAmount uint64) []byte {
func calcStateMetadata(initParams dict.Dict, commonAccountAmount uint64, schemaVersion uint32) []byte {
s := transaction.NewStateMetadata(
L1Commitment(initParams, commonAccountAmount),
gas.DefaultFeePolicy(),
migrations.BaseSchemaVersion+uint32(len(migrations.Migrations)),
schemaVersion,
"",
)
return s.Bytes()
Expand All @@ -134,6 +133,7 @@ func NewChainOriginTransaction(
initParams dict.Dict,
unspentOutputs iotago.OutputSet,
unspentOutputIDs iotago.OutputIDs,
schemaVersion uint32,
) (*iotago.Transaction, *iotago.AliasOutput, isc.ChainID, error) {
if len(unspentOutputs) != len(unspentOutputIDs) {
panic("mismatched lengths of outputs and inputs slices")
Expand All @@ -151,7 +151,7 @@ func NewChainOriginTransaction(

aliasOutput := &iotago.AliasOutput{
Amount: deposit,
StateMetadata: calcStateMetadata(initParams, deposit), // NOTE: Updated bellow.
StateMetadata: calcStateMetadata(initParams, deposit, schemaVersion), // NOTE: Updated below.
Conditions: iotago.UnlockConditions{
&iotago.StateControllerAddressUnlockCondition{Address: stateControllerAddress},
&iotago.GovernorAddressUnlockCondition{Address: governanceControllerAddress},
Expand All @@ -167,7 +167,7 @@ func NewChainOriginTransaction(
aliasOutput.Amount = minAmount
}
// update the L1 commitment to not include the minimumSD
aliasOutput.StateMetadata = calcStateMetadata(initParams, aliasOutput.Amount-minSD)
aliasOutput.StateMetadata = calcStateMetadata(initParams, aliasOutput.Amount-minSD, schemaVersion)

txInputs, remainderOutput, err := transaction.ComputeInputsAndRemainder(
walletAddr,
Expand Down
12 changes: 4 additions & 8 deletions packages/origin/origin_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package origin_test

import (
"bytes"
"encoding/hex"
"testing"

Expand All @@ -20,7 +19,7 @@ import (
"github.com/iotaledger/wasp/packages/testutil/utxodb"
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/core/migrations"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
"github.com/iotaledger/wasp/packages/vm/gas"
)

Expand Down Expand Up @@ -67,6 +66,7 @@ func TestCreateOrigin(t *testing.T) {
nil,
allOutputs,
ids,
allmigrations.DefaultScheme.LatestSchemaVersion(),
)
require.NoError(t, err)

Expand Down Expand Up @@ -103,15 +103,11 @@ func TestCreateOrigin(t *testing.T) {
governance.DefaultMinBaseTokensOnCommonAccount,
),
gas.DefaultFeePolicy(),
migrations.BaseSchemaVersion+uint32(len(migrations.Migrations)),
allmigrations.DefaultScheme.LatestSchemaVersion(),
"",
)

require.True(t,
bytes.Equal(
originStateMetadata.Bytes(),
anchor.StateData),
)
require.EqualValues(t, anchor.StateData, originStateMetadata.Bytes())

// only one output is expected in the ledger under the address of chainID
outs, ids := u.GetUnspentOutputs(chainID.AsAddress())
Expand Down
1 change: 1 addition & 0 deletions packages/solo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (ch *Chain) runTaskNoLock(reqs []isc.Request, estimateGas bool) *vm.VMTaskR
// state baseline is always valid in Solo
EnableGasBurnLogging: true,
EstimateGasMode: estimateGas,
MigrationsOverride: ch.migrationScheme,
}

res, err := vmimpl.Run(task)
Expand Down
8 changes: 8 additions & 0 deletions packages/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm/core/coreprocessors"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/core/migrations"
"github.com/iotaledger/wasp/packages/vm/processors"
_ "github.com/iotaledger/wasp/packages/vm/sandbox"
"github.com/iotaledger/wasp/packages/vm/vmtypes"
Expand Down Expand Up @@ -116,6 +117,8 @@ type Chain struct {
RequestsBlock uint32

metrics *metrics.ChainMetrics

migrationScheme *migrations.MigrationScheme
}

var _ chain.ChainCore = &Chain{}
Expand Down Expand Up @@ -278,6 +281,7 @@ func (env *Solo) deployChain(
initParams,
outs,
outIDs,
0,
)
require.NoError(env.T, err)

Expand Down Expand Up @@ -469,6 +473,10 @@ func (ch *Chain) collateAndRunBatch() {
}
}

func (ch *Chain) AddMigration(m migrations.Migration) {
ch.migrationScheme.Migrations = append(ch.migrationScheme.Migrations, m)
}

func (ch *Chain) GetCandidateNodes() []*governance.AccessNodeInfo {
panic("unimplemented")
}
Expand Down
2 changes: 0 additions & 2 deletions packages/solo/solotest/solo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,4 @@ func TestLoadSnapshot(t *testing.T) {
nativeTokenID, err := ch.GetNativeTokenIDByFoundrySN(1)
require.NoError(t, err)
ch.AssertL2NativeTokens(ch.OriginatorAgentID, nativeTokenID, 1000)

require.NotEmpty(t, ch.L2NFTs(ch.OriginatorAgentID))
}
3 changes: 1 addition & 2 deletions packages/testutil/dummystatemetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package testutil
import (
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/vm/core/migrations"
"github.com/iotaledger/wasp/packages/vm/gas"
)

func DummyStateMetadata(commitment *state.L1Commitment) *transaction.StateMetadata {
return transaction.NewStateMetadata(
commitment,
gas.DefaultFeePolicy(),
migrations.BaseSchemaVersion+uint32(len(migrations.Migrations)),
0,
"",
)
}
2 changes: 2 additions & 0 deletions packages/testutil/testchain/test_chain_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/iotaledger/wasp/packages/testutil/utxodb"
"github.com/iotaledger/wasp/packages/transaction"
"github.com/iotaledger/wasp/packages/vm/core/accounts"
"github.com/iotaledger/wasp/packages/vm/core/migrations/allmigrations"
"github.com/iotaledger/wasp/packages/vm/core/root"
"github.com/iotaledger/wasp/packages/vm/gas"
)
Expand Down Expand Up @@ -57,6 +58,7 @@ func (tcl *TestChainLedger) MakeTxChainOrigin(committeeAddress iotago.Address) (
nil,
outs,
outIDs,
allmigrations.DefaultScheme.LatestSchemaVersion(),
)
require.NoError(tcl.t, err)
stateAnchor, aliasOutput, err := transaction.GetAnchorFromTransaction(originTX)
Expand Down
2 changes: 1 addition & 1 deletion packages/testutil/testdbhash/TestStorageContract.hex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x7e9abb191e14b1426edc8a1c2ed107a638b47cba392f2ec9cd34246e7d049115
0x1b4cea9b2a3cb5d9c9e21b37e3c03468f5b07048c5fec76b4c7e7e5d6b89b3a3
34 changes: 26 additions & 8 deletions packages/vm/core/accounts/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/iotaledger/wasp/packages/kv/collections"
)

func newFoundriesArray(state kv.KVStore) *collections.Array {
return collections.NewArray(state, keyNewFoundries)
}

func accountFoundriesMap(state kv.KVStore, agentID isc.AgentID) *collections.Map {
return collections.NewMap(state, foundriesMapKey(agentID))
}
Expand All @@ -16,7 +20,7 @@ func accountFoundriesMapR(state kv.KVStoreReader, agentID isc.AgentID) *collecti
return collections.NewMapReadOnly(state, foundriesMapKey(agentID))
}

func allFoundriesMap(state kv.KVStore) *collections.Map {
func AllFoundriesMap(state kv.KVStore) *collections.Map {
return collections.NewMap(state, keyFoundryOutputRecords)
}

Expand All @@ -27,25 +31,39 @@ func allFoundriesMapR(state kv.KVStoreReader) *collections.ImmutableMap {
// SaveFoundryOutput stores foundry output into the map of all foundry outputs (compressed form)
func SaveFoundryOutput(state kv.KVStore, f *iotago.FoundryOutput, blockIndex uint32, outputIndex uint16) {
foundryRec := foundryOutputRec{
// TransactionID is unknown yet, will be filled next block
OutputID: iotago.OutputIDFromTransactionIDAndIndex(iotago.TransactionID{}, outputIndex),
Amount: f.Amount,
TokenScheme: f.TokenScheme,
Metadata: []byte{},
BlockIndex: blockIndex,
OutputIndex: outputIndex,
}
allFoundriesMap(state).SetAt(codec.EncodeUint32(f.SerialNumber), foundryRec.Bytes())
AllFoundriesMap(state).SetAt(codec.EncodeUint32(f.SerialNumber), foundryRec.Bytes())
newFoundriesArray(state).Push(codec.EncodeUint32(f.SerialNumber))
}

func updateFoundryOutputIDs(state kv.KVStore, anchorTxID iotago.TransactionID) {
newFoundries := newFoundriesArray(state)
allFoundries := AllFoundriesMap(state)
n := newFoundries.Len()
for i := uint32(0); i < n; i++ {
k := newFoundries.GetAt(i)
rec := mustFoundryOutputRecFromBytes(allFoundries.GetAt(k))
rec.OutputID = iotago.OutputIDFromTransactionIDAndIndex(anchorTxID, rec.OutputID.Index())
allFoundries.SetAt(k, rec.Bytes())
}
newFoundries.Erase()
}

// DeleteFoundryOutput deletes foundry output from the map of all foundries
func DeleteFoundryOutput(state kv.KVStore, sn uint32) {
allFoundriesMap(state).DelAt(codec.EncodeUint32(sn))
AllFoundriesMap(state).DelAt(codec.EncodeUint32(sn))
}

// GetFoundryOutput returns foundry output, its block number and output index
func GetFoundryOutput(state kv.KVStoreReader, sn uint32, chainID isc.ChainID) (*iotago.FoundryOutput, uint32, uint16) {
func GetFoundryOutput(state kv.KVStoreReader, sn uint32, chainID isc.ChainID) (*iotago.FoundryOutput, iotago.OutputID) {
data := allFoundriesMapR(state).GetAt(codec.EncodeUint32(sn))
if data == nil {
return nil, 0, 0
return nil, iotago.OutputID{}
}
rec := mustFoundryOutputRecFromBytes(data)

Expand All @@ -59,7 +77,7 @@ func GetFoundryOutput(state kv.KVStoreReader, sn uint32, chainID isc.ChainID) (*
},
Features: nil,
}
return ret, rec.BlockIndex, rec.OutputIndex
return ret, rec.OutputID
}

// hasFoundry checks if specific account owns the foundry
Expand Down
9 changes: 3 additions & 6 deletions packages/vm/core/accounts/foundryoutputrec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

// foundryOutputRec contains information to reconstruct output
type foundryOutputRec struct {
BlockIndex uint32
OutputIndex uint16
OutputID iotago.OutputID
Amount uint64 // always storage deposit
TokenScheme iotago.TokenScheme
Metadata []byte
Expand All @@ -35,8 +34,7 @@ func mustFoundryOutputRecFromBytes(data []byte) *foundryOutputRec {

func (rec *foundryOutputRec) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rec.BlockIndex = rr.ReadUint32()
rec.OutputIndex = rr.ReadUint16()
rr.ReadN(rec.OutputID[:])
rec.Amount = rr.ReadUint64()
tokenScheme := rr.ReadBytes()
if rr.Err == nil {
Expand All @@ -48,8 +46,7 @@ func (rec *foundryOutputRec) Read(r io.Reader) error {

func (rec *foundryOutputRec) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteUint32(rec.BlockIndex)
ww.WriteUint16(rec.OutputIndex)
ww.WriteN(rec.OutputID[:])
ww.WriteUint64(rec.Amount)
if ww.Err == nil {
tokenScheme := codec.EncodeTokenScheme(rec.TokenScheme)
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/core/accounts/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func foundryDestroy(ctx isc.Sandbox) dict.Dict {
panic(vm.ErrUnauthorized)
}

out, _, _ := GetFoundryOutput(state, sn, ctx.ChainID())
out, _ := GetFoundryOutput(state, sn, ctx.ChainID())
simpleTokenScheme := util.MustTokenScheme(out.TokenScheme)
if !util.IsZeroBigInt(big.NewInt(0).Sub(simpleTokenScheme.MintedTokens, simpleTokenScheme.MeltedTokens)) {
panic(errFoundryWithCirculatingSupply)
Expand Down Expand Up @@ -286,7 +286,7 @@ func foundryModifySupply(ctx isc.Sandbox) dict.Dict {
panic(vm.ErrUnauthorized)
}

out, _, _ := GetFoundryOutput(state, sn, ctx.ChainID())
out, _ := GetFoundryOutput(state, sn, ctx.ChainID())
nativeTokenID, err := out.NativeTokenID()
ctx.RequireNoError(err, "internal")

Expand Down
2 changes: 1 addition & 1 deletion packages/vm/core/accounts/impl_views.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func viewFoundryOutput(ctx isc.SandboxView) dict.Dict {
ctx.Log().Debugf("accounts.viewFoundryOutput")

sn := ctx.Params().MustGetUint32(ParamFoundrySN)
out, _, _ := GetFoundryOutput(ctx.StateR(), sn, ctx.ChainID())
out, _ := GetFoundryOutput(ctx.StateR(), sn, ctx.ChainID())
if out == nil {
panic(errFoundryNotFound)
}
Expand Down
Loading

0 comments on commit 881524e

Please sign in to comment.