Skip to content

Commit

Permalink
fix: adding locks to accounts in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Aug 30, 2024
1 parent f9a9d93 commit e6911d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
6 changes: 6 additions & 0 deletions components/ledger/internal/machine/vm/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/binary"
"fmt"
"math/big"
"slices"

"github.com/formancehq/ledger/internal/machine"

Expand Down Expand Up @@ -574,6 +575,9 @@ func (m *Machine) ResolveResources(ctx context.Context, store Store) ([]string,
if err != nil {
return nil, nil, err
}
if val.GetType() == machine.TypeAccount {
involvedAccountsMap[machine.Address(idx)] = string(val.(machine.AccountAddress))
}
case program.VariableAccountBalance:
acc, _ := m.getResource(res.Account)
address := string((*acc).(machine.AccountAddress))
Expand Down Expand Up @@ -617,6 +621,8 @@ func (m *Machine) ResolveResources(ctx context.Context, store Store) ([]string,
writeLockAccounts = append(writeLockAccounts, involvedAccountsMap[machineAddress])
}

slices.Sort(readLockAccounts)
slices.Sort(writeLockAccounts)
return readLockAccounts, writeLockAccounts, nil
}

Expand Down
43 changes: 37 additions & 6 deletions components/ledger/internal/machine/vm/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,6 @@ func TestNeededBalances2(t *testing.T) {
}

m := NewMachine(*p)
if err != nil {
t.Fatalf("did not expect error on SetVars, got: %v", err)
}
_, involvedSources, err := m.ResolveResources(context.Background(), EmptyStore)
require.NoError(t, err)
require.Equal(t, []string{"a"}, involvedSources)
Expand All @@ -1058,9 +1055,6 @@ send $balance (
}

m := NewMachine(*p)
if err != nil {
t.Fatalf("did not expect error on SetVars, got: %v", err)
}
rlAccounts, wlAccounts, err := m.ResolveResources(context.Background(), EmptyStore)
require.NoError(t, err)
require.Equal(t, []string{"a"}, wlAccounts)
Expand All @@ -1072,6 +1066,43 @@ send $balance (
require.Equal(t, []string{"acc", "a"}, store.RequestedAccounts)
}

func TestNeededBalancesBalanceOfMeta(t *testing.T) {
p, err := compiler.Compile(`vars {
account $src = meta(@x, "k")
}
send [COIN 1] (
source = $src
destination = @dest
)`)

if err != nil {
t.Fatalf("did not expect error on Compile, got: %v", err)
}
m := NewMachine(*p)

staticStore := StaticStore{
"x": &AccountWithBalances{
Account: ledger.Account{
Address: "x",
Metadata: metadata.Metadata{
"k": "src",
},
},
Balances: map[string]*big.Int{},
},
}
rlAccounts, wlAccounts, err := m.ResolveResources(context.Background(), staticStore)
require.NoError(t, err)
require.Equal(t, []string{"src"}, wlAccounts)
require.Equal(t, []string{"dest"}, rlAccounts)

store := mockStore{}
err = m.ResolveBalances(context.Background(), &store)
require.NoError(t, err)
require.Equal(t, []string{"src"}, store.RequestedAccounts)
}

func TestSetTxMeta(t *testing.T) {
p, err := compiler.Compile(`
set_tx_meta("aaa", @platform)
Expand Down

0 comments on commit e6911d3

Please sign in to comment.