Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion apps/solana/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ func ExtractTransfersFromTransaction(ctx context.Context, tx *solana.Transaction
innerInstructions[inner.Index] = sis
}

for _, balance := range meta.PreTokenBalances {
bs := meta.PreTokenBalances
bs = append(bs, meta.PostTokenBalances...)
for _, balance := range bs {
if account, err := msg.Account(balance.AccountIndex); err == nil {
tokenAccounts[account] = token.Account{
Owner: *balance.Owner,
Expand Down Expand Up @@ -465,6 +467,36 @@ func ExtractTransfersFromTransaction(ctx context.Context, tx *solana.Transaction
return transfers, nil
}

func ExtractTransferFromTransactionByIndex(ctx context.Context, tx *solana.Transaction, meta *rpc.TransactionMeta, index int64) *Transfer {
if meta.Err != nil {
panic(fmt.Sprint(meta.Err))
}
msg := tx.Message

var (
tokenAccounts = map[solana.PublicKey]token.Account{}
owners = []*solana.PublicKey{}
)

bs := meta.PreTokenBalances
bs = append(bs, meta.PostTokenBalances...)
for _, balance := range bs {
if account, err := msg.Account(balance.AccountIndex); err == nil {
tokenAccounts[account] = token.Account{
Owner: *balance.Owner,
Mint: balance.Mint,
}
if !slices.ContainsFunc(owners, func(owner *solana.PublicKey) bool {
return owner.Equals(*balance.Owner)
}) {
owners = append(owners, balance.Owner)
}
}
}

return extractTransfersFromInstruction(&msg, msg.Instructions[index], tokenAccounts, owners, nil)
}

func ExtractMintsFromTransaction(tx *solana.Transaction) []string {
var assets []string
for index, ix := range tx.Message.Instructions {
Expand Down
2 changes: 1 addition & 1 deletion cmd/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ComputerBootCmd(c *cli.Context) error {
return err
}
defer kd.Close()
computer := computer.NewNode(kd, group, messenger, mc.Computer, client, mw)
computer := computer.NewNode(kd, group, messenger, mc, client, mw)
computer.Boot(ctx, version)

if mmc := mc.Computer.MonitorConversationId; mmc != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func bundleComputerState(ctx context.Context, node *computer.Node, mixin *mixin.
state = state + fmt.Sprintf("💍 Payer %s Balance: %s SOL\n", node.SolanaPayer(), b)

limit := mc.NewIntegerFromString("0.5")
if xinBalance.Cmp(limit) < 0 || solBalance.Cmp(limit) < 0 || mc.NewIntegerFromString(b).Cmp(limit) < 0 {
if node.Prod() && (xinBalance.Cmp(limit) < 0 || solBalance.Cmp(limit) < 0 || mc.NewIntegerFromString(b).Cmp(limit) < 0) {
state = state + "@40518661\n"
}

Expand Down
14 changes: 3 additions & 11 deletions config/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import (
"net/http"
_ "net/http/pprof"

computer "github.com/MixinNetwork/computer/solana"
"github.com/MixinNetwork/mixin/logger"
)

const MainNetworkName = "main"
const TestNetworkName = "test"

type DevConfig struct {
ProfilePort int `toml:"profile-port"`
LogLevel int `toml:"log-level"`
Network string `toml:"network"`
}

func handleDevConfig(c *DevConfig) {
func handleDevConfig(c *computer.DevConfig) {
logger.SetLevel(logger.INFO)
if c == nil {
return
Expand All @@ -28,6 +20,6 @@ func handleDevConfig(c *DevConfig) {
go http.ListenAndServe(l, http.DefaultServeMux)
}
if c.Network == "" {
c.Network = MainNetworkName
c.Network = computer.MainNetworkName
}
}
9 changes: 2 additions & 7 deletions config/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import (
"github.com/pelletier/go-toml"
)

type Configuration struct {
Computer *computer.Configuration `toml:"computer"`
Dev *DevConfig `toml:"dev"`
}

func ReadConfiguration(path, role string) (*Configuration, error) {
func ReadConfiguration(path, role string) (*computer.Config, error) {
if strings.HasPrefix(path, "~/") {
usr, _ := user.Current()
path = filepath.Join(usr.HomeDir, (path)[2:])
Expand All @@ -24,7 +19,7 @@ func ReadConfiguration(path, role string) (*Configuration, error) {
if err != nil {
return nil, err
}
var conf Configuration
var conf computer.Config
err = toml.Unmarshal(f, &conf)
if err != nil {
return nil, err
Expand Down
32 changes: 26 additions & 6 deletions solana/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestCompaction(t *testing.T) {
if i == 2 {
require.Equal("", ar.Compaction)
require.Len(ar.Transactions, 1)
require.Len(call.GetRefundIds(), 1)
} else {
require.Equal(mtg.StorageAssetId, ar.Compaction)
require.Len(ar.Transactions, 0)
Expand All @@ -173,6 +174,22 @@ func TestDepositCompaction(t *testing.T) {
err = node.store.LockNonceAccountWithMix(ctx, nonce.Address, user.MixAddress)
require.Nil(err)

now := time.Now()
id := uuid.Must(uuid.NewV4()).String()
for _, node := range nodes {
call := &store.SystemCall{
RequestId: id,
Superior: id,
Type: store.CallTypeDeposit,
State: common.RequestStateDone,
Hash: sql.NullString{Valid: true, String: "6b33cca6e650a1c2abe4122a466eb7d02f7faa47ee935c80536178bacd913a56"},
CreatedAt: now,
UpdatedAt: now,
}
err := node.store.TestWriteCall(ctx, call)
require.Nil(err)
}

sequence += 10
out, err := testWriteOutputForNodes(ctx, mds, conf.AppId, mtg.StorageAssetId, "", "", sequence, decimal.RequireFromString("0.90432841"))
out.OutputId = "329346e1-34c2-4de0-8e35-729518eda8bd"
Expand All @@ -195,14 +212,18 @@ func TestDepositCompaction(t *testing.T) {
sequence += 100
_, err = testWriteOutputForNodes(ctx, mds, conf.AppId, mtg.StorageAssetId, "", "", uint64(sequence), decimal.RequireFromString("0.36"))
require.Nil(err)
a.Sequence = sequence

out.Sequence = sequence
for _, node := range nodes {
a.TestAttachActionToGroup(node.group)
txs, compaction := node.processDeposit(ctx, a)
txs, compaction := node.processDeposit(ctx, a, i > 0)
if i == 2 {
require.Len(txs, 1)
require.Equal("", compaction)

call, err := node.store.ReadSystemCallByRequestId(ctx, id, 0)
require.Nil(err)
require.Len(call.GetRefundIds(), 1)
} else {
require.Len(txs, 0)
require.Equal(mtg.StorageAssetId, compaction)
Expand Down Expand Up @@ -307,6 +328,7 @@ func testObserverConfirmPostProcessCall(ctx context.Context, require *require.As
call, err := node.store.ReadSystemCallByRequestId(ctx, sub.Superior, common.RequestStateDone)
require.Nil(err)
require.NotNil(call)
require.Len(call.GetRefundIds(), 1)

ar, _, err := node.store.ReadActionResult(ctx, nid, nid)
require.Nil(err)
Expand Down Expand Up @@ -915,9 +937,7 @@ func testPrepare(require *require.Assertions, signerTest bool) (context.Context,

func testBuildNode(ctx context.Context, require *require.Assertions, root string, i int) (*Node, *mtg.SQLite3Store) {
f, _ := os.ReadFile("../config/example.toml")
var conf struct {
Computer *Configuration `toml:"computer"`
}
var conf Config
err := toml.Unmarshal(f, &conf)
require.Nil(err)

Expand Down Expand Up @@ -948,7 +968,7 @@ func testBuildNode(ctx context.Context, require *require.Assertions, root string
require.Nil(err)
group.EnableDebug()

node := NewNode(kd, group, nil, conf.Computer, nil, nil)
node := NewNode(kd, group, nil, &conf, nil, nil)
group.AttachWorker(node.conf.AppId, node)
return node, md
}
Expand Down
4 changes: 2 additions & 2 deletions solana/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (node *Node) processAction(ctx context.Context, out *mtg.Action) ([]*mtg.Tr

isDeposit := node.verifyKernelTransaction(ctx, out)
if isDeposit {
return node.processDeposit(ctx, out)
return node.processDeposit(ctx, out, out.Restored())
}

req, err := node.parseRequest(out)
Expand All @@ -58,7 +58,7 @@ func (node *Node) processAction(ctx context.Context, out *mtg.Action) ([]*mtg.Tr
}
if ar != nil {
if req.Restored && ar.Compaction != "" {
err = node.store.ResetRequest(ctx, req)
err = node.store.ResetRequest(ctx, req.Id, req.Sequence)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions solana/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,10 @@ func buildSystemCallView(call *store.SystemCall) map[string]any {
"state": state,
"hash": call.Hash.String,
}
uid := ""
if call.Type == store.CallTypeMain {
uid = call.UserIdFromPublicPath()
v["user_id"] = call.UserIdFromPublicPath()
v["refund_traces"] = call.GetRefundIds()
}
v["user_id"] = uid
return v
}

Expand Down
11 changes: 11 additions & 0 deletions solana/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"github.com/MixinNetwork/safe/mtg"
)

type Config struct {
Computer *Configuration `toml:"computer"`
Dev *DevConfig `toml:"dev"`
}

type DevConfig struct {
ProfilePort int `toml:"profile-port"`
LogLevel int `toml:"log-level"`
Network string `toml:"network"`
}

type Configuration struct {
AppId string `toml:"app-id"`
StoreDir string `toml:"store-dir"`
Expand Down
Loading
Loading