github.com/annchain/OG@v0.0.9/arefactor_core/core/receipt.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package core 15 16 import ( 17 "fmt" 18 ogTypes "github.com/annchain/OG/arefactor/og_interface" 19 "github.com/annchain/commongo/marshaller" 20 ) 21 22 type ReceiptStatus uint8 23 24 const ( 25 ReceiptStatusSuccess ReceiptStatus = iota 26 ReceiptStatusVMFailed 27 ReceiptStatusUnknownTxType 28 ReceiptStatusFailed 29 ) 30 31 //go:generate msgp 32 33 //msgp:tuple Receipt 34 type Receipt struct { 35 TxHash ogTypes.Hash 36 Status ReceiptStatus 37 ProcessResult string 38 ContractAddress ogTypes.Address 39 } 40 41 func NewReceipt(hash ogTypes.Hash, status ReceiptStatus, pResult string, addr ogTypes.Address) *Receipt { 42 return &Receipt{ 43 TxHash: hash, 44 Status: status, 45 ProcessResult: pResult, 46 ContractAddress: addr, 47 } 48 } 49 50 func (r *Receipt) ToJsonMap() map[string]interface{} { 51 jm := make(map[string]interface{}) 52 jm["hash"] = r.TxHash.Hex() 53 jm["status"] = fmt.Sprintf("%d", r.Status) 54 jm["result"] = r.ProcessResult 55 jm["contractAddress"] = r.ContractAddress.Hex() 56 57 return jm 58 } 59 60 /** 61 marshalling part 62 */ 63 64 func (r *Receipt) MarshalMsg() ([]byte, error) { 65 var err error 66 b := make([]byte, marshaller.HeaderSize) 67 68 // Hash TxHash 69 b, err = marshaller.AppendIMarshaller(b, r.TxHash) 70 if err != nil { 71 return nil, err 72 } 73 // uint8 Status 74 b = append(b, byte(r.Status)) 75 // string ProcessResult 76 b = marshaller.AppendString(b, r.ProcessResult) 77 // Address ContractAddress 78 b, err = marshaller.AppendIMarshaller(b, r.ContractAddress) 79 if err != nil { 80 return nil, err 81 } 82 83 b = marshaller.FillHeaderData(b) 84 return b, nil 85 } 86 87 func (r *Receipt) UnmarshalMsg(b []byte) ([]byte, error) { 88 b, _, err := marshaller.DecodeHeader(b) 89 if err != nil { 90 return nil, err 91 } 92 93 r.TxHash, b, err = ogTypes.UnmarshalHash(b) 94 if err != nil { 95 return nil, err 96 } 97 98 r.Status = ReceiptStatus(b[0]) 99 b = b[1:] 100 101 r.ProcessResult, b, err = marshaller.ReadString(b) 102 if err != nil { 103 return nil, err 104 } 105 106 r.ContractAddress, b, err = ogTypes.UnmarshalAddress(b) 107 if err != nil { 108 return nil, err 109 } 110 111 return b, nil 112 } 113 114 func (r *Receipt) MsgSize() int { 115 size := 0 116 117 size += marshaller.CalIMarshallerSize(r.TxHash.MsgSize()) + 1 + // TxHash + Status 118 marshaller.CalStringSize(r.ProcessResult) + // ProcessResult 119 marshaller.CalIMarshallerSize(r.ContractAddress.MsgSize()) // ContractAddress 120 121 return size 122 } 123 124 //msgp:tuple ReceiptSet 125 type ReceiptSet map[ogTypes.HashKey]*Receipt