Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txdag: support find all prev tx from rw list; #183

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions core/types/mvstates.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ type RWEventItem struct {
Slot common.Hash
}

type StateWrites struct {
type RWTxList struct {
list []int
}

func NewStateWrites() *StateWrites {
return &StateWrites{
func NewStateWrites() *RWTxList {
return &RWTxList{
list: make([]int, 0),
}
}

func (w *StateWrites) Append(pw int) {
func (w *RWTxList) Append(pw int) {
if i, found := w.SearchTxIndex(pw); found {
w.list[i] = pw
return
Expand All @@ -270,7 +270,7 @@ func (w *StateWrites) Append(pw int) {
}
}

func (w *StateWrites) SearchTxIndex(txIndex int) (int, bool) {
func (w *RWTxList) SearchTxIndex(txIndex int) (int, bool) {
n := len(w.list)
i, j := 0, n
for i < j {
Expand All @@ -285,7 +285,7 @@ func (w *StateWrites) SearchTxIndex(txIndex int) (int, bool) {
return i, i < n && w.list[i] == txIndex
}

func (w *StateWrites) FindLastWrite(txIndex int) int {
func (w *RWTxList) FindLastWrite(txIndex int) int {
var i, _ = w.SearchTxIndex(txIndex)
for j := i - 1; j >= 0; j-- {
if w.list[j] < txIndex {
Expand All @@ -296,8 +296,19 @@ func (w *StateWrites) FindLastWrite(txIndex int) int {
return -1
}

func (w *StateWrites) Copy() *StateWrites {
np := &StateWrites{}
func (w *RWTxList) FindPrevWrites(txIndex int) []int {
var i, _ = w.SearchTxIndex(txIndex)
for j := i - 1; j >= 0; j-- {
if w.list[j] < txIndex {
return w.list[:j+1]
}
}

return nil
}

func (w *RWTxList) Copy() *RWTxList {
np := &RWTxList{}
for i, item := range w.list {
np.list[i] = item
}
Expand All @@ -318,9 +329,12 @@ var (
)

type MVStates struct {
rwSets []RWSet
accWriteSet map[common.Address]map[AccountState]*StateWrites
slotWriteSet map[common.Address]map[common.Hash]*StateWrites
rwSets []RWSet
accWriteSet map[common.Address]map[AccountState]*RWTxList
slotWriteSet map[common.Address]map[common.Hash]*RWTxList
// TODO: maintain read tx list for states here
accReadSet map[common.Address]map[AccountState]*RWTxList
slotReadSet map[common.Address]map[common.Hash]*RWTxList
nextFinaliseIndex int
gasFeeReceivers []common.Address
// dependency map cache for generating TxDAG
Expand All @@ -342,8 +356,8 @@ type MVStates struct {

func NewMVStates(txCount int, gasFeeReceivers []common.Address) *MVStates {
s := &MVStates{
accWriteSet: make(map[common.Address]map[AccountState]*StateWrites, txCount),
slotWriteSet: make(map[common.Address]map[common.Hash]*StateWrites, txCount),
accWriteSet: make(map[common.Address]map[AccountState]*RWTxList, txCount),
slotWriteSet: make(map[common.Address]map[common.Hash]*RWTxList, txCount),
rwEventCh: make(chan []RWEventItem, 100),
gasFeeReceivers: gasFeeReceivers,
}
Expand Down Expand Up @@ -375,15 +389,15 @@ func (s *MVStates) Copy() *MVStates {
for addr, sub := range s.accWriteSet {
for state, writes := range sub {
if _, ok := ns.accWriteSet[addr]; !ok {
ns.accWriteSet[addr] = make(map[AccountState]*StateWrites)
ns.accWriteSet[addr] = make(map[AccountState]*RWTxList)
}
ns.accWriteSet[addr][state] = writes.Copy()
}
}
for addr, sub := range s.slotWriteSet {
for slot, writes := range sub {
if _, ok := ns.slotWriteSet[addr]; !ok {
ns.slotWriteSet[addr] = make(map[common.Hash]*StateWrites)
ns.slotWriteSet[addr] = make(map[common.Hash]*RWTxList)
}
ns.slotWriteSet[addr][slot] = writes.Copy()
}
Expand Down Expand Up @@ -432,6 +446,7 @@ func (s *MVStates) handleRWEvents(items []RWEventItem) {
switch item.Event {
// recorde current read/write event
case ReadAccRWEvent, ReadSlotRWEvent:
// TODO: maintain read list here
if readFrom < 0 {
readFrom = i
}
Expand Down Expand Up @@ -679,7 +694,7 @@ func (s *MVStates) innerFinalise(index int, applyWriteSet bool) error {
// append to pending write set
for addr, sub := range rwSet.accWriteSet {
if _, exist := s.accWriteSet[addr]; !exist {
s.accWriteSet[addr] = make(map[AccountState]*StateWrites)
s.accWriteSet[addr] = make(map[AccountState]*RWTxList)
}
for state := range sub {
if _, exist := s.accWriteSet[addr][state]; !exist {
Expand All @@ -690,7 +705,7 @@ func (s *MVStates) innerFinalise(index int, applyWriteSet bool) error {
}
for addr, sub := range rwSet.slotWriteSet {
if _, exist := s.slotWriteSet[addr]; !exist {
s.slotWriteSet[addr] = make(map[common.Hash]*StateWrites)
s.slotWriteSet[addr] = make(map[common.Hash]*RWTxList)
}
for slot := range sub {
if _, exist := s.slotWriteSet[addr][slot]; !exist {
Expand All @@ -705,7 +720,7 @@ func (s *MVStates) innerFinalise(index int, applyWriteSet bool) error {
func (s *MVStates) finaliseSlotWrite(index int, addr common.Address, slot common.Hash) {
// append to pending write set
if _, exist := s.slotWriteSet[addr]; !exist {
s.slotWriteSet[addr] = make(map[common.Hash]*StateWrites)
s.slotWriteSet[addr] = make(map[common.Hash]*RWTxList)
}
if _, exist := s.slotWriteSet[addr][slot]; !exist {
s.slotWriteSet[addr][slot] = NewStateWrites()
Expand All @@ -716,22 +731,22 @@ func (s *MVStates) finaliseSlotWrite(index int, addr common.Address, slot common
func (s *MVStates) finaliseAccWrite(index int, addr common.Address, state AccountState) {
// append to pending write set
if _, exist := s.accWriteSet[addr]; !exist {
s.accWriteSet[addr] = make(map[AccountState]*StateWrites)
s.accWriteSet[addr] = make(map[AccountState]*RWTxList)
}
if _, exist := s.accWriteSet[addr][state]; !exist {
s.accWriteSet[addr][state] = NewStateWrites()
}
s.accWriteSet[addr][state].Append(index)
}

func (s *MVStates) queryAccWrites(addr common.Address, state AccountState) *StateWrites {
func (s *MVStates) queryAccWrites(addr common.Address, state AccountState) *RWTxList {
if _, exist := s.accWriteSet[addr]; !exist {
return nil
}
return s.accWriteSet[addr][state]
}

func (s *MVStates) querySlotWrites(addr common.Address, slot common.Hash) *StateWrites {
func (s *MVStates) querySlotWrites(addr common.Address, slot common.Hash) *RWTxList {
if _, exist := s.slotWriteSet[addr]; !exist {
return nil
}
Expand All @@ -754,7 +769,7 @@ func (s *MVStates) resolveDepsMapCacheByWrites(index int, reads []RWEventItem) {
// check tx dependency, only check key
for _, item := range reads {
// check account states & slots
var writes *StateWrites
var writes *RWTxList
if item.Event == ReadAccRWEvent {
writes = s.queryAccWrites(item.Addr, item.State)
} else {
Expand Down Expand Up @@ -782,6 +797,7 @@ func (s *MVStates) resolveDepsMapCacheByWrites(index int, reads []RWEventItem) {
}
}
}
// TODO: check read before write dependency here
for _, addr := range s.gasFeeReceivers {
if _, ok := addrMap[addr]; ok {
rwSet.cannotGasFeeDelay = true
Expand Down
Loading