Commit 363b6ec1 authored by wuxinyang's avatar wuxinyang

编写交易from标记并测试

parent a1c42c3a
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b5e0db37-7f3c-47e2-965e-a9d467d53f11" name="Default Changelist" comment="" />
<list default="true" id="b5e0db37-7f3c-47e2-965e-a9d467d53f11" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/core/transcut/transactionMark.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core/transcut/transactionMark_test.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......@@ -25,10 +28,21 @@
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="go.tried.to.enable.integration.vgo.integrator" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/core" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />
<property name="node.js.path.for.package.tslint" value="project" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/core" />
</key>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
......
package transcut
import (
"go-ethereum-advance/common"
"go-ethereum-advance/core/types"
"sync"
)
//type ContractItem interface {
// SetElement(txHash common.Hash,addr common.Address)
// GetKey() common.Hash
// UpdateRecord(addr string)
// GetRecordMarkValue(addr string) uint32
// GetAddress() common.Address
//}
//
//type TransactionMarkMap struct {
// tsMarkMap map[common.Hash]*list.Element
// tsList *list.List
//}
//
//func NewTransactionMarkMap() *TransactionMarkMap {
// return &TransactionMarkMap{
// tsMarkMap: make(map[common.Hash]*list.Element),
// tsList: list.New(),
// }
//}
//
///**批次交易的标记记录**/
//func (t *TransactionMarkMap) Exists(data ContractItem) bool {
// _,exist := t.tsMarkMap[data.GetKey()]
// return exist
//}
//
///**向标记记录里面添加数据项**/
//func (t *TransactionMarkMap) Push(data ContractItem) {
// ele := t.tsList.PushBack(data)
// t.tsMarkMap[data.GetKey()] = ele
//}
//
///**向标记记录里面删除数据项**/
//func (t *TransactionMarkMap) Remove(data ContractItem) {
// t.tsList.Remove(t.tsMarkMap[data.GetKey()])
// delete(t.tsMarkMap,data.GetKey())
//}
//
//func (t *TransactionMarkMap) CleanUp() {
// t.tsList= list.New()
// t.tsMarkMap = make(map[common.Hash]*list.Element)
//}
//
//func (t *TransactionMarkMap) Size() int {
// return t.tsList.Len()
//}
//
//func (t *TransactionMarkMap) Walk(cb func(data ContractItem)) {
// for ele := t.tsList.Front();ele != nil;ele = ele.Next() {
// cb(ele.Value.(ContractItem))
// }
//}
//
//type Element struct {
// txHash common.Hash
// address common.Address
// record map[string]uint32
// lock sync.RWMutex
//}
//
//func NewElement() *Element {
// return &Element{
// record: make(map[string]uint32),
// }
//}
//
//func (e *Element) GetKey() common.Hash {
// txData := e.txHash.Bytes()
// addData := e.address.Bytes()
// data := BytesCombine(txData,addData)
// return common.BytesToHash(data)
//}
//
//func (e *Element) GetAddress() common.Address{
// return e.address
//}
//
//func BytesCombine(pBytes ...[]byte) []byte {
// length := len(pBytes)
// s := make([][]byte,length)
// for ind :=0;ind< length;ind++ {
// s[ind] = pBytes[ind]
// }
// sep := []byte("")
// return bytes.Join(s,sep)
//}
//
///**设置值**/
//func (e *Element) SetElement(txHash common.Hash,addr common.Address) {
// e.txHash = txHash
// e.address = addr
//}
//
///**更新合约地址下的标记数值**/
//func (e *Element) UpdateRecord(addr string) {
// e.lock.Lock()
// defer e.lock.Unlock()
// value,ok := e.record[addr]
// if ok {
// value++
// }else {
// value = 1
// }
// e.record[addr] = value
//}
//
///**获取对应合约地址下的标记数值**/
//func (e *Element) GetRecordMarkValue(addr string) uint32 {
// e.lock.RLock()
// defer e.lock.RUnlock()
// value,ok := e.record[addr]
// if ok {
// return value
// }
// return 0
//}
/**from的标记**/
type Item struct {
tx *types.Transaction
mark uint32
}
type FromMarkCache struct {
fromCache map[common.Address][]*Item
toCache map[common.Address]struct{}
maxLevel uint32
lock sync.RWMutex
}
func NewFromMarkCache() *FromMarkCache{
return &FromMarkCache{
fromCache: make(map[common.Address][]*Item),
toCache: make(map[common.Address]struct{}),
}
}
/**放入到标记档中标记无需设置
0x0001 -> 0x0002 0
0x0001 -> 0x0003 1
0x0004 -> 0x0001 2
-------------------------------
0x0001 -> 0x0002 0
0x0002 -> 0x0003 1
0x0001 -> 0x0004 2
-------------------------------
0x0001 -> 0x0002 0
0x0003 -> 0x0005 0
0x0004 -> 0x0006 0
0x0002 -> 0x0003 1
**/
func (f *FromMarkCache) SetMarkCache(addr common.Address,data *Item) {
f.lock.Lock()
defer f.lock.Unlock()
v,ok := f.fromCache[addr]
dataTo := data.tx.To()
if ok {
data.mark = f.maxLevel+1
f.maxLevel = data.mark
v = append(v,data)
f.toCache[*dataTo] = struct{}{}
}else {
v = make([]*Item,0)
//如果交易的from不在cache中
//1.需要判断to是否在以前交易的from中
//2.需要判断to是否在以前交易的to中
//3.如果都没有就则标记应为0
_,ok = f.fromCache[*dataTo]
if ok {
data.mark = f.maxLevel+1
f.maxLevel = data.mark
f.toCache[*dataTo] = struct{}{}
}else {
_,ok = f.toCache[*dataTo]
if ok {
data.mark = f.maxLevel+1
f.maxLevel = data.mark
}
f.toCache[*dataTo] = struct{}{}
}
v = append(v,data)
}
f.fromCache[addr] = v
}
/**找到某个层级的所有交易**/
func (f *FromMarkCache) GetLevelTxsWithMark(index uint32) []*types.Transaction {
f.lock.RLock()
defer f.lock.RUnlock()
arr := make([]*types.Transaction,0)
for _,v := range f.fromCache{
for _,data := range v{
if data.mark == index {
arr = append(arr,data.tx)
}
}
}
return arr
}
func (f *FromMarkCache) GetMaxLevel() uint32 {
return f.maxLevel+1
}
package transcut
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
"testing"
)
/*func TestNewTransactionMarkMap(t *testing.T) {
tsMarkMap := NewTransactionMarkMap()
item := NewElement()
item.address = common.Address{0x0001}
item.txHash = common.Hash{0x0002}
cAddr := common.Address{0x00004}
item.UpdateRecord(cAddr.Hex())
cAddr1 := common.Address{0x0005}
item.UpdateRecord(cAddr1.Hex())
cAddr2 := common.Address{0x0007}
item.UpdateRecord(cAddr2.Hex())
tsMarkMap.Push(item)
v := tsMarkMap.Size()
println("v::",v)
tsMarkMap.Walk(func(data ContractItem) {
v := data.GetRecordMarkValue(cAddr2.Hex())
println("v:",v)
})
}
*/
func TestNewFromMarkCache(t *testing.T) {
//addr := common.HexToAddress("0x00001")
//tx1 := types.NewTransaction(1,addr,big.NewInt(20),20,big.NewInt(20),nil)
//
//addr1 := common.HexToAddress("0x00002")
//tx2 := types.NewTransaction(1,addr1,big.NewInt(20),20,big.NewInt(30),nil)
//
//addr3 := common.HexToAddress("0x00003")
//tx3 := types.NewTransaction(1,addr3,big.NewInt(20),20,big.NewInt(20),nil)
//
//tx4 := types.NewTransaction(2,addr1,big.NewInt(20),20,big.NewInt(20),nil)
//
//tx5 := types.NewTransaction(3,addr3,big.NewInt(20),20,big.NewInt(30),nil)
//
//addr4 := common.HexToAddress("0x00004")
//
//tx6 := types.NewTransaction(1,addr4,big.NewInt(30),30,big.NewInt(30),nil)
//
//tx7 := types.NewTransaction(2,addr4,big.NewInt(25),29,big.NewInt(30),nil)
//
//tx8 := types.NewTransaction(2,addr,big.NewInt(30),10,big.NewInt(30),nil)
/**
0x001->0x002 0
0x003->0x005 0
0x004->0x001 1
**/
addr := common.HexToAddress("0x001")
addr2 := common.HexToAddress("0x002")
addr3 := common.HexToAddress("0x003")
addr4 := common.HexToAddress("0x004")
addr5 := common.HexToAddress("0x005")
tx1 := types.NewTransaction(1,addr2,big.NewInt(20),20,big.NewInt(20),nil)
tx2 := types.NewTransaction(1,addr5,big.NewInt(20),20,big.NewInt(20),nil)
tx3 := types.NewTransaction(1,addr,big.NewInt(20),20,big.NewInt(20),nil)
txs := make([]*types.Transaction,0)
txs = append(txs,tx1,tx2,tx3)
/**
0x001->0x002 0
0x003->0x005 0
0x004->0x001 1
**/
cache := NewFromMarkCache()
for i,tx := range txs{
var key common.Address
item := &Item{
tx:tx,
}
switch i {
case 0:
key = addr
case 1:
key = addr3
case 2:
key = addr4
}
cache.SetMarkCache(key,item)
}
v := cache.maxLevel
println("v max level:",v)
txs1 := cache.GetLevelTxsWithMark(0)
for _,tx := range txs1 {
println("tx info:",tx.ChainId().String())
println("tx info:",tx.To().String())
}
}
func TestFromMarkCache_GetLevelTxsWithMark(t *testing.T) {
/**
0x0001 -> 0x0002 0
0x0002 -> 0x0003 1
0x0001 -> 0x0004 2
**/
addr1 := common.HexToAddress("0x001")
addr2 := common.HexToAddress("0x002")
addr3 := common.HexToAddress("0x003")
addr4 := common.HexToAddress("0x004")
tx1 := types.NewTransaction(1,addr2,big.NewInt(20),20,big.NewInt(20),nil)
tx2 := types.NewTransaction(1,addr3,big.NewInt(20),20,big.NewInt(20),nil)
tx3 := types.NewTransaction(1,addr4,big.NewInt(20),20,big.NewInt(20),nil)
txs := make([]*types.Transaction,0)
txs = append(txs,tx1,tx2,tx3)
cache := NewFromMarkCache()
for i,tx := range txs {
var key common.Address
item := &Item{
tx:tx,
}
switch i {
case 0:
key = addr1
case 1:
key = addr2
case 2:
key =addr1
}
cache.SetMarkCache(key,item)
}
v := cache.GetMaxLevel()
println("v:",v)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment