[#68] Use unified format for transferX details

Unified format uses transfer type as the first byte
and extra details next. List of transfer types used in
contracts defined in `transfer.go`. It includes:
- mint,
- burn,
- lock,
- unlock,
- container fee.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-04-07 09:17:00 +03:00 committed by Alex Vanin
parent 6ff45edbc9
commit f884e3d665
3 changed files with 52 additions and 15 deletions

View file

@ -136,7 +136,7 @@ func TransferX(from, to interop.Hash160, amount int, details []byte) bool {
return result return result
} }
func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool { func Lock(txDetails []byte, from, to interop.Hash160, amount, until int) bool {
ctx := storage.GetContext() ctx := storage.GetContext()
multiaddr := common.AlphabetAddress() multiaddr := common.AlphabetAddress()
@ -151,14 +151,16 @@ func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool {
} }
common.SetSerialized(ctx, to, lockAccount) common.SetSerialized(ctx, to, lockAccount)
result := token.transfer(ctx, from, to, amount, true, lockTransferMsg) details := common.LockTransferDetails(txDetails)
result := token.transfer(ctx, from, to, amount, true, details)
if !result { if !result {
// consider using `return false` to remove votes // consider using `return false` to remove votes
panic("lock: can't lock funds") panic("lock: can't lock funds")
} }
runtime.Log("lock: created lock account") runtime.Log("lock: created lock account")
runtime.Notify("Lock", txID, from, to, amount, until) runtime.Notify("Lock", txDetails, from, to, amount, until)
return true return true
} }
@ -184,15 +186,16 @@ func NewEpoch(epochNum int) bool {
} }
if epochNum >= acc.Until { if epochNum >= acc.Until {
details := common.UnlockTransferDetails(epochNum)
// return assets back to the parent // return assets back to the parent
token.transfer(ctx, addr, acc.Parent, acc.Balance, true, unlockTransferMsg) token.transfer(ctx, addr, acc.Parent, acc.Balance, true, details)
} }
} }
return true return true
} }
func Mint(to interop.Hash160, amount int, details []byte) bool { func Mint(to interop.Hash160, amount int, txDetails []byte) bool {
ctx := storage.GetContext() ctx := storage.GetContext()
multiaddr := common.AlphabetAddress() multiaddr := common.AlphabetAddress()
@ -200,6 +203,8 @@ func Mint(to interop.Hash160, amount int, details []byte) bool {
panic("mint: this method must be invoked from inner ring") panic("mint: this method must be invoked from inner ring")
} }
details := common.MintTransferDetails(txDetails)
ok := token.transfer(ctx, nil, to, amount, true, details) ok := token.transfer(ctx, nil, to, amount, true, details)
if !ok { if !ok {
panic("mint: can't transfer assets") panic("mint: can't transfer assets")
@ -214,7 +219,7 @@ func Mint(to interop.Hash160, amount int, details []byte) bool {
return true return true
} }
func Burn(from interop.Hash160, amount int, details []byte) bool { func Burn(from interop.Hash160, amount int, txDetails []byte) bool {
ctx := storage.GetContext() ctx := storage.GetContext()
multiaddr := common.AlphabetAddress() multiaddr := common.AlphabetAddress()
@ -222,6 +227,8 @@ func Burn(from interop.Hash160, amount int, details []byte) bool {
panic("burn: this method must be invoked from inner ring") panic("burn: this method must be invoked from inner ring")
} }
details := common.BurnTransferDetails(txDetails)
ok := token.transfer(ctx, from, nil, amount, true, details) ok := token.transfer(ctx, from, nil, amount, true, details)
if !ok { if !ok {
panic("burn: can't transfer assets") panic("burn: can't transfer assets")

34
common/transfer.go Normal file
View file

@ -0,0 +1,34 @@
package common
var (
mintPrefix = []byte{0x01}
burnPrefix = []byte{0x02}
lockPrefix = []byte{0x03}
unlockPrefix = []byte{0x04}
containerFeePrefix = []byte{0x10}
)
func WalletToScriptHash(wallet []byte) []byte {
return wallet[1 : len(wallet)-4]
}
func MintTransferDetails(txDetails []byte) []byte {
return append(mintPrefix, txDetails...)
}
func BurnTransferDetails(txDetails []byte) []byte {
return append(burnPrefix, txDetails...)
}
func LockTransferDetails(txDetails []byte) []byte {
return append(lockPrefix, txDetails...)
}
func UnlockTransferDetails(epoch int) []byte {
var buf interface{} = epoch
return append(unlockPrefix, buf.([]byte)...)
}
func ContainerFeeTransferDetails(cid []byte) []byte {
return append(containerFeePrefix, cid...)
}

View file

@ -50,8 +50,7 @@ const (
) )
var ( var (
containerFeeTransferMsg = []byte("container creation fee") eACLPrefix = []byte("eACL")
eACLPrefix = []byte("eACL")
) )
func Init(owner, addrNetmap, addrBalance, addrID interop.Hash160) { func Init(owner, addrNetmap, addrBalance, addrID interop.Hash160) {
@ -111,10 +110,11 @@ func Put(container []byte, signature interop.Signature, publicKey interop.Public
return true return true
} }
from := walletToScriptHash(ownerID) from := common.WalletToScriptHash(ownerID)
netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160) netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160)
balanceContractAddr := storage.Get(ctx, balanceContractKey).(interop.Hash160) balanceContractAddr := storage.Get(ctx, balanceContractKey).(interop.Hash160)
containerFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, containerFeeKey).(int) containerFee := contract.Call(netmapContractAddr, "config", contract.ReadOnly, containerFeeKey).(int)
details := common.ContainerFeeTransferDetails(containerID)
// todo: check if new container with unique container id // todo: check if new container with unique container id
@ -128,7 +128,7 @@ func Put(container []byte, signature interop.Signature, publicKey interop.Public
from, from,
to, to,
containerFee, containerFee,
containerFeeTransferMsg, // consider add container id to the message details,
) )
if !tx.(bool) { if !tx.(bool) {
panic("put: can't transfer assets for container creation") panic("put: can't transfer assets for container creation")
@ -454,10 +454,6 @@ func getEACL(ctx storage.Context, cid []byte) extendedACL {
return extendedACL{val: []byte{}, sig: interop.Signature{}, pub: interop.PublicKey{}} return extendedACL{val: []byte{}, sig: interop.Signature{}, pub: interop.PublicKey{}}
} }
func walletToScriptHash(wallet []byte) []byte {
return wallet[1 : len(wallet)-4]
}
func verifySignature(msg []byte, sig interop.Signature, keys []interop.PublicKey) bool { func verifySignature(msg []byte, sig interop.Signature, keys []interop.PublicKey) bool {
for i := range keys { for i := range keys {
key := keys[i] key := keys[i]
@ -495,7 +491,7 @@ func isSignedByOwnerKey(msg []byte, sig interop.Signature, owner []byte, key int
} }
func isOwnerFromKey(owner []byte, key interop.PublicKey) bool { func isOwnerFromKey(owner []byte, key interop.PublicKey) bool {
ownerSH := walletToScriptHash(owner) ownerSH := common.WalletToScriptHash(owner)
keySH := contract.CreateStandardAccount(key) keySH := contract.CreateStandardAccount(key)
return common.BytesEqual(ownerSH, keySH) return common.BytesEqual(ownerSH, keySH)