|
| 1 | +package block_parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "das_database/config" |
| 6 | + "das_database/dao" |
| 7 | + "fmt" |
| 8 | + "github.com/dotbitHQ/das-lib/common" |
| 9 | + "github.com/dotbitHQ/das-lib/witness" |
| 10 | + "github.com/nervosnetwork/ckb-sdk-go/address" |
| 11 | + "strconv" |
| 12 | +) |
| 13 | + |
| 14 | +func (b *BlockParser) ActionEditDidCellRecords(req FuncTransactionHandleReq) (resp FuncTransactionHandleResp) { |
| 15 | + if isCV, err := isCurrentVersionTx(req.Tx, common.DasContractNameDidCellType); err != nil { |
| 16 | + resp.Err = fmt.Errorf("isCurrentVersion err: %s", err.Error()) |
| 17 | + return |
| 18 | + } else if !isCV { |
| 19 | + log.Warn("not current version edit didcell records tx") |
| 20 | + return |
| 21 | + } |
| 22 | + log.Info("ActionEditDidCellRecords:", req.BlockNumber, req.TxHash, req.Action) |
| 23 | + |
| 24 | + txDidEntity, err := witness.TxToDidEntity(req.Tx) |
| 25 | + if err != nil { |
| 26 | + resp.Err = fmt.Errorf("witness.TxToDidEntity err: %s", err.Error()) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + account, _, err := witness.GetAccountAndExpireFromDidCellData(req.Tx.OutputsData[txDidEntity.Outputs[0].Target.Index]) |
| 31 | + if err != nil { |
| 32 | + resp.Err = fmt.Errorf("config.GetDidCellAccountAndExpire err: %s", err.Error()) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + //account := didCellData.Account |
| 37 | + accountId := common.Bytes2Hex(common.GetAccountIdByAccount(account)) |
| 38 | + var recordsInfos []dao.TableRecordsInfo |
| 39 | + recordList := txDidEntity.Outputs[0].DidCellWitnessDataV0.Records |
| 40 | + for _, v := range recordList { |
| 41 | + recordsInfos = append(recordsInfos, dao.TableRecordsInfo{ |
| 42 | + AccountId: accountId, |
| 43 | + Account: account, |
| 44 | + Key: v.Key, |
| 45 | + Type: v.Type, |
| 46 | + Label: v.Label, |
| 47 | + Value: v.Value, |
| 48 | + Ttl: strconv.FormatUint(uint64(v.TTL), 10), |
| 49 | + }) |
| 50 | + } |
| 51 | + oldDidCellOutpoint := common.OutPointStruct2String(req.Tx.Inputs[txDidEntity.Inputs[0].Target.Index].PreviousOutput) |
| 52 | + var didCellInfo dao.TableDidCellInfo |
| 53 | + didCellInfo.AccountId = accountId |
| 54 | + didCellInfo.BlockNumber = req.BlockNumber |
| 55 | + didCellInfo.Outpoint = common.OutPoint2String(req.Tx.Hash.Hex(), uint(txDidEntity.Outputs[0].Target.Index)) |
| 56 | + |
| 57 | + mode := address.Mainnet |
| 58 | + if config.Cfg.Server.Net != common.DasNetTypeMainNet { |
| 59 | + mode = address.Testnet |
| 60 | + } |
| 61 | + anyLockAddr, err := address.ConvertScriptToAddress(mode, req.Tx.Outputs[txDidEntity.Outputs[0].Target.Index].Lock) |
| 62 | + if err != nil { |
| 63 | + resp.Err = fmt.Errorf("address.ConvertScriptToAddress err: %s", err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + txInfo := dao.TableTransactionInfo{ |
| 67 | + BlockNumber: req.BlockNumber, |
| 68 | + AccountId: accountId, |
| 69 | + Account: account, |
| 70 | + Action: common.DidCellActionEditRecords, |
| 71 | + ServiceType: dao.ServiceTypeRegister, |
| 72 | + ChainType: common.ChainTypeAnyLock, |
| 73 | + Address: anyLockAddr, |
| 74 | + Capacity: 0, |
| 75 | + Outpoint: didCellInfo.Outpoint, |
| 76 | + BlockTimestamp: req.BlockTimestamp, |
| 77 | + } |
| 78 | + |
| 79 | + if err := b.dbDao.CreateDidCellRecordsInfos(oldDidCellOutpoint, didCellInfo, recordsInfos, txInfo); err != nil { |
| 80 | + log.Error("CreateDidCellRecordsInfos err:", err.Error()) |
| 81 | + resp.Err = fmt.Errorf("CreateDidCellRecordsInfos err: %s", err.Error()) |
| 82 | + } |
| 83 | + |
| 84 | + return |
| 85 | +} |
| 86 | + |
| 87 | +func (b *BlockParser) ActionEditDidCellOwner(req FuncTransactionHandleReq) (resp FuncTransactionHandleResp) { |
| 88 | + if isCV, err := isCurrentVersionTx(req.Tx, common.DasContractNameDidCellType); err != nil { |
| 89 | + resp.Err = fmt.Errorf("isCurrentVersion err: %s", err.Error()) |
| 90 | + return |
| 91 | + } else if !isCV { |
| 92 | + log.Warn("not current version edit didcell owner") |
| 93 | + return |
| 94 | + } |
| 95 | + log.Info("ActionEditDidCellOwner:", req.BlockNumber, req.TxHash, req.Action) |
| 96 | + |
| 97 | + txDidEntity, err := witness.TxToDidEntity(req.Tx) |
| 98 | + if err != nil { |
| 99 | + resp.Err = fmt.Errorf("TxToDidEntity err: %s", err.Error()) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + account, _, err := witness.GetAccountAndExpireFromDidCellData(req.Tx.OutputsData[txDidEntity.Outputs[0].Target.Index]) |
| 104 | + if err != nil { |
| 105 | + resp.Err = fmt.Errorf("config.GetDidCellAccountAndExpire err: %s", err.Error()) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + didCellArgs := common.Bytes2Hex(req.Tx.Outputs[txDidEntity.Outputs[0].Target.Index].Lock.Args) |
| 110 | + accountId := common.Bytes2Hex(common.GetAccountIdByAccount(account)) |
| 111 | + didCellInfo := dao.TableDidCellInfo{ |
| 112 | + BlockNumber: req.BlockNumber, |
| 113 | + Outpoint: common.OutPoint2String(req.TxHash, uint(txDidEntity.Outputs[0].Target.Index)), |
| 114 | + AccountId: accountId, |
| 115 | + Args: didCellArgs, |
| 116 | + LockCodeHash: req.Tx.Outputs[txDidEntity.Outputs[0].Target.Index].Lock.CodeHash.Hex(), |
| 117 | + } |
| 118 | + |
| 119 | + oldOutpoint := common.OutPointStruct2String(req.Tx.Inputs[txDidEntity.Inputs[0].Target.Index].PreviousOutput) |
| 120 | + |
| 121 | + preInput := req.Tx.Inputs[txDidEntity.Inputs[0].Target.Index].PreviousOutput |
| 122 | + preTx, err := b.dasCore.Client().GetTransaction(context.Background(), preInput.TxHash) |
| 123 | + if err != nil { |
| 124 | + resp.Err = fmt.Errorf("GetTransaction err: %s", err.Error()) |
| 125 | + return |
| 126 | + } |
| 127 | + mode := address.Mainnet |
| 128 | + if config.Cfg.Server.Net != common.DasNetTypeMainNet { |
| 129 | + mode = address.Testnet |
| 130 | + } |
| 131 | + anyLockAddr, err := address.ConvertScriptToAddress(mode, preTx.Transaction.Outputs[preInput.Index].Lock) |
| 132 | + if err != nil { |
| 133 | + resp.Err = fmt.Errorf("address.ConvertScriptToAddress err: %s", err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + txInfo := dao.TableTransactionInfo{ |
| 137 | + BlockNumber: req.BlockNumber, |
| 138 | + AccountId: accountId, |
| 139 | + Account: account, |
| 140 | + Action: common.DidCellActionEditOwner, |
| 141 | + ServiceType: dao.ServiceTypeRegister, |
| 142 | + ChainType: common.ChainTypeAnyLock, |
| 143 | + Address: anyLockAddr, |
| 144 | + Capacity: 0, |
| 145 | + Outpoint: didCellInfo.Outpoint, |
| 146 | + BlockTimestamp: req.BlockTimestamp, |
| 147 | + } |
| 148 | + |
| 149 | + var recordsInfos []dao.TableRecordsInfo |
| 150 | + recordList := txDidEntity.Outputs[0].DidCellWitnessDataV0.Records |
| 151 | + for _, v := range recordList { |
| 152 | + recordsInfos = append(recordsInfos, dao.TableRecordsInfo{ |
| 153 | + AccountId: accountId, |
| 154 | + Account: account, |
| 155 | + Key: v.Key, |
| 156 | + Type: v.Type, |
| 157 | + Label: v.Label, |
| 158 | + Value: v.Value, |
| 159 | + Ttl: strconv.FormatUint(uint64(v.TTL), 10), |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + if err := b.dbDao.EditDidCellOwner(oldOutpoint, didCellInfo, txInfo, recordsInfos); err != nil { |
| 164 | + log.Error("EditDidCellOwner err:", err.Error()) |
| 165 | + resp.Err = fmt.Errorf("EditDidCellOwner err: %s", err.Error()) |
| 166 | + } |
| 167 | + return |
| 168 | +} |
| 169 | + |
| 170 | +func (b *BlockParser) ActionDidCellRecycle(req FuncTransactionHandleReq) (resp FuncTransactionHandleResp) { |
| 171 | + didEntity, err := witness.TxToOneDidEntity(req.Tx, witness.SourceTypeInputs) |
| 172 | + if err != nil { |
| 173 | + resp.Err = fmt.Errorf("TxToOneDidEntity err: %s", err.Error()) |
| 174 | + return |
| 175 | + } |
| 176 | + preTx, err := b.dasCore.Client().GetTransaction(b.ctx, req.Tx.Inputs[didEntity.Target.Index].PreviousOutput.TxHash) |
| 177 | + if err != nil { |
| 178 | + resp.Err = fmt.Errorf("GetTransaction err: %s", err.Error()) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + if isCV, err := isCurrentVersionTx(preTx.Transaction, common.DasContractNameDidCellType); err != nil { |
| 183 | + resp.Err = fmt.Errorf("isCurrentVersion err: %s", err.Error()) |
| 184 | + return |
| 185 | + } else if !isCV { |
| 186 | + log.Warn("not current version didcell recycle") |
| 187 | + return |
| 188 | + } |
| 189 | + log.Info("ActionDidCellRecycle:", req.BlockNumber, req.TxHash, req.Action) |
| 190 | + account, _, err := witness.GetAccountAndExpireFromDidCellData(preTx.Transaction.OutputsData[req.Tx.Inputs[didEntity.Target.Index].PreviousOutput.Index]) |
| 191 | + if err != nil { |
| 192 | + resp.Err = fmt.Errorf("config.GetDidCellAccountAndExpire err: %s", err.Error()) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + accountId := common.Bytes2Hex(common.GetAccountIdByAccount(account)) |
| 197 | + oldOutpoint := common.OutPointStruct2String(req.Tx.Inputs[0].PreviousOutput) |
| 198 | + |
| 199 | + mode := address.Mainnet |
| 200 | + if config.Cfg.Server.Net != common.DasNetTypeMainNet { |
| 201 | + mode = address.Testnet |
| 202 | + } |
| 203 | + anyLockAddr, err := address.ConvertScriptToAddress(mode, preTx.Transaction.Outputs[req.Tx.Inputs[didEntity.Target.Index].PreviousOutput.Index].Lock) |
| 204 | + if err != nil { |
| 205 | + resp.Err = fmt.Errorf("address.ConvertScriptToAddress err: %s", err.Error()) |
| 206 | + return |
| 207 | + } |
| 208 | + txInfo := dao.TableTransactionInfo{ |
| 209 | + BlockNumber: req.BlockNumber, |
| 210 | + AccountId: accountId, |
| 211 | + Account: account, |
| 212 | + Action: common.DidCellActionRecycle, |
| 213 | + ServiceType: dao.ServiceTypeRegister, |
| 214 | + ChainType: common.ChainTypeAnyLock, |
| 215 | + Address: anyLockAddr, |
| 216 | + Capacity: 0, |
| 217 | + Outpoint: common.OutPoint2String(req.TxHash, 0), |
| 218 | + BlockTimestamp: req.BlockTimestamp, |
| 219 | + } |
| 220 | + |
| 221 | + if err := b.dbDao.DidCellRecycle(oldOutpoint, accountId, txInfo); err != nil { |
| 222 | + log.Error("DidCellRecycle err:", err.Error()) |
| 223 | + resp.Err = fmt.Errorf("DidCellRecycle err: %s", err.Error()) |
| 224 | + } |
| 225 | + return |
| 226 | +} |
0 commit comments