forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx_execute.go
91 lines (77 loc) · 1.95 KB
/
tx_execute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"github.com/MetalBlockchain/metalgo/chains/atomic"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/vms/avm/txs"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
)
var _ txs.Visitor = (*executeTx)(nil)
type executeTx struct {
tx *txs.Tx
batch database.Batch
sharedMemory atomic.SharedMemory
parser txs.Parser
}
func (et *executeTx) BaseTx(*txs.BaseTx) error {
return et.batch.Write()
}
func (et *executeTx) ImportTx(t *txs.ImportTx) error {
utxoIDs := make([][]byte, len(t.ImportedIns))
for i, in := range t.ImportedIns {
inputID := in.UTXOID.InputID()
utxoIDs[i] = inputID[:]
}
return et.sharedMemory.Apply(
map[ids.ID]*atomic.Requests{
t.SourceChain: {
RemoveRequests: utxoIDs,
},
},
et.batch,
)
}
func (et *executeTx) ExportTx(t *txs.ExportTx) error {
txID := et.tx.ID()
elems := make([]*atomic.Element, len(t.ExportedOuts))
codec := et.parser.Codec()
for i, out := range t.ExportedOuts {
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(len(t.Outs) + i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
}
utxoBytes, err := codec.Marshal(txs.CodecVersion, utxo)
if err != nil {
return err
}
inputID := utxo.InputID()
elem := &atomic.Element{
Key: inputID[:],
Value: utxoBytes,
}
if out, ok := utxo.Out.(avax.Addressable); ok {
elem.Traits = out.Addresses()
}
elems[i] = elem
}
return et.sharedMemory.Apply(
map[ids.ID]*atomic.Requests{
t.DestinationChain: {
PutRequests: elems,
},
},
et.batch,
)
}
func (et *executeTx) CreateAssetTx(t *txs.CreateAssetTx) error {
return et.BaseTx(&t.BaseTx)
}
func (et *executeTx) OperationTx(t *txs.OperationTx) error {
return et.BaseTx(&t.BaseTx)
}