Skip to content

Commit

Permalink
remove withdrawal asset validataion and fuel transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Oct 14, 2023
1 parent 03be88b commit 9f1e12d
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 113 deletions.
6 changes: 2 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,8 @@ func transactionToMap(tx *common.VersionedTransaction) map[string]any {
}
if w := out.Withdrawal; w != nil {
output["withdrawal"] = map[string]any{
"chain": w.Chain,
"asset_key": w.AssetKey,
"address": w.Address,
"tag": w.Tag,
"address": w.Address,
"tag": w.Tag,
}
}
outputs = append(outputs, output)
Expand Down
11 changes: 0 additions & 11 deletions common/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,3 @@ func (a *Asset) AssetId() crypto.Hash {
panic(a.ChainId)
}
}

func (a *Asset) FeeAssetId() crypto.Hash {
switch a.ChainId {
case ethereum.EthereumChainId:
return ethereum.EthereumChainId
case bitcoin.BitcoinChainId:
return bitcoin.BitcoinChainId
default:
panic(a.ChainId)
}
}
11 changes: 0 additions & 11 deletions common/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,6 @@ func (dec *Decoder) ReadOutput() (*Output, error) {
return nil, err
} else if hw {
w := &WithdrawalData{}
err := dec.Read(w.Chain[:])
if err != nil {
return nil, err
}

ak, err := dec.ReadBytes()
if err != nil {
return nil, err
}
w.AssetKey = string(ak)

ab, err := dec.ReadBytes()
if err != nil {
return nil, err
Expand Down
4 changes: 0 additions & 4 deletions common/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ func (enc *Encoder) EncodeOutput(o *Output) {
enc.Write(null)
} else {
enc.Write(magic)
enc.Write(w.Chain[:])

enc.WriteInt(len(w.AssetKey))
enc.Write([]byte(w.AssetKey))

enc.WriteInt(len(w.Address))
enc.Write([]byte(w.Address))
Expand Down
4 changes: 1 addition & 3 deletions common/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ func TestIntEncoding(t *testing.T) {
func TestEncoding(t *testing.T) {
require := require.New(t)

raw := "777700052dc0ab2919c77daea5cfc0b37a2beea02142e8fdc4f60409fd40b256bb13ea290007eff98bbf1fd4632380b3f81bec40b54ceaa5d10e181b2c9e141da28b3d13c5460001000000000000a348712f7881be7a7bec9935d46578fd612a96e1cd0ac0f83520e2c0db0e98e2000100000000000030ad61194c5c3c19c0397d3ae98fb25ea4ead720fd49a86f2ab7b6db888ea61b00010000000000005981c0b5df48c066b4b3858ea949990c82cc27e030127d9eda70ff57fe9d9feb0001000000000000ad2fccec444b26794a13fb52f71348308de20f72a6dc4544195cef79f60910660001000000000000c817d2cac077b5ab21af4166f7451d9678a836db3f709b2903a971bf763b7f890001000000000000a4df50c83ed97db449ec856d660f4b0ef1f888bf2824800cf1bcf71418b32e580001000000000000000200a100060417bce6c80000000000000000000000000000000000000000000000000000000000000000000000000077772dc0ab2919c77daea5cfc0b37a2beea02142e8fdc4f60409fd40b256bb13ea29002465656139303061382d623332372d343838632d386438642d313432383730326665323430006b344b45397734746e65417472324257736d6877693645624231436257716779424248326f4367397677676e39346e5a5a4d6379694c7655347a596b6277703277754e4a595651556b77795a46664e3846726238345178556770673174656e574c61647834554552583270480000000000053be744043b00012f4d6a6fd5720be42930533d2efd2f5659f6179ea0e677edad599ef1dc6293b8ae2ebb91eac8ceb937f92c7dd5ffdb577b6506c6fbe0f2c7baf35d399dbc7bab0003fffe01000000000000001581a154c4107e519774e8784192b35a93ef68fff6ee0000"
raw := "777700052dc0ab2919c77daea5cfc0b37a2beea02142e8fdc4f60409fd40b256bb13ea290007eff98bbf1fd4632380b3f81bec40b54ceaa5d10e181b2c9e141da28b3d13c5460001000000000000a348712f7881be7a7bec9935d46578fd612a96e1cd0ac0f83520e2c0db0e98e2000100000000000030ad61194c5c3c19c0397d3ae98fb25ea4ead720fd49a86f2ab7b6db888ea61b00010000000000005981c0b5df48c066b4b3858ea949990c82cc27e030127d9eda70ff57fe9d9feb0001000000000000ad2fccec444b26794a13fb52f71348308de20f72a6dc4544195cef79f60910660001000000000000c817d2cac077b5ab21af4166f7451d9678a836db3f709b2903a971bf763b7f890001000000000000a4df50c83ed97db449ec856d660f4b0ef1f888bf2824800cf1bcf71418b32e580001000000000000000200a100060417bce6c8000000000000000000000000000000000000000000000000000000000000000000000000007777006b344b45397734746e65417472324257736d6877693645624231436257716779424248326f4367397677676e39346e5a5a4d6379694c7655347a596b6277703277754e4a595651556b77795a46664e3846726238345178556770673174656e574c61647834554552583270480000000000053be744043b00012f4d6a6fd5720be42930533d2efd2f5659f6179ea0e677edad599ef1dc6293b8ae2ebb91eac8ceb937f92c7dd5ffdb577b6506c6fbe0f2c7baf35d399dbc7bab0003fffe01000000000000001581a154c4107e519774e8784192b35a93ef68fff6ee0000"

val, _ := hex.DecodeString(raw)
signed, err := NewDecoder(val).DecodeTransaction()
require.Nil(err)

w := signed.Outputs[0].Withdrawal
require.NotNil(w)
require.Equal("2dc0ab2919c77daea5cfc0b37a2beea02142e8fdc4f60409fd40b256bb13ea29", w.Chain.String())
require.Equal("eea900a8-b327-488c-8d8d-1428702fe240", w.AssetKey)
require.Equal("4KE9w4tneAtr2BWsmhwi6EbB1CbWqgyBBH2oCg9vwgn94nZZMcyiLvU4zYkbwp2wuNJYVQUkwyZFfN8Frb84QxUgpg1tenWLadx4UERX2pH", w.Address)
require.Equal("", w.Tag)

Expand Down
4 changes: 0 additions & 4 deletions common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const (

OutputTypeScript = 0x00
OutputTypeWithdrawalSubmit = 0xa1
OutputTypeWithdrawalFuel = 0xa2
OutputTypeNodePledge = 0xa3
OutputTypeNodeAccept = 0xa4
outputTypeNodeResign = 0xa5
Expand All @@ -36,7 +35,6 @@ const (
TransactionTypeMint = 0x01
TransactionTypeDeposit = 0x02
TransactionTypeWithdrawalSubmit = 0x03
TransactionTypeWithdrawalFuel = 0x04
TransactionTypeWithdrawalClaim = 0x05
TransactionTypeNodePledge = 0x06
TransactionTypeNodeAccept = 0x07
Expand Down Expand Up @@ -124,8 +122,6 @@ func (tx *SignedTransaction) TransactionType() uint8 {
switch out.Type {
case OutputTypeWithdrawalSubmit:
return TransactionTypeWithdrawalSubmit
case OutputTypeWithdrawalFuel:
return TransactionTypeWithdrawalFuel
case OutputTypeWithdrawalClaim:
return TransactionTypeWithdrawalClaim
case OutputTypeNodePledge:
Expand Down
1 change: 0 additions & 1 deletion common/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (tx *VersionedTransaction) UnspentOutputs() []*UTXOWithLock {
OutputTypeNodeCancel,
OutputTypeNodeAccept,
OutputTypeNodeRemove,
OutputTypeWithdrawalFuel,
OutputTypeWithdrawalClaim,
OutputTypeCustodianUpdateNodes:
case OutputTypeWithdrawalSubmit,
Expand Down
3 changes: 0 additions & 3 deletions common/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func (ver *VersionedTransaction) Validate(store DataStore, snapTime uint64, fork
return tx.validateDeposit(store, ver.PayloadHash(), ver.SignaturesMap, snapTime)
case TransactionTypeWithdrawalSubmit:
return tx.validateWithdrawalSubmit(inputsFilter)
case TransactionTypeWithdrawalFuel:
return tx.validateWithdrawalFuel(store, inputsFilter)
case TransactionTypeWithdrawalClaim:
return tx.validateWithdrawalClaim(store, inputsFilter, snapTime)
case TransactionTypeNodePledge:
Expand Down Expand Up @@ -267,7 +265,6 @@ func (tx *Transaction) validateOutputs(store GhostLocker, hash crypto.Hash, fork

switch o.Type {
case OutputTypeWithdrawalSubmit,
OutputTypeWithdrawalFuel,
OutputTypeWithdrawalClaim,
OutputTypeNodePledge,
OutputTypeNodeCancel,
Expand Down
70 changes: 2 additions & 68 deletions common/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@ import (

"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/domains/bitcoin"
"github.com/MixinNetwork/mixin/domains/ethereum"
)

type WithdrawalData struct {
Chain crypto.Hash
AssetKey string
Address string
Tag string
}

func (w *WithdrawalData) Asset() *Asset {
return &Asset{
ChainId: w.Chain,
AssetKey: w.AssetKey,
}
Address string
Tag string
}

func (tx *Transaction) validateWithdrawalSubmit(inputs map[string]*UTXO) error {
Expand All @@ -45,13 +34,6 @@ func (tx *Transaction) validateWithdrawalSubmit(inputs map[string]*UTXO) error {
return fmt.Errorf("invalid withdrawal submit data")
}

if err := submit.Withdrawal.Asset().Verify(); err != nil {
return fmt.Errorf("invalid asset data %s", err.Error())
}
if id := submit.Withdrawal.Asset().AssetId(); id != tx.Asset {
return fmt.Errorf("invalid asset %s %s", tx.Asset, id)
}

if len(submit.Keys) != 0 {
return fmt.Errorf("invalid withdrawal submit keys %d", len(submit.Keys))
}
Expand All @@ -62,54 +44,6 @@ func (tx *Transaction) validateWithdrawalSubmit(inputs map[string]*UTXO) error {
return fmt.Errorf("invalid withdrawal submit mask %s", submit.Mask)
}

chainId := submit.Withdrawal.Asset().ChainId
switch chainId {
case ethereum.EthereumChainId:
return ethereum.VerifyAddress(submit.Withdrawal.Address)
case bitcoin.BitcoinChainId:
return bitcoin.VerifyAddress(submit.Withdrawal.Address)
}
return fmt.Errorf("invalid withdrawal chain id %s", chainId)
}

func (tx *Transaction) validateWithdrawalFuel(store DataStore, inputs map[string]*UTXO) error {
for _, in := range inputs {
if in.Type != OutputTypeScript {
return fmt.Errorf("invalid utxo type %d", in.Type)
}
}

if len(tx.Outputs) > 2 {
return fmt.Errorf("invalid outputs count %d for withdrawal fuel transaction", len(tx.Outputs))
}
if len(tx.Outputs) == 2 && tx.Outputs[1].Type != OutputTypeScript {
return fmt.Errorf("invalid change type %d for withdrawal fuel transaction", tx.Outputs[1].Type)
}

fuel := tx.Outputs[0]
if fuel.Type != OutputTypeWithdrawalFuel {
return fmt.Errorf("invalid output type %d for withdrawal fuel transaction", fuel.Type)
}

var hash crypto.Hash
if len(tx.Extra) != len(hash) {
return fmt.Errorf("invalid extra %d for withdrawal fuel transaction", len(tx.Extra))
}
copy(hash[:], tx.Extra)
submit, _, err := store.ReadTransaction(hash)
if err != nil {
return err
}
if submit == nil {
return fmt.Errorf("invalid withdrawal submit data")
}
withdrawal := submit.Outputs[0].Withdrawal
if withdrawal == nil || submit.Outputs[0].Type != OutputTypeWithdrawalSubmit {
return fmt.Errorf("invalid withdrawal submit data")
}
if id := withdrawal.Asset().FeeAssetId(); id != tx.Asset {
return fmt.Errorf("invalid fee asset %s %s", tx.Asset, id)
}
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,8 @@ func transactionToMap(tx *common.VersionedTransaction) map[string]any {
}
if w := out.Withdrawal; w != nil {
output["withdrawal"] = map[string]any{
"chain": w.Chain,
"asset_key": w.AssetKey,
"address": w.Address,
"tag": w.Tag,
"address": w.Address,
"tag": w.Tag,
}
}
outputs = append(outputs, output)
Expand Down

0 comments on commit 9f1e12d

Please sign in to comment.